Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
-- Test add_payment
|
|
|
|
|
set client_min_messages to warning;
|
|
|
|
|
create extension if not exists pgtap;
|
|
|
|
|
reset client_min_messages;
|
|
|
|
|
|
|
|
|
|
begin;
|
|
|
|
|
|
2024-08-15 01:51:30 +00:00
|
|
|
|
select plan(20);
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
|
|
|
|
|
set search_path to numerus, auth, public;
|
|
|
|
|
|
|
|
|
|
select has_function('numerus', 'add_payment', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]']);
|
|
|
|
|
select function_lang_is('numerus', 'add_payment', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'plpgsql');
|
|
|
|
|
select function_returns('numerus', 'add_payment', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'uuid');
|
|
|
|
|
select isnt_definer('numerus', 'add_payment', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]']);
|
|
|
|
|
select volatility_is('numerus', 'add_payment', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'volatile');
|
|
|
|
|
|
|
|
|
|
select function_privs_are('numerus', 'add_payment', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'guest', array []::text[]);
|
|
|
|
|
select function_privs_are('numerus', 'add_payment', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
|
|
|
|
|
select function_privs_are('numerus', 'add_payment', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'admin', array ['EXECUTE']);
|
|
|
|
|
select function_privs_are('numerus', 'add_payment', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'authenticator', array []::text[]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
set client_min_messages to warning;
|
|
|
|
|
truncate expense_payment cascade;
|
|
|
|
|
truncate payment cascade;
|
|
|
|
|
truncate payment_account cascade;
|
2024-08-15 01:51:30 +00:00
|
|
|
|
truncate expense_tax cascade;
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
truncate expense cascade;
|
|
|
|
|
truncate contact cascade;
|
2024-08-15 01:51:30 +00:00
|
|
|
|
truncate tax cascade;
|
|
|
|
|
truncate tax_class cascade;
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
truncate payment_method cascade;
|
|
|
|
|
truncate company cascade;
|
|
|
|
|
reset client_min_messages;
|
|
|
|
|
|
|
|
|
|
set constraints "company_default_payment_method_id_fkey" deferred;
|
|
|
|
|
|
|
|
|
|
insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, default_payment_method_id)
|
|
|
|
|
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', 111)
|
|
|
|
|
, (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD', 222)
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
insert into payment_method (payment_method_id, company_id, name, instructions)
|
|
|
|
|
values (111, 1, 'cash', 'cash')
|
|
|
|
|
, (222, 2, 'cash', 'cash')
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
set constraints "company_default_payment_method_id_fkey" immediate;
|
|
|
|
|
|
2024-08-15 01:51:30 +00:00
|
|
|
|
insert into tax_class (tax_class_id, company_id, name)
|
|
|
|
|
values (11, 1, 'tax')
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
insert into tax (tax_id, company_id, tax_class_id, name, rate)
|
|
|
|
|
values (2, 1, 11, 'IRPF -15 %', -0.15)
|
|
|
|
|
, (3, 1, 11, 'IVA 4 %', 0.04)
|
|
|
|
|
, (4, 1, 11, 'IVA 10 %', 0.10)
|
|
|
|
|
;
|
|
|
|
|
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
insert into contact (contact_id, company_id, name)
|
|
|
|
|
values ( 9, 1, 'Customer 1')
|
|
|
|
|
, (10, 2, 'Customer 2')
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
insert into expense (expense_id, company_id, invoice_number, contact_id, invoice_date, amount, currency_code)
|
|
|
|
|
values (12, 1, 'REF123', 9, '2011-01-11', 111, 'EUR')
|
|
|
|
|
, (13, 2, 'INV001', 10, '2011-01-11', 111, 'USD')
|
|
|
|
|
, (14, 2, 'INV002', 10, '2022-02-22', 222, 'USD')
|
|
|
|
|
, (15, 2, 'INV003', 10, '2022-02-22', 222, 'USD')
|
2024-08-15 01:51:30 +00:00
|
|
|
|
, (16, 1, 'REF001', 9, '2023-03-03', 10000, 'EUR')
|
|
|
|
|
, (17, 1, 'REF002', 9, '2023-03-03', 10000, 'EUR')
|
|
|
|
|
, (18, 1, 'REF003', 9, '2023-03-03', 10000, 'EUR')
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
insert into expense_tax (expense_id, tax_id, tax_rate)
|
|
|
|
|
values (16, 3, 0.04)
|
|
|
|
|
, (17, 2, -0.15)
|
|
|
|
|
, (18, 2, -0.15)
|
|
|
|
|
, (18, 4, 0.10)
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
insert into payment_account (payment_account_id, company_id, payment_account_type, name)
|
|
|
|
|
values (11, 1, 'other', 'Other 1')
|
|
|
|
|
, (22, 2, 'cash', 'Cash 2')
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(1, null, '2023-05-02', 11, '“Protection”', '11.11', array['tag1', 'tag2']) $$,
|
|
|
|
|
'Should be able to insert a payment, unrelated to any expense, for the first company'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(2, 13, '2023-05-03', 22, 'Payment of INV001', '1.11', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to insert a complete payment for the first expense'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(2, 14, '2023-05-04', 22, 'First payment of INV002', '1.00', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to insert a partial payment for the second expense'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(2, 14, '2023-05-05', 22, 'Second payment of INV002', '1.22', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to insert a partial, and final, payment for the second expense'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(2, 15, '2023-05-06', 22, 'Partial payment of INV003', '1.11', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to insert a partial payment for the third expense'
|
|
|
|
|
);
|
|
|
|
|
|
2024-08-15 01:51:30 +00:00
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(1, 16, '2023-03-06', 11, 'Re: REF001', '103.99', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to pay an expense with taxes'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(1, 17, '2023-03-06', 11, 'Re: REF002', '85', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to pay an expense with negative taxes'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select lives_ok(
|
|
|
|
|
$$ select add_payment(1, 18, '2023-03-06', 11, 'Re: REF003', '95', array[]::tag_name[]) $$,
|
|
|
|
|
'Should be able to pay an expense with multiple taxes'
|
|
|
|
|
);
|
|
|
|
|
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
select bag_eq(
|
|
|
|
|
$$ select company_id, description, payment_date::text, payment_account_id, amount, currency_code, payment_status, tags::text, created_at from payment $$,
|
|
|
|
|
$$ values (1, '“Protection”', '2023-05-02', 11, 1111, 'EUR', 'complete', '{tag1,tag2}', current_timestamp)
|
|
|
|
|
, (2, 'Payment of INV001', '2023-05-03', 22, 111, 'USD', 'complete', '{}', current_timestamp)
|
|
|
|
|
, (2, 'First payment of INV002', '2023-05-04', 22, 100, 'USD', 'partial', '{}', current_timestamp)
|
|
|
|
|
, (2, 'Second payment of INV002', '2023-05-05', 22, 122, 'USD', 'partial', '{}', current_timestamp)
|
|
|
|
|
, (2, 'Partial payment of INV003', '2023-05-06', 22, 111, 'USD', 'partial', '{}', current_timestamp)
|
2024-08-15 01:51:30 +00:00
|
|
|
|
, (1, 'Re: REF001', '2023-03-06', 11, 10399, 'EUR', 'partial', '{}', current_timestamp)
|
|
|
|
|
, (1, 'Re: REF002', '2023-03-06', 11, 8500, 'EUR', 'complete', '{}', current_timestamp)
|
|
|
|
|
, (1, 'Re: REF003', '2023-03-06', 11, 9500, 'EUR', 'complete', '{}', current_timestamp)
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
$$,
|
|
|
|
|
'Should have created all payments'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select bag_eq(
|
|
|
|
|
$$ select expense_id, description from expense_payment join payment using (payment_id) $$,
|
|
|
|
|
$$ values (13, 'Payment of INV001')
|
|
|
|
|
, (14, 'First payment of INV002')
|
|
|
|
|
, (14, 'Second payment of INV002')
|
|
|
|
|
, (15, 'Partial payment of INV003')
|
2024-08-15 01:51:30 +00:00
|
|
|
|
, (16, 'Re: REF001')
|
|
|
|
|
, (17, 'Re: REF002')
|
|
|
|
|
, (18, 'Re: REF003')
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
$$,
|
|
|
|
|
'Should have linked all expenses to payments'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
select bag_eq(
|
|
|
|
|
$$ select expense_id, expense_status from expense $$,
|
|
|
|
|
$$ values (12, 'pending')
|
|
|
|
|
, (13, 'paid')
|
|
|
|
|
, (14, 'paid')
|
|
|
|
|
, (15, 'partial')
|
2024-08-15 01:51:30 +00:00
|
|
|
|
, (16, 'partial')
|
|
|
|
|
, (17, 'paid')
|
|
|
|
|
, (18, 'paid')
|
Add the payments section
This actually should be the “payments and receivables” section, however
this is quite a mouthful; a “receivable” is a payment made **to** you,
therefore “payments” is ok.
In fact, there is still no receivables in there, as they should be in
a separate relation, to constraint them to invoices instead of expenses.
It will be done in a separate commit.
Since this section will be, in a sense, sort of simplified accounting,
i needed to introduce the “payment account” concept. There is no way,
yet, for users to add them, because i have to revamp the “tax details”
section, but this commit started to grow too big already.
The same reasoning for the attachment payment slips as PDF to payment:
something i have to add, but not yet in this commit.
2024-08-10 02:34:07 +00:00
|
|
|
|
$$,
|
|
|
|
|
'Should have updated the status of expenses'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
select *
|
|
|
|
|
from finish();
|
|
|
|
|
|
|
|
|
|
rollback;
|