diff --git a/html/admin/sql/patch/upgrade93.sql b/html/admin/sql/patch/upgrade93.sql new file mode 100644 index 000000000..aa8ce4f70 --- /dev/null +++ b/html/admin/sql/patch/upgrade93.sql @@ -0,0 +1,122 @@ +begin; + +alter table operation_analytique drop column pa_id; +ALTER TABLE operation_analytique ADD CONSTRAINT operation_analytique_oa_amount_check CHECK (oa_amount >= 0::numeric); + +create type anc_table_card_type as (po_id bigint,pa_id bigint,PO_NAME TEXT,po_description text,sum_amount numeric(25,4),f_id bigint,card_account text,name text); + +drop function comptaproc.table_analytic_card (p_from text,p_to text); + +create or replace function comptaproc.table_analytic_card (p_from text,p_to text) +returns setof anc_table_card_type +as +$BODY$ +declare + ret ANC_table_card_type%ROWTYPE; + sql_from text:=''; + sql_to text:=''; + sWhere text:=''; + sAnd text:=''; + sResult text:=''; +begin +if p_from <> '' and p_from is not null then + sql_from:='oa_date >= to_date('''||p_from::text||''',''DD.MM.YYYY'')'; + sWhere:=' where '; +end if; + +if p_to <> '' and p_to is not null then + sql_to=' oa_date <= to_date('''||p_to::text||''',''DD.MM.YYYY'')'; + sWhere := ' where '; +end if; + +if sql_to <> '' and sql_from <> '' then + sAnd :=' and '; +end if; + +sResult := sWhere || sql_from || sAnd || sql_to; + +for ret in EXECUTE ' SELECT po.po_id, po.pa_id, po.po_name, po.po_description, sum( + CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount + END) AS sum_amount, jrnx.f_id, jrnx.j_qcode, ( SELECT fiche_detail.ad_value + FROM fiche_detail + WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id) AS name + FROM operation_analytique + JOIN poste_analytique po USING (po_id) + JOIN jrnx USING (j_id)'|| sResult ||' + GROUP BY po.po_id, po.po_name, po.pa_id, jrnx.f_id, jrnx.j_qcode, ( SELECT fiche_detail.ad_value + FROM fiche_detail + WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id), po.po_description + HAVING sum( +CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount +END) <> 0::numeric;' + + + loop + return next ret; +end loop; +end; +$BODY$ language plpgsql; + + +create type anc_table_account_type as (po_id bigint,pa_id bigint,PO_NAME TEXT,po_description text,sum_amount numeric(25,4),card_account text,name text); + +drop function comptaproc.table_analytic_account (p_from text,p_to text); +create or replace function comptaproc.table_analytic_account (p_from text,p_to text) +returns setof anc_table_account_type +as +$BODY$ +declare + ret ANC_table_account_type%ROWTYPE; + sql_from text:=''; + sql_to text:=''; + sWhere text:=''; + sAnd text:=''; + sResult text:=''; +begin +if p_from <> '' and p_from is not null then + sql_from:='oa_date >= to_date('''||p_from::text||''',''DD.MM.YYYY'')'; + sWhere:=' where '; +end if; + +if p_to <> '' and p_to is not null then + sql_to=' oa_date <= to_date('''||p_to::text||''',''DD.MM.YYYY'')'; + sWhere := ' where '; +end if; + +if sql_to <> '' and sql_from <> '' then + sAnd:=' and '; +end if; + +sResult := sWhere || sql_from || sAnd || sql_to; + +for ret in EXECUTE 'SELECT po.po_id, + po.pa_id, po.po_name, + po.po_description,sum( + CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount + END) AS sum_amount, jrnx.j_poste, tmp_pcmn.pcm_lib AS name + FROM operation_analytique + JOIN poste_analytique po USING (po_id) + JOIN jrnx USING (j_id) + JOIN tmp_pcmn ON jrnx.j_poste::text = tmp_pcmn.pcm_val::text +'|| sResult ||' + GROUP BY po.po_id, po.po_name, po.pa_id, jrnx.j_poste, tmp_pcmn.pcm_lib, po.po_description + HAVING sum( +CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount +END) <> 0::numeric ' + loop + return next ret; +end loop; +end; +$BODY$ language plpgsql; + + +update version set val=94; +commit; diff --git a/include/class_anc_acc_list.php b/include/class_anc_acc_list.php index cbd5f68d9..564189d71 100644 --- a/include/class_anc_acc_list.php +++ b/include/class_anc_acc_list.php @@ -63,10 +63,25 @@ class Anc_Acc_List extends Anc_Acc_Link $date=($date != '')?" $date":''; $sql_from_poste=($this->from_poste!='')?" and po.po_name >= upper('".Database::escape_string($this->from_poste)."')":''; $sql_to_poste=($this->to_poste!='')?" and po.po_name <= upper('".Database::escape_string($this->to_poste)."')":''; - $this->arow=$this->db->get_array(" SELECT po_id, pa_id, po_name, po_description, sum_amount, j_poste, name - FROM v_table_analytic_account - where - pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste." order by po_id,j_poste",array($this->pa_id)); + $this->arow=$this->db->get_array(" + SELECT po.po_id, po.pa_id, po.po_name, po.po_description, sum( + CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount + END) AS sum_amount, jrnx.j_poste, tmp_pcmn.pcm_lib AS name + FROM operation_analytique + JOIN poste_analytique po USING (po_id) + JOIN jrnx USING (j_id) + JOIN tmp_pcmn ON jrnx.j_poste::text = tmp_pcmn.pcm_val::text ". +" where + pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste." + + GROUP BY po.po_id, po.po_name, po.pa_id, jrnx.j_poste, tmp_pcmn.pcm_lib, po.po_description + HAVING sum( +CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount +END) <> 0::numeric order by po_id,j_poste",array($this->pa_id)); } /** @@ -79,12 +94,26 @@ class Anc_Acc_List extends Anc_Acc_Link $date=($date != '')?" $date":''; $sql_from_poste=($this->from_poste!='')?" and po.po_name >= upper('".Database::escape_string($this->from_poste)."')":''; $sql_to_poste=($this->to_poste!='')?" and po.po_name <= upper('".Database::escape_string($this->to_poste)."')":''; - - $this->arow=$this->db->get_array(" SELECT po_id, pa_id, po_name, po_description, oa_date, sum_amount, f_id, - j_qcode, name - FROM v_table_analytic_card - where - pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste." order by po_id,name",array($this->pa_id)); + $this->arow=$this->db->get_array(" SELECT po.po_id, po.pa_id, po.po_name, po.po_description, sum( + CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount + END) AS sum_amount, jrnx.f_id, jrnx.j_qcode, ( SELECT fiche_detail.ad_value + FROM fiche_detail + WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id) AS name + FROM operation_analytique + JOIN poste_analytique po USING (po_id) + JOIN jrnx USING (j_id) ". + " where pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste + ." + GROUP BY po.po_id, po.po_name, po.pa_id, jrnx.f_id, jrnx.j_qcode, ( SELECT fiche_detail.ad_value + FROM fiche_detail + WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id), po.po_description + HAVING sum( +CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount +END) <> 0::numeric order by po_name,name",array($this->pa_id)); } @@ -98,10 +127,24 @@ class Anc_Acc_List extends Anc_Acc_Link $date=($date != '')?" $date":''; $sql_from_poste=($this->from_poste!='')?" and po.po_name >= upper('".Database::escape_string($this->from_poste)."')":''; $sql_to_poste=($this->to_poste!='')?" and po.po_name <= upper('".Database::escape_string($this->to_poste)."')":''; - $this->arow=$this->db->get_array(" SELECT po_id, pa_id, po_name, po_description, sum_amount, j_poste, name - FROM v_table_analytic_account - where - pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste." order by j_poste,po_name",array($this->pa_id)); + $this->arow=$this->db->get_array("SELECT po.po_id, po.pa_id, po.po_name, po.po_description, sum( + CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount + END) AS sum_amount, jrnx.j_poste, tmp_pcmn.pcm_lib AS name + FROM operation_analytique + JOIN poste_analytique po USING (po_id) + JOIN jrnx USING (j_id) + JOIN tmp_pcmn ON jrnx.j_poste::text = tmp_pcmn.pcm_val::text ". +" where + pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste." + + GROUP BY po.po_id, po.po_name, po.pa_id, jrnx.j_poste, tmp_pcmn.pcm_lib, po.po_description + HAVING sum( +CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount +END) <> 0::numeric order by j_poste,po_name",array($this->pa_id)); } @@ -116,12 +159,26 @@ class Anc_Acc_List extends Anc_Acc_Link $sql_from_poste=($this->from_poste!='')?" and po.po_name >= upper('".Database::escape_string($this->from_poste)."')":''; $sql_to_poste=($this->to_poste!='')?" and po.po_name <= upper('".Database::escape_string($this->to_poste)."')":''; - $this->arow=$this->db->get_array(" SELECT po_id, pa_id, po_name, po_description, oa_date, sum_amount, f_id, - j_qcode, name - FROM v_table_analytic_card - where - pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste." order by name,po_name",array($this->pa_id)); - + $this->arow=$this->db->get_array(" SELECT po.po_id, po.pa_id, po.po_name, po.po_description, sum( + CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount + END) AS sum_amount, jrnx.f_id, jrnx.j_qcode, ( SELECT fiche_detail.ad_value + FROM fiche_detail + WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id) AS name + FROM operation_analytique + JOIN poste_analytique po USING (po_id) + JOIN jrnx USING (j_id) ". + " where pa_id=$1 ".$date.$sql_from_poste.$sql_to_poste + ." + GROUP BY po.po_id, po.po_name, po.pa_id, jrnx.f_id, jrnx.j_qcode, ( SELECT fiche_detail.ad_value + FROM fiche_detail + WHERE fiche_detail.ad_id = 1 AND fiche_detail.f_id = jrnx.f_id), po.po_description + HAVING sum( +CASE + WHEN operation_analytique.oa_debit = true THEN operation_analytique.oa_amount * (-1)::numeric + ELSE operation_analytique.oa_amount +END) <> 0::numeric order by name,po_name",array($this->pa_id)); } /** *@brief display the button export CSV @@ -297,7 +354,7 @@ class Anc_Acc_List extends Anc_Acc_Link $tot_card=bcadd($tot_card,$amount); $tot_glob=bcadd($tot_glob,$amount); - echo '