From a77dfacbf8bc4c64eeb049ef5fd97a0c6cf1307e Mon Sep 17 00:00:00 2001 From: sparkyx Date: Sat, 23 Dec 2023 16:55:42 +0100 Subject: [PATCH] 0002317: Paiement : forcer la fiche pour les banques --- include/class/acc_ledger.class.php | 2 +- include/class/acc_ledger_fin.class.php | 3 +- include/class/acc_ledger_purchase.class.php | 23 +++-- include/class/acc_ledger_sale.class.php | 27 ++++-- .../include/class/acc_ledger_purchaseTest.php | 97 +++++++++++++++++++ .../include/class/acc_ledger_saleTest.php | 90 ++++++++++++++++- 6 files changed, 222 insertions(+), 20 deletions(-) diff --git a/include/class/acc_ledger.class.php b/include/class/acc_ledger.class.php index 94f89cb11..267b5f79b 100644 --- a/include/class/acc_ledger.class.php +++ b/include/class/acc_ledger.class.php @@ -128,7 +128,7 @@ class Acc_Ledger extends jrn_def_sql } /** * @brief Return the type of a ledger (ACH,VEN,ODS or FIN) or GL - * + * @return string FIN ODS ACH VEN or GL if id == 0 */ function get_type() { diff --git a/include/class/acc_ledger_fin.class.php b/include/class/acc_ledger_fin.class.php index 5ad81d64e..166037523 100644 --- a/include/class/acc_ledger_fin.class.php +++ b/include/class/acc_ledger_fin.class.php @@ -1136,11 +1136,12 @@ class Acc_Ledger_Fin extends Acc_Ledger } /** - * return the fiche_id of the bank + * @brief find and FICHE.F_ID of the bank or -1 if not found */ function get_bank() { $bank_id=$this->db->get_value('select jrn_def_bank from jrn_def where jrn_def_id=$1', array($this->id)); + if (empty($bank_id)) return -1; return $bank_id; } diff --git a/include/class/acc_ledger_purchase.class.php b/include/class/acc_ledger_purchase.class.php index f9a6ca35d..6f32d645c 100644 --- a/include/class/acc_ledger_purchase.class.php +++ b/include/class/acc_ledger_purchase.class.php @@ -950,20 +950,22 @@ class Acc_Ledger_Purchase extends Acc_Ledger $mp=new Acc_Payment($this->db,$e_mp); $mp->load(); - /* fiche */ - if ($mp->get_parameter('qcode') == '') - $fqcode=${'e_mp_qcode_'.$e_mp}; - else - $fqcode=$mp->get_parameter('qcode'); - - $acfiche = new Fiche($this->db); - $acfiche->get_by_qcode($fqcode); - /* jrnx */ $acseq=$this->db->get_next_seq('s_grpt'); $acjrn=new Acc_Ledger($this->db,$mp->get_parameter('ledger_target')); $acinternal=$acjrn->compute_internal_code($acseq); - + /* + * for the use of the card of the bank + */ + if ( $acjrn->get_type()=='FIN') { + $acjrn=new Acc_Ledger_Fin($this->db, $mp->get_parameter('ledger_target')); + $acfiche=new Fiche($this->db,$acjrn->get_bank()); + $fqcode=$acfiche->strAttribut(ATTR_DEF_QUICKCODE); + } else { + $fqcode = ${'e_mp_qcode_' . $e_mp}; + $acfiche = new Fiche($this->db); + $acfiche->get_by_qcode($fqcode); + } /* Insert paid by */ $acc_pay=new Acc_Operation($this->db); $acc_pay->date=$e_date; @@ -2141,6 +2143,7 @@ EOF; return $array; } + } diff --git a/include/class/acc_ledger_sale.class.php b/include/class/acc_ledger_sale.class.php index c8ca25eb4..69da7c3aa 100644 --- a/include/class/acc_ledger_sale.class.php +++ b/include/class/acc_ledger_sale.class.php @@ -242,9 +242,13 @@ class Acc_Ledger_Sale extends Acc_Ledger { $this->check_currency_setting($p_currency_code); } - /*!\brief insert into the database, it calls first the verify function, - * change the value of this->jr_id and this->jr_internal - * * It generates the document if gen_invoice is set and save the middle of payment if any ($e_mp) + /*! + * \brief insert into the database, it calls first the verify function, + * store the value of the inserted operation in $this->jr_id and this->jr_internal + * + * It generates the document if gen_invoice is set and save the middle of payment if any ($e_mp) + * + * It also create a second operation if there is a payment * * \param $p_array is usually $_POST or a predefined operation * \return string : internal number @@ -653,16 +657,25 @@ class Acc_Ledger_Sale extends Acc_Ledger { $mp = new Acc_Payment($this->db, $e_mp); $mp->load(); - /* fiche */ - $fqcode = ${'e_mp_qcode_' . $e_mp}; - $acfiche = new Fiche($this->db); - $acfiche->get_by_qcode($fqcode); /* jrnx */ $acseq = $this->db->get_next_seq('s_grpt'); $acjrn = new Acc_Ledger($this->db, $mp->get_parameter('ledger_target')); $acinternal = $acjrn->compute_internal_code($acseq); + /* + * for the use of the card of the bank + */ + if ( $acjrn->get_type()=='FIN') { + $acjrn=new Acc_Ledger_Fin($this->db, $mp->get_parameter('ledger_target')); + $acfiche=new Fiche($this->db,$acjrn->get_bank()); + $fqcode=$acfiche->strAttribut(ATTR_DEF_QUICKCODE); + } else { + $fqcode = ${'e_mp_qcode_' . $e_mp}; + $acfiche = new Fiche($this->db); + $acfiche->get_by_qcode($fqcode); + } + /* Insert paid by */ $acc_pay = new Acc_Operation($this->db); $acc_pay->date = $pay_date; diff --git a/unit-test/include/class/acc_ledger_purchaseTest.php b/unit-test/include/class/acc_ledger_purchaseTest.php index 9f17ab819..e7c13a162 100644 --- a/unit-test/include/class/acc_ledger_purchaseTest.php +++ b/unit-test/include/class/acc_ledger_purchaseTest.php @@ -222,6 +222,103 @@ class Acc_Ledger_PurchaseTest extends TestCase $this->clean_operation(); } + /** + * @covers Acc_Ledger_Purchase::insert + */ + public function testInsertPayment() + { + global $g_connection; + $array=$this->array; + $array["mt"]="1572704002.1732"; + $array["pa_id"]=array(2); + $array["op"]=array(0); + $array["amount_t0"]=658.25; + $array['hplan']=array(array(-1)); + $array["val"]=array(array(658.25)); + $sql=" + from quant_purchase + join jrnx using(j_id) + join jrn on (jr_grpt_id=j_grpt) + where + jr_mt='1572704002.1732' + and j_qcode='LOYER' + "; + + $this->clean_operation(); + $array=array_merge($array, array("e_march1"=>"DOCUME", + "e_march1_price"=>18.25, + "e_quant1"=>"", + "htva_march1"=>18.25, + "e_march1_tva_id"=>1, + "e_march1_tva_amount"=>22.08, + "tva_march1"=>3.83, + "tvac_march1"=>22.08 + ,"p_currency_rate"=>1 + ,"p_currency_code"=>0 + )); + + // create a payment method with a valid card + $payment_methodSQL=$this->insert_payment_method(); + $array['mp_date'] =""; + $array['acompte'] = 0; + $array['e_mp'] = $payment_methodSQL->getp("mp_id"); + $array['e_mp_qcode_'.$array['e_mp']]='CDOLLAR'; + $this->object->insert($array); + + $this->assertEquals($array['htva_march0'],$g_connection->get_value("select qp_price ".$sql)); + + // check payment + $nQuant_FinId=$this->get_reconcilied_operation(); + + $quant_fin=new Quant_Fin_SQL($g_connection,$nQuant_FinId); + $nQuantFin_Amount=$quant_fin->getp("qf_amount") ; + + $this->assertTrue($nQuantFin_Amount == -658.25,"error : purchase 658.25 and payment {$nQuantFin_Amount} not equal "); + + // check card used in bank + $expected_bank=$g_connection->get_value(" + select jrn_def_bank + from + jrn_def jd1 + join payment_method pm1 on (jd1.jrn_def_id=pm1.mp_jrn_def_id) + where mp_id=$1",[$array['e_mp']]); + + $found_bank =$quant_fin->getp("qf_bank"); + + $this->assertTrue($expected_bank==$found_bank,"error : payment done with a wrong card {$found_bank} instead of $expected_bank"); + $payment_methodSQL->delete(); + $this->clean_operation(); + + } + + private function insert_payment_method() + { + global $g_connection; + $payment_methodSQl=new Payment_method_SQL($g_connection); + $payment_methodSQl->from_array([ + "mp_lib"=>"caisse" + ,"mp_jrn_def_id"=>1 + ,'mp_fd_id'=>3 + ,"jrn_def_id"=>3 + ]); + $payment_methodSQl->insert(); + return $payment_methodSQl; + + + } + /** + * @brief return the reconcilied operation of this->object + * @return mixed|string + * @throws Exception + */ + private function get_reconcilied_operation() + { + global $g_connection; + $nValue=$g_connection->get_value("select jra_concerned + from jrn_rapt where jr_id=$1",[$this->object->jr_id]); + $nQuant_FinId=$g_connection->get_value("select qf_id from quant_fin where jr_id=$1",[$nValue]); + return $nQuant_FinId; + } /** * @brief set special attributes to test NOT DEDUCTIBLE : private, VAT and tax * @global type $g_connection diff --git a/unit-test/include/class/acc_ledger_saleTest.php b/unit-test/include/class/acc_ledger_saleTest.php index 2d821f2ba..de982eff6 100644 --- a/unit-test/include/class/acc_ledger_saleTest.php +++ b/unit-test/include/class/acc_ledger_saleTest.php @@ -83,8 +83,23 @@ class Acc_Ledger_SaleTest extends TestCase private function clean_operation() { global $g_connection; - $g_connection->exec_sql("delete from jrn where jr_mt=$1", ["1572714478.3155"]); + $mt="1572714478.3155"; + //delete reconcilied operations + $g_connection->exec_sql(" + delete from jrn + where jr_id in (select jr2.jr_id + from jrn_rapt ra1 join jrn jr2 on (ra1.jr_id=jr2.jr_id) + where jr2.jr_mt=$1)",[$mt]); + + $g_connection->exec_sql(" + delete from jrn + where jr_id in (select jr2.jr_id + from jrn_rapt ra1 join jrn jr2 on (ra1.jra_concerned=jr2.jr_id) + where jr2.jr_mt=$1)",[$mt]); + + $g_connection->exec_sql("delete from jrn where jr_mt=$1", [$mt]); $g_connection->exec_sql("delete from jrnx where j_grpt not in (select jr_grpt_id from jrn)"); + $g_connection->exec_sql("alter sequence s_jrn_pj2 restart with 40"); } /** @@ -168,6 +183,78 @@ class Acc_Ledger_SaleTest extends TestCase } + + /** + * @covers Acc_Ledger_Sale::insert + */ + public function testInsertPayment() + { + global $g_connection; + $this->clean_operation(); + $cnt=$g_connection->get_value("select count(*) from jrn where jr_mt=$1",["1572714478.3155"]); + $this->assertEquals(0,$cnt); + + $sql=" + from quant_sold + join jrnx using(j_id) + join jrn on (jr_grpt_id=j_grpt) + where + jr_mt='1572714478.3155' + and j_qcode='MARCHA' + "; + $array=$this->array; + $array["pa_id"]=array(2); + $array["op"]=array(0, 1); + $array["amount_t0"]=24.2; + $array["hplan"]=array(array(-1), array(-1)); + $array["val"]=array(array(24, 2), array(1212.5)); + $array["mt"]="1572714478.3155"; + + // create a payment method with a valid card + $array['mp_date'] =""; + $array['acompte'] = 0; + $array['e_mp'] = 1; + + $this->object->insert($array); + $this->assertEquals($array['htva_march1'],$g_connection->get_value("select qs_price ".$sql)); + + // check payment + $nQuant_FinId=$this->get_reconcilied_operation(); + + $quant_fin=new Quant_Fin_SQL($g_connection,$nQuant_FinId); + $nQuantFin_Amount=$quant_fin->getp("qf_amount") ; + + $this->assertTrue($nQuantFin_Amount ==bcadd( $array['tvac_march1'],$array['tvac_march0']),"error : sale {$array['tvac_march1']} and payment {$nQuantFin_Amount} not equal "); + + // check card used in bank + $expected_bank=$g_connection->get_value(" + select jrn_def_bank + from + jrn_def jd1 + join payment_method pm1 on (jd1.jrn_def_id=pm1.mp_jrn_def_id) + where mp_id=$1",[$array['e_mp']]); + + $found_bank =$quant_fin->getp("qf_bank"); + + $this->assertTrue($expected_bank==$found_bank,"error : payment done with a wrong card {$found_bank} instead of $expected_bank"); + + $this->clean_operation(); + + } + + /** + * @brief return the reconcilied operation of this->object + * @return mixed|string + * @throws Exception + */ + private function get_reconcilied_operation() + { + global $g_connection; + $nValue=$g_connection->get_value("select jra_concerned + from jrn_rapt where jr_id=$1",[$this->object->jr_id]); + $nQuant_FinId=$g_connection->get_value("select qf_id from quant_fin where jr_id=$1",[$nValue]); + return $nQuant_FinId; + } /** * @covers Acc_Ledger_Sale::confirm */ @@ -240,4 +327,5 @@ class Acc_Ledger_SaleTest extends TestCase $ret=$this->object->get_detail_sale(92,103,'unpaid'); $this->assertEquals(5,Database::num_row($ret),'only unpaid operations'); } + }