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

begin;

select plan(14);

set search_path to auth, numerus, public;

select has_function('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]']);
select function_lang_is('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'plpgsql');
select function_returns('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'uuid');
select isnt_definer('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]']);
select volatility_is('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'volatile');
select function_privs_are('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'tag_name[]', 'edited_invoice_product[]'], 'authenticator', array []::text[]);


set client_min_messages to warning;
truncate invoice_product_tax cascade;
truncate invoice_product cascade;
truncate invoice cascade;
truncate contact cascade;
truncate product 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)
;

insert into payment_method (payment_method_id, company_id, name, instructions)
values (111, 1, 'cash', 'cash')
     , (112, 1, 'bank', 'send money to my bank account')
;

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 (3, 1, 11, 'IRPF -15 %', -0.15)
     , (4, 1, 11, 'IVA 21 %', 0.21)
;

insert into product (product_id, company_id, name, price)
values ( 7, 1, 'Product 1.1', 1212)
     , ( 8, 1, 'Product 2.2', 2424)
     , ( 9, 1, 'Product 3.3', 3636)
;

insert into contact (contact_id, company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code)
values (12, 1, 'Contact 2.1', 'XX555', '', '777-777-777', 'c@c', '', '', '', '', '', 'ES')
     , (13, 1, 'Contact 2.2', 'XX666', '', '888-888-888', 'd@d', '', '', '', '', '', 'ES')
;

insert into invoice (invoice_id, company_id, slug, invoice_number, invoice_date, contact_id, payment_method_id, currency_code, tags)
values (15, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'INV1', '2023-03-10', 12, 111, 'EUR', '{tag1}')
     , (16, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'INV2', '2023-03-09', 13, 111, 'EUR', '{tag2}')
;

insert into invoice_product (invoice_product_id, invoice_id, product_id, name, price)
values (19, 15, 7, 'P1.0', 1100)
     , (20, 15, 8, 'P2.0', 2200)
     , (21, 16, 7, 'P1.1', 1111)
     , (22, 16, 8, 'P2.1', 2211)
;

insert into invoice_product_tax (invoice_product_id, tax_id, tax_rate)
values (19, 4, 0.21)
     , (20, 4, 0.21)
     , (21, 3, -0.07)
     , (21, 4, 0.21)
     , (22, 3, -0.15)
;

select lives_ok(
	$$ select edit_invoice('7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'paid', 13, 'Notes 1', 112, array['tag1'], '{"(20,7,p1.0,D1,11.01,2,0.50,{4})"}') $$,
	'Should be able to edit the first invoice'
);

select lives_ok(
	$$ select edit_invoice('b57b980b-247b-4be4-a0b7-03a7819c53ae', 'sent', 12, 'Notes 2', 111, array['tag1', 'tag3'], '{"(21,7,P1.1,,11.11,1,0.0,{3})","(22,8,p2.1,D2,24.00,3,0.75,\"{3,4}\")","(,9,p3.3,,36.36,2,0.05,{4})"}') $$,
	'Should be able to edit the second invoice'
);

select bag_eq(
	$$ select invoice_number, invoice_date, contact_id, invoice_status, notes, tags, payment_method_id from invoice $$,
	$$ values ('INV1', '2023-03-10'::date, 13, 'paid', 'Notes 1', '{tag1}'::tag_name[], 112)
	        , ('INV2', '2023-03-09'::date, 12, 'sent', 'Notes 2', '{tag1,tag3}'::tag_name[], 111)
	$$,
	'Should have updated all invoices'
);

select bag_eq(
	$$ select invoice_number, product_id, name, description, price, quantity, discount_rate from invoice_product join invoice using (invoice_id) $$,
	$$ values ('INV1', 7, 'p1.0', 'D1', 1101, 2, 0.50)
	        , ('INV2', 7, 'P1.1', '', 1111, 1, 0.00)
		, ('INV2', 8, 'p2.1', 'D2', 2400, 3, 0.75)
		, ('INV2', 9, 'p3.3', '', 3636, 2, 0.05)
	$$,
	'Should have updated all existing invoice products, added new ones, and removed the ones not give to the function'
);

select bag_eq(
	$$ select invoice_number, product_id, tax_id, tax_rate from invoice_product_tax join invoice_product using (invoice_product_id) join invoice using (invoice_id) $$,
	$$ values ('INV1', 7, 4, 0.21)
		, ('INV2', 7, 3, -0.15)
		, ('INV2', 8, 3, -0.15)
		, ('INV2', 8, 4, 0.21)
		, ('INV2', 9, 4, 0.21)
	$$,
	'Should have updated all invoice product taxes, added new ones, and removed the ones not given to the function'
);


select *
from finish();

rollback;
