From c285d69f757c366333ac37c002fef079ef8b5a24 Mon Sep 17 00:00:00 2001 From: sparkyx Date: Wed, 5 Jan 2022 13:42:13 +0100 Subject: [PATCH] =?UTF-8?q?New=20#0002117:=20Lettrage=20:=20montre=20l'op?= =?UTF-8?q?=C3=A9ration=20li=C3=A9e?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../acc_reconciliation_lettering.class.php | 59 ++ include/class/lettering.class.php | 526 ++++++++++-------- include/constant.php | 3 +- unit-test/include/class/letteringTest.php | 444 +++++++++++++++ 4 files changed, 802 insertions(+), 230 deletions(-) create mode 100644 include/class/acc_reconciliation_lettering.class.php create mode 100644 unit-test/include/class/letteringTest.php diff --git a/include/class/acc_reconciliation_lettering.class.php b/include/class/acc_reconciliation_lettering.class.php new file mode 100644 index 000000000..c6789278d --- /dev/null +++ b/include/class/acc_reconciliation_lettering.class.php @@ -0,0 +1,59 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + + +/*** + * @file + * @brief this class let you insert reconcilied operation from Lettering::save without calling auto_letter + * + */ + +/*** + * @class + * @brief this class let you insert reconcilied operation from Lettering::save without calling auto_letter + * + */ +class Acc_Reconciliation_Lettering extends Acc_Reconciliation +{ + const sql_find_operation="select jr_id from jrn join jrnx on (jr_grpt_id=j_grpt) where j_id=$1"; + /** + * @brief do nothing , it let you skip Acc_Reconcililation + * @param type $p_jr_id + * @return type + */ + public function auto_letter($p_jr_id) + { + return ; + } + /** + * @brief find the jrn.jr_id and call the Acc_Reconciliation::insert_rapt , the auto_letter will be skipped + * @param int $p_jrnx_id jrnx.j_id row id to reconcield + * @param int $p_jrnx_id_rec jrnx.j_id row id to reconcield + */ + public function insert_reconcilied(int $p_jrnx_id,int $p_jrnx_id_rec) + { + // find jr_id from both + $jrn_id = $this->db->get_value(self::sql_find_operation, + [$p_jrnx_id]); + $jrn_id2 = $this->db->get_value(self::sql_find_operation,[$p_jrnx_id_rec]); + $this->set_jr_id($jrn_id); + return $this->insert_rapt($jrn_id2); + } +} diff --git a/include/class/lettering.class.php b/include/class/lettering.class.php index 6d6187f13..91228c874 100644 --- a/include/class/lettering.class.php +++ b/include/class/lettering.class.php @@ -1,4 +1,5 @@ "account", => the accounting of the j_id (use by Lettering_Account) * - "quick_code"=>"quick_code", => the quick_code of the j_id (used by Lettering_Card) * - "start"=>"start", => date of the first day * - "end"=>"end", => date of the last day * - "sql_ledger"=>"sql_ledger" => the sql clause to filter on the available ledgers -*/ + */ class Lettering { protected $variable=array("account"=>"account", /* the accounting of the j_id (use by Lettering_Account) */ - "quick_code"=>"quick_code", /* the quick_code of the j_id (used by Lettering_Card) */ - "start"=>"start", /* date of the first day */ - "end"=>"end", /* date of the last day */ - "sql_ledger"=>"sql_ledger" /* the sql clause to filter on the available ledgers */ - ) - ; - + "quick_code"=>"quick_code", /* the quick_code of the j_id (used by Lettering_Card) */ + "start"=>"start", /* date of the first day */ + "end"=>"end", /* date of the last day */ + "sql_ledger"=>"sql_ledger" /* the sql clause to filter on the available ledgers */ + ) + + ; + /** * constructor - *@param $p_init resource to database - *@note by default start and end are the 1.1.exercice to 31.12.exercice + * @param $p_init resource to database + * @note by default start and end are the 1.1.exercice to 31.12.exercice */ - function __construct ($p_init) + function __construct($p_init) { $this->db=$p_init; $a=new User($p_init); $exercice=$a->get_exercice(); - if ($exercice > 0) { + if ($exercice>0) + { $periode=new Periode($p_init); $aLimite=$periode->get_limit($exercice); $this->start=$aLimite[0]->first_day(); $this->end=$aLimite[1]->last_day(); - } else { + } + else + { $this->start='01.01.'.$exercice; $this->end='31.12.'.$exercice; } // available ledgers - $this->sql_ledger=str_replace('jrn_def_id','jr_def_id',$a->get_ledger_sql('ALL',3)); - + $this->sql_ledger=str_replace('jrn_def_id', 'jr_def_id', $a->get_ledger_sql('ALL', 3)); } + public function get_parameter($p_string) { - if ( array_key_exists($p_string,$this->variable) ) + if (array_key_exists($p_string, $this->variable)) { $idx=$this->variable[$p_string]; return $this->$idx; } else - throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant'); + throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant'); } - public function set_parameter($p_string,$p_value) + + public function set_parameter($p_string, $p_value) { - if ( array_key_exists($p_string,$this->variable) ) + if (array_key_exists($p_string, $this->variable)) { $idx=$this->variable[$p_string]; $this->$idx=$p_value; } else - throw new Exception (__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant'); + throw new Exception(__FILE__.":".__LINE__.$p_string.'Erreur attribut inexistant'); } + /** - *Use to just insert a couple of lettered operation + * @brief Use to just insert a couple of lettered operation but do not make a link between operation */ - function insert_couple($j_id1,$j_id2) + function insert_couple($j_id1, $j_id2) { /* take needed data */ - $first=$this->db->get_value('select j_debit from jrnx where j_id=$1',array($j_id1)); - if ( $this->db->count() == 0 ) throw new Exception ('Opération non existante'); + $first=$this->db->get_value('select j_debit from jrnx where j_id=$1', array($j_id1)); + if ($this->db->count()==0) + throw new Exception('Opération non existante'); - $second=$this->db->get_value('select j_debit from jrnx where j_id=$1',array($j_id2)); - if ( $this->db->count() == 0 ) throw new Exception ('Opération non existante'); - $sql_already="select distinct(jl_id) + $second=$this->db->get_value('select j_debit from jrnx where j_id=$1', array($j_id2)); + if ($this->db->count()==0) + throw new Exception('Opération non existante'); + $sql_already="select distinct(jl_id) from jnt_letter left outer join letter_deb using (jl_id) left outer join letter_cred using (jl_id) where letter_deb.j_id = $1 or letter_cred.j_id=$1"; - $let1=0;$let2=0; - $already=$this->db->get_array($sql_already,array($j_id1)); - if ( count ($already ) > 0) { - if ( count($already)==1) { - // retrieve the letter - $let1=$this->db->get_value("select distinct(jl_id) - from jnt_letter - left outer join letter_deb using (jl_id) - left outer join letter_cred using (jl_id) - where - letter_deb.j_id = $1 or letter_cred.j_id=$1",array($j_id1)); - }else - { - return; - } - } + $let1=0; $let2=0; + $already=$this->db->get_array($sql_already, array($j_id1)); + if (count($already)>0) + { + if (count($already)==1) + { + // retrieve the letter + $let1=$this->db->get_value("select distinct(jl_id) + from jnt_letter + left outer join letter_deb using (jl_id) + left outer join letter_cred using (jl_id) + where + letter_deb.j_id = $1 or letter_cred.j_id=$1", array($j_id1)); + } + else + { + return; + } + } - $already=$this->db->get_array($sql_already,array($j_id2)); - if ( count ($already ) > 0) { - if ( count($already)==1) { - // retrieve the letter - $let2=$this->db->get_value("select distinct(jl_id) - from jnt_letter - left outer join letter_deb using (jl_id) - left outer join letter_cred using (jl_id) - where - letter_deb.j_id = $1 or letter_cred.j_id=$1",array($j_id2)); - }else { - return; - } - } - $jl_id=0; - // already linked together - if ( $let1 != 0 && $let1 == $let2 )return; + $already=$this->db->get_array($sql_already, array($j_id2)); + if (count($already)>0) + { + if (count($already)==1) + { + // retrieve the letter + $let2=$this->db->get_value("select distinct(jl_id) + from jnt_letter + left outer join letter_deb using (jl_id) + left outer join letter_cred using (jl_id) + where + letter_deb.j_id = $1 or letter_cred.j_id=$1", array($j_id2)); + } + else + { + return; + } + } + $jl_id=0; + // already linked together + if ($let1!=0&&$let1==$let2) + return; - // already linked - if ( $let1 != 0 && $let2!=0 && $let1 != $let2 )return; + // already linked + if ($let1!=0&&$let2!=0&&$let1!=$let2) + return; - // none is linked - if ( $let1 == 0 && $let2==0) - { - $jl_id=$this->db->get_next_seq("jnt_letter_jl_id_seq"); - $this->db->exec_sql('insert into jnt_letter(jl_id) values($1)', - array($jl_id)); - } - // one is linked but not the other - if ( $let1 == 0 && $let2 != 0 ) $jl_id=$let2; - if ( $let1 != 0 && $let2 == 0 ) $jl_id=$let1; + // none is linked + if ($let1==0&&$let2==0) + { + $jl_id=$this->db->get_next_seq("jnt_letter_jl_id_seq"); + $this->db->exec_sql('insert into jnt_letter(jl_id) values($1)', array($jl_id)); + } + // one is linked but not the other + if ($let1==0&&$let2!=0) + $jl_id=$let2; + if ($let1!=0&&$let2==0) + $jl_id=$let1; - /* insert */ - if ( $first == 't') + /* insert */ + if ($first=='t') { // save into letter_deb - if ($let1 == 0) $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($j_id1,$jl_id)); + if ($let1==0) + $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id', + array($j_id1, $jl_id)); } else { - if ($let1 == 0)$lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id',array($j_id1,$jl_id)); + if ($let1==0) + $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id', + array($j_id1, $jl_id)); } - if ( $second == 't') + if ($second=='t') { // save into letter_deb - if ($let2 == 0)$ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($j_id2,$jl_id)); + if ($let2==0) + $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id', + array($j_id2, $jl_id)); } else { - if ($let2 == 0)$lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id',array($j_id2,$jl_id)); + if ($let2==0) + $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id', + array($j_id2, $jl_id)); } - } + public function get_info() { - return var_export(self::$variable,true); + return var_export(self::$variable, true); } + public function verify() { // Verify that the elt we want to add is correct } + /** - *@brief save from array - *@param $p_array - @code - 'gDossier' => string '13' (length=2) - 'letter_j_id' => - ck => array - @endcode - */ + * @brief save from array, letter the accounting (or card) and create a link between operation + * + * @param $p_array + @code + gDossier => string '13' (length=2) + letter_j_id => is an array of j_id value + ck => array of j_id to letter with j_id + j_id => row to link + @endcode + */ public function save($p_array) { - if ( ! isset ($p_array['letter_j_id'])) return; - $this->db->exec_sql('delete from jnt_letter where jl_id=$1',array($p_array['jnt_id'])); - - $this->db->start(); - $jl_id=$this->db->get_next_seq("jnt_letter_jl_id_seq"); - $this->db->exec_sql('insert into jnt_letter(jl_id) values($1)', - array($jl_id)); - - // save the source - $deb=$this->db->get_value('select j_debit,j_montant from jrnx where j_id=$1',array($p_array['j_id'])); - if ( $deb == 't') + if (!isset($p_array['letter_j_id'])) { - // save into letter_deb - $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($p_array['j_id'],$jl_id)); + // if nothing selected then remove + $this->db->exec_sql('delete from jnt_letter where jl_id=$1', array($p_array['jnt_id'])); + return; } - else - { - $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id',array($p_array['j_id'],$jl_id)); + $nb_letter_j_id=count($p_array['letter_j_id']); + + if ($nb_letter_j_id == 0 ) { + $this->db->exec_sql('delete from jnt_letter where jl_id=$1', array($p_array['jnt_id'])); + return; } - $count=0; - // save dest - for($i=0;$idb->start(); + + $this->db->exec_sql('delete from jnt_letter where jl_id=$1', array($p_array['jnt_id'])); + + $jl_id=$this->db->get_next_seq("jnt_letter_jl_id_seq"); + $this->db->exec_sql('insert into jnt_letter(jl_id) values($1)', array($jl_id)); + + // save the source + $deb=$this->db->get_value('select j_debit,j_montant from jrnx where j_id=$1', array($p_array['j_id'])); + if ($deb=='t') + { + // save into letter_deb + $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id', + array($p_array['j_id'], $jl_id)); + } + else + { + $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id', + array($p_array['j_id'], $jl_id)); + } + // save dest + for ($i=0; $i<$nb_letter_j_id; $i++) + { + if (! isset($p_array['ck'][$i]) || $p_array['ck'][$i] == "-2" ) { continue ; } + // save the dest - $deb=$this->db->get_value('select j_debit,j_montant from jrnx where j_id=$1',array($p_array['ck'][$i])); - if ( $deb == 't') + $deb=$this->db->get_value('select j_debit,j_montant from jrnx where j_id=$1', + array($p_array['ck'][$i])); + if ($deb=='t') { - $count++; // save into letter_deb - $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id',array($p_array['ck'][$i],$jl_id)); + $ld_id=$this->db->get_value('insert into letter_deb(j_id,jl_id) values($1,$2) returning ld_id', + array($p_array['ck'][$i], $jl_id)); } else { - $count++; - $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id',array($p_array['ck'][$i],$jl_id)); + $lc_id=$this->db->get_value('insert into letter_cred(j_id,jl_id) values($1,$2) returning lc_id', + array($p_array['ck'][$i], $jl_id)); } - } //end if 1 - } //end for - // save into jnt_letter - /* if only one row we delete the joint */ - if ( $count==0) + $acc_reconciliation_lettering=new Acc_Reconciliation_Lettering($this->db); + $acc_reconciliation_lettering->insert_reconcilied($p_array['ck'][$i], $p_array['j_id']); + } //end for + + } + catch (Exception $exc) { + echo $exc->getMessage(); + error_log($exc->getTraceAsString()); $this->db->rollback(); } + + $this->db->commit(); } + /** - *@brief retrieve * row thanks a condition + * @brief retrieve * row thanks a condition */ - public function seek($cond,$p_array=null) + public function seek($cond, $p_array=null) { /* $sql="select * from * where $cond"; return $this->cn->get_array($cond,$p_array) - */ + */ } + public function insert() { - if ( $this->verify() != 0 ) return; - + if ($this->verify()!=0) + return; } + /** - *show all the record from jrnx and their status (linked or not) - *it fills the array $this->content + * show all the record from jrnx and their status (linked or not) + * it fills the array $this->content */ protected function show_all() { @@ -270,9 +324,10 @@ class Lettering ob_end_clean(); return $r; } - function get_linked($p_jlid) - { - $sql="select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt, + + function get_linked($p_jlid) + { + $sql="select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt, j_montant,j_debit,jr_comment,jr_internal,jr_id,jr_def_id,jr_pj_number, coalesce(comptaproc.get_letter_jnt(j_id),-1) as letter from jrnx join jrn on (j_grpt = jr_grpt_id) @@ -282,11 +337,12 @@ class Lettering select j_id from letter_deb where jl_id=$1) order by j_date"; - $this->linked=$this->db->get_array($sql,array($p_jlid)); - } + $this->linked=$this->db->get_array($sql, array($p_jlid)); + } + /** - *show only the lettered records from jrnx - *it fills the array $this->content + * show only the lettered records from jrnx + * it fills the array $this->content */ protected function show_lettered() { @@ -298,9 +354,10 @@ class Lettering ob_end_clean(); return $r; } - /** - *show only the lettered records from jrnx - *it fills the array $this->content + + /** + * show only the lettered records from jrnx + * it fills the array $this->content */ protected function show_lettered_diff() { @@ -314,10 +371,9 @@ class Lettering } /** - *show only the not lettered records from jrnx - *it fills the array $this->content + * show only the not lettered records from jrnx + * it fills the array $this->content */ - protected function show_not_lettered() { $this->get_unletter(); @@ -328,82 +384,88 @@ class Lettering ob_end_clean(); return $r; } + /** - *wrapper : it call show_all, show_lettered or show_not_lettered depending + * wrapper : it call show_all, show_lettered or show_not_lettered depending * of the parameter - *@param $p_type poss. values are all, unletter, letter + * @param $p_type poss. values are all, unletter, letter */ public function show_list($p_type) { - switch($p_type) + switch ($p_type) { - case 'all': + case 'all': return $this->show_all(); - break; - case 'unletter': - return $this->show_not_lettered(); - break; - case 'letter': - return $this->show_lettered(); - break; - case 'letter_diff': + break; + case 'unletter': + return $this->show_not_lettered(); + break; + case 'letter': + return $this->show_lettered(); + break; + case 'letter_diff': return $this->show_lettered_diff(); break; } - throw new Exception ("[$p_type] is no unknown"); + throw new Exception("[$p_type] is no unknown"); } public function show_letter($p_jid) { - $j_debit=$this->db->get_value('select j_Debit from jrnx where j_id=$1',array($p_jid)); - $amount_init=$this->db->get_value('select j_montant from jrnx where j_id=$1',array($p_jid)); + $j_debit=$this->db->get_value('select j_Debit from jrnx where j_id=$1', array($p_jid)); + $amount_init=$this->db->get_value('select j_montant from jrnx where j_id=$1', array($p_jid)); $this->get_filter($p_jid); // retrieve jnt_letter.id $sql="select distinct(jl_id) from jnt_letter left outer join letter_deb using (jl_id) left outer join letter_cred using (jl_id) where letter_deb.j_id = $1 or letter_cred.j_id=$2"; - $a_jnt_id=$this->db->get_array($sql,array($p_jid,$p_jid)); + $a_jnt_id=$this->db->get_array($sql, array($p_jid, $p_jid)); - if (count($a_jnt_id)==0 ) - { - $jnt_id=-2; - } else - { - $jnt_id=$a_jnt_id[0]['jl_id']; - } - $this->get_linked($jnt_id); + if (count($a_jnt_id)==0) + { + $jnt_id=-2; + } + else + { + $jnt_id=$a_jnt_id[0]['jl_id']; + } + $this->get_linked($jnt_id); ob_start(); require_once NOALYSS_TEMPLATE.'/letter_prop.php'; $r=ob_get_contents(); ob_end_clean(); - $r.=HtmlInput::hidden('j_id',$p_jid); - $r.=HtmlInput::hidden('jnt_id',$jnt_id); + $r.=HtmlInput::hidden('j_id', $p_jid); + $r.=HtmlInput::hidden('jnt_id', $jnt_id); return $r; } public function update() { - if ( $this->verify() != 0 ) return; + if ($this->verify()!=0) + return; } public function load() -{} + { + + } public function delete() { - throw new Exception ('delete not implemented'); + throw new Exception('delete not implemented'); } } + /** * only for operation retrieved thanks a account (jrnx.j_poste) * manage the accounting entries for a given account */ - class Lettering_Account extends Lettering { - function __construct($p_init,$p_account=null) + + function __construct($p_init, $p_account=null) { parent::__construct($p_init); $this->account=$p_account; @@ -426,24 +488,23 @@ class Lettering_Account extends Lettering { switch ($this->fil_deb) { - case 0: - $filter_deb=" and j_debit='t' "; - break; - case 1: - $filter_deb=" and j_debit='f' "; - break; - case 2: - $filter_deb=" "; - break; + case 0: + $filter_deb=" and j_debit='t' "; + break; + case 1: + $filter_deb=" and j_debit='f' "; + break; + case 2: + $filter_deb=" "; + break; } - } $filter_amount=""; - if ( isset ($this->fil_amount_max ) && - isset ($this->fil_amount_min ) && - isNumber($this->fil_amount_max)==1 && - isNumber($this->fil_amount_min)==1 && - ($this->fil_amount_max != 0 || $this->fil_amount_min != 0) ) + if (isset($this->fil_amount_max)&& + isset($this->fil_amount_min)&& + isNumber($this->fil_amount_max)==1&& + isNumber($this->fil_amount_min)==1&& + ($this->fil_amount_max!=0||$this->fil_amount_min!=0)) $filter_amount=" and (j_montant >= $this->fil_amount_min and j_montant<=$this->fil_amount_max or (coalesce(comptaproc.get_letter_jnt($p_jid),-1)= coalesce(comptaproc.get_letter_jnt(j_id),-1) and coalesce(comptaproc.get_letter_jnt($p_jid),-1) <> -1 )) "; $sql=" select j_id,j_date,to_char(j_date,'DD.MM.YYYY') as j_date_fmt, @@ -461,7 +522,7 @@ class Lettering_Account extends Lettering $filter_amount order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->account, $this->start, $this->end)); } /** @@ -490,8 +551,9 @@ class Lettering_Account extends Lettering and $this->sql_ledger order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->account, $this->start, $this->end)); } + /** * same as get_all but only for lettered operation */ @@ -518,9 +580,10 @@ class Lettering_Account extends Lettering where j_poste = $1 and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY') and $this->sql_ledger order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->account, $this->start, $this->end)); } - /** + + /** * same as get_all but only for lettered operation */ public function get_letter_diff() @@ -548,12 +611,12 @@ class Lettering_Account extends Lettering and $this->sql_ledger and diff_letter1 <> 0 order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->account, $this->start, $this->end)); } + /** * same as get_all but only for unlettered operation */ - public function get_unletter() { $sql=" @@ -572,27 +635,30 @@ class Lettering_Account extends Lettering and $this->sql_ledger and j_id not in (select j_id from letter_jl) order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->account,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->account, $this->start, $this->end)); } } + /** * only for operation retrieved thanks a quick_code * manage the accounting entries for a given card */ class Lettering_Card extends Lettering { + /** - *constructor - *@param $p_init db resource - *@param $p_qcode quick_code of the jrnx.j_id + * constructor + * @param $p_init db resource + * @param $p_qcode quick_code of the jrnx.j_id */ - function __construct($p_init,$p_qcode=null) + function __construct($p_init, $p_qcode=null) { parent::__construct($p_init); $this->quick_code=$p_qcode; $this->object_type='card'; } + /** * fills the this->content, datas are filtered thanks * - fil_deb poss values t (debit), f(credit), ' ' (everything) @@ -609,25 +675,24 @@ class Lettering_Card extends Lettering { switch ($this->fil_deb) { - case 0: - $filter_deb=" and j_debit='t' "; - break; - case 1: - $filter_deb=" and j_debit='f' "; - break; - case 2: - $filter_deb=" "; - break; + case 0: + $filter_deb=" and j_debit='t' "; + break; + case 1: + $filter_deb=" and j_debit='f' "; + break; + case 2: + $filter_deb=" "; + break; } - } $filter_amount=""; - if ( isset ($this->fil_amount_max ) && - isset ($this->fil_amount_min ) && - isNumber($this->fil_amount_max)==1 && - isNumber($this->fil_amount_min)==1 && - ($this->fil_amount_max != 0 || $this->fil_amount_min != 0) ) - $filter_amount=" and (j_montant between $this->fil_amount_min and $this->fil_amount_max or (coalesce(comptaproc.get_letter_jnt($p_jid),-1)= coalesce(comptaproc.get_letter_jnt(j_id),-1) and coalesce(comptaproc.get_letter_jnt($p_jid),-1) <> -1 )) "; + if (isset($this->fil_amount_max)&& + isset($this->fil_amount_min)&& + isNumber($this->fil_amount_max)==1&& + isNumber($this->fil_amount_min)==1&& + ($this->fil_amount_max!=0||$this->fil_amount_min!=0)) + $filter_amount=" and (j_montant between $this->fil_amount_min and $this->fil_amount_max or (coalesce(comptaproc.get_letter_jnt($p_jid),-1)= coalesce(comptaproc.get_letter_jnt(j_id),-1) and coalesce(comptaproc.get_letter_jnt($p_jid),-1) <> -1 )) "; $sql=" with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1 from @@ -651,8 +716,9 @@ class Lettering_Card extends Lettering $filter_amount order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->quick_code, $this->start, $this->end)); } + /** * fills this->content with all the operation for the this->quick_code(j_qcode) */ @@ -680,12 +746,12 @@ class Lettering_Card extends Lettering and $this->sql_ledger order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->quick_code, $this->start, $this->end)); } + /** * same as get_all but only for lettered operation */ - public function get_letter() { $sql=" @@ -709,9 +775,10 @@ class Lettering_Card extends Lettering where j_qcode = upper($1) and j_date >= to_date($2,'DD.MM.YYYY') and j_date <= to_date ($3,'DD.MM.YYYY') and $this->sql_ledger order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->quick_code, $this->start, $this->end)); } - public function get_letter_diff() + + public function get_letter_diff() { $sql=" with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1 @@ -735,8 +802,9 @@ class Lettering_Card extends Lettering and $this->sql_ledger and diff_letter1 <>0 order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->quick_code, $this->start, $this->end)); } + /** * same as get_all but only for unlettered operation */ @@ -757,8 +825,9 @@ class Lettering_Card extends Lettering and $this->sql_ledger and j_id not in (select j_id from letter_deb join jnt_letter using (jl_id) union select j_id from letter_cred join jnt_letter using (jl_id) ) order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start,$this->end)); + $this->content=$this->db->get_array($sql, array($this->quick_code, $this->start, $this->end)); } + /** * fill $this->content with the rows from this query * Columns are @@ -782,9 +851,8 @@ class Lettering_Card extends Lettering */ public function get_balance_ageing($p_type) { - $sql_let = ($p_type =='unlet')?' let_diff.jl_id is null and':''; - $sql = - " with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1 + $sql_let=($p_type=='unlet')?' let_diff.jl_id is null and':''; + $sql=" with let_diff as (select jl_id,deb_amount-cred_amount as diff_letter1 from ( select jl_id,coalesce(sum(j_montant),0) as cred_amount from letter_cred join jrnx using (j_id) group by jl_id) as CRED left join (select jl_id,coalesce(sum(j_montant),0) as deb_amount from letter_deb join jrnx using (j_id) group by jl_id) as DEB using (jl_id)) , @@ -805,7 +873,7 @@ class Lettering_Card extends Lettering and j_date >= to_date($2,'DD.MM.YYYY') and {$this->sql_ledger} order by j_date,j_id"; - $this->content=$this->db->get_array($sql,array($this->quick_code,$this->start)); + $this->content=$this->db->get_array($sql, array($this->quick_code, $this->start)); + } - } } diff --git a/include/constant.php b/include/constant.php index c2edf150b..a453ed2ac 100644 --- a/include/constant.php +++ b/include/constant.php @@ -598,7 +598,8 @@ function noalyss_class_autoloader($class) { "html_input_noalyss"=>"class/html_input_noalyss.class.php", "card_property"=>"class/card_property.class.php", "pdfland"=>"class/pdf_land.class.php", - "pdf_anc_acc_list"=>"class/pdf_anc_acc_list.class.php" + "pdf_anc_acc_list"=>"class/pdf_anc_acc_list.class.php", + 'acc_reconciliation_lettering'=>'class/acc_reconciliation_lettering.class.php' ); if ( isset ($aClass[$class]) ) { require_once NOALYSS_INCLUDE."/".$aClass[$class]; diff --git a/unit-test/include/class/letteringTest.php b/unit-test/include/class/letteringTest.php new file mode 100644 index 000000000..6b7eb3b36 --- /dev/null +++ b/unit-test/include/class/letteringTest.php @@ -0,0 +1,444 @@ + + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + * + * Author : Dany De Bontridder danydb@noalyss.eu + * + */ + +/** + * @file + * @brief + */ +use PHPUnit\Framework\TestCase; + +/** + * @backupGlobals enabled + * @coversDefaultClass \Lettering + */ +require 'global.php'; + +class LetteringTest extends TestCase +{ + + // const sql = find the row to letter + const sql="select * + from jrnx + join jrn on (j_grpt=jr_grpt_id) + where jr_mt=$1 and j_montant=$2 and j_poste = $3"; + // const sql_letter count the row where we have j_id + const sql_letter="select count(*) + from jnt_letter + join letter_cred using (jl_id) + join letter_deb using(jl_id) + where + letter_cred.j_id = $1 or letter_deb.j_id=$2"; +// const sql_letter count the row where we have j_id + const sql_letter_id="select jl_id + from jnt_letter + join letter_cred using (jl_id) + join letter_deb using(jl_id) + where + letter_cred.j_id = $1 or letter_deb.j_id=$1"; + const number_row_jnt_letter=9; + + /** + * @var Fiche + */ + protected $object; + + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test method is executed. + */ + protected function setUp(): void + { + + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test method is executed. + */ + protected function tearDown(): void + { + + } + + static function insert_sale() + { + include 'global.php'; + + $object=new Acc_Ledger_Sale($g_connection, 2); + $array=array( + "ledger_type"=>"VEN", + "ac"=>"COMPTA/VENMENU/VEN", + "sa"=>"p", + "gDossier"=>25, + "nb_item"=>2, + "p_jrn"=>2, + "p_jrn_predef"=>2, + "action"=>"use_opd", + "jrn_type"=>"VEN", + "filter"=>"", + "e_date"=>"24.08.2019", + "e_ech"=>"", + "e_client"=>"CLIENT", + "e_pj"=>"VEN10-PHPUNIT", + "e_pj_suggest"=>"VEN10", + "e_comm"=>"Vente Service LetteringAccount", + "e_march0"=>"DEPLAC", + "e_march0_price"=>20, + "e_quant0"=>1.21, + "htva_march0"=>24.2, + "e_march0_tva_id"=>1, + "e_march0_tva_amount"=>5.08, + "tva_march0"=>5.08, + "tvac_march0"=>29.28, + "e_march1"=>"MARCHA", + "e_march1_price"=>48.5, + "e_quant1"=>25, + "htva_march1"=>1212.5, + "e_march1_tva_id"=>1, + "e_march1_tva_amount"=>254.63, + "tva_march1"=>254.63, + "tvac_march1"=>1467.13, + "mp_date"=>"", + "acompte"=>0, + "e_comm_paiement"=>"", + "e_mp"=>"0", + "e_mp_qcode_1"=>"COMPTE", + "e_mp_qcode_2"=>"", + "p_currency_rate"=>1, + "p_currency_code"=>0, + "mt"=>"1-phpunit-sale-lettering", + "view_invoice"=>"Enregistrer"); + $object->insert($array); + } + + static function insert_purchase() + { + include 'global.php'; + $object=new Acc_Ledger_Purchase($g_connection, 3); + $array=array + ( + "gDossier"=>25, + "nb_item"=>1, + "p_jrn"=>3, + "p_jrn_predef"=>3, + "action"=>"use_opd", + "jrn_type"=>"ACH", + "filter"=>"", + "e_date"=>"24.02.2018", + "e_ech"=>"", + "e_client"=>"FOURNI", + "e_pj"=>"ACH6-PHPUNIT", + "e_pj_suggest"=>"ACH6", + "e_comm"=>"Loyer Appartement LetteringAccount", + "e_march0"=>"LOYER", + "e_march0_price"=>658.25, + "e_quant0"=>1, + "htva_march0"=>658.25, + "e_march0_tva_id"=>4, + "e_march0_tva_amount"=>0, + "tva_march0"=>0, + "tvac_march0"=>658.25, + "p_action"=>"ach", + "sa"=>"p", + "e_mp"=>0, + "view_invoice"=>"Enregistrer", + "ac"=>"ACH", + "p_currency_rate"=>1.09, + "p_currency_code"=>1, + "mt"=>"1-phpunit-purchase-lettering", + ); + $object->insert($array); + } + + static function insert_misc_op() + { + include 'global.php'; + $array=[ + "pa_id"=>array(2), + "e_date"=>"01.09.2018", + "desc"=>"test", + "period"=>100, + "e_pj"=>"ODS1", + "e_pj_suggest"=>"ODS1", + "e_comm"=>"test", + "jrn_type"=>"ODS", + "p_jrn"=>4, + "nb_item"=>5, + "jrn_concerned"=>"", + "gDossier"=>25, + "poste0"=>601, + "ld0"=>"Achats de fournitures", + "ck0"=>"", + "amount0"=>100, + "op"=>Array(0), + "amount_t0"=>100, + "hplan"=>array(array(-1)), + "val"=>array(array("0"=>array("0"=>100))), + "poste1"=>"4511", + "ld1"=>"TVA à payer 21%", + "amount1"=>100, + "opd_name"=>"", + "od_description"=>"", + "reverse_date"=>"", + "ext_label"=>"", + "jr_optype"=>"NOR", + "p_currency_rate"=>1, + "p_currency_code"=>0, + "mt"=>"1-phpunit-ods-lettering" + ]; + global $g_connection; + $ledger=new Acc_Ledger($g_connection, 4); + $ledger->save($array); + } + + static function insert_financial() + { + include 'global.php'; + $array=array + ( + "ac"=>"COMPTA/MENUFIN/FIN", + "pa_id"=>array("0"=>1), + "p_jrn"=>1, + "nb_item"=>5, + "last_sold"=>0, + "first_sold"=>0, + "e_pj"=>'FIN48', + "e_pj_suggest"=>'FIN48', + "e_date"=>"13.06.2020", + "mt"=>"1-phpunit-fin-lettering", + "sa"=>"n", + "e_other0"=>"CLIENT", + "e_other0_comment"=>"", + "e_other0_amount"=>1496.41, + "e_concerned0"=>"", + "dateop0"=>"", + "chdate"=>1, + "e_other1"=>"FOURNI", + "e_other1_comment"=>"", + "e_other1_amount"=>-603.9, + "e_concerned1"=>"", + "dateop1"=>"", + "e_other2"=>"", + "e_other2_comment"=>"", + "e_other2_amount"=>0, + "e_concerned2"=>'', + "dateop2"=>'', + "e_other3"=>'', + "e_other3_comment"=>'', + "e_other3_amount"=>0, + "e_concerned3"=>'', + "dateop3"=>'', + "e_other4"=>'', + "e_other4_comment"=>'', + "e_other4_amount"=>0, + "e_concerned4"=>"", + "dateop4"=>'', + "confirm"=>"Confirmer", + "pj"=>"FIN-LET1" + ); + $object=new Acc_Ledger_Fin($g_connection, 1); + $object->insert($array); + } + + /** + * the setUpBeforeClass() template methods is called before the first test of the test case + * class is run + */ + public static function setUpBeforeClass(): void + { + include 'global.php'; + LetteringTest::insert_sale(); + LetteringTest::insert_purchase(); + LetteringTest::insert_financial(); + LetteringTest::insert_misc_op(); + } + + /** + * tearDownAfterClass() template methods is calleafter the last test of the test case class is run, + * + */ + static function tearDownAfterClass(): void + { + include 'global.php'; + $g_connection->exec_sql("delete from jrnx where j_grpt in (select jr_grpt_id + from jrn where jr_mt like '1-phpunit-%-lettering')"); + $g_connection->exec_sql("delete from jrn where jr_mt like '1-phpunit-%-lettering'"); + } + + /** + * @brief save couple of operation + * @testdox Lettering:insert_couple() + */ + function testInsertCouple() + { + global $g_connection; + $lettering=new Lettering_Card($g_connection); + + // find j_id from sale & financial and insert_couple + $j_id_sale=$g_connection->get_row(self::sql, ['1-phpunit-sale-lettering', 1496.41, '4000004']); + $j_id_financial=$g_connection->get_row(self::sql, ['1-phpunit-fin-lettering', 1496.41, '4000004']); + + // insert into lettering and check + $lettering->insert_couple($j_id_financial['j_id'], $j_id_sale['j_id']); + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_sale['j_id'], $j_id_sale['j_id']]); + $this->assertEquals(1, $is_inserted, 'sale : lettering not done'); + + // insert into lettering and check + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_financial['j_id'], $j_id_financial['j_id']]); + $this->assertEquals(1, $is_inserted, 'fin: lettering not done'); + // find j_id from purchase & financial and insert_couple + $j_id_purchase=$g_connection->get_row(self::sql, ['1-phpunit-purchase-lettering', 603.90, '4400004']); + $j_id_financial2=$g_connection->get_row(self::sql, ['1-phpunit-fin-lettering', 603.90, '4400004']); + + // insert into lettering and check + $lettering->insert_couple($j_id_financial2['j_id'], $j_id_purchase['j_id']); + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_purchase['j_id'], $j_id_purchase['j_id']]); + $this->assertEquals(1, $is_inserted, 'purchase: lettering not done'); + + // insert into lettering and check + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_financial2['j_id'], $j_id_financial2['j_id']]); + $this->assertEquals(1, $is_inserted, 'fin: lettering not done'); + $this->clean_letter(); + $this->assertEquals(self::number_row_jnt_letter, $g_connection->get_value("select count(*) from jnt_letter"), + 'insertCouple lettering not cleaned'); + } + + function clean_letter() + { + global $g_connection; + $delete=" + delete from jnt_letter + where jl_id in (select jl_id from letter_cred + where + j_id in (select j_id from jrn join jrnx on (jr_grpt_id=j_grpt) + where jr_mt like '1-phpunit-%-lettering') + ) + or jl_id in (select jl_id from letter_deb + where + j_id in (select j_id from jrn join jrnx on (jr_grpt_id=j_grpt) + where jr_mt like '1-phpunit-%-lettering') + ) + "; + $g_connection->exec_sql($delete); + } + + /** + * @brief save array of date + * @testdox Lettering:save() + */ + function save() + { + global $g_connection; + $g_connection=Dossier::connect(); + $lettering=new Lettering_Card($g_connection); + + $j_id_sale=$g_connection->get_row(self::sql, ['1-phpunit-sale-lettering', 1496.41, '4000004']); + $j_id_financial=$g_connection->get_row(self::sql, ['1-phpunit-fin-lettering', 1496.41, '4000004']); + + $array=array( + "letter_j_id"=>array($j_id_sale['j_id']), + "ck"=>array($j_id_sale['j_id']), + 'j_id'=>$j_id_financial['j_id'], + 'jnt_id'=>-2 + ); + $lettering->save($array); + + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_sale['j_id'], $j_id_sale['j_id']]); + $this->assertEquals(1, $is_inserted, 'sale : lettering not done'); + $key=$g_connection->get_row(self::sql_letter_id, [ + $j_id_sale['j_id'] + ]); + // insert into lettering and check + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_financial['j_id'], $j_id_financial['j_id']]); + $this->assertEquals(1, $is_inserted, 'fin: lettering not done'); + $key2=$g_connection->get_row(self::sql_letter_id, [ + $j_id_sale['j_id'] + ]); + $this->assertTrue($key['jl_id']==$key2['jl_id'], "operation not linked together"); + + + $this->assertEquals(1,$g_connection->get_value("select count(*) from jrn_rapt + where jr_id=$1 or jra_concerned=$1",[$j_id_sale['jr_id']]),"Lettering::save does not reconcilied"); + $g_connection->exec_sql("delete from jrn_rapt where jr_id=$1 or jra_concerned=$1", + [$j_id_sale['jr_id']]); + } + + function insert_reconcilied() + { + global $g_connection; + $j_id_sale=$g_connection->get_row(self::sql, ['1-phpunit-sale-lettering', 1496.41, '4000004']); + $j_id_financial=$g_connection->get_row(self::sql, ['1-phpunit-fin-lettering', 1496.41, '4000004']); + $acc_reconciliation_lettering=new Acc_Reconciliation_Lettering($g_connection); + $acc_reconciliation_lettering->insert_reconcilied($j_id_financial['j_id'], $j_id_sale['j_id']); + + // not in jnt_letter + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_financial['j_id'], $j_id_financial['j_id']]); + $this->assertEquals(0,$is_inserted,"Error : also inserted in lettering"); + + $is_inserted=$g_connection->get_value(self::sql_letter, [$j_id_sale['j_id'], $j_id_sale['j_id']]); + $this->assertEquals(0,$is_inserted,"Error : also inserted in lettering"); + + $this->assertEquals(1,$g_connection->get_value("select count(*) from jrn_rapt where jr_id = $1 + or jra_concerned=$1",array($j_id_sale["jr_id"]))," sale not reconcilied"); + + $this->assertEquals(1,$g_connection->get_value("select count(*) from jrn_rapt where jr_id = $1 + or jra_concerned=$1",array($j_id_financial["jr_id"]))," fin not reconcilied"); + + + } + /** + * @testdox Lettering:save() + */ + function testSave() + { + global $g_connection; + $g_connection=Dossier::connect(); + $this->clean_letter(); + $this->assertEquals(self::number_row_jnt_letter, $g_connection->get_value("select count(*) from jnt_letter"), + 'testSave lettering not cleaned'); + + $this->save(); + $this->clean_letter(); + $this->assertEquals(self::number_row_jnt_letter, $g_connection->get_value("select count(*) from jnt_letter"), + 'testSave lettering not cleaned'); + } + + /** + * @testdox Acc_Reconciliation_Lettering::insert_reconcilied() + */ + function testInsertReconcilied() + { + global $g_connection; + $g_connection=Dossier::connect(); + $this->clean_letter(); + $this->assertEquals(self::number_row_jnt_letter, $g_connection->get_value("select count(*) from jnt_letter"), + 'testSave lettering not cleaned'); + + $this->insert_reconcilied(); + $this->clean_letter(); + $this->assertEquals(self::number_row_jnt_letter, $g_connection->get_value("select count(*) from jnt_letter"), + 'testSave lettering not cleaned'); + } + +}