-- Test add_collection
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_collection', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]']);
select function_lang_is('numerus', 'add_collection', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'plpgsql');
select function_returns('numerus', 'add_collection', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'uuid');
select isnt_definer('numerus', 'add_collection', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]']);
select volatility_is('numerus', 'add_collection', array['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'volatile');

select function_privs_are('numerus', 'add_collection', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'add_collection', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'add_collection', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'add_collection', array ['integer', 'integer', 'date', 'integer', 'text', 'text', 'tag_name[]'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate invoice_collection cascade;
truncate collection cascade;
truncate payment_account cascade;
truncate invoice_product_tax cascade;
truncate invoice_product cascade;
truncate invoice cascade;
truncate contact_tax_details 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 contact_tax_details (contact_id, business_name, vatin, address, city, province, postal_code, country_code)
values (9, 'Customer 1', 'XX555', '', '', '', '', 'ES')
     , (10, 'Customer 2', 'XX666', '', '', '', '', 'ES')
;

insert into invoice (invoice_id, company_id, invoice_number, contact_id, invoice_date, payment_method_id, 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', 111, 'EUR')
     , (17, 1, 'REF002',  9, '2023-03-03', 111, 'EUR')
     , (18, 1, 'REF003',  9, '2023-03-03', 111, 'EUR')
;

insert into invoice_product (invoice_product_id, invoice_id, name, price)
values (19, 12, 'P1', 100)
     , (20, 12, 'P2',  11)
     , (21, 13, 'P1',  50)
     , (22, 13, 'P2',  61)
     , (23, 14, 'P1', 100)
     , (24, 14, 'P2', 100)
     , (25, 14, 'P3',  11)
     , (26, 14, 'P4',  11)
     , (27, 15, 'P1', 222)
     , (28, 16, 'P*', 10000)
     , (29, 17, 'P*', 10000)
     , (30, 18, 'P*', 10000)
;

insert into invoice_product_tax (invoice_product_id, tax_id, tax_rate)
values (28, 3,  0.04)
     , (29, 2, -0.15)
     , (30, 2, -0.15)
     , (30, 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_collection(1, null, '2023-05-02', 11, '“Protection”', '11.11', array['tag1', 'tag2']) $$,
	'Should be able to insert a collection, unrelated to any invoice, for the first company'
);

select lives_ok(
	$$ select add_collection(2, 13, '2023-05-03', 22, 'Collection of INV001', '1.11', array[]::tag_name[]) $$,
	'Should be able to insert a complete collection for the first invoice'
);

select lives_ok(
	$$ select add_collection(2, 14, '2023-05-04', 22, 'First collection of INV002', '1.00', array[]::tag_name[]) $$,
	'Should be able to insert a partial collection for the second invoice'
);

select lives_ok(
	$$ select add_collection(2, 14, '2023-05-05', 22, 'Second collection of INV002', '1.22', array[]::tag_name[]) $$,
	'Should be able to insert a partial, and final, collection for the second invoice'
);

select lives_ok(
	$$ select add_collection(2, 15, '2023-05-06', 22, 'Partial collection of INV003', '1.11', array[]::tag_name[]) $$,
	'Should be able to insert a partial collection for the third invoice'
);

select lives_ok(
	$$ select add_collection(1, 16, '2023-03-06', 11, 'Re: REF001', '103.99', array[]::tag_name[]) $$,
	'Should be able to collect an invoice with taxes'
);

select lives_ok(
	$$ select add_collection(1, 17, '2023-03-06', 11, 'Re: REF002', '85', array[]::tag_name[]) $$,
	'Should be able to collect an invoice with negative taxes'
);

select lives_ok(
	$$ select add_collection(1, 18, '2023-03-06', 11, 'Re: REF003', '95', array[]::tag_name[]) $$,
	'Should be able to collect an invoice with multiple taxes'
);

select bag_eq(
	$$ select company_id, description, collection_date::text, payment_account_id, amount, currency_code, payment_status, tags::text, created_at from collection $$,
	$$ values (1, '“Protection”', '2023-05-02', 11, 1111, 'EUR', 'complete', '{tag1,tag2}', current_timestamp)
	        , (2, 'Collection of INV001', '2023-05-03', 22,  111, 'USD', 'complete', '{}', current_timestamp)
	        , (2, 'First collection of INV002', '2023-05-04', 22,  100, 'USD', 'partial', '{}', current_timestamp)
	        , (2, 'Second collection of INV002', '2023-05-05', 22,  122, 'USD', 'partial', '{}', current_timestamp)
	        , (2, 'Partial collection 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 collections'
);

select bag_eq(
	$$ select invoice_id, description from invoice_collection join collection using (collection_id) $$,
	$$ values (13, 'Collection of INV001')
	        , (14, 'First collection of INV002')
	        , (14, 'Second collection of INV002')
	        , (15, 'Partial collection of INV003')
	        , (16, 'Re: REF001')
	        , (17, 'Re: REF002')
	        , (18, 'Re: REF003')
	$$,
	'Should have linked all invoices to collections'
);

select bag_eq(
	$$ select invoice_id, invoice_status from invoice $$,
	$$ values (12, 'created')
	        , (13, 'paid')
	        , (14, 'paid')
	        , (15, 'partial')
	        , (16, 'partial')
	        , (17, 'paid')
	        , (18, 'paid')
	$$,
	'Should have updated the status of invoices'
);


select *
from finish();

rollback;
