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

begin;

select plan(15);

set search_path to auth, camper, public;

select has_function('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]']);
select function_lang_is('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]'], 'plpgsql');
select function_returns('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]'], 'uuid');
select isnt_definer('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]']);
select volatility_is('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]'], 'volatile');
select function_privs_are('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]'], 'guest', array []::text[]);
select function_privs_are('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]'], 'employee', array ['EXECUTE']);
select function_privs_are('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', 'edited_invoice_product[]'], 'admin', array ['EXECUTE']);
select function_privs_are('camper', 'edit_invoice', array ['uuid', 'text', 'integer', 'text', 'integer', '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;



insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, rtc_number, tourist_tax, tourist_tax_max_days, country_code, currency_code, default_lang_tag)
values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', '', 60, 7, 'ES', 'EUR', 'ca')
;

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')
;

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, name, id_document_type_id, id_document_number, address, city, province, postal_code, country_code)
values (12, 1, 'Contact 2.1', 'D', '41440443Q', 'Fake St', 'City', 'Province', '17600', 'ES')
     , (13, 1, 'Contact 2.2', 'D', '41440443Q', 'Fake St', 'City', 'Province', '17600', 'ES')
;

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

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

insert into invoice_product_product (invoice_product_id, product_id)
values (19, 7)
     , (20, 8)
     , (21, 7)
     , (22, 8)
;

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, '{"(20,,p1.0,D1,11.01,2,0.50,{4})","(,,p1.3,D3,33.33,3,0.05,{3})"}') $$,
	'Should be able to edit the first invoice'
);

select lives_ok(
	$$ select edit_invoice('b57b980b-247b-4be4-a0b7-03a7819c53ae', 'sent', 12, 'Notes 2', 111, '{"(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, payment_method_id from invoice $$,
	$$ values ('INV1', '2023-03-10'::date, 13, 'paid', 'Notes 1', 112)
	        , ('INV2', '2023-03-09'::date, 12, 'sent', 'Notes 2', 111)
	$$,
	'Should have updated all invoices'
);

select bag_eq(
	$$ select invoice_number, name, description, price, quantity, discount_rate from invoice_product join invoice using (invoice_id) $$,
	$$ values ('INV1', 'p1.0', 'D1', 1101, 2, 0.50)
	        , ('INV1', 'p1.3', 'D3', 3333, 3, 0.05)
	        , ('INV2', 'P1.1', '', 1111, 1, 0.00)
		, ('INV2', 'p2.1', 'D2', 2400, 3, 0.75)
		, ('INV2', '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, name from invoice_product left join invoice_product_product using (invoice_product_id) join invoice using (invoice_id) $$,
	$$ values ('INV1', NULL, 'p1.0')
	        , ('INV1', NULL, 'p1.3')
	        , ('INV2', 7, 'P1.1')
		, ('INV2', 8, 'p2.1')
		, ('INV2', 9, 'p3.3')
	$$,
	'Should have updated all existing invoice products id, added new ones, and removed the ones not give to the function'
);

select bag_eq(
	$$ select invoice_number, name, tax_id, tax_rate from invoice_product_tax join invoice_product using (invoice_product_id) join invoice using (invoice_id) $$,
	$$ values ('INV1', 'p1.0', 4, 0.21)
		, ('INV1', 'p1.3', 3, -0.15)
		, ('INV2', 'P1.1', 3, -0.15)
		, ('INV2', 'p2.1', 3, -0.15)
		, ('INV2', 'p2.1', 4, 0.21)
		, ('INV2', 'p3.3', 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;
