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

begin;

select plan(16);

set search_path to auth, numerus, public;

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


set client_min_messages to warning;
truncate quote_product_tax cascade;
truncate quote_product cascade;
truncate quote_payment_method cascade;
truncate quote_contact cascade;
truncate quote 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, name)
values (12, 1, 'Contact 2.1')
     , (13, 1, 'Contact 2.2')
;

insert into quote (quote_id, company_id, slug, quote_number, quote_date, currency_code, tags)
values (15, 1, '7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'QUO1', '2023-03-10', 'EUR', '{tag1}')
     , (16, 1, 'b57b980b-247b-4be4-a0b7-03a7819c53ae', 'QUO2', '2023-03-09', 'EUR', '{tag2}')
     , (17, 1, '0b899316-f7d0-4175-a9d5-cea855844716', 'QUO3', '2023-03-11', 'EUR', '{}')
;

insert into quote_payment_method (quote_id, payment_method_id)
values (15, 111)
     , (16, 111)
;

insert into quote_contact (quote_id, contact_id)
values (15, 12)
     , (16, 13)
;

insert into quote_product (quote_product_id, quote_id, name, price)
values (19, 15, 'P1.0', 1100)
     , (20, 15, 'P2.0', 2200)
     , (21, 16, 'P1.1', 1111)
     , (22, 16, 'P2.1', 2211)
     , (23, 17, 'P3.1', 3311)
;

insert into quote_product_product (quote_product_id, product_id)
values (19, 7)
     , (20, 8)
     , (21, 7)
     , (22, 8)
;

insert into quote_product_tax (quote_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_quote('7ac3ae0e-b0c1-4206-a19b-0be20835edd4', 'accepted', null, 'Terms 1', 'Notes 1', 112, array['tag1'], '{"(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_quote('b57b980b-247b-4be4-a0b7-03a7819c53ae', 'sent', 12, 'Terms 2', 'Notes 2', null, 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 quote'
);

select lives_ok(
	$$ select edit_quote('0b899316-f7d0-4175-a9d5-cea855844716', 'rejected', 13, '', '', 111, '{}', '{"(23,,p3.1,,41.41,1,0.0,{})"}') $$,
	'Should be able to edit the third quote'
);

select bag_eq(
	$$ select quote_number, quote_date, contact_id, quote_status, terms_and_conditions, notes, tags, payment_method_id from quote left join quote_contact using (quote_id) left join quote_payment_method using (quote_id) $$,
	$$ values ('QUO1', '2023-03-10'::date, null, 'accepted', 'Terms 1', 'Notes 1', '{tag1}'::tag_name[], 112)
	        , ('QUO2', '2023-03-09'::date, 12, 'sent', 'Terms 2', 'Notes 2', '{tag1,tag3}'::tag_name[], null)
	        , ('QUO3', '2023-03-11'::date, 13, 'rejected', '', '', '{}'::tag_name[], 111)
	$$,
	'Should have updated all quotes'
);

select bag_eq(
	$$ select quote_number, name, description, price, quantity, discount_rate from quote_product join quote using (quote_id) $$,
	$$ values ('QUO1', 'p1.0', 'D1', 1101, 2, 0.50)
	        , ('QUO1', 'p1.3', 'D3', 3333, 3, 0.05)
	        , ('QUO2', 'P1.1', '', 1111, 1, 0.00)
		, ('QUO2', 'p2.1', 'D2', 2400, 3, 0.75)
		, ('QUO2', 'p3.3', '', 3636, 2, 0.05)
		, ('QUO3', 'p3.1', '', 4141, 1, 0.00)
	$$,
	'Should have updated all existing quote products, added new ones, and removed the ones not give to the function'
);

select bag_eq(
	$$ select quote_number, product_id, name from quote_product left join quote_product_product using (quote_product_id) join quote using (quote_id) $$,
	$$ values ('QUO1', NULL, 'p1.0')
	        , ('QUO1', NULL, 'p1.3')
	        , ('QUO2', 7, 'P1.1')
		, ('QUO2', 8, 'p2.1')
		, ('QUO2', 9, 'p3.3')
		, ('QUO3', NULL, 'p3.1')
	$$,
	'Should have updated all existing quote products id, added new ones, and removed the ones not give to the function'
);

select bag_eq(
	$$ select quote_number, name, tax_id, tax_rate from quote_product_tax join quote_product using (quote_product_id) join quote using (quote_id) $$,
	$$ values ('QUO1', 'p1.0', 4, 0.21)
		, ('QUO1', 'p1.3', 3, -0.15)
		, ('QUO2', 'P1.1', 3, -0.15)
		, ('QUO2', 'p2.1', 3, -0.15)
		, ('QUO2', 'p2.1', 4, 0.21)
		, ('QUO2', 'p3.3', 4, 0.21)
	$$,
	'Should have updated all quote product taxes, added new ones, and removed the ones not given to the function'
);

select *
from finish();

rollback;
