noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_anc_operation.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 definition of Anc_Operation
00024  */
00025 require_once("class_ibutton.php");
00026 require_once("class_ihidden.php");
00027 require_once("class_iselect.php");
00028 require_once("class_itext.php");
00029 require_once("class_anc_plan.php");
00030 require_once ("user_common.php");
00031 
00032 /*! \brief this class is used to show the form for entering an
00033  *   operation only FOR analytic operation
00034  *   to save it, to display or to get a list from a certain period
00035  *
00036  */
00037 class Anc_Operation
00038 {
00039     var $db;    /*!< database connection */
00040     var $row;   /*!< array of row for one operation*/
00041     var $list;  /*!< array of all operation */
00042     var $id;    /*!< = oa_id (one row) */
00043     var $po_id; /*!< poste analytique */
00044     var $oa_amount;     /*!< amount for one row */
00045     var $oa_description;        /*!< comment for one row */
00046     var $oa_debit;      /*!< true for debit or false */
00047     var $j_id;          /*!< foreign key to a jrnx operation
00048                                       (or null if none */
00049     var $oa_group;   /*!< group of operation  */
00050     var $oa_date;          /*!< equal to j_date if j_id is not    null */
00051     var $pa_id; /*!< the plan analytique id */
00052     /**
00053      * In the case, the amount comes from a ND VAT, the variable
00054      * contents the jrnx.j_id of the source which was used to compute 
00055      * the amount
00056      */
00057     var $oa_jrnx_id_source; 
00058     /**
00059      * @brief signed of the amount
00060      */
00061     var $oa_positive;
00062     var $in_div; /*< name of the div if any, default empty string*/
00063     /*!\brief constructor
00064      *
00065      */
00066     function Anc_Operation ($p_cn,$p_id=0)
00067     {
00068         $this->db=$p_cn;
00069         $this->id=$p_id;
00070         $this->oa_jrnx_id_source=null;
00071         $this->oa_positive='Y';
00072         $this->has_data=0;
00073         $this->in_div="";
00074     }
00075     /*!\brief add a row  to the table operation_analytique
00076      * \note if $this->oa_group if 0 then a sequence id will be computed for
00077      * the oa_group, if $this->j_id=0 then it will be null
00078      *
00079      */
00080     function add($p_seq=0)
00081     {
00082 
00083         if ( $this->oa_group == 0)
00084         {
00085             $this->oa_group=$this->db->get_next_seq('s_oa_group');
00086         }
00087 
00088         if ( $this->j_id == 0 )
00089         {
00090             $this->j_id=null;
00091         }
00092 
00093 
00094         // we don't save null operations
00095         if ( $this->oa_amount == 0 || $this->po_id==-1)
00096             return;
00097         
00098         if ( $this->oa_amount< 0) 
00099         {
00100             $this->oa_debit=($this->oa_debit=='t')?'f':'t';
00101             $this->oa_positive='N';
00102         }
00103         
00104         $oa_row=(isset($this->oa_row))?$this->oa_row:null;
00105         $sql="insert into operation_analytique (
00106              po_id, 
00107              oa_amount,
00108              oa_description,
00109              oa_debit,
00110              oa_group,
00111              j_id,
00112              oa_date,
00113              oa_row,
00114              oa_jrnx_id_source,
00115              oa_positive
00116              ) values ($1,$2,$3,$4,$5,$6,to_date($7,'DD.MM.YYYY'),$8,$9,$10)";
00117 
00118         $this->db->exec_sql($sql,array(
00119                 $this->po_id, // 1
00120                 abs($this->oa_amount), //2
00121                 $this->oa_description, //3
00122                 $this->oa_debit, //4
00123                 $this->oa_group, //5
00124                 $this->j_id, //6
00125                 $this->oa_date, //7
00126                 $oa_row, //8
00127                 $this->oa_jrnx_id_source, //8
00128                 $this->oa_positive
00129                 ));
00130 
00131     }
00132     /*!\brief delete a row from the table operation_analytique
00133      * \note be carefull : do not delete a row when we have a group
00134      */
00135     function delete()
00136     {
00137         $sql="delete from operation_analytique where oa_id=$1";
00138 
00139         $this->db->exec_sql($sql,array($this->oa_id));
00140     }
00141 
00142     /*!\brief get a list of row from a certain periode
00143      */
00144     function get_list($p_from,$p_to,$p_from_poste="",$p_to_poste="")
00145     {
00146         $cond="";
00147         $cond_poste="";
00148 
00149         if ($p_from!="")
00150             $cond="and (jr_date >= to_date('$p_from','DD.MM.YYYY')  or oa_date >= to_date('$p_from','DD.MM.YYYY') )";
00151         if ( $p_to!="" )
00152             $cond.="and (jr_date <=to_date('$p_to','DD.MM.YYYY') or  oa_date <=to_date('$p_to','DD.MM.YYYY')) ";
00153 
00154         if ($p_from_poste != "" )
00155             $cond_poste=" and upper(po_name) >= upper('".$p_from_poste."')";
00156         if ($p_to_poste != "" )
00157             $cond_poste.=" and upper(po_name) <= upper('".$p_to_poste."')";
00158         $pa_id_cond="";
00159         if ( isset ( $this->pa_id) && $this->pa_id !='')
00160             $pa_id_cond= "pa_id=".$this->pa_id." and";
00161         $sql="
00162         
00163         select oa_id,
00164         po_name,
00165         oa_description,
00166         po_description,
00167         oa_debit,
00168         (case when jr_date is not null then to_char(jr_date,'DD.MM.YYYY') else to_char(oa_date,'DD.MM.YYYY') end )  as oa_date,
00169         oa_amount,
00170         oa_group,
00171         j_id ,
00172         jr_internal,
00173         jr_id,
00174         jr_comment,
00175         j_poste,
00176         jrnx.f_id,
00177         ( select ad_value from fiche_Detail where f_id=jrnx.f_id and ad_id=23) as qcode,
00178         jr_pj_number
00179         from operation_analytique as B join poste_analytique using(po_id)
00180         left join jrnx using (j_id)
00181         left join jrn on  (j_grpt=jr_grpt_id)
00182              where $pa_id_cond oa_amount <> 0.0 $cond $cond_poste
00183         order by jr_date,oa_group,oa_debit desc,oa_id";
00184 
00185         $RetSql=$this->db->exec_sql($sql);
00186         $array=Database::fetch_all($RetSql);
00187         return $array;
00188     }
00189 
00190     /*\brief show the HTML table for the operation
00191      */
00192     function html_table($p_from)
00193     {
00194         if ($p_from=="")
00195         {
00196             $from="";
00197             $to="";
00198         }
00199         else
00200         {
00201             $p=new Periode($this->db);
00202             list($from,$to)=$p->get_date_limit($p_from);
00203         }
00204 
00205         $array=$this->get_list($from,$to);
00206         if ( empty($array)  )
00207             return "Pas d'enregistrement trouv&eacute;";
00208 
00209         // navigation_bar
00210         $step=$_SESSION['g_pagesize'];
00211         $page=(isset($_GET['offset']))?$_GET['page']:1;
00212         $offset=(isset($_GET['offset']))?$_GET['offset']:0;
00213         $bar=navigation_bar($offset+1,count($array),$step,$page);
00214 
00215         if ( $step !=-1)
00216             $view=array_splice($array,$offset,$step);
00217         else
00218             $view=$array;
00219 
00220         $gDossier=dossier::id();
00221         $ret="";
00222         $ret.=$bar;
00223 
00224         $count=0;
00225         $group=0;
00226         $oldgroup=0;
00227         $oldjrid=0;
00228 
00229         foreach ($view as $row)
00230         {
00231             $group=$row['oa_group'];
00232             if ( $group !=$oldgroup )
00233             {
00234                 if ( $oldgroup!=0 )
00235                 {
00236 
00237                     $efface=new IButton();
00238                     $efface->javascript="anc_remove_operation(".$gDossier.",".$oldgroup.")";
00239                     $efface->name="Efface";
00240                     $efface->label="Efface";
00241                     $ret.="<td>".$efface->input()."</td>";
00242 
00243                     $this->oa_group=$oldgroup;
00244                     $jr_id=$this->get_jrid();
00245 
00246                     if ( $jr_id != 0)
00247                     {
00248                         // get the old jr_id
00249                         $detail=new IButton();
00250                         $detail->javascript="viewOperation($jr_id,$gDossier)";
00251                         $detail->name="Detail";
00252                         $detail->label="Detail";
00253                         $ret.="<td>".$detail->input()."</td>";
00254                     }
00255                     $ret.='</table>';
00256 
00257                 }
00258                 $ret.='<table id="'.$row['oa_group'].'" class="result">';
00259 
00260                 $ret.="<tr class=\"highlight\">".
00261                       td($row['oa_date']).
00262                       "<td>".
00263                       HtmlInput::detail_op($row['jr_id'],  h($row['oa_description']." ".$row['jr_pj_number'])).
00264                       "</td>".
00265                         td();
00266 
00267                 $ret.="<td>".
00268                       "Groupe id : ".$row['oa_group'].
00269                       "</td>".
00270 
00271                 $oldgroup=$group;
00272 
00273             }
00274 
00275             $class=($count%2==0)?"odd":"even";
00276             $count++;
00277             $cred= ( $row['oa_debit'] == 'f')?"CREDIT":"DEBIT";
00278             $ret.="<tr class=\"$class\">";
00279             $ret.= "<td>".
00280                    h($row['po_name']).
00281                    "</td>";
00282 
00283             $ret.=td(h($row['po_description']));
00284 
00285             $ret.='<td class="num">'.   nbm($row['oa_amount']).
00286                   "</td>".
00287                   "<td>".
00288                   $cred.
00289                   "</td>".
00290 
00291                   "</tr>";
00292         }
00293 
00294 
00295         $efface=new IButton();
00296         $efface->javascript="anc_remove_operation("."$gDossier,".$oldgroup.")";
00297         $efface->name="Efface";
00298         $efface->label="Efface";
00299         $ret.="<td>".$efface->input()."</td>";
00300         // get the old jr_id
00301         $this->oa_group=$oldgroup;
00302         $jr_id=$this->get_jrid();
00303         if ( $jr_id != 0 )
00304         {
00305             $detail=new IButton();
00306             $detail->javascript="modifyOperation($jr_id,'".$gDossier."')";
00307             $detail->name="Detail";
00308             $detail->label="Detail";
00309             $ret.="<td>".$detail->input()."</td>";
00310         }
00311         $ret.='</table>';
00312         $ret.=$bar;
00313         return $ret;
00314     }
00315     /*!\brief retrieve an operation thanks a jrnx.j_id
00316      * \param the jrnx.j_id
00317      * \return null if nothing is found other an array
00318      */
00319     function get_by_jid($p_jid)
00320     {
00321         $array=array();
00322         $a_plan=$this->db->get_array('select pa_id from plan_analytique order by pa_id');
00323         $res=array();
00324         /*
00325          * For one oa_row
00326          */
00327         $a_rowcount=$this->db->get_array("select distinct  oa_row "
00328                 ." from operation_analytique where j_id=$1 order by oa_row", array($p_jid));
00329 
00330         for ($i=0; $i<count($a_rowcount); $i++)
00331         {
00332             /*
00333              * Get one row as template for filling the missing 
00334              */
00335             $a_existing=$this->db->get_array('
00336                     select distinct oa_id,
00337                      po_id,
00338                      oa_amount,
00339                      oa_description,
00340                      oa_debit,
00341                      j_id,
00342                      oa_group,
00343                      oa_date,
00344                      pa_id,
00345                      oa_row,
00346                      oa_positive
00347                      from operation_analytique join poste_analytique using (po_id)
00348                      where
00349                      j_id=$1 and oa_row = $2 
00350                      order by j_id,oa_row', 
00351                     array($p_jid, $a_rowcount[$i]['oa_row']));
00352             
00353             // the first row we found will be the template
00354             $template=$a_existing[0];
00355             /*
00356              * For each plan
00357              */
00358             for ($j=0; $j<count($a_plan); $j++)
00359             {
00360                 /*
00361                  * Fetch the row with this pa_id, oa_row, max : 1 row
00362                  */
00363                 $a_fetch=$this->db->get_array('
00364                     select distinct oa_id,
00365                      po_id,
00366                      oa_amount,
00367                      oa_description,
00368                      oa_debit,
00369                      j_id,
00370                      oa_group,
00371                      oa_date,
00372                      pa_id,
00373                      oa_row,
00374                      oa_positive
00375                      from operation_analytique join poste_analytique using (po_id)
00376                      where
00377                      j_id=$1 and oa_row = $2  and pa_id=$3', array($p_jid,
00378                     $a_rowcount[$i]['oa_row'],
00379                     $a_plan[$j]['pa_id']
00380                         )
00381                 );
00382                 if (count($a_fetch)==0)
00383                 {
00384                     $a_fetch=$template;
00385                     $a_fetch['pa_id']=$a_plan[$j]['pa_id'];
00386                     $a_fetch['po_id']=-1;
00387                     $a_fetch['oa_id']='';
00388                     $res[]=$a_fetch;
00389                 }
00390                 else
00391                 if (count($a_fetch)==1)
00392                 {
00393                     $res[]=$a_fetch[0];
00394                 }
00395             }
00396         }
00397 
00398         foreach ($res as $row)
00399         {
00400             $a=new Anc_Operation($this->db);
00401             foreach ($row as $attr=> $value)
00402             {
00403                 $a->$attr=$row[$attr];
00404             }
00405             $array[]=clone $a;
00406         }
00407 
00408 
00409         return $array;
00410     }
00411 
00412     /*!\brief modify an op from modify_op.php
00413      *
00414      */
00415     function update_from_jrnx($p_po_id)
00416     {
00417         $a=$this->get_by_jid($this->j_id);
00418         if ( $a == null )
00419         {
00420             // retrieve data from jrnx
00421             $sql="select jr_date,j_montant,j_debit from jrnx ".
00422                  " join jrn on (jr_grpt_id = j_grpt) ".
00423                  "where j_id=".$this->j_id;
00424             $res=$this->db->exec_sql($sql);
00425             if (Database::num_row($res) == 0 ) return;
00426             $row=Database::fetch_array($res,0);
00427             $this->oa_amount=$row['j_amount'];
00428             $this->oa_date=$row['jr_date'];
00429             $this->oa_debit=$row['j_debit'];
00430             $this->oa_description=$row['jr_comment'];
00431             $this->add();
00432         }
00433         else
00434         {
00435             foreach ($a as $row )
00436             {
00437                 if ( $row->pa_id == $this->pa_id )
00438                 {
00439                     $row->po_id=$p_po_id;
00440                     $row->update();
00441                 }
00442             }
00443         }
00444     }
00445     /*!\brief retrieve the jr_id thanks the oa_group */
00446     function get_jrid()
00447     {
00448         $sql="select distinct jr_id from jrn join jrnx on (j_grpt=jr_grpt_id) join operation_analytique using (j_id) where j_id is not null and oa_group=".$this->oa_group;
00449         $res=$this->db->exec_sql($sql);
00450         if ( Database::num_row($res) == 0 ) return 0;
00451         $ret=Database::fetch_all($res);
00452         return $ret[0]['jr_id'];
00453     }
00454     /*\brief this function get the balance for a certain period
00455      *\param $p_from from date (accountancy period)
00456      *\param $p_to to dat  (accountancy period)
00457      *\param  $p_plan_id the plan id
00458      */
00459     function get_balance($p_from,$p_to,$p_plan_id)
00460     {
00461         // for the operation connected to jrnx
00462         $cond=sql_filter_per($this->db,$p_from,$p_to,'p_id','j_date');
00463         $sql="select oa_id, po_id, oa_amount, oa_debit, j_date from jrnx join operation_analytique using (j_id)
00464              join poste_analytique using (po_id)
00465              where
00466              $cond and j_id is not null and pa_id=$p_plan_id";
00467 
00468         // OD
00469         $cond=sql_filter_per($this->db,$p_from,$p_to,'p_id','oa_date');
00470         $sql="union select oa_id, po_id, oa_amount, oa_debit,oa_date from
00471              operation_analytique
00472              join poste_analytique using (po_id)
00473              where j_id is null and
00474              $cond and pa_id=$p_plan_id ";
00475         try
00476         {
00477             $res=$this->db->exec_sql($sql);
00478             $array=Database::fetch_all($res);
00479         }
00480         catch (Exception $e)
00481         {
00482             var_dump($e);
00483         }
00484     }
00485     /*!\brief display the form for PA
00486      *\param $p_array contains POST (or GET) data (val[] hplan[][] op[])
00487      * \param $p_null = 1 if PA optional otherwise 0 mandatory
00488      * \param $p_mode == form 1 ==> read/write otherwise 0==>readonly
00489      * \param $p_seq number of the row
00490      * \param $p_amount amount
00491      * \param $p_id operation is detailled in a HTML popup, if several
00492      *  are opened, the tableid MUST be different. So we need to use a new parameter
00493      * \param $p_add_button true, show the button, false don't display them
00494      * \see save_form_plan
00495     @note
00496     - op is an array containing the line number
00497     - pa_id is an array of the existing array
00498     - hplan is an array of the POSTE ANALYTIQUE id used, the size of hplan from 0 to x,
00499     x can be bigger than the number of plan id
00500     - val contains the amount by row inside the table. One operation (j_id) you can have several rows
00501     @code
00502     0 =>
00503       array
00504         'op' => int 200
00505     'pa_id' =>
00506       array
00507         0 => string '14' (length=2)
00508         1 => string '15' (length=2)
00509     'hplan' =>
00510       array
00511         1 =>
00512           array
00513             0 => string '25' (length=2)
00514             1 => string '26' (length=2)
00515     'val' =>
00516       array
00517         1 =>
00518           array
00519             0 => string '100.0000' (length=8)
00520 
00521     @endcode
00522      */
00523     function display_form_plan($p_array,$p_null,$p_mode,$p_seq,$p_amount,$p_id='',$p_add_button=true)
00524     {
00525         if ( $p_array != null)
00526             extract ($p_array);
00527         $result="";
00528         $plan=new Anc_Plan($this->db);
00529         $a_plan=$plan->get_list(" order by pa_id ");
00530         if ( empty ($a_plan) ) return "";
00531         $table_id="t".$p_seq;
00532         $hidden=new IHidden();
00533 
00534                 $readonly=($p_mode==1)?false:true;
00535 
00536         $result.=$hidden->input('amount_'.$table_id,$p_amount);
00537         if ( $p_mode==1 )
00538             $result.='<table id="'.$p_id.$table_id.'">';
00539         else
00540             $result.='<table>';
00541         $result.="<tr>".$plan->header()."<th>montant</th></tr>";
00542 
00543         /* compute the number of rows */
00544         $nb_row=(isset($val[$p_seq]))?count($val[$p_seq]):1;
00545         $count=0;
00546 
00547                 $remain=$p_amount;
00548                 $ctrl_remain="remain".$this->in_div.$table_id;
00549 
00550         for ( $i=0; $i < $nb_row;$i++)
00551         {
00552             $result.='<tr>';
00553 
00554             foreach ($a_plan as $r_plan)
00555             {
00556 
00557                 $array=$this->db->make_array(
00558                            "select po_id as value,".
00559                            " html_quote(po_name) as label from poste_analytique ".
00560                            " where pa_id = ".$r_plan['id'].
00561                            " order by po_name",$p_null);
00562                 $select =new ISelect("hplan[".$p_seq."][]",$array);
00563                 $select->table=0;
00564                 // view only or editables
00565                 if (  $p_mode==1 )
00566                 {
00567                     // editable
00568                     $select->readonly=false;
00569                     if ( isset($hplan) && isset($hplan[$p_seq][$count]) ){
00570                         $select->selected=$hplan[$p_seq][$count];
00571 
00572                                         }
00573                 }
00574                 else
00575                 {
00576                                         if ( isset($hplan) && isset($hplan[$p_seq][$count]) ){
00577                         $select->selected=$hplan[$p_seq][$count];
00578                                         }
00579                     // view only
00580                     $select->readOnly=true;
00581                 }
00582                 if ($p_mode==1)
00583                     $result.='<td>'.$select->input().'</td>';
00584                 else
00585                     $result.='<td>'.$select->display().'</td>';
00586                 $count++;
00587 
00588 
00589             }
00590             $value=new INum();
00591             $value->javascript='onchange="format_number(this);anc_refresh_remain(\''.$this->in_div.$table_id.'\',\''.$p_seq.'\')"';
00592             $value->name="val[".$p_seq."][]";
00593             $value->size=6;
00594             $value->value=(isset($val[$p_seq][$i]))?$val[$p_seq][$i]:$p_amount;
00595             $value->readOnly=($p_mode==1)?false:true;
00596                         $remain=bcsub($remain,$value->value);
00597             $result.='<td>'.$value->input().'</td>';
00598 
00599             $result.="</tr>";
00600         }
00601 
00602         $result.="</table>";
00603 
00604         if ($p_add_button && $p_mode == 1)
00605         {
00606             $style_remain=($remain==0)?'style="color:green"':' style="color:red"';
00607             $result.=" Reste à imputer =  ".
00608                      '<span class="remain" '.$style_remain.' id="'.$ctrl_remain.'">'.
00609                             $remain.'</span>';
00610             // add a button to add a row
00611             $button=new IButton();
00612             $button->javascript="add_row('".$p_id."$table_id',$p_seq);";
00613             $button->name="js".$p_id.$p_seq;
00614             $button->label=_("Nouvelle ligne");
00615 
00616             $result.="<br>".$button->input();
00617             /**
00618              * Add a button for distribution key
00619              * 
00620              */
00621             $ledger=HtmlInput::default_value_post("p_jrn", 0);
00622             if ($ledger==0) {
00623                 $ledger=$this->db->get_value('select j_jrn_def from jrnx where j_id=$1',array($this->j_id));
00624             }
00625             $gDossier=Dossier::id();
00626             $button_key=new IButton();
00627             $button_key->javascript="anc_key_choice(".$gDossier.",'".$p_id."$table_id',$p_amount,'".$ledger."');";
00628             $button_key->name="js".$p_id.$p_seq;
00629             $button_key->label=_("Clef");
00630             $result .= $button_key->input();
00631 
00632         }
00633 
00634         return $result;
00635     }
00636     /**
00637      * Save the ND VAT with prorata
00638      * 
00639      * @param $p_array usually $_POST
00640      * @param $p_item item of the form
00641      * @param $p_j_id jrnx.j_id concerned writing
00642      * @param $p_nd amount nd vat
00643      * @see Anc_Operation::save_form_plan_vat_nd
00644      * @return type
00645      */
00646     function save_form_plan_vat_nd($p_array,$p_item,$p_j_id,$p_nd)
00647     {
00648         bcscale(4);
00649         extract($p_array);
00650         if (! isset ($hplan) ) return;
00651         
00652         if ( ! isset(${'amount_t'.$p_item}) )
00653             throw new Exception ('amount not set');
00654         
00655         $tot=0;
00656         /* variable for in array
00657            pa_id array of existing pa_id
00658            hplan double array with the pa_id (column)
00659            val double array by row with amount
00660            op contains sequence
00661            p_item is used to identify what op is concerned
00662         */
00663         /* echo "j_id = $j_id p_item = $p_item hplan=".var_export($hplan[$p_item],true)." val = ".var_export($val[$p_item],true).'<br>'; */
00664         /* for each row */
00665         //   for ($i=0;$i<count($val[$p_item]);$i++) {
00666         $idx_pa_id=0;
00667         $row=0;
00668         $a_Anc_Operation=array();
00669         // foreach col PA
00670         for ($e=0;$e<count($hplan[$p_item]);$e++)
00671         {
00672             if ( $idx_pa_id == count($pa_id))
00673             {
00674                 $idx_pa_id=0;
00675                 $row++;
00676             }
00677             if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
00678             {
00679                 $op=new Anc_Operation($this->db);
00680                 $op->po_id=$hplan[$p_item][$e];
00681                 $op->oa_group=$this->oa_group;
00682                 $op->j_id=$p_j_id;
00683                 $ratio=bcdiv($val[$p_item][$row],${"amount_t".$p_item});
00684                 $amount=  bcmul($p_nd, $ratio);
00685                 $op->oa_amount=abs(round($amount,2));
00686                 $op->oa_debit=$this->oa_debit;
00687                 $op->oa_date=$this->oa_date;
00688 
00689                 $op->oa_description=$this->oa_description;
00690                 $op->oa_row=$row;
00691                 $op->oa_jrnx_id_source=$this->oa_jrnx_id_source;
00692                 $a_Anc_Operation[]=clone $op;
00693             }
00694             $idx_pa_id++;
00695         }
00696         $nb_op=count($a_Anc_Operation);
00697         bcscale(2);
00698         for ($i=0;$i<$nb_op;$i++)
00699         {
00700             $tot=bcadd($tot,$a_Anc_Operation[$i]->oa_amount);
00701         }
00702         if ( $tot != $p_nd )
00703         {
00704             $diff=  bcsub($tot, $p_nd);
00705             $a_Anc_Operation[0]->oa_amount=bcsub($a_Anc_Operation[0]->oa_amount,$diff);
00706         }
00707         for ($i=0;$i<$nb_op;$i++)
00708         {
00709             $a_Anc_Operation[$i]->add();
00710         }
00711     }
00712     /*!\brief it called for each item, the data are taken from $p_array
00713      *  data and set before in this.
00714      * \param $p_item if the item nb for each item (purchase or selling
00715      *  merchandise)
00716      * \param $p_array structure
00717      * \verbatim
00718       nb_tA A is the number of the item it contains the number of
00719               rows of CA for this card
00720       valAlR amount for the CA (item A row R)
00721       ta_AoCrow_R contains the value of the pa_id and po_id for this
00722                   row with the form pa_id_po_id %d_%d
00723      *\endverbatim
00724      * \attention The idea is one j_id matches several oa_id,
00725      *  serveral data are set before the call :
00726      *   -j_id
00727      *   -oa_debit
00728      *   -oa_group
00729      *   -oa_date
00730      *   -oa_description
00731      *
00732      */
00733     function save_form_plan($p_array,$p_item,$p_j_id)
00734     {
00735         extract($p_array);
00736         if (! isset ($hplan) ) return;
00737         /* variable for in array
00738            pa_id array of existing pa_id
00739            hplan double array with the pa_id (column)
00740            val double array by row with amount
00741            op contains sequence
00742            p_item is used to identify what op is concerned
00743         */
00744         /* echo "j_id = $j_id p_item = $p_item hplan=".var_export($hplan[$p_item],true)." val = ".var_export($val[$p_item],true).'<br>'; */
00745         /* for each row */
00746         //   for ($i=0;$i<count($val[$p_item]);$i++) {
00747         $idx_pa_id=0;
00748         $row=0;
00749         // foreach col PA
00750         for ($e=0;$e<count($hplan[$p_item]);$e++)
00751         {
00752             if ( $idx_pa_id == count($pa_id))
00753             {
00754                 $idx_pa_id=0;
00755                 $row++;
00756             }
00757             if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
00758             {
00759                 $op=new Anc_Operation($this->db);
00760                 $op->po_id=$hplan[$p_item][$e];
00761                 $op->oa_group=$this->oa_group;
00762                 $op->j_id=$p_j_id;
00763                 $op->oa_amount=$val[$p_item][$row];
00764                 $op->oa_debit=$this->oa_debit;
00765                 $op->oa_date=$this->oa_date;
00766 
00767                 $op->oa_description=$this->oa_description;
00768                 $op->oa_row=$row;
00769                 $op->add();
00770             }
00771             $idx_pa_id++;
00772         }
00773         // }
00774     }
00775 
00776     /**
00777     *@brief save a whole form from a update box
00778     *@param $p_array  for ALL j_id
00779     *@return
00780     *@note
00781     *@see save_form_plan to_request
00782     @code
00783 
00784     @endcode
00785     */
00786     function save_update_form($p_array)
00787     {
00788         extract($p_array);
00789         for ($i = 0; $i < count($op); $i++)
00790         {
00791             /* clean operation_analytique */
00792             $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($op[$i]));
00793 
00794             /* get missing data for adding */
00795             $a_missing = $this->db->get_array("select to_char(jr_date,'DD.MM.YYYY') 
00796                     as mdate,j_montant,j_debit,jr_comment ,j_poste
00797                     from jrnx join jrn on (j_grpt=jr_grpt_id) where j_id=$1", array($op[$i]));
00798             $missing = $a_missing[0];
00799             $poste=substr(trim($missing['j_poste']),0,1);
00800             if ( $poste == 6 || $poste == 7)  {
00801                 $this->oa_debit = ($poste == 6) ?'t':'f';
00802             }else {
00803                 $this->oa_debit=$missing['j_debit'];
00804             }
00805             $this->oa_description = $missing['jr_comment'];
00806             $this->j_id = $op[$i];
00807             $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
00808             $this->oa_group = $group;
00809             $this->oa_date = $missing['mdate'];
00810             $this->save_form_plan($p_array, $i, $op[$i]);
00811             
00812             // There is ND VAT amount
00813             $a_nd = $this->db->get_array('select j_id from operation_analytique
00814                 where oa_jrnx_id_source=$1', array($op[$i]));
00815             if (count($a_nd) > 0)
00816             {
00817                 // for each ND VAT amount
00818                 for ($e=0;$e<count($a_nd);$e++)
00819                 {
00820                     $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($a_nd[$e]['j_id']));
00821                     /* get missing data for adding */
00822                     $a_missing_vat = $this->db->get_array("select to_char(jr_date,'DD.MM.YYYY') as mdate,j_montant,j_debit,jr_comment from jrnx join jrn on (j_grpt=jr_grpt_id) where j_id=$1", array($a_nd[$e]['j_id']));
00823                     $missing_vat = $a_missing_vat[0];
00824                     $this->oa_debit = 't';
00825                     $this->oa_description = $missing_vat['jr_comment'];
00826                     $this->j_id = $op[$i];
00827                     $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
00828                     $this->oa_group = $group;
00829                     $this->oa_date = $missing_vat['mdate'];
00830                     $this->oa_jrnx_id_source=$op[$i];
00831                     $p_array['amount_t'.$i]=$missing['j_montant'];
00832                     $this->save_form_plan_vat_nd($p_array, $i, $a_nd[$e]['j_id'],$missing_vat['j_montant']);
00833                 }
00834             }
00835         }
00836     }
00837 
00838     /*\brief transform a array of operation into a array usage by
00839      *display_form_plan & save_form_plan
00840      *\param $p_array array of operation
00841      *\param $p_line line
00842      *\return an array complying with \see save_form_plan
00843      */
00844     function to_request ($p_array,$p_line)
00845     {
00846         $result=array();
00847         $result[]=array('op'=>$this->j_id);
00848         $pa_id=array();
00849 
00850         /* First add the pa_id */
00851         for ($i=0;$i < count($p_array);$i++)
00852         {
00853             if ( in_array($p_array[$i]->pa_id,$pa_id)==false)
00854                 $pa_id[]=$p_array[$i]->pa_id;
00855         }
00856         $result['pa_id']=$pa_id;
00857 
00858         /* add the hplan */
00859         $seq=0;
00860         for ($i=0;$i < count($p_array);$i++)
00861         {
00862             $hplan[$p_line][$i]=$p_array[$i]->po_id;
00863         }
00864         $result['hplan']=$hplan;
00865         /* Add the amount */
00866         $idx_pa=0;
00867         $jrn_def=$this->db->get_value('select jrn_def_type from jrnx join jrn_def on (j_jrn_def=jrn_def_id) where j_id=$1',array($this->j_id));
00868         for ($i=0;$i < count($p_array);$i++)
00869         {
00870             
00871             /*
00872              * For the bank, negatif are always on the debit and positif on the credit
00873              */
00874             if ( $jrn_def != 'FIN')
00875             {
00876                 $val[$p_line][$p_array[$i]->oa_row]=($p_array[$i]->oa_positive=='Y')?$p_array[$i]->oa_amount:($p_array[$i]->oa_amount*(-1));
00877             }
00878             else
00879             {
00880                 $val[$p_line][$p_array[$i]->oa_row]=$p_array[$i]->oa_amount;
00881             }
00882         }
00883         $result['val']=$val;
00884         return $result;
00885     }
00886     /*!
00887      * \brief delete from operation_analytique
00888      * \param $p_jid the operation_analytique.j_id field
00889      *
00890      * \return none
00891      */
00892     function delete_by_jid($p_jid)
00893     {
00894         $sql="delete from operation_analytique where j_id=$p_jid";
00895         $this->db->exec_sql($sql);
00896     }
00897     /*\brief Display a table with analytic accounting in
00898      *       detail of operation
00899      *@note $this->j_id must be set
00900      *\param $p_mode 0 = readonly or 1=writable
00901      *\param $p_amount amount
00902      *\param $p_id unique id
00903      *@see display_form_plan
00904      *\return string to display
00905      */
00906     function display_table($p_mode,$p_amount,$p_id)
00907     {
00908         static $seq=-1;         /* first seq == 0 */
00909         $seq++;
00910 
00911         $array=$this->get_by_jid($this->j_id) ;
00912         if ( $array != null )
00913         {
00914             $request=$this->to_request($array,$seq);
00915             return "<td>".$this->display_form_plan($request,1,$p_mode,$seq,$p_amount,$p_id)."</td>";
00916         }
00917         else
00918         {
00919             return '<td>'.$this->display_form_plan(null,1,$p_mode,$seq,$p_amount,$p_id)."</TD>";
00920         }
00921         return "";
00922 
00923     }
00924 ///////////////////////////////////////////////////////////////////////////
00925 // TEST
00926 ///////////////////////////////////////////////////////////////////////////
00927     /*\brief test the class
00928      *\param
00929      *\param
00930      *\return
00931      */
00932     function test_me()
00933     {
00934         $cn=new Database(dossier::id());
00935         $anco=new Anc_Operation($cn);
00936         $j_id=200;
00937         $anco->j_id=$j_id;
00938         $array=$anco->get_by_jid($j_id);
00939         $a=$anco->to_request($array,1);
00940         echo '<form>';
00941         echo dossier::hidden();
00942         echo HtmlInput::hidden('j_id',$j_id);
00943         echo HtmlInput::hidden('test_select',$_REQUEST['test_select']);
00944         echo $anco->display_table(1,15002,0);
00945         echo '<input type="submit" name="save">';
00946         echo '</form>';
00947         if ( isset($_REQUEST['save']))
00948         {
00949             echo "to_save";
00950             var_dump($_REQUEST);
00951         }
00952         var_dump($a);
00953 
00954     }
00955 
00956 }
 All Data Structures Namespaces Files Functions Variables Enumerations