noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_anc_group_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 group of object operations, used for misc operation
00024  */
00025 
00026 /*! \brief group of object operations, used for misc operation
00027  *
00028  */
00029 require_once("class_idate.php");
00030 require_once("class_itext.php");
00031 require_once("class_iselect.php");
00032 require_once("class_icheckbox.php");
00033 require_once ("class_anc_operation.php");
00034 require_once('class_database.php');
00035 require_once ('class_anc_plan.php');
00036 require_once ('class_dossier.php');
00037 
00038 class Anc_Group_Operation
00039 {
00040     var $db;    /*!< database connection */
00041     var  $id;   /*!< oa_group, a group contains
00042                           several rows of
00043                           operation_analytique linked by the
00044                           group id */
00045 
00046     var $a_operation;                                           /*!< array of operations */
00047     var $date;                                                  /*!< date of the operations */
00048     var $pa_id;                                                 /*!< the concerned pa_id */
00049 
00050     /*!\brief constructor */
00051     function  Anc_Group_Operation($p_cn,$p_id=0)
00052     {
00053         $this->db=$p_cn;
00054         $this->id=$p_id;
00055         $this->date=date("d.m.Y");
00056         $this->nMaxRow=10;
00057     }
00058     /*!\brief add several rows */
00059     function add()
00060     {
00061 
00062         $amount=0;
00063         try
00064         {
00065             $this->db->start();
00066             foreach ($this->a_operation as $row)
00067             {
00068                 $add=round($row->oa_amount,2);
00069                 $add=($row->oa_debit=='t')?$add:$add*(-1);
00070                 $amount+=round($add,2);
00071                 $row->add();
00072             }
00073             if ( $amount != 0 ) throw new Exception (_('Operation non equilibrée'));
00074         }
00075         catch (Exception $e)
00076         {
00077             echo $e->getTrace();
00078             $this->db->rollback();
00079             exit();
00080         }
00081         $this->db->commit();
00082     }
00083     /*!\brief show a form for the operation (several rows)
00084      * \return the string containing the form but without the form tag
00085      *
00086      */
00087     function form($p_readonly=0)
00088     {
00089         $wDate=new IDate("pdate",$this->date);
00090         $wDate->table=1;
00091         $wDate->size=10;
00092         $wDate->readonly=$p_readonly;
00093 
00094         $wDescription=new IText("pdesc");
00095         $wDescription->table=0;
00096         $wDescription->size=80;
00097         $wDescription->readonly=$p_readonly;
00098         // Show an existing operation
00099         //
00100         if ( isset ($this->a_operation[0]))
00101         {
00102             $wDate->value=$this->a_operation[0]->oa_date;
00103             $wDescription->value=$this->a_operation[0]->oa_description;
00104         }
00105 
00106         $ret="";
00107 
00108         $ret.='<table style="result"    >';
00109 
00110         $ret.="<TR>".$wDate->input()."</tr>";
00111         $ret.='<tr><td>Description</td>'.
00112               '<td colspan="3">'.
00113               $wDescription->input()."</td></tr>";
00114         $Plan=new Anc_Plan($this->db);
00115         $aPlan=$Plan->get_list();
00116         $max=(count($this->a_operation)<$this->nMaxRow)?$this->nMaxRow:count($this->a_operation);
00117         $ret.='</table><table  id="ago" style="width: 100%;">';
00118         /* show 10 rows */
00119         $ret.="<tr>";
00120         foreach ($aPlan as $d)
00121         {
00122             $idx=$d['id'];
00123             /* array of possible value for the select */
00124             $aPoste[$idx]=$this->db->make_array("select po_id as value,".
00125                                                 " po_name||':'||coalesce(po_description,'-') as label ".
00126                                                 " from poste_analytique ".
00127                                                 " where pa_id = ".$idx.
00128                                                 " order by po_name ");
00129 
00130             $ret.="<th> Poste </th>";
00131         }
00132         $ret.="<th></th>".
00133               "<th> Montant</th>".
00134               "<th>D&eacute;bit</th>".
00135               "</tr>";
00136 
00137         for ($i = 0;$i < $max;$i++)
00138         {
00139             $ret.="<tr>";
00140 
00141             foreach ($aPlan as $d)
00142             {
00143                 $idx=$d['id'];
00144                 // init variable
00145                 $wSelect=new ISelect("pop".$i."plan".$idx);
00146                 $wSelect->value=$aPoste[$idx];
00147                 $wSelect->size=12;
00148 
00149                 $wSelect->readOnly=$p_readonly;
00150                 if ( isset($this->a_operation[$i]))
00151                 {
00152                     $wSelect->selected=$this->a_operation[$i]->po_id;
00153                 }
00154                 $ret.=td($wSelect->input());
00155             }
00156             $wAmount=new INum("pamount$i",0.0);
00157             $wAmount->size=12;
00158             $wAmount->table=1;
00159             $wAmount->javascript=" onChange=format_number(this);caod_checkTotal()";
00160             $wAmount->readOnly=$p_readonly;
00161 
00162             $wDebit=new ICheckBox("pdeb$i");
00163             $wDebit->readOnly=$p_readonly;
00164             $wDebit->javascript=" onChange=caod_checkTotal()";
00165             if ( isset ($this->a_operation[$i]))
00166             {
00167                 $wSelect->selected=$this->a_operation[$i]->po_id;
00168                 $wAmount->value=$this->a_operation[$i]->oa_amount;
00169                 $wDebit->value=$this->a_operation[$i]->oa_debit;
00170                 if ( $wDebit->value=='t')
00171                 {
00172                     $wDebit->selected=true;
00173                 }
00174 
00175             }
00176 
00177             // build the table
00178 
00179             $ret.="<TD></TD>";
00180             $ret.=$wAmount->input();
00181             $ret.=td($wDebit->input());
00182 
00183             $ret.="</tr>";
00184         }
00185         $ret.="</table>";
00186         if ( $p_readonly==false)
00187         {
00188             $add_row=new IButton('Ajouter');
00189             $add_row->label=_('Ajouter une ligne');
00190             $add_row->javascript='anc_add_row(\'ago\');';
00191             $ret.=HtmlInput::hidden('nbrow',$max);
00192 
00193             $ret.=$add_row->input();
00194         }
00195         return $ret;
00196     }
00197     /*!\brief fill row from $_POST data
00198      *
00199      */
00200     function get_from_array($p_array)
00201     {
00202         $Plan=new Anc_Plan($this->db);
00203         $aPlan=$Plan->get_list();
00204 
00205 
00206         for ( $i = 0;$i <$p_array['nbrow'];$i++)
00207         {
00208             foreach ($aPlan as $d)
00209             {
00210                 $idx=$d['id'];
00211                 $p=new Anc_Operation($this->db);
00212                 $p->oa_amount=$p_array["pamount$i"];
00213 
00214                 $p->oa_description=$p_array["pdesc"];
00215                 $p->oa_date=$p_array['pdate'];
00216                 $p->j_id=0;
00217                 $p->oa_debit=(isset ($p_array["pdeb$i"]))?'t':'f';
00218                 $p->oa_group=0;
00219 
00220                 $p->po_id=$p_array["pop$i"."plan".$idx];
00221                 $p->pa_id=$idx;
00222                 $this->a_operation[]=clone $p;
00223             }
00224         }
00225     }
00226     /*!\brief save the group of operation but only if the amount is
00227        balanced  */
00228     function save()
00229     {
00230         $this->db->start();
00231         try
00232         {
00233             $oa_group=$this->db->get_next_seq('s_oa_group');
00234             for ($i=0;$i<count($this->a_operation);$i++)
00235             {
00236                 $this->a_operation[$i]->oa_group=$oa_group;
00237                 $this->a_operation[$i]->add();
00238             }
00239         }
00240         catch (Exception $ex)
00241         {
00242             echo '<span class="error">'.
00243             'Erreur dans l\'enregistrement '.
00244             __FILE__.':'.__LINE__.' '.
00245             $ex->getMessage();
00246             $p_cn->rollback();
00247             exit();
00248 
00249         }
00250         $this->db->commit();
00251     }
00252     /*!\brief show the form */
00253     function show()
00254     {
00255         return $this->form(1);
00256     }
00257     static function test_me()
00258     {
00259         $dossier=dossier::id();
00260         $cn=new Database($dossier);
00261 
00262         if ( isset($_POST['go']))
00263         {
00264             $b=new Anc_Group_Operation($cn);
00265             $b->get_from_array($_POST);
00266             exit();
00267         }
00268 
00269         $a=new Anc_Group_Operation($cn);
00270         echo '<form method="post">';
00271         echo $a->form();
00272         echo dossier::hidden();
00273         echo '<input type="submit" name="go">';
00274         echo '</form>';
00275 
00276     }
00277 
00278 }
 All Data Structures Namespaces Files Functions Variables Enumerations