/*

*igning tests. Thinking:

1) Correct number of lines on summary report
2) Correct total on summary report
3) Correct number of lines on detail report
4) Correct totals on detail report
5) Correct total summary report for future year
6) Correct number of lines for report on future year
7) Correct number of lines on detail report for futture year
8) Correct totals on lines of details report for future year

Invoices 8:

1) AP transaction: Reportable amount 1000, non-reportable amount $10, paid
in full in current year.
2) AP transaction: Reportable amount $1000, non-reportable amount $10,
partially paid ($500) in current year
3) AP Transaction: Reportable amount $1000, non-reportable amount $10,
paid $500 currnet year, $500 in future year

4) AP invoice: Reportable amount 1000, non-reportable amount $10, paid in
full in current year.
5) AP invoice: Reportable amount $1000, non-reportable amount $10,
partially paid ($500) in current year
6) AP invice: Reportable amount $1000, non-reportable amount $10, paid
$500 currnet year, $500 in future year

7 like 1 but different vendor
8 like 4 but different vendor

--CT

*/

BEGIN;
    -- Load the TAP functions.
    CREATE EXTENSION pgtap;

    -- Plan the tests.

    SELECT plan(84);

    -- Add data

    \i sql/modules/test/Base.sql

    -- Validate required tables

    SELECT has_table('ac_tax_form');
    SELECT has_table('country_tax_form');
    SELECT has_table('eca_tax');
    SELECT has_table('invoice_tax_form');
    SELECT has_table('tax');
    SELECT has_table('tax_extended');
    SELECT has_table('taxmodule');

    -- Validate required functions

    SELECT has_function('tax_form__get',ARRAY['integer']);
    SELECT has_function('tax_form__list_all','{}'::text[]);
    SELECT has_function('tax_form__list_ext','{}'::text[]);
    SELECT has_function('tax_form__save',ARRAY['integer','integer','text','boolean','boolean']);

    -- Basic setup

    INSERT INTO account_heading(id, accno ) VALUES (-255, '-billion');
    INSERT INTO account (id, accno, category, heading ) VALUES (-255, '-billion', 'L', -255);
    INSERT INTO account (id, accno, category, heading ) VALUES (-256, '-billiontest', 'L', -255);

    INSERT INTO country_tax_form (country_id, form_name, id) VALUES (232, 'Testing Form', -511);

    INSERT INTO parts (id, partnumber, description) values (-255, '-test1', 'test 1');
    INSERT INTO parts (id, partnumber, description) values (-256, '-test2', 'test 2');

    -- Set up an ECAs, for AP.

    INSERT INTO entity_credit_account (id, entity_id, entity_class, meta_number, taxform_id, ar_ap_account_id) VALUES (-255, -100, 1, 'Test account 1', -511, -255);

    INSERT INTO company (id, entity_id, legal_name) VALUES (-1024, -100, 'Testing Tax Form');

    INSERT INTO entity_credit_account (id, entity_id, entity_class, meta_number, taxform_id, ar_ap_account_id) VALUES (-256, -101, 1, 'Test account 2', -511, -255);

    INSERT INTO company (id, entity_id, legal_name) VALUES (-1025, -101, 'Testing Tax Form');

    CREATE OR REPLACE FUNCTION date1() RETURNS date AS
    $$
    SELECT (extract('YEAR' from now())|| '-12-01')::date;
    $$ language sql;

    CREATE OR REPLACE FUNCTION date2() RETURNS date AS
    $$
    SELECT ((extract('YEAR' from now())|| '-12-01')::date
            + '1 year'::interval)::date;
    $$ language sql;

    INSERT INTO account_link (account_id, description)
    values (-1000, 'AP');

    INSERT INTO account_link (account_id, description)
    values (-1001, 'AP_amount');

    INSERT INTO account_link (account_id, description)
    values (-1002, 'AP_paid');
    --AP transactions.

    --1) AP transaction: Reportable amount 1000, non-reportable amount $10, paid
    -- in full in current year. id -1024

    INSERT INTO ap (id, transdate, amount, netamount, curr, entity_credit_account,
                    approved)
    values(-1024, date1(), 1010, 1010, 'USD', -255, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1024, -1000, date1(), 1010, true, -111);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1024, -1001, date1(), -1000, true, -112);

    INSERT INTO ac_tax_form(entry_id, reportable) values (-112, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1024, -1001, date1(), -10, true, -113);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1024, -1002, date1(), 1010, true, -114);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1024, -1000, date1(), -1010, true, -115);

    -- 2) AP transaction: Reportable amount $1000, non-reportable amount $10,
    -- partially paid ($500) in current year -1025

    INSERT INTO ap (id, transdate, amount, netamount, curr, entity_credit_account,
                approved)
    values(-1025, date1(), 1010, 1010, 'USD', -255, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1025, -1000, date1(), 1010, true, -121);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1025, -1001, date1(), -1000, true, -122);

    INSERT INTO ac_tax_form(entry_id, reportable) values (-122, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1025, -1001, date1(), -10, true, -123);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1025, -1002, date1(), 505, true, -124);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1025, -1000, date1(), -505, true, -125);

    -- 3)_AP Transaction: Reportable amount $1000, non-reportable amount $10,
    -- paid $500 currnet year, $500 in future year -1026


    INSERT INTO ap (id, transdate, amount, netamount, curr, entity_credit_account,
                approved)
          values(-1026, date1(), 1010, 1010, 'USD', -255, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1000, date1(), 1010, true, -131);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1001, date1(), -1000, true, -132);

    INSERT INTO ac_tax_form(entry_id, reportable) values (-132, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1001, date1(), -10, true, -133);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1002, date1(), 505, true, -134);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1000, date1(), -505, true, -135);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1002, date2(), 505, true, -136);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1026, -1000, date2(), -505, true, -137);

    -- 4) AP transaction: Reportable amount 1000, non-reportable amount $10, paid
    -- $500 currnet year, $500 in future year -1027, to second vendor
    -- Vendor Invoices.

    INSERT INTO ap (id, transdate, amount, netamount, curr, entity_credit_account,
                approved)
    values(-1027, date1(), 1010, 1010, 'USD', -256, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1027, -1000, date1(), 1010, true, -141);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1027, -1001, date1(), -1000, true, -142);

    INSERT INTO ac_tax_form(entry_id, reportable) values (-142, true);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1027, -1001, date1(), -10, true, -143);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1027, -1002, date1(), 1010, true, -144);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1027, -1000, date1(), -1010, true, -145);


    -- 2) AP invoice: Reportable amount $1000, non-reportable amount $10,
    -- partially paid ($500) in current year -1035

    INSERT INTO ar (id, transdate, amount, netamount, curr, entity_credit_account,
                    approved, invoice, invnumber)
          values(-1035, date1(), 1010, 1010, 'USD', -255, true, true, 'test1');

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1035, -1000, date1(), 1010, true, -221);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1035, -1001, date1(), -1000, true, -222);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1035, -1001, date1(), -10, true, -223);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1035, -1002, date1(), 505, true, -224);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1035, -1000, date1(), -505, true, -225);

    insert into invoice(trans_id, id, parts_id, description, qty, sellprice)
         VALUES (-1035, -211, -255, 'test 1', -1, 1000);

    INSERT INTO invoice_tax_form(invoice_id, reportable)
         VALUES (-211, true);

    insert into invoice(trans_id, id, parts_id, description, qty, sellprice)
         VALUES (-1035, -212, -256, 'test 1', -1, 10);

    -- 3)_AP invoice: Reportable amount $1000, non-reportable amount $10,
    -- paid $500 currnet year, $500 in future year -1036

    INSERT INTO ar (id, transdate, amount, netamount, curr, entity_credit_account,
                    approved, invoice, invnumber)
          values(-1036, date1(), 1010, 1010, 'USD', -255, true, true, 'test2');

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1000, date1(), 1010, true, -231);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1001, date1(), -1000, true, -232);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1001, date1(), -10, true, -233);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1002, date1(), 505, true, -234);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1000, date1(), -505, true, -235);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1002, date2(), 505, true, -236);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1036, -1000, date2(), -505, true, -237);

    insert into invoice(trans_id, id, parts_id, description, qty, sellprice)
         VALUES (-1036, -221, -255, 'test 1', -1, 1000);

    INSERT INTO invoice_tax_form(invoice_id, reportable)
         VALUES (-221, true);

    insert into invoice(trans_id, id, parts_id, description, qty, sellprice)
         VALUES (-1036, -222, -256, 'test 1', -1, 10);

    -- 4) AP invoice: Reportable amount 1000, non-reportable amount $10, paid
    -- $500 currnet year, $500 in future year -1037, to second vendor


    INSERT INTO ar (id, transdate, amount, netamount, curr, entity_credit_account,
                    approved, invoice, invnumber)
          values(-1037, date1(), 1010, 1010, 'USD', -256, true, true, 'test3');

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1037, -1000, date1(), 1010, true, -241);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1037, -1001, date1(), -1000, true, -242);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1037, -1001, date1(), -10, true, -243);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1037, -1002, date1(), 1010, true, -244);

    INSERT INTO acc_trans(trans_id, chart_id, transdate, amount, approved, entry_id)
         VALUES (-1037, -1000, date1(), -1010, true, -245);

    insert into invoice(trans_id, id, parts_id, description, qty, sellprice)
         VALUES (-1037, -231, -255, 'test 1', -1, 1000);

    INSERT INTO invoice_tax_form(invoice_id, reportable)
         VALUES (-231, true);

    insert into invoice(trans_id, id, parts_id, description, qty, sellprice)
         VALUES (-1037, -232, -256, 'test 1', -1, 10);

    -- Run tests

    PREPARE test AS SELECT count(*) = 2
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date);
    SELECT results_eq('test',ARRAY[true],'2 rows on current summary report');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 2
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date);
    SELECT results_eq('test',ARRAY[true],'2 rows on current accrual summary report');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 1
      FROM tax_form_summary_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date);
    SELECT results_eq('test',ARRAY[true],'1 row on future summary report');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 0
      FROM tax_form_summary_report_accrual(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date);
    SELECT results_eq('test',ARRAY[true],'0 rows on future summary accrual report');
    DEALLOCATE test;



    PREPARE test AS SELECT invoice_sum = 1000
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'inv_sum for test vendor 1, current report is $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 2000
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'inv_sum for test vendor 1, current accrual report is $2000');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 500
      FROM tax_form_summary_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'inv_sum for test vendor 1, future report is $500');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 1000
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 2';
    SELECT results_eq('test',ARRAY[true],'inv_sum for test vendor 2, current report is $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 1000
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 2';
    SELECT results_eq('test',ARRAY[true],'inv_sum for test vendor 2, current accrual report is $1000');
    DEALLOCATE test;


    PREPARE test AS SELECT total_sum = 3000
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'total_sum for test vendor 1, current report is $3000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum = 5000
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'total_sum for test vendor 1, current accrual report is $5000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum = 1000
      FROM tax_form_summary_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'total_sum for test vendor 1, future report is $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum = 2000
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 2';
    SELECT results_eq('test',ARRAY[true],'total_sum for test vendor 2, current report is $2000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum = 2000
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 2';
    SELECT results_eq('test',ARRAY[true],'total_sum for test vendor 2, current accrual report is $2000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 2000
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'ac_sum for test vendor 1, current report is $2000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 3000
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'ac_sum for test vendor 1, current accrual report is $3000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 500
      FROM tax_form_summary_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date)
     where meta_number = 'Test account 1';
    SELECT results_eq('test',ARRAY[true],'ac_sum for test vendor 1, future report is $500');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 1000
      FROM tax_form_summary_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 2';
    SELECT results_eq('test',ARRAY[true],'ac_sum for test vendor 2, current report is $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 1000
      FROM tax_form_summary_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date)
     where meta_number = 'Test account 2';
    SELECT results_eq('test',ARRAY[true],'ac_sum for test vendor 2, current accrual report is $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 5
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1');
    SELECT results_eq('test',ARRAY[true],'6 in detail report for current report, vendor 1');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 5
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1');
    SELECT results_eq('test',ARRAY[true],'6 in detail report for current accrual report, vendor 1');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 2
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2');
    SELECT results_eq('test',ARRAY[true],'2 in detail report for current report, vendor 2');
    DEALLOCATE test;
    PREPARE test AS SELECT count(*) = 2
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2');
    SELECT results_eq('test',ARRAY[true],'2 in detail report for current accrual report, vendor 2');
    DEALLOCATE test;


    PREPARE test AS SELECT count(*) = 2
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1');
    SELECT results_eq('test',ARRAY[true],'2 in detail report for future report, vendor 1');
    DEALLOCATE test;

    PREPARE test AS SELECT count(*) = 0
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 2');
    SELECT results_eq('test',ARRAY[true],'0 in detail report for future report, vendor 2');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 1000
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1024;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1024, acc $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1024;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1024, acc $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1025;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1025, acc $500');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1025;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1025, acc $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1026, acc $500');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1026, acc $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 1000
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1027;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1027, acc $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1027;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1027, acc $1000');
    DEALLOCATE test;


    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1024;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1024, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1024;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1024, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1025;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1025, total $500');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1025;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1025, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1026, total $500');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1026, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1027;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1027, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1027;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1027, total $1000');
    DEALLOCATE test;



    PREPARE test AS SELECT invoice_sum= 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1024;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1024, inv_total 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1024;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1024, inv_total 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1025;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1025, inv 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1025;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1025, inv 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1026, inv 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1026, inv 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1027;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1027, inv 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1027;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1027, inv 0');
    DEALLOCATE test;



    PREPARE test AS SELECT invoice_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1035;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1035, inv $500');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1035;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1035, inv $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1036, inv $500');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1036, inv $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 1000
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1037;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1037, inv $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1037;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1037, inv $1000');
    DEALLOCATE test;



    PREPARE test AS SELECT total_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1035;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1035, total $500');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 500
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1036, total $500');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1037;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1037, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1035;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1035, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1036, total $1000');
    DEALLOCATE test;

    PREPARE test AS SELECT total_sum= 1000
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1037;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1037, total $1000');
    DEALLOCATE test;




    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1035;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1035, acc 0');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1036, acc 0');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1037;
    SELECT results_eq('test',ARRAY[true],'current report, invoice -1037, acc 0');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1035;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1035, acc 0');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1036, acc 0');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report_accrual(-511, (date1() - '1 day'::interval)::date,
                                      (date1() + '1 day'::interval)::date,
                                    'Test account 2')
        WHERE invoice_id = -1037;
    SELECT results_eq('test',ARRAY[true],'current accrual report, invoice -1037, acc 0');
    DEALLOCATE test;



    PREPARE test AS SELECT acc_sum = 500
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'future report, invoice -1026, acc 500');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 0
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'future report, invoice -1026, inv 0');
    DEALLOCATE test;


    PREPARE test AS SELECT total_sum = 500
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1026;
    SELECT results_eq('test',ARRAY[true],'future report, invoice -1026, total 500');
    DEALLOCATE test;

    PREPARE test AS SELECT acc_sum = 0
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'future report, invoice -1036, acc 0');
    DEALLOCATE test;

    PREPARE test AS SELECT invoice_sum = 500
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'future report, invoice -1036, inv 500');
    DEALLOCATE test;


    PREPARE test AS SELECT total_sum = 500
        FROM tax_form_details_report(-511, (date2() - '1 day'::interval)::date,
                                      (date2() + '1 day'::interval)::date,
                                    'Test account 1')
        WHERE invoice_id = -1036;
    SELECT results_eq('test',ARRAY[true],'future report, invoice -1036, total 500');
    DEALLOCATE test;

    -- Finish the tests and clean up.
    SELECT * FROM finish();

ROLLBACK;
