-- Test add_payment set client_min_messages to warning; create extension if not exists pgtap; reset client_min_messages; begin; select plan(20); 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; truncate expense_tax cascade; truncate expense cascade; truncate contact cascade; truncate tax cascade; truncate tax_class cascade; 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; 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) ; 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') , (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) ; 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' ); 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' ); 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) , (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) $$, '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') , (16, 'Re: REF001') , (17, 'Re: REF002') , (18, 'Re: REF003') $$, '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') , (16, 'partial') , (17, 'paid') , (18, 'paid') $$, 'Should have updated the status of expenses' ); select * from finish(); rollback;