From 6c0d7719e8cdbdeee52817f138bb251055feade1 Mon Sep 17 00:00:00 2001 From: Dany De Bontridder Date: Fri, 9 Jul 2010 18:45:12 +0000 Subject: [PATCH] FS#76 - Extourne : table QUANT ==================== In rel510 , the record had only a A flag, now the quant_sold / purchase and also updated when an operation is reversed --- sql/correct-quant.sql | 54 ++++++++++++++++++ sql/correct_quant_sold.sql | 53 ++++++++++++++++++ sql/upgrade.sql | 111 +++++++++++++++++++++++++++++++++++++ 3 files changed, 218 insertions(+) create mode 100644 sql/correct-quant.sql create mode 100644 sql/correct_quant_sold.sql diff --git a/sql/correct-quant.sql b/sql/correct-quant.sql new file mode 100644 index 000000000..ec4a5d18c --- /dev/null +++ b/sql/correct-quant.sql @@ -0,0 +1,54 @@ +CREATE OR REPLACE FUNCTION correct_quant_purchase() returns void +as +$BODY$ +declare + r_invalid quant_purchase; + s_QuickCode text; + b_j_debit bool; + r_new record; + r_jrnx record; +begin + +for r_invalid in select * from quant_purchase where qp_valid='A' +loop + +-- get qcode +select j_qcode into s_QuickCode from vw_poste_qcode where f_id=r_invalid.qp_fiche; +raise notice 'qp_id % Quick code is %',r_invalid.qp_id,s_QuickCode; + +-- get deb or cred +select j_debit,j_grpt,j_jrn_def,j_montant into r_jrnx from jrnx where j_id=r_invalid.j_id; +if NOT FOUND then + raise notice 'error not found jrnx %',r_invalid.j_id; + update quant_purchase set qp_valid='Y' where qp_id=r_invalid.qp_id; + continue; +end if; +raise notice 'j_debit % , j_grpt % ,j_jrn_def % qp_price %',r_jrnx.j_debit,r_jrnx.j_grpt,r_jrnx.j_jrn_def ,r_invalid.qp_price; + +select jr_internal,j_id,j_montant into r_new + from jrnx join jrn on (j_grpt=jr_grpt_id) + where + j_jrn_def=r_jrnx.j_jrn_def + and j_id not in (select j_id from quant_purchase) + and j_qcode=s_QuickCode + and j_montant=r_jrnx.j_montant + and j_debit != r_jrnx.j_debit; + +if NOT FOUND then + raise notice 'error not found %', r_invalid.j_id; + update quant_purchase set qp_valid='Y' where qp_id=r_invalid.qp_id; + continue; +end if; +raise notice 'j_id % found amount %',r_new.j_id,r_new.j_montant; + +-- insert into quant_purchase +insert into quant_purchase (qp_internal,j_id,qp_fiche,qp_quantite,qp_price,qp_vat,qp_nd_amount,qp_nd_tva_recup,qp_valid,qp_dep_priv,qp_supplier,qp_vat_code) +values (r_new.jr_internal,r_invalid.j_id,r_invalid.qp_fiche,(r_invalid.qp_quantite * (-1)),r_invalid.qp_price * (-1),r_invalid.qp_vat*(-1),r_invalid.qp_nd_amount*(-1),r_invalid.qp_nd_tva_recup*(-1) ,'Y',r_invalid.qp_dep_priv*(-1),r_invalid.qp_supplier,r_invalid.qp_vat_code); + +update quant_purchase set qp_valid='Y' where qp_id=r_invalid.qp_id; +end loop; +return; +end; +$BODY$ + LANGUAGE 'plpgsql' VOLATILE; + diff --git a/sql/correct_quant_sold.sql b/sql/correct_quant_sold.sql new file mode 100644 index 000000000..9fbc74904 --- /dev/null +++ b/sql/correct_quant_sold.sql @@ -0,0 +1,53 @@ +CREATE OR REPLACE FUNCTION correct_quant_sale() returns void +as +$BODY$ +declare + r_invalid quant_sold; + s_QuickCode text; + b_j_debit bool; + r_new record; + r_jrnx record; +begin + +for r_invalid in select * from quant_sold where qs_valid='A' +loop + +-- get qcode +select j_qcode into s_QuickCode from vw_poste_qcode where f_id=r_invalid.qs_fiche; +raise notice 'qp_id % Quick code is %',r_invalid.qs_id,s_QuickCode; + +-- get deb or cred +select j_debit,j_grpt,j_jrn_def,j_montant into r_jrnx from jrnx where j_id=r_invalid.j_id; +if NOT FOUND then + update quant_sold set qs_valid='Y' where qs_id=r_invalid.qs_id; + raise notice 'error not found jrnx %',r_invalid.j_id; + continue; +end if; +raise notice 'j_debit % , j_grpt % ,j_jrn_def % qs_price %',r_jrnx.j_debit,r_jrnx.j_grpt,r_jrnx.j_jrn_def ,r_invalid.qs_price; + +select jr_internal,j_id,j_montant into r_new + from jrnx join jrn on (j_grpt=jr_grpt_id) + where + j_jrn_def=r_jrnx.j_jrn_def + and j_id not in (select j_id from quant_sold) + and j_qcode=s_QuickCode + and j_montant=r_jrnx.j_montant + and j_debit != r_jrnx.j_debit; + +if NOT FOUND then + update quant_sold set qs_valid='Y' where qs_id=r_invalid.qs_id; + raise notice 'error not found %', r_invalid.j_id; + continue; +end if; +raise notice 'j_id % found amount %',r_new.j_id,r_new.j_montant; + +-- insert into quant_sold + + insert into quant_sold (qs_internal,j_id,qs_fiche,qs_quantite,qs_price,qs_vat,qs_valid,qs_client,qs_vat_code) + values (r_new.jr_internal,r_invalid.j_id,r_invalid.qs_fiche,(r_invalid.qs_quantite * (-1)),r_invalid.qs_price * (-1),r_invalid.qs_vat*(-1),'Y',r_invalid.qs_client,r_invalid.qs_vat_code); + update quant_sold set qs_valid='Y' where qs_id=r_invalid.qs_id; +end loop; +return; +end; +$BODY$ + LANGUAGE 'plpgsql' VOLATILE; \ No newline at end of file diff --git a/sql/upgrade.sql b/sql/upgrade.sql index 3f47fbeea..68a06883d 100644 --- a/sql/upgrade.sql +++ b/sql/upgrade.sql @@ -7,3 +7,114 @@ alter table quant_purchase alter qp_internal drop not null; ALTER TABLE jrnx ADD CONSTRAINT jrnx_f_id_fkey FOREIGN KEY (f_id) REFERENCES fiche (f_id) MATCH SIMPLE ON UPDATE cascade ON DELETE NO ACTION; CREATE INDEX fki_jrnx_f_id ON jrnx USING btree (f_id); +CREATE OR REPLACE FUNCTION correct_quant_purchase() returns void +as +$BODY$ +declare + r_invalid quant_purchase; + s_QuickCode text; + b_j_debit bool; + r_new record; + r_jrnx record; +begin + +for r_invalid in select * from quant_purchase where qp_valid='A' +loop + +-- get qcode +select j_qcode into s_QuickCode from vw_poste_qcode where f_id=r_invalid.qp_fiche; +raise notice 'qp_id % Quick code is %',r_invalid.qp_id,s_QuickCode; + +-- get deb or cred +select j_debit,j_grpt,j_jrn_def,j_montant into r_jrnx from jrnx where j_id=r_invalid.j_id; +if NOT FOUND then + raise notice 'error not found jrnx %',r_invalid.j_id; + update quant_purchase set qp_valid='Y' where qp_id=r_invalid.qp_id; + continue; +end if; +raise notice 'j_debit % , j_grpt % ,j_jrn_def % qp_price %',r_jrnx.j_debit,r_jrnx.j_grpt,r_jrnx.j_jrn_def ,r_invalid.qp_price; + +select jr_internal,j_id,j_montant into r_new + from jrnx join jrn on (j_grpt=jr_grpt_id) + where + j_jrn_def=r_jrnx.j_jrn_def + and j_id not in (select j_id from quant_purchase) + and j_qcode=s_QuickCode + and j_montant=r_jrnx.j_montant + and j_debit != r_jrnx.j_debit; + +if NOT FOUND then + raise notice 'error not found %', r_invalid.j_id; + update quant_purchase set qp_valid='Y' where qp_id=r_invalid.qp_id; + continue; +end if; +raise notice 'j_id % found amount %',r_new.j_id,r_new.j_montant; + +-- insert into quant_purchase +insert into quant_purchase (qp_internal,j_id,qp_fiche,qp_quantite,qp_price,qp_vat,qp_nd_amount,qp_nd_tva_recup,qp_valid,qp_dep_priv,qp_supplier,qp_vat_code) +values (r_new.jr_internal,r_invalid.j_id,r_invalid.qp_fiche,(r_invalid.qp_quantite * (-1)),r_invalid.qp_price * (-1),r_invalid.qp_vat*(-1),r_invalid.qp_nd_amount*(-1),r_invalid.qp_nd_tva_recup*(-1) ,'Y',r_invalid.qp_dep_priv*(-1),r_invalid.qp_supplier,r_invalid.qp_vat_code); + +update quant_purchase set qp_valid='Y' where qp_id=r_invalid.qp_id; +end loop; +return; +end; +$BODY$ + LANGUAGE 'plpgsql' VOLATILE; + +select correct_quant_purchase(); + +CREATE OR REPLACE FUNCTION correct_quant_sale() returns void +as +$BODY$ +declare + r_invalid quant_sold; + s_QuickCode text; + b_j_debit bool; + r_new record; + r_jrnx record; +begin + +for r_invalid in select * from quant_sold where qs_valid='A' +loop + +-- get qcode +select j_qcode into s_QuickCode from vw_poste_qcode where f_id=r_invalid.qs_fiche; +raise notice 'qp_id % Quick code is %',r_invalid.qs_id,s_QuickCode; + +-- get deb or cred +select j_debit,j_grpt,j_jrn_def,j_montant into r_jrnx from jrnx where j_id=r_invalid.j_id; +if NOT FOUND then + update quant_sold set qs_valid='Y' where qs_id=r_invalid.qs_id; + raise notice 'error not found jrnx %',r_invalid.j_id; + continue; +end if; +raise notice 'j_debit % , j_grpt % ,j_jrn_def % qs_price %',r_jrnx.j_debit,r_jrnx.j_grpt,r_jrnx.j_jrn_def ,r_invalid.qs_price; + +select jr_internal,j_id,j_montant into r_new + from jrnx join jrn on (j_grpt=jr_grpt_id) + where + j_jrn_def=r_jrnx.j_jrn_def + and j_id not in (select j_id from quant_sold) + and j_qcode=s_QuickCode + and j_montant=r_jrnx.j_montant + and j_debit != r_jrnx.j_debit; + +if NOT FOUND then + update quant_sold set qs_valid='Y' where qs_id=r_invalid.qs_id; + raise notice 'error not found %', r_invalid.j_id; + continue; +end if; +raise notice 'j_id % found amount %',r_new.j_id,r_new.j_montant; + +-- insert into quant_sold + + insert into quant_sold (qs_internal,j_id,qs_fiche,qs_quantite,qs_price,qs_vat,qs_valid,qs_client,qs_vat_code) + values (r_new.jr_internal,r_invalid.j_id,r_invalid.qs_fiche,(r_invalid.qs_quantite * (-1)),r_invalid.qs_price * (-1),r_invalid.qs_vat*(-1),'Y',r_invalid.qs_client,r_invalid.qs_vat_code); + update quant_sold set qs_valid='Y' where qs_id=r_invalid.qs_id; +end loop; +return; +end; +$BODY$ + LANGUAGE 'plpgsql' VOLATILE; + +select correct_quant_sale() ; \ No newline at end of file