class_acc_payment.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: 4877 $ */
00020 
00021 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00022 
00023 /*!\file
00024  * \brief Handle the table mod_payment
00025  */
00026 require_once("class_iselect.php");
00027 require_once("class_icard.php");
00028 require_once("class_ispan.php");
00029 require_once('class_acc_ledger.php');
00030 require_once('class_fiche.php');
00031 require_once('class_fiche_def.php');
00032 require_once('constant.php');
00033 /*!\brief Handle the table mod_payment
00034  *\note the private data member are accessed via
00035   - mp_id  ==> id ( Primary key )
00036   - mp_lib ==> lib (label)
00037   - mp_jrn_def_id ==> ledger (Number of the ledger where to save)
00038   - mp_fd_id ==> fiche_def (fiche class to use)
00039   - mp_qcode ==> qcode (quick_code of the card)
00040  *
00041  */
00042 class Acc_Payment
00043 {
00044 
00045     private static $variable=array("id"=>"mp_id",
00046                                    "lib"=>"mp_lib",
00047                                    "qcode"=>"mp_qcode",
00048                                    "ledger_target"=>"mp_jrn_def_id",
00049                                    "ledger_source"=>"jrn_def_id",
00050                                    "fiche_def"=>"mp_fd_id");
00051 
00052 
00053     private  $mp_lib;
00054     private  $mp_qcode;
00055     private  $mp_jrn_def_if;
00056     private  $jrn_def_id;
00057     private  $mp_fd_id;
00058 
00059     function __construct ($p_cn,$p_init=0)
00060     {
00061         $this->cn=$p_cn;
00062         $this->mp_id=$p_init;
00063     }
00064     public function get_parameter($p_string)
00065     {
00066         if ( array_key_exists($p_string,self::$variable) )
00067         {
00068             $idx=self::$variable[$p_string];
00069             return $this->$idx;
00070         }
00071         else
00072                 {
00073                         throw new Exception("Attribut inexistant $p_string");
00074             exit (__FILE__.":".__LINE__.'Erreur attribut inexistant');
00075                 }
00076     }
00077     public function set_parameter($p_string,$p_value)
00078     {
00079         if ( array_key_exists($p_string,self::$variable) )
00080         {
00081             $idx=self::$variable[$p_string];
00082             $this->$idx=$p_value;
00083         }
00084         else
00085             exit (__FILE__.":".__LINE__.'Erreur attribut inexistant');
00086 
00087 
00088     }
00089     public function get_info()
00090     {
00091         return var_export(self::$variable,true);
00092     }
00093     public function verify()
00094     {
00095         // Verify that the elt we want to add is correct
00096     }
00097     public function save()
00098     {
00099         /* please adapt */
00100         if (  $this->get_parameter("id") == 0 )
00101             $this->insert();
00102         else
00103             $this->update();
00104     }
00105 
00106     public function insert()
00107     {
00108         if ( $this->verify() != 0 ) return;
00109         $sql='INSERT INTO mod_payment(
00110              mp_lib, mp_jrn_def_id, mp_fd_id, mp_qcode,jrn_def_id)
00111              VALUES ($1, $2, $3, upper($4),$5) returning mp_id';
00112         $this->mp_id=$this->cn->exec_sql($sql,array(
00113                                              $this->mp_lib,
00114                                              $this->mp_jrn_def_id,
00115                                              $this->mp_fd_id,
00116                                              $this->mp_qcode,
00117                                              $this->jrn_def_id));
00118     }
00119 
00120     public function update()
00121     {
00122         if ( $this->verify() != 0 ) return;
00123 
00124         $sql="update mod_payment set mp_lib=$1,mp_qcode=$2,mp_jrn_def_id=$3,mp_fd_id=$4,jrn_def_id=$5 ".
00125              " where mp_id = $6";
00126         $res=$this->cn->exec_sql(
00127                  $sql,
00128                  array($this->mp_lib,
00129                        $this->mp_qcode,
00130                        $this->mp_jrn_def_id,
00131                        $this->mp_fd_id,
00132                        $this->jrn_def_id,
00133                        $this->mp_id)
00134              );
00135         if ( strlen (trim($this->mp_jrn_def_id))==0)
00136             $this->cn->exec_sql(
00137                 'update mod_payment '.
00138                 'set mp_jrn_def_id = null where mp_id=$1',
00139                 array($this->mp_id));
00140         if ( strlen (trim($this->jrn_def_id))==0)
00141             $this->cn->exec_sql(
00142                 'update mod_payment '.
00143                 'set mp_jrn_def_id = null where mp_id=$1',
00144                 array($this->mp_id));
00145         if ( strlen (trim($this->mp_qcode))==0)
00146             $this->cn->exec_sql(
00147                 'update mod_payment '.
00148                 'set mp_qcode = null where mp_id=$1',
00149                 array($this->mp_id));
00150         if ( strlen (trim($this->mp_fd_id))==0)
00151             $this->cn->exec_sql(
00152                 'update mod_payment '.
00153                 'set mp_fd_id = null where mp_id=$1',
00154                 array($this->mp_id));
00155 
00156     }
00157 
00158     public function load()
00159     {
00160         $sql='select mp_id,mp_lib,mp_fd_id,mp_jrn_def_id,mp_qcode,jrn_def_id from mod_payment '.
00161              ' where mp_id = $1';
00162         $res=$this->cn->exec_sql(
00163                  $sql,
00164                  array($this->mp_id)
00165              );
00166 
00167         if ( Database::num_row($res) == 0 ) return;
00168         $row=Database::fetch_array($res,0);
00169         foreach ($row as $idx=>$value)
00170         {
00171             $this->$idx=$value;
00172         }
00173 
00174     }
00175     /**
00176      *@brief remove a middle of payment
00177      */
00178     public function delete()
00179     {
00180         $sql="delete from mod_payment where mp_id=$1";
00181         $this->cn->exec_sql($sql,array($this->mp_id));
00182     }
00183     /*!\brief retrieve all the data for all ledgers
00184      *\param non
00185      *\return an array of row
00186      */
00187     public function get_all()
00188     {
00189         $sql='select mp_id,mp_lib '.
00190              ' from mod_payment order by mp_lib';
00191         $array=$this->cn->get_array($sql);
00192         $ret=array();
00193         if ( !empty($array) )
00194         {
00195             foreach ($array as $row)
00196             {
00197                 $t=new Acc_Payment($this->cn,$row['mp_id']);
00198                 $t->load();
00199                 $ret[]=$t;
00200             }
00201         }
00202         return $ret;
00203     }
00204     /*!\brief retrieve all the data for a ledger but filter on the
00205      *valid record (jrn and fd not null
00206      *\param non
00207      *\return an array of row
00208      */
00209     public function get_valide()
00210     {
00211         $sql='select mp_id '.
00212              ' from mod_payment '.
00213              ' where jrn_def_id=$1 and mp_jrn_def_id is not null and '.
00214              ' (mp_fd_id is not null or mp_qcode is not null)';
00215         $array=$this->cn->get_array($sql,array($this->jrn_def_id));
00216         $ret=array();
00217         if ( !empty($array) )
00218         {
00219             foreach ($array as $row)
00220             {
00221                 $t=new Acc_Payment($this->cn,$row['mp_id']);
00222                 $t->load();
00223                 $ret[]=$t;
00224             }
00225         }
00226         return $ret;
00227     }
00228     /**
00229      *@deprecated
00230      * @return string
00231      */
00232     public function row_deprecated()
00233     {
00234         //---------------------------------------------------------------------------
00235         // Common variable
00236         $td='<TD>';
00237         $etd='</td>';
00238         $tr='<tr>';
00239         $etr='</tr>';
00240         $th='<th>';
00241         $eth='</th>';
00242 
00243         $r='';
00244         if ( $this->jrn_def_id != '' )
00245         {
00246             $name=$this->cn->get_value("select jrn_def_name from jrn_def where jrn_def_id=$1",
00247                     array($this->jrn_def_id));
00248         }
00249         $r.=td($this->mp_lib);
00250 
00251         if ( $this->mp_fd_id != NULL && $this->mp_fd_id !=0)
00252         {
00253             $fiche=new Fiche_Def($this->cn,$this->mp_fd_id);
00254             $fiche->Get();
00255             $r.=td($fiche->label);
00256         }
00257         else
00258             $r.=$td.$etd;
00259         $jrn=new Acc_Ledger($this->cn,$this->mp_jrn_def_id);
00260         $r.=$td.$jrn->get_name().$etd;
00261         if ( strlen(trim($this->mp_qcode)) != 0 )
00262         {
00263             $f=new Fiche($this->cn);
00264             $f->get_by_qcode($this->mp_qcode);
00265             $r.=td($f->strAttribut(ATTR_DEF_NAME));
00266 
00267         }
00268         else
00269             $r.=$td.$etd;
00270         return $r;
00271     }
00272     /*!\brief return a string with a form (into a table)
00273      *\param none
00274      *\return a html string
00275      */
00276     public function form()
00277     {
00278         //label
00279         $lib=new IText('mp_lib');
00280         $lib->value=$this->mp_lib;
00281                 $f_lib=$lib->input();
00282 
00283 
00284         $ledger_source=new ISelect('jrn_def_id');
00285         $ledger_source->value=$this->cn->make_array("select jrn_def_id,jrn_Def_name from
00286                               jrn_def where jrn_def_type  in ('ACH','VEN') order by jrn_def_name");
00287                 $ledger_source->selected=$this->jrn_def_id;
00288         $f_source=$ledger_source->input();
00289 
00290         // type of card
00291         $tcard=new ISelect('mp_fd_id');
00292         $tcard->value=$this->cn->make_array('select fd_id,fd_label from fiche_def join fiche_def_ref '.
00293                                             ' using (frd_id) where frd_id in (25,4) order by fd_label');
00294                 $tcard->selected=$this->mp_fd_id;
00295 
00296         $f_type_fiche=$tcard->input();
00297         $ledger_record=new ISelect('mp_jrn_def_id');
00298         $ledger_record->value=$this->cn->make_array("select jrn_def_id,jrn_Def_name from
00299                               jrn_def where jrn_def_type  in ('ODS','FIN')");
00300                 $ledger_record->selected=$this->mp_jrn_def_id;
00301         $f_ledger_record=$ledger_record->input();
00302 
00303         // the card
00304         $qcode=new ICard();
00305         $qcode->noadd=true;
00306         $qcode->name='mp_qcode';
00307         $list=$this->cn->make_list('select fd_id from fiche_def where frd_id in (25,4)');
00308         $qcode->typecard=$list;
00309                 $qcode->dblclick='fill_ipopcard(this);';
00310                 $qcode->value=$this->mp_qcode;
00311 
00312         $f_qcode=$qcode->input();
00313 
00314                 $msg="Modification de ".$this->mp_lib;
00315         ob_start();
00316         require_once('template/new_mod_payment.php');
00317         $r=ob_get_contents();
00318         ob_end_clean();
00319         return $r;
00320 
00321     }
00322     /*!\brief show several lines with radio button to select the payment
00323      *method we want to use, the $_POST['e_mp'] will be set
00324      *\param none
00325      *\return html string
00326      */
00327     public function select()
00328     {
00329         $r='';
00330         $array=$this->get_valide();
00331         $r.=HtmlInput::hidden('gDossier',dossier::id());
00332 
00333         if ( empty($array)==false ) {
00334             $acompte=new INum('acompte');
00335             $acompte->value=0;
00336             $r.=_(" Acompte à déduire");
00337             $r.=$acompte->input();
00338         }
00339 
00340         $r.='<ol>';
00341         $r.='<li ><input type="radio" name="e_mp" value="0" checked>'._('Paiement encodé plus tard');
00342         if ( empty($array ) == false )
00343         {
00344             foreach ($array as $row)
00345             {
00346                 $f='';
00347                 /* if the qcode is  null the propose a search button to select
00348                    the card */
00349                 if ( $row->mp_qcode==NULL)
00350                 {
00351                     $a=new ICard();
00352                     $a->jrn=$row->mp_jrn_def_id;
00353                     $a->set_attribute('typecard',$row->mp_fd_id);
00354                     $a->name='e_mp_qcode_'.$row->mp_id;
00355                     $a->set_dblclick("fill_ipopcard(this);");
00356                     $a->set_callback('filter_card');
00357                     $a->set_function('fill_data');
00358                     $a->set_attribute('ipopup','ipopcard');
00359                     $a->set_attribute('label',$a->name.'_label');
00360 
00361                     $s=new ISpan();
00362                     $s->name=$a->name.'_label';
00363                     $f=_(" paiement par ").$a->input().$s->input();
00364                 }
00365                 else
00366                 {
00367                     /* if the qcode is not null then add a hidden variable with
00368                        the qcode */
00369 
00370                     $fiche=new Fiche($this->cn);
00371                     $fiche->get_by_qcode($row->mp_qcode);
00372                     $f=HtmlInput::hidden('e_mp_qcode_'.$row->mp_id,$row->mp_qcode);
00373 
00374                     //    $f.=$fiche->strAttribut(ATTR_DEF_NAME);
00375                 }
00376                 $r.='<li><input type="radio" name="e_mp" value="'.$row->mp_id.'">';
00377                 $r.=$row->mp_lib.'  '.$f;
00378 
00379             }
00380         }
00381         $r.='</ol>';
00382         return $r;
00383     }
00384 
00385     /*!\brief convert an array into an Acc_Payment object
00386      *\param array to convert
00387      */
00388     public function from_array($p_array)
00389     {
00390         $idx=array('mp_id','mp_lib','mp_fd_id','mp_jrn_def_id','mp_qcode','jrn_def_id');
00391         foreach ($idx as $l)
00392         if (isset($p_array[$l])) $this->$l=$p_array[$l];
00393     }
00394     /**
00395      *@brief return an html with a form to add a new middle of payment
00396      */
00397     public function blank()
00398     {
00399         //label
00400         $lib=new IText('mp_lib');
00401         $f_lib=$lib->input();
00402 
00403         $ledger_source=new ISelect('jrn_def_id');
00404         $ledger_source->value=$this->cn->make_array("select jrn_def_id,jrn_Def_name from
00405                               jrn_def where jrn_def_type  in ('ACH','VEN') order by jrn_def_name");
00406         $f_source=$ledger_source->input();
00407 
00408         // type of card
00409         $tcard=new ISelect('mp_fd_id');
00410         $tcard->value=$this->cn->make_array('select fd_id,fd_label from fiche_def join fiche_def_ref '.
00411                                             ' using (frd_id) where frd_id in (25,4) order by fd_label');
00412         $f_type_fiche=$tcard->input();
00413         $ledger_record=new ISelect('mp_jrn_def_id');
00414         $ledger_record->value=$this->cn->make_array("select jrn_def_id,jrn_Def_name from
00415                               jrn_def where jrn_def_type  in ('ODS','FIN')");
00416         $f_ledger_record=$ledger_record->input();
00417 
00418         // the card
00419         $qcode=new ICard();
00420         $qcode->noadd=true;
00421         $qcode->name='mp_qcode';
00422         $list=$this->cn->make_list('select fd_id from fiche_def where frd_id in (25,4)');
00423         $qcode->typecard=$list;
00424                 $qcode->dblclick='fill_ipopcard(this);';
00425 
00426         $f_qcode=$qcode->input();
00427                 $msg="Ajout d'un nouveau moyen de paiement";
00428         ob_start();
00429         require_once('template/new_mod_payment.php');
00430         $r=ob_get_contents();
00431         ob_end_clean();
00432         return $r;
00433     }
00434     /*!\brief test function
00435      */
00436     static function test_me()
00437     {
00438 
00439     }
00440 
00441 }
00442 
00443