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

begin;

select plan(14);

set search_path to numerus, auth, public;

select has_function('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]']);
select function_lang_is('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'plpgsql');
select function_returns('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'new_expense_amount');
select isnt_definer('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]']);
select volatility_is('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'stable');
select function_privs_are('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'guest', array []::text[]);
select function_privs_are('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'invoicer', array ['EXECUTE']);
select function_privs_are('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'admin', array ['EXECUTE']);
select function_privs_are('numerus', 'compute_new_expense_amount', array ['integer', 'text', 'integer[]'], 'authenticator', array []::text[]);

set client_min_messages to warning;
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 1', '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')
;

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)
     , (5, 1, 11, 'IVA 21 %', 0.21)
;

select is(
	compute_new_expense_amount(1, '', array[]::integer[]),
	'("{}",0.00)'::new_expense_amount
);

select is(
	compute_new_expense_amount(1, '4.60', array[2,5,3]),
	'("{{IRPF -15 %,-0.69},{IVA 4 %,0.18},{IVA 21 %,0.97}}",5.06)'::new_expense_amount
);

select is(
	compute_new_expense_amount(1, '17.32', array[2,4,5]),
	'("{{IRPF -15 %,-2.60},{IVA 10 %,1.73},{IVA 21 %,3.64}}",20.09)'::new_expense_amount
);

select is(
	compute_new_expense_amount(1, '52.17', array[3,4,5]),
	'("{{IVA 4 %,2.09},{IVA 10 %,5.22},{IVA 21 %,10.96}}",70.44)'::new_expense_amount
);

select is(
	compute_new_expense_amount(1, '62.16', array[]::integer[]),
	'("{}",62.16)'::new_expense_amount
);

select *
from finish();

rollback;
