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