class_pre_operation.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of PhpCompta.
00004  *
00005  *   PhpCompta 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  *   PhpCompta 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 PhpCompta; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 /* $Revision: 4822 $ */
00020 
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00022 
00023 /*!\file
00024  * \brief definition of Pre_operation
00025  */
00026 
00027 /*! \brief manage the predefined operation, link to the table op_def
00028  * and op_def_detail
00029  *
00030  */
00031 require_once("class_iselect.php");
00032 require_once("class_ihidden.php");
00033 class Pre_operation
00034 {
00035     var $db;                                            /*!< $db database connection */
00036     var $nb_item;                                       /*!< $nb_item nb of item */
00037     var $p_jrn;                                 /*!< $p_jrn jrn_def_id */
00038     var $jrn_type;                                      /*!< $jrn_type */
00039     var $name;                                          /*!< $name name of the predef. operation */
00040 
00041     function Pre_operation($cn)
00042     {
00043         $this->db=$cn;
00044         $this->od_direct='false';
00045     }
00046     /*!\brief fill the object with the $_POST variable */
00047     function get_post()
00048     {
00049         $this->nb_item=$_POST['nb_item'];
00050         $this->p_jrn=$_REQUEST['p_jrn'];
00051         $this->jrn_type=$_POST['jrn_type'];
00052 
00053         $this->name=$_POST['opd_name'];
00054 
00055         $this->name=(trim($this->name)=='')?$_POST['e_comm']:$this->name;
00056 
00057         if ( $this->name=="")
00058         {
00059             $n=$this->db->get_next_seq('op_def_op_seq');
00060             $this->name=$this->jrn_type.$n;
00061             // common value
00062         }
00063     }
00064     function delete ()
00065     {
00066         $sql="delete from op_predef where od_id=".$this->od_id;
00067         $this->db->exec_sql($sql);
00068     }
00069     /*!\brief save the predef check first is the name is unique
00070      * \return true op.success otherwise false
00071      */
00072     function save()
00073     {
00074 
00075         if (    $this->db->count_sql("select * from op_predef ".
00076                                   "where upper(od_name)=upper('".Database::escape_string($this->name)."')".
00077                                   "and jrn_def_id=".$this->p_jrn)
00078                 != 0 )
00079         {
00080             echo "<span class=\"notice\"> Ce modèle d' op&eacute;ration a d&eacute;j&agrave; &eacute;t&eacute; sauv&eacute;</span>";
00081             return false;
00082         }
00083         if ( $this->count()  > MAX_PREDEFINED_OPERATION )
00084         {
00085             echo '<span class="notice">Vous avez atteint le max. d\'op&eacute;ration pr&eacute;d&eacute;finie, d&eacute;sol&eacute;</span>';
00086             return false;
00087         }
00088         $sql=sprintf('insert into op_predef (jrn_def_id,od_name,od_item,od_jrn_type,od_direct)'.
00089                      'values'.
00090                      "(%d,'%s',%d,'%s','%s')",
00091                      $this->p_jrn,
00092                      Database::escape_string($this->name),
00093                      $this->nb_item,
00094                      $this->jrn_type,
00095                      $this->od_direct);
00096         $this->db->exec_sql($sql);
00097         $this->od_id=$this->db->get_current_seq('op_def_op_seq');
00098         return true;
00099     }
00100     /*!\brief load the data from the database and return an array
00101      * \return an array
00102      */
00103     function load()
00104     {
00105         $sql="select od_id,jrn_def_id,od_name,od_item,od_jrn_type".
00106              " from op_predef where od_id=".$this->od_id.
00107              " and od_direct='".$this->od_direct."'".
00108              " order by od_name";
00109         $res=$this->db->exec_sql($sql);
00110         $array=Database::fetch_all($res);
00111 
00112         return $array;
00113     }
00114     function compute_array()
00115     {
00116         $p_array=$this->load();
00117         $array=array(
00118                    "e_comm"=>$p_array[0]["od_name"],
00119                    "nb_item"=>(($p_array[0]["od_item"]<10?10:$p_array[0]["od_item"]))   ,
00120                    "p_jrn"=>$p_array[0]["jrn_def_id"],
00121                    "jrn_type"=>$p_array[0]["od_jrn_type"]
00122                );
00123         return $array;
00124 
00125     }
00126 
00127     /*!\brief show the button for selecting a predefined operation */
00128     function show_button()
00129     {
00130 
00131         $select=new ISelect();
00132         $value=$this->db->make_array("select od_id,od_name from op_predef ".
00133                                      " where jrn_def_id=".$this->p_jrn.
00134                                      " and od_direct ='".$this->od_direct."'".
00135                                      " order by od_name");
00136 
00137         if ( empty($value)==true) return "";
00138         $select->value=$value;
00139         $r=$select->input("pre_def");
00140 
00141         return $r;
00142     }
00143     /*!\brief count the number of pred operation for a ledger */
00144     function count()
00145     {
00146         $a=$this->db->count_sql("select od_id,od_name from op_predef ".
00147                                 " where jrn_def_id=".$this->p_jrn.
00148                                 " and od_direct ='".$this->od_direct."'".
00149                                 " order by od_name");
00150         return $a;
00151     }
00152     /*!\brief get the list of the predef. operation of a ledger
00153      * \return string
00154      */
00155     function get_list_ledger()
00156     {
00157         $sql="select od_id,od_name from op_predef ".
00158              " where jrn_def_id=".$this->p_jrn.
00159              " and od_direct ='".$this->od_direct."'".
00160              " order by od_name";
00161         $res=$this->db->exec_sql($sql);
00162         $all=Database::fetch_all($res);
00163         return $all;
00164     }
00165     /*!\brief set the ledger
00166      * \param $p_jrn is the ledger (jrn_id)
00167      */
00168     function set_jrn($p_jrn)
00169     {
00170         $this->p_jrn=$p_jrn;
00171     }
00172 }
00173 
00174 /*!\brief mother of the pre_op_XXX, it contains only one data : an
00175  * object Pre_Operation. The child class contains an array of
00176  * Pre_Operation object
00177  */
00178 class Pre_operation_detail
00179 {
00180     var $operation;
00181     function __construct($p_cn)
00182     {
00183         $this->db=$p_cn;
00184         $this->operation=new Pre_operation($this->db);
00185         $this->valid=array('ledger'=>'jrn_def_id','ledger_type'=>'jrn_type','direct'=>'od_direct');
00186                 $this->jrn_def_id=-1;
00187     }
00188 
00189 
00190     /*!\brief show a form to use pre_op
00191      */
00192     function form_get ()
00193     {
00194 
00195         $hid=new IHidden();
00196         $r=$hid->input("action","use_opd");
00197         $r.=$hid->input("jrn_type",$this->get("ledger_type"));
00198         $r.= HtmlInput::submit('use_opd','Utilisez une op&eacute;ration pr&eacute;d&eacute;finie');
00199         $r.= $this->show_button();
00200         return $r;
00201 
00202     }
00203     /*!\brief count the number of pred operation for a ledger */
00204     function count()
00205     {
00206         $a=$this->db->count_sql("select od_id,od_name from op_predef ".
00207                                 " where jrn_def_id=".$this->jrn_def_id.
00208                                 " and od_direct ='".$this->od_direct."'".
00209                                 " order by od_name");
00210         return $a;
00211     }
00212     /*!\brief show the button for selecting a predefined operation */
00213     function show_button()
00214     {
00215 
00216         $select=new ISelect();
00217 
00218         $value=$this->get_operation();
00219         //      if ( empty($value)==true) return "";
00220         $select->value=$value;
00221         $r=$select->input("pre_def");
00222         return $r;
00223     }
00224     public function   get_operation()
00225     {
00226                 if ( $this->jrn_def_id=='') return array();
00227         $value=$this->db->make_array("select od_id,od_name from op_predef ".
00228                                      " where jrn_def_id=".sql_string($this->jrn_def_id).
00229                                      " and od_direct ='".sql_string($this->od_direct)."'".
00230                                      " order by od_name",1);
00231         return $value;
00232     }
00233     function set($p_param,$value)
00234     {
00235         if ( ! isset ($this->valid[$p_param] ) )
00236         {
00237             echo(" le parametre $p_param n'existe pas ".__FILE__.':'.__LINE__);
00238             exit();
00239         }
00240         $attr=$this->valid[$p_param];
00241         $this->$attr=$value;
00242     }
00243     function get($p_param)
00244     {
00245 
00246         if ( ! isset ($this->valid[$p_param] ) )
00247         {
00248             echo(" le parametre $p_param n'existe pas ".__FILE__.':'.__LINE__);
00249             exit();
00250         }
00251         $attr=$this->valid[$p_param];
00252         return $this->$attr;
00253     }
00254 
00255     function get_post()
00256     {
00257         $this->operation->get_post();
00258     }
00259 
00260 }