-- Test add_payment
set client_min_messages to warning;
create extension if not exists pgtap;
reset client_min_messages;

begin;

select plan(17);

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 cascade;
truncate contact 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 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')
;

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 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)
	$$,
	'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')
	$$,
	'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')
	$$,
	'Should have updated the status of expenses'
);


select *
from finish();

rollback;
