noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_acc_reconciliation.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS is free software; you can redistribute it and/or modify
00006  *   it under the terms of the GNU General Public License as published by
00007  *   the Free Software Foundation; either version 2 of the License, or
00008  *   (at your option) any later version.
00009  *
00010  *   NOALYSS is distributed in the hope that it will be useful,
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  *   GNU General Public License for more details.
00014  *
00015  *   You should have received a copy of the GNU General Public License
00016  *   along with NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00020 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00021 
00022 /*!\file
00023  *   \brief class acc_reconciliation, this class is new and the code
00024  *   must use it
00025  *
00026  */
00027 require_once("class_iconcerned.php");
00028 require_once ('class_database.php');
00029 require_once ('class_dossier.php');
00030 require_once 'class_lettering.php';
00031 
00032 /*! \brief new class for managing the reconciliation it must be used
00033  * instead of the function InsertRapt, ...
00034  *
00035  */
00036 class Acc_Reconciliation
00037 {
00038     var $db;                    /*!< database connection */
00039     var $jr_id;                 /*!< jr_id */
00040 
00041     function   __construct($cn)
00042     {
00043         $this->db=$cn;
00044         $this->jr_id=0;
00045     }
00046 
00047     function set_jr_id($jr_id)
00048     {
00049         $this->jr_id=$jr_id;
00050     }
00051     /*! \brief return a widget of type js_concerned
00052      */
00053     function widget()
00054     {
00055         $wConcerned=new IConcerned();
00056         $wConcerned->extra=0; // with 0 javascript search from e_amount... field (see javascript)
00057 
00058         return $wConcerned;
00059 
00060     }
00061     /*!
00062      *\brief   Insert into jrn_rapt the concerned operations
00063      *
00064      * \param $jr_id2 (jrn.jr_id) => jrn_rapt.jra_concerned or a string
00065      * like "jr_id2,jr_id3,jr_id4..."
00066      *
00067      * \return none
00068      *
00069      */
00070     function insert($jr_id2)
00071     {
00072         if ( trim($jr_id2) == "" )
00073             return;
00074         if ( strpos($jr_id2,',') !== 0 )
00075         {
00076             $aRapt=explode(',',$jr_id2);
00077             foreach ($aRapt as $rRapt)
00078             {
00079                 if ( isNumber($rRapt) == 1 )
00080                 {
00081                     $this->insert_rapt($rRapt);
00082                 }
00083             }
00084         }
00085         else
00086             if ( isNumber($jr_id2) == 1 )
00087             {
00088                 $this->insert_rapt($jr_id2);
00089             }
00090     }
00091 
00092     /*!
00093      *\brief   Insert into jrn_rapt the concerned operations
00094      * should not  be called directly, use insert instead
00095      *
00096      * \param $jr_id2 (jrn.jr_id) => jrn_rapt.jra_concerned
00097      *
00098      * \return none
00099      *
00100      */
00101     function insert_rapt($jr_id2)
00102     {
00103         if ( isNumber($this->jr_id)  == 0 ||  isNumber($jr_id2) == 0 )
00104         {
00105             return false;
00106         }
00107         if ( $this->jr_id==$jr_id2)
00108             return true;
00109 
00110                 if ( $this->db->count_sql("select jr_id from jrn where jr_id=".$this->jr_id)==0 )
00111                                 return false;
00112                 if ( $this->db->count_sql("select jr_id from jrn where jr_id=".$jr_id2)==0 )
00113                                 return false;
00114 
00115         // verify if exists
00116         if ( $this->db->count_sql(
00117                     "select jra_id from jrn_rapt where jra_concerned=".$this->jr_id.
00118                     " and jr_id=$jr_id2
00119                     union
00120                     select jra_id from jrn_rapt where jr_id=".$this->jr_id.
00121                     " and jra_concerned=$jr_id2 ")
00122                 ==0)
00123         {
00124             // Ok we can insert
00125             $Res=$this->db->exec_sql("insert into jrn_rapt(jr_id,jra_concerned) values ".
00126                                     "(".$this->jr_id.",$jr_id2)");
00127             // try to letter automatically same account from both operation
00128             $this->auto_letter($jr_id2);
00129             
00130             // update date of paiement -----------------------------------------------------------------------
00131             $source_type=$this->db->get_value("select substr(jr_internal,1,1) from jrn where jr_id=$1",array($this->jr_id));
00132             $dest_type=$this->db->get_value("select substr(jr_internal,1,1) from jrn where jr_id=$1",array($jr_id2));
00133             if (($source_type =='A' || $source_type=='V') && ($dest_type != 'A' && $dest_type != 'V'))
00134             {
00135                 // set the date on source
00136                 $date=$this->db->get_value('select jr_date from jrn where jr_id=$1',array($jr_id2));
00137                 if ( trim ($date) == '') $date=null;
00138                 $this->db->exec_sql('update jrn set jr_date_paid=$1 where jr_id=$2 and jr_date_paid is null ',array($date,$this->jr_id));
00139             }
00140             if (($source_type !='A' && $source_type !='V') && ($dest_type == 'A' || $dest_type == 'V'))
00141             {
00142                 // set the date on dest
00143                 $date=$this->db->get_value('select jr_date from jrn where jr_id=$1',array($this->jr_id));
00144                 if (trim($date) == '') $date=null;
00145                 $this->db->exec_sql('update jrn set jr_date_paid=$1 where jr_id=$2 and jr_date_paid is null ',array($date,$jr_id2));
00146             }
00147         }
00148         return true;
00149     }
00150         /**
00151          * @brief try to letter same card between $p_jrid and $this->jr_id
00152          * @param jrn.jr_id $p_jrid  the operation to reconcile
00153          */
00154         function auto_letter($p_jrid)
00155         {
00156                 // Try to find same card from both operation
00157                 $sql="select j1.f_id as fiche ,coalesce(j1.j_id,-1) as jrnx_id1,coalesce(j2.j_id,-1) as jrnx_id2,
00158 j1.j_poste as poste
00159                                 from jrnx as j1
00160                                         join jrn as jr1 on (j1.j_grpt=jr1.jr_grpt_id)
00161                                         join jrnx as j2 on (coalesce(j1.f_id,-1)=coalesce(j2.f_id,-1) and j1.j_poste=j2.j_poste)
00162                                         join jrn as jr2 on (j2.j_grpt=jr2.jr_grpt_id)
00163                                 where
00164                                         jr1.jr_id=$1
00165                                         and
00166                                         jr2.jr_id= $2";
00167                 $result=$this->db->get_array($sql,array($this->jr_id,$p_jrid));
00168                 if ( count($result) == 0)
00169                 {
00170                         return;
00171                 }
00172                 for ($i=0;$i<count($result);$i++)
00173                 {
00174                         if ( $result[$i]['fiche'] != -1)
00175                         {
00176                                 $letter = new Lettering_Card($this->db);
00177                                 $letter->insert_couple($result[$i]['jrnx_id1'],$result[$i]['jrnx_id2']);
00178                         }
00179                         else
00180                         {
00181                                 $letter = new Lettering_Account($this->db);
00182                                 $letter->insert_couple($result[$i]['jrnx_id1'],$result[$i]['jrnx_id2']);
00183                         }
00184                 }
00185 
00186         }
00187 
00188         /*!
00189      *\brief   Insert into jrn_rapt the concerned operations
00190      *
00191      * \param $this->jr_id (jrn.jr_id) => jrn_rapt.jr_id
00192      * \param $jr_id2 (jrn.jr_id) => jrn_rapt.jra_concerned
00193      *
00194      * \return none
00195      */
00196     function remove($jr_id2)
00197     {
00198         if ( isNumber($this->jr_id)  == 0 or
00199                 isNumber($jr_id2) == 0 )
00200         {
00201             return;
00202         }
00203         // verify if exists
00204         if ( $this->db->count_sql("select jra_id from jrn_rapt where ".
00205                                   " jra_concerned=".$this->jr_id."  and jr_id=$jr_id2
00206                                   union
00207                                   select jra_id from jrn_rapt where jra_concerned=$jr_id2 ".
00208                                   " and jr_id=".$this->jr_id) !=0)
00209         {
00210                         /**
00211                          * remove also lettering between both operation
00212                          */
00213                         $sql = " delete from
00214                                         jnt_letter
00215                                         where jl_id in ( select jl_id from jnt_letter
00216                                                                                 join letter_cred as lc using(jl_id)
00217                                                                                 join letter_deb as ld using (jl_id)
00218                                                                         where
00219                                                                                 lc.j_id in (select j_id
00220                                                                                                         from jrnx join jrn on (j_grpt=jr_grpt_id)
00221                                                                                                         where jr_id in ($1,$2))
00222                                                                                 or
00223                                                                                 ld.j_id in (select j_id
00224                                                                                                         from jrnx join jrn on (j_grpt=jr_grpt_id)
00225                                                                                                         where jr_id in ($1,$2))
00226 
00227 
00228 
00229                                                         )";
00230                         $this->db->exec_sql($sql, array($jr_id2, $this->jr_id));
00231                         // Ok we can delete
00232                         $Res=$this->db->exec_sql("delete from jrn_rapt where ".
00233                                      "(jra_concerned=$jr_id2 and jr_id=".$this->jr_id.") or
00234                                      (jra_concerned=".$this->jr_id." and jr_id=$jr_id2) ");
00235         }
00236     }
00237 
00238     /*!
00239      *\brief   Return an array of the concerned operation
00240      *
00241      *
00242      *\param database connection
00243      * \return array if something is found or null
00244      */
00245     function get ( )
00246     {
00247         $sql=" select jr_id as cn from jrn_rapt where jra_concerned=".$this->jr_id.
00248              " union ".
00249              " select jra_concerned as cn from jrn_rapt where jr_id=".$this->jr_id;
00250         $Res=$this->db->exec_sql($sql);
00251 
00252         // If nothing is found return null
00253         $n=Database::num_row($Res);
00254 
00255         if ($n ==0 ) return null;
00256 
00257         // put everything in an array
00258         for ($i=0;$i<$n;$i++)
00259         {
00260             $l=Database::fetch_array($Res,$i);
00261             $r[$i]=$l['cn'];
00262         }
00263         return $r;
00264     }
00265     function fill_info()
00266     {
00267         $sql="select jr_id,jr_date,jr_comment,jr_internal,jr_montant,jr_pj_number,jr_def_id,jrn_def_name,jrn_def_type
00268              from jrn join jrn_def on (jrn_def_id=jr_def_id)
00269              where jr_id=$1";
00270         $a=$this->db->get_array($sql,array($this->jr_id));
00271         return $a[0];
00272     }
00273     /**
00274      *@brief return array of not-reconciled operation
00275     * Prepare and put in memory the SQL detail_quant
00276     */
00277     function get_not_reconciled()
00278     {
00279       $filter_date=$this->filter_date();
00280       /* create ledger filter */
00281       $sql_jrn=$this->ledger_filter();
00282 
00283         $array=$this->db->get_array("select distinct jr_id,jr_date from jrn where $filter_date and $sql_jrn and jr_id not in (select jr_id from jrn_rapt union select jra_concerned from jrn_rapt) order by jr_date");
00284         $ret=array();
00285         for ($i=0;$i<count($array);$i++)
00286         {
00287             $this->jr_id=$array[$i]['jr_id'];
00288             $ret[$i]['first']=$this->fill_info();
00289         }
00290         $this->db->prepare('detail_quant','select * from v_quant_detail where jr_id=$1');
00291         return $ret;
00292     }
00293     /**
00294      *Create a sql condition to filter by security and by asked ledger
00295      * based on $this->a_jrn
00296      *@return a valid sql stmt to include
00297      *@see get_not_reconciled get_reconciled
00298      */
00299     function ledger_filter ()
00300     {
00301         global $g_user;
00302         /* get the available ledgers for current user */
00303         $sql=$g_user->get_ledger_sql('ALL',3);
00304         $sql=str_replace('jrn_def_id','jr_def_id',$sql);
00305         $r='';
00306         /* filter by this->r_jrn */
00307         if ($this->a_jrn != null )
00308         {
00309             $sep='';
00310             $r='and jr_def_id in (';
00311             foreach( $this->a_jrn as $key=>$value)
00312             {
00313                 $r.=$sep.$value;
00314                 $sep=',';
00315             }
00316             $r.=')';
00317         }
00318         return $sql.'  '.$r;
00319     }
00320     /**
00321      *@brief return array of reconciled operation
00322      * Prepare and put in memory the SQL detail_quant
00323      *@return
00324      *@note
00325      *@see
00326      @code
00327 
00328      @endcode
00329     */
00330     function get_reconciled()
00331     {
00332       $filter_date=$this->filter_date();
00333 
00334 
00335         /* create ledger filter */
00336         $sql_jrn=$this->ledger_filter();
00337 
00338         $array=$this->db->get_array("select distinct jr_id,jr_date from jrn where $filter_date and $sql_jrn and jr_id  in (select jr_id from jrn_rapt union select jra_concerned from jrn_rapt) order by jr_date");
00339         $ret=array();
00340         for ($i=0;$i<count($array);$i++)
00341         {
00342             $this->jr_id=$array[$i]['jr_id'];
00343             $ret[$i]['first']=$this->fill_info();
00344             $atmp=$this->get();
00345             for ( $e=0;$e<count($atmp);$e++)
00346             {
00347                 $this->jr_id=$atmp[$e];
00348                 $ret[$i]['depend'][$e]=$this->fill_info();
00349             }
00350         }
00351         $this->db->prepare('detail_quant','select * from v_quant_detail where jr_id=$1');
00352         return $ret;
00353     }
00354     /**
00355      *@brief
00356      * Prepare and put in memory the SQL detail_quant
00357      *@param
00358      *@return
00359      *@note
00360      *@see
00361     @code
00362 
00363     @endcode
00364      */
00365     function get_reconciled_amount($p_equal=false)
00366     {
00367         $array=$this->get_reconciled();
00368         $ret=array();
00369         bcscale(2);
00370         for ($i=0;$i<count($array);$i++)
00371         {
00372             $first_amount=$array[$i]['first']['jr_montant'];
00373             $second_amount=0;
00374             for ($e=0;$e<count($array[$i]['depend']);$e++)
00375             {
00376                 $second_amount=bcadd($second_amount,$array[$i]['depend'][$e]['jr_montant']);
00377             }
00378             if ( $p_equal &&  $first_amount==$second_amount)
00379             {
00380                 $ret[]=$array[$i];
00381             }
00382             if ( ! $p_equal &&  $first_amount != $second_amount)
00383             {
00384                 $ret[]=$array[$i];
00385             }
00386         }
00387         return $ret;
00388     }
00389   /**
00390    *@brief create a string to filter thanks the date
00391    *@return a sql string like jr_date > ... and jr_date < ....
00392    *@note use the data member start_day and end_day
00393    *@see get_reconciled get_not_reconciled
00394    */
00395     function filter_date()
00396     {
00397       global $g_user;
00398       list($start,$end)=$g_user->get_limit_current_exercice();
00399 
00400       if (isDate($this->start_day) ==null)
00401         {
00402           $this->start_day=$start;
00403         }
00404       if ( isDate($this->end_day) == null)
00405         {
00406           $this->end_day=$end;
00407         }
00408       $sql=" (jr_date >= to_date('".$this->start_day."','DD.MM.YYYY')
00409                 and jr_date <= to_date('".$this->end_day."','DD.MM.YYYY'))";
00410       return $sql;
00411 
00412     }
00413     function show_detail($p_ret)
00414     {
00415         if (Database::num_row($p_ret)> 0)
00416         {
00417             echo '<tr class="odd">';
00418             echo '<td></td>';
00419             echo '<td colspan="5" style="border:1px solid black;width:auto">';
00420             include 'template/impress_reconciliation_detail.php';
00421             echo '</td>';
00422             echo '</tr>';
00423         }
00424     }
00425     /**
00426      * Export to CSV
00427      * @param type $p_choice 
00428      * 
00429      * @note must be set before calling
00430      *    - $this->a_jrn       array of ledger
00431      *    - $this->start_day start date
00432      *    - $this->end_day end date
00433      * @see Acc_Reconciliation::get_data
00434      */
00435     function export_csv($p_choice)
00436     {
00437         $array = $this->get_data($p_choice);
00438         for ($i = 0; $i < count($array); $i++)
00439         {
00440             // ---------------------------------------
00441             // first index has 2 arrays : first & depend[]
00442             // ---------------------------------------
00443 
00444             $first = $array[$i]['first'];
00445             $a_depend = array();
00446             if (isset($array[$i]['depend']))
00447             {
00448                 $a_depend = $array[$i]['depend'];
00449                 //----- HEADER ----
00450                 if ($i == 0)
00451                 {
00452                     printf('"n°";"Date";"internal";"libellé";"n° pièce";"nom journal";"type journal";"montant"');
00453                     printf(';"<->";"Date";"internal";"libellé";"n° pièce";"nom journal";"type journal";"montant"' . "\n\r");
00454                 }
00455             }
00456             else
00457             {
00458                 //----- HEADER ----
00459                 if ($i == 0)
00460                 {
00461                     printf('"n°";"Date";"internal";"libellé";"n° pièce";"nom journal";"type journal";"montant"' . "\n\r");
00462                 }
00463             }
00464             // --------------------------
00465             // Print First
00466             // --------------------------
00467             printf('%d;"%s";"%s";"%s";"%s";"%s";"%s";%f',$i, $first['jr_date'], $first['jr_internal'], $first['jr_comment'], $first['jr_pj_number'], $first['jrn_def_name'], $first['jrn_def_type'], $first['jr_montant']);
00468             if (count($a_depend) > 0)
00469             {
00470                 // --------------------------------------
00471                 // Print first depending operation
00472                 // --------------------------------------
00473                 $depend = $a_depend[0];
00474                 printf(';"<->";"%s";"%s";"%s";"%s";"%s";"%s";%f' . "\n\r", $depend['jr_date'], $depend['jr_internal'], $depend['jr_comment'], $depend['jr_pj_number'], $depend['jrn_def_name'], $depend['jrn_def_type'], $depend['jr_montant']);
00475 
00476                 // --------------------------------------
00477                 // print other depending operation if any
00478                 // --------------------------------------
00479                 for ($e = 1; $e < count($a_depend); $e++)
00480                 {
00481                     $depend = $a_depend[$e];
00482                     printf(';;;;;;;"<->";');
00483                     printf('"%s";"%s";"%s";"%s";"%s";"%s";%f' . "\n\r", $depend['jr_date'], $depend['jr_internal'], $depend['jr_comment'], $depend['jr_pj_number'], $depend['jrn_def_name'], $depend['jrn_def_type'], $depend['jr_montant']);
00484                 }
00485             }
00486             else
00487             {
00488                 printf("\n\r");
00489             }
00490         }
00491     }
00492 
00493     /**
00494      * 
00495      * @param type $p_choice
00496      *       - 0 : operation reconcilied
00497      *       - 1 : reconcilied with different amount
00498      *       - 2 : reconcilied with same amount
00499      *       - 3 : not reconcilied 
00500      * @return $array
00501      */
00502     function get_data($p_choice)
00503     {
00504         switch ($p_choice)
00505         {
00506             case 0:
00507                 $array = $this->get_reconciled();
00508                 break;
00509             case 1:
00510                 $array = $this->get_reconciled_amount(false);
00511                 break;
00512             case 2:
00513                 $array = $this->get_reconciled_amount(true);
00514                 break;
00515             case 3:
00516                 $array = $this->get_not_reconciled();
00517                 break;
00518             default:
00519                 echo "Choix invalid";
00520                 exit();
00521         }
00522         return $array;
00523     }
00524 
00525     static function test_me()
00526     {
00527         $cn=new Database(dossier::id());
00528         $rap=new Acc_Reconciliation($cn);
00529         var_dump($rap->get_reconciled_amount('',false));
00530     }
00531 
00532 }
 All Data Structures Namespaces Files Functions Variables Enumerations