diff --git a/deploy/add_invoice.sql b/deploy/add_invoice.sql index 7c998c9..01a21a2 100644 --- a/deploy/add_invoice.sql +++ b/deploy/add_invoice.sql @@ -8,6 +8,7 @@ -- requires: tax -- requires: invoice_product -- requires: invoice_product_tax +-- requires: next_invoice_number begin; @@ -22,6 +23,10 @@ declare ccode text; ipid integer; begin + if invoice_number is null or length(trim(invoice_number)) = 0 then + invoice_number = next_invoice_number(company_id, invoice_date); + end if; + insert into invoice (company_id, invoice_number, invoice_date, contact_id, notes, currency_code) select company.company_id , invoice_number diff --git a/sqitch.plan b/sqitch.plan index 001bbd5..898cf29 100644 --- a/sqitch.plan +++ b/sqitch.plan @@ -53,6 +53,6 @@ add_product [schema_numerus product product_tax parse_price company currency] 20 edit_product [schema_numerus product product_tax parse_price company currency] 2023-02-14T11:06:03Z jordi fita mas # Add function to edit products invoice_product_tax [schema_numerus invoice_product tax tax_rate] 2023-02-15T13:20:30Z jordi fita mas # Add relation for taxes in invoice products new_invoice_product [schema_numerus] 2023-02-16T21:06:01Z jordi fita mas # Add type for passing products to new invoices -add_invoice [schema_numerus invoice company currency parse_price new_invoice_product tax invoice_product invoice_product_tax] 2023-02-16T21:12:46Z jordi fita mas # Add function to create new invoices invoice_number_counter [schema_numerus company] 2023-02-17T13:04:48Z jordi fita mas # Add relation to count invoice numbers next_invoice_number [schema_numerus invoice_number_counter] 2023-02-17T13:21:48Z jordi fita mas # Add function to retrieve the next invoice number +add_invoice [schema_numerus invoice company currency parse_price new_invoice_product tax invoice_product invoice_product_tax next_invoice_number] 2023-02-16T21:12:46Z jordi fita mas # Add function to create new invoices diff --git a/test/add_invoice.sql b/test/add_invoice.sql index a18ef76..2714db2 100644 --- a/test/add_invoice.sql +++ b/test/add_invoice.sql @@ -5,7 +5,7 @@ reset client_min_messages; begin; -select plan(15); +select plan(17); set search_path to auth, numerus, public; @@ -21,6 +21,7 @@ select function_privs_are('numerus', 'add_invoice', array ['integer', 'text', 'd set client_min_messages to warning; +truncate invoice_number_counter cascade; truncate invoice_product_tax cascade; truncate invoice_product cascade; truncate invoice cascade; @@ -30,9 +31,14 @@ truncate tax 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, country_code, currency_code) -values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR') - , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD') +insert into company (company_id, business_name, vatin, trade_name, phone, email, web, address, city, province, postal_code, country_code, currency_code, invoice_number_format) +values (1, 'Company 2', 'XX123', '', '555-555-555', 'a@a', '', '', '', '', '', 'ES', 'EUR', '"F"YYYY0000') + , (2, 'Company 4', 'XX234', '', '666-666-666', 'b@b', '', '', '', '', '', 'FR', 'USD', '"INV"000-YY') +; + +insert into invoice_number_counter (company_id, year, currval) +values (1, 2023, '5') + , (2, 2023, '55') ; insert into tax (tax_id, company_id, name, rate) @@ -73,21 +79,35 @@ select lives_ok( 'Should be able to insert an invoice for the second company with a product' ); +select lives_ok( + $$ select add_invoice(1, NULL, '2023-03-15', 13, '', '{"(7,PA1,DA1,44.33,1,0.50,{})"}') $$, + 'Should be able to insert an invoice with an autogenerated number' +); + +select lives_ok( + $$ select add_invoice(2, ' ', '2023-04-16', 14, '', '{"(11,PA2,DA2,55.33,10,0.75,{})"}') $$, + 'Should consider non-null, but otherwise empty numbers the same as null and autogenerate it' +); + select bag_eq( $$ select company_id, invoice_number, invoice_date, contact_id, invoice_status, notes, currency_code, created_at from invoice $$, - $$ values (1, 'INV001', '2023-02-15'::date, 12, 'created', 'Notes 1', 'EUR', current_timestamp) - , (1, 'INV002', '2023-02-16'::date, 13, 'created', 'Notes 2', 'EUR', current_timestamp) - , (2, 'INV101', '2023-02-14'::date, 15, 'created', 'Notes 3', 'USD', current_timestamp) + $$ values (1, 'INV001', '2023-02-15'::date, 12, 'created', 'Notes 1', 'EUR', current_timestamp) + , (1, 'INV002', '2023-02-16'::date, 13, 'created', 'Notes 2', 'EUR', current_timestamp) + , (2, 'INV101', '2023-02-14'::date, 15, 'created', 'Notes 3', 'USD', current_timestamp) + , (1, 'F20230006', '2023-03-15'::date, 13, 'created', '', 'EUR', current_timestamp) + , (2, 'INV056-23', '2023-04-16'::date, 14, 'created', '', 'USD', current_timestamp) $$, 'Should have created 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 ('INV001', 7, 'Product 1', 'Description 1', 1224, 2, 0.00) - , ('INV002', 7, 'Product 1 bis', 'Description 1 bis', 3333, 1, 0.50) - , ('INV002', 8, 'Product 2', 'Description 2', 2400, 3, 0.75) - , ('INV101', 11, 'Product 4.3', '', 1111, 1, 0.0) + $$ values ('INV001', 7, 'Product 1', 'Description 1', 1224, 2, 0.00) + , ('INV002', 7, 'Product 1 bis', 'Description 1 bis', 3333, 1, 0.50) + , ('INV002', 8, 'Product 2', 'Description 2', 2400, 3, 0.75) + , ('INV101', 11, 'Product 4.3', '', 1111, 1, 0.0) + , ('F20230006', 7, 'PA1', 'DA1', 4433, 1, 0.50) + , ('INV056-23', 11, 'PA2', 'DA2', 5533, 10, 0.75) $$, 'Should have created all invoice products' );