Main Page | Namespace List | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

class_fiche.php

Go to the documentation of this file.
00001 <?
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: 1.14 $ */
00020 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00021 include_once("class_attribut.php");
00030 class fiche_def {
00031   var $cn;           // database connection
00032   var $id;                      // id (fiche_def.fd_id
00033   var $label;                   // fiche_def.fd_label
00034   var $class_base;              // fiche_def.fd_class_base
00035   var $fiche_def;               // fiche_def.frd_id = fiche_def_ref.frd_id
00036   var $create_account;          // fd_create_account: flag
00037   var $all;
00038   var $attribut;                // get from attr_xxx tables
00039   function fiche_def($p_cn,$p_id = 0) {
00040     $this->cn=$p_cn;
00041     $this->id=$p_id;
00042   }
00043 
00051   function GetAttribut() {
00052     $sql="select * from jnt_fic_attr ".
00053       " natural join attr_def where fd_id=".$this->id.
00054       " order by ad_id";
00055 
00056     $Ret=ExecSql($this->cn,$sql);
00057 
00058     if ( ($Max=pg_NumRows($Ret)) == 0 )
00059       return ;
00060     for ($i=0;$i < $Max;$i++) {
00061       $row=pg_fetch_array($Ret,$i);
00062       $t = new Attribut($row['ad_id']);
00063       $t->ad_text=$row['ad_text'];
00064       $this->attribut[$i]=$t;
00065     }
00066     return $this->attribut;
00067   }
00068 
00074   function Get() {
00075     if ( $this->id == 0 ) 
00076       return 0;
00077     $sql="select * from fiche_def ".
00078       " where fd_id=".$this->id;
00079     $Ret=ExecSql($this->cn,$sql);
00080     if ( ($Max=pg_NumRows($Ret)) == 0 )
00081       return ;
00082     $row=pg_fetch_array($Ret,0);
00083     $this->label=$row['fd_label'];
00084     $this->class_base=$row['fd_class_base'];
00085     $this->fiche_def=$row['frd_id'];
00086     $this->create_account=$row['fd_create_account'];
00087   }
00094  function GetAll() {
00095    $sql="select * from fiche_def ";
00096 
00097     $Ret=ExecSql($this->cn,$sql);
00098     if ( ($Max=pg_NumRows($Ret)) == 0 )
00099       return ;
00100 
00101     for ( $i = 0; $i < $Max;$i++) {
00102       $row=pg_fetch_array($Ret,$i);
00103       $this->all[$i]=new fiche_def($this->cn,$row['fd_id']);
00104       $this->all[$i]->label=$row['fd_label'];
00105       $this->all[$i]->class_base=$row['fd_class_base'];
00106       $this->all[$i]->fiche_def=$row['frd_id'];
00107       $this->all[$i]->create_account=$row['fd_create_account'];
00108     }
00109   }
00119  function HasAttribute($p_attr) {
00120    return (CountSql($this->cn,"select * from vw_fiche_def where ad_id=$p_attr and fd_id=".$this->id)>0)?true:false;
00121 
00122  }
00129  function Display() 
00130    {
00131 
00132      $r=sprintf("<TD>%s</TD>",$this->id);
00133      $r.=sprintf("<TD>%s</TD>",$this->label);
00134      $r.=sprintf("<TD>%s</TD>",$this->class_base);
00135      $r.=sprintf("<TD>%s</TD>",$this->fiche_def);
00136      return $r;
00137    }
00138 
00139 }
00145 //-----------------------------------------------------
00146 // class fiche
00147 //-----------------------------------------------------
00148 class fiche {
00149   var $cn;           
00150   var $id;           
00151   var $fiche_def;    
00152   var $attribut;     
00153   var $fiche_def_ref; 
00155   function fiche($p_cn,$p_id=0) {
00156     $this->cn=$p_cn;
00157     $this->id=$p_id;
00158   }
00167   function GetByQCode($p_qcode,$p_all=true)
00168     {
00169 
00170       $p_qcode=FormatString($p_qcode);
00171       $sql="select f_id from jnt_fic_att_value join attr_value 
00172            using (jft_id)  where ad_id=23 and av_text='".$p_qcode."'";
00173       $Res=ExecSql($this->cn,$sql);
00174       $r=pg_fetch_all($Res);
00175       echo_debug('fiche',__LINE__,'result:'.var_export($r,true).'size '.sizeof($r));
00176       if ( $r == null  ) 
00177         return 1;
00178       $this->id=$r[0]['f_id'];
00179       echo_debug('class_fiche',__LINE__,'f_id = '.$this->id);
00180 
00181       if ( $p_all )
00182         $this->getAttribut();
00183       return 0;
00184     }
00190   function getAttribut() {
00191     if ( $this->id == 0){
00192       return;
00193     }
00194      $sql="select * 
00195            from jnt_fic_att_value 
00196                natural join fiche 
00197                natural join attr_value
00198                left join attr_def using (ad_id) where f_id=".$this->id.
00199        " order by ad_id";
00200 
00201     $Ret=ExecSql($this->cn,$sql);
00202     if ( ($Max=pg_NumRows($Ret)) == 0 )
00203       return ;
00204     for ($i=0;$i<$Max;$i++) {
00205       $row=pg_fetch_array($Ret,$i);
00206       $this->fiche_def=$row['fd_id'];
00207       $t=new Attribut ($row['ad_id']);
00208       $t->ad_text=$row['ad_text'];
00209       $t->av_text=$row['av_text'];
00210       $this->attribut[$i]=$t;
00211     }
00212     $e=new Fiche_def($this->cn,$this->fiche_def);
00213     $e->GetAttribut();
00214 
00215     if ( sizeof($this->attribut) != sizeof($e->attribut ) ) {
00216 
00217       // !!! Missing attribute
00218       foreach ($e->attribut as $f ) {
00219         $flag=0;
00220         foreach ($this->attribut as $g ) {
00221           if ( $g->ad_id == $f->ad_id ) 
00222             $flag=1;
00223         }
00224         if ( $flag == 0 ) { 
00225           // there's a missing one, we insert it
00226           $t=new Attribut ($f->ad_id);
00227           $t->av_text="";
00228           $t->ad_text=$f->ad_text;
00229           $this->attribut[$Max]=$t;
00230           $Max++;
00231         } // if flag == 0
00232 
00233       }// foreach 
00234       $this->attribut=SortAttributeById($this->attribut);
00235 
00236 
00237     }//missing attribut
00238   }
00239 
00240 
00246   function size() {
00247     if ( isset ($this->ad_id))
00248       return sizeof($this->ad_id);
00249     else
00250       return 0;
00251   }
00252 
00253 
00260   function GetByType($p_fd_id) {
00261      $sql="select * 
00262            from
00263                fiche 
00264             where fd_id=".$p_fd_id;
00265 
00266 
00267     $Ret=ExecSql($this->cn,$sql);
00268     if ( ($Max=pg_NumRows($Ret)) == 0 )
00269       return ;
00270     $all[0]=new fiche($this->cn);
00271 
00272     for ($i=0;$i<$Max;$i++) {
00273       $row=pg_fetch_array($Ret,$i);
00274       $t=new fiche($this->cn,$row['f_id']);
00275       $t->getAttribut();
00276       $all[$i]=$t;
00277 
00278     }
00279     return $all;
00280   }
00291   function CountByDef($p_frd_id,$p_search="",$p_sql="") {
00292      $sql="select * 
00293            from
00294                fiche join fiche_Def using (fd_id)
00295             where frd_id=".$p_frd_id;
00296      if ( $p_search != "" ) 
00297        {
00298          $a=FormatString($p_search);
00299          $sql="select * from vw_fiche_attr where frd_id=".$p_frd_id.
00300            " and vw_name ~* '$p_search'";
00301        }
00302 
00303     $Ret=ExecSql($this->cn,$sql.$p_sql);
00304     
00305     return pg_NumRows($Ret) ;
00306   }
00317   function GetByDef($p_frd_id,$p_offset=-1,$p_search="") {
00318     if ( $p_offset == -1 ) 
00319       {
00320         $sql="select * 
00321            from
00322                fiche join fiche_Def using (fd_id)
00323             where frd_id=".$p_frd_id." $p_search order by f_id";
00324       } 
00325     else 
00326       {
00327         $limit=$_SESSION['g_pagesize'];
00328         $sql="select * 
00329            from
00330                fiche join fiche_Def using (fd_id)
00331             where frd_id=".$p_frd_id." $p_search order by f_id
00332            limit ".$limit." offset ".$p_offset;
00333 
00334       }
00335 
00336     $Ret=ExecSql($this->cn,$sql);
00337     if ( ($Max=pg_NumRows($Ret)) == 0 )
00338       return ;
00339     $all[0]=new fiche($this->cn);
00340 
00341     for ($i=0;$i<$Max;$i++) {
00342       $row=pg_fetch_array($Ret,$i);
00343       $t=new fiche($this->cn,$row['f_id']);
00344       $t->getAttribut();
00345       $all[$i]=$t;
00346 
00347     }
00348     return $all;
00349   }
00350   function ShowTable() {
00351     echo "<TR><TD> ".
00352       $this->id."</TD>".
00353       "<TR> <TD>".
00354       $this->attribut_value."</TD>".
00355       "<TR> <TD>".
00356       $this->attribut_def."</TD></TR>";
00357   }
00366   function strAttribut($p_ad_id) 
00367     {
00368       if ( sizeof ($this->attribut) == 0 ) 
00369         {
00370           if ($this->id==0) return '- ERROR -';
00371           // object is not in memory we need to look into the database
00372           $sql="select av_text from attr_value join jnt_fic_att_value using(jft_id)
00373                 where f_id=".FormatString($this->id)." and ad_id=".$p_ad_id;
00374           $Res=ExecSql($this->cn,$sql);
00375           $row=pg_fetch_all($Res);
00376           // if not found return error
00377           if ( $row == false ) 
00378             return ' - ERROR -';
00379           
00380           return $row[0]['av_text'];
00381         }
00382 
00383       foreach ($this->attribut as $e)
00384         {
00385           if ( $e->ad_id == $p_ad_id ) 
00386             return $e->av_text;
00387         }
00388         return '- ERROR -';
00389     }
00399   function blank($p_fiche_def) 
00400     {
00401       // array = array of attribute object sorted on ad_id
00402       $f=new fiche_def($this->cn,$p_fiche_def);
00403       $array=$f->getAttribut();
00404       $r='<table>';
00405       foreach ($array as $attr)
00406         {
00407           $msg="";
00408           if ( $attr->ad_id == ATTR_DEF_ACCOUNT) 
00409             {
00410               $r.=JS_SEARCH_POSTE;
00411               $w=new widget("js_search_poste");
00412               //  account created automatically
00413               $sql="select account_auto($p_fiche_def)";
00414               echo_debug("class_fiche",__LINE__,$sql);
00415               $ret_sql=ExecSql($this->cn,$sql);
00416               $a=pg_fetch_array($ret_sql,0);
00417               if ( $a['account_auto'] == 't' )
00418                 $msg="<TD> <font color=\"red\">Rappel: Poste créé automatiquement !</font></TD> ";
00419 
00420              }
00421           elseif ( $attr->ad_id == ATTR_DEF_TVA) 
00422             {
00423               $r.=JS_TVA;
00424               $w=new widget("js_tva");
00425             }
00426           elseif ( $attr->ad_id == ATTR_DEF_COMPANY )
00427             {
00428               $r.=JS_SEARCH_CARD;
00429               $w=new widget("js_search");
00430               // filter on frd_id
00431               $w->extra=FICHE_TYPE_CLIENT.','.FICHE_TYPE_FOURNISSEUR.','.FICHE_TYPE_ADM_TAX; 
00432               $w->extra2=0;      // jrn = 0
00433               $label=new widget("span");
00434               $label->name="av_text".$attr->ad_id."_label";
00435               $msg=$label->IOValue();
00436             }
00437           else
00438             {
00439               $w=new widget("text");
00440             }
00441           $w->table=1;
00442           $w->label=$attr->ad_text;
00443           $w->name="av_text".$attr->ad_id;
00444 
00445           $r.="<TR>".$w->IOValue()."$msg </TR>";
00446         }
00447       $r.= '</table>';
00448       return $r;
00449     }
00450 
00451   
00461   function Display($p_readonly) 
00462     {
00463       $this->GetAttribut();
00464       $attr=$this->attribut;
00465 
00466       $ret="<table>";
00467 
00468       foreach ( $attr as $r) 
00469         {
00470           $msg="";
00471           if ( $p_readonly) 
00472             {
00473               $w=new widget("text");
00474             }
00475           if ($p_readonly==false)
00476             {
00477               if ( $r->ad_id == ATTR_DEF_ACCOUNT) 
00478                 {
00479                   $ret.=JS_SEARCH_POSTE;
00480                   $w=new widget("js_search_poste");
00481                   //  account created automatically
00482                   $sql="select account_auto($this->fiche_def)";
00483                   echo_debug("class_fiche",__LINE__,$sql);
00484                   $ret_sql=ExecSql($this->cn,$sql);
00485                   $a=pg_fetch_array($ret_sql,0);
00486                   if ( $a['account_auto'] == 't' )
00487                     $msg="<TD> <font color=\"red\">si vide le Poste sera créer automatiquement</font></TD> ";
00488                 }
00489               elseif ( $r->ad_id == ATTR_DEF_TVA) 
00490                 {
00491                   $ret.=JS_TVA;
00492                   $w=new widget("js_tva");
00493             }
00494               elseif ( $r->ad_id == ATTR_DEF_COMPANY )
00495                 {
00496                   $ret.=JS_SEARCH_CARD;
00497                   $w=new widget("js_search");
00498                   // filter on frd_id
00499                   $w->extra=FICHE_TYPE_CLIENT.','.FICHE_TYPE_FOURNISSEUR.','.FICHE_TYPE_ADM_TAX; 
00500                   $w->extra2=0;      // jrn = 0
00501                   $label=new widget("span");
00502                   $label->name="av_text".$r->ad_id."_label";
00503                   $msg=$label->IOValue();
00504                 }
00505             
00506               else 
00507                 {
00508                   $w=new widget("text");
00509                 }
00510             }
00511           $w->label=$r->ad_text;
00512           $w->value=$r->av_text;
00513           $w->name="av_text".$r->ad_id;
00514           $w->readonly=$p_readonly;
00515           $w->table=1;
00516 
00517           $ret.="<TR>".$w->IOValue()."$msg </TR>";
00518         }
00519       $ret.="</table>";
00520       return $ret;
00521     }
00528   function Save($p_fiche_def=0) 
00529     {
00530       // new card or only a update ?
00531       if ( $this->id == 0 ) 
00532         $this->insert($p_fiche_def);
00533       else
00534         $this->update();
00535     }
00542   function insert($p_fiche_def) 
00543     {
00544       $fiche_id=NextSequence($this->cn,'s_fiche');
00545       $this->id=$fiche_id;
00546       // first we create the card
00547       StartSql($this->cn);
00548       $sql=sprintf("insert into fiche(f_id,fd_id)". 
00549                    " values (%d,%d)",
00550                    $fiche_id,$p_fiche_def);
00551       $Ret=ExecSql($this->cn,$sql);
00552       // parse the $_POST array
00553       foreach ($_POST as $name=>$value ) 
00554         {
00555           echo_debug ("class_fiche",__LINE__,"Name = $name value $value") ;
00556           list ($id) = sscanf ($name,"av_text%d");
00557           if ( $id == null ) continue;
00558           echo_debug("class_fiche",__LINE__,"add $id");
00559           
00560           // Special traitement
00561           // quickcode
00562           if ( $id == ATTR_DEF_QUICKCODE) 
00563             {
00564               echo_debug("Modify ATTR_DEF_QUICKCODE");
00565               $sql=sprintf("select insert_quick_code(%d,'%s')",
00566                            $fiche_id,FormatString($value));
00567               ExecSql($this->cn,$sql);
00568               continue;
00569             }
00570           // name
00571           if ( $id == ATTR_DEF_NAME ) 
00572             {
00573               echo_debug("Modify ATTR_DEF_NAME");
00574               if ( strlen(trim($value)) == 0 )
00575                 $value="pas de nom";
00576               
00577             }
00578           // account
00579           if ( $id == ATTR_DEF_ACCOUNT ) 
00580             {
00581               echo_debug("Modify ATTR_DEF_ACCOUNT");
00582               $v=FormatString($value);
00583               if ( isNumber($v) == 1 )
00584                 {
00585                   $sql=sprintf("select account_insert(%d,%d)",
00586                                $this->id,$v);
00587                   $Ret=ExecSql($this->cn,$sql);
00588                 }
00589               if ( strlen(trim($v))==0  )
00590                 {
00591                   $sql=sprintf("select account_insert(%d,null)",
00592                                $this->id);
00593                   $Ret=ExecSql($this->cn,$sql);
00594                 }
00595               
00596               continue;
00597             }
00598         // TVA
00599           if ( $id == ATTR_DEF_TVA ) 
00600             {
00601               echo_debug("Modify ATTR_DEF_TVA");
00602               // Verify if the rate exists, if not then do not update
00603               if ( strlen(trim($value)) != 0 ) 
00604                 {
00605                   if ( CountSql($this->cn,"select * from tva_rate where tva_id=".$value) == 0) 
00606                     {
00607                       echo_debug("class_fiche",__LINE__,"Tva invalide $value");
00608                       continue;
00609                     }
00610                 }
00611             }
00612           // The contact has a company attribut
00613           if ( $id == ATTR_DEF_COMPANY ) 
00614             {
00615               $exist=CountSql($this->cn,"select f_id from fiche join fiche_def using (fd_id) ".
00616                               " join jnt_fic_att_value using (f_id) join attr_value using (jft_id) ".
00617                               " where frd_id in (8,9,14) and ad_id=".ATTR_DEF_QUICKCODE.
00618                               " and av_text='".FormatString($value)."'");
00619               if ( $exist == 0 ) 
00620                 {
00621                   $value="Attention : pas de société  ";
00622                 }
00623             }
00624           // Normal traitement
00625           $value2=FormatString($value);
00626 
00627           $sql=sprintf("select attribut_insert(%d,%d,'%s')",
00628                        $fiche_id,$id,$value2);
00629           ExecSql($this->cn,$sql);
00630         }
00631       Commit($this->cn);
00632       return;
00633       
00634 
00635     }
00636 
00637    
00638 
00642  function update() 
00643      {
00644        // parse the $_POST array
00645        foreach ($_POST as $name=>$value ) 
00646          {
00647            echo_debug ("class_fiche",__LINE__,"Name = $name value $value") ;
00648            list ($id) = sscanf ($name,"av_text%d");
00649            if ( $id == null ) continue;
00650            echo_debug("class_fiche",__LINE__,"modify $id");
00651            
00652            // retrieve jft_id to update table attr_value
00653            $sql=" select jft_id from jnt_fic_att_value where ad_id=$id and f_id=$this->id";
00654            $Ret=ExecSql($this->cn,$sql);
00655            if ( pg_NumRows($Ret) != 1 ) {
00656              echo_error ("class_fiche ".__LINE__." INVALID ID !!! ");
00657              return;
00658            }
00659            $tmp=pg_fetch_array($Ret,0);
00660            $jft_id=$tmp['jft_id'];
00661            
00662            // Special traitement
00663            // quickcode
00664            if ( $id == ATTR_DEF_QUICKCODE) 
00665              {
00666                echo_debug("Modify ATTR_DEF_QUICKCODE");
00667                $sql=sprintf("select update_quick_code(%d,'%s')",
00668                             $jft_id,FormatString($value));
00669                ExecSql($this->cn,$sql);
00670                continue;
00671              }
00672            // name
00673            if ( $id == ATTR_DEF_NAME ) 
00674              {
00675                echo_debug("Modify ATTR_DEF_NAME");
00676                if ( strlen(trim($value)) == 0 )
00677                  continue;
00678                
00679              }
00680            // account
00681            if ( $id == ATTR_DEF_ACCOUNT ) 
00682              {
00683                echo_debug("Modify ATTR_DEF_ACCOUNT");
00684                $v=FormatString($value);
00685                if ( isNumber($v) == 1 )
00686                  {
00687                    $sql=sprintf("select account_update(%d,%d)",
00688                                 $this->id,$v);
00689                    $Ret=ExecSql($this->cn,$sql);
00690                  }
00691                if ( strlen (trim($v)) == 0 ) 
00692                  {
00693                    $sql=sprintf("select account_update(%d,null)",
00694                                 $this->id);
00695                    $Ret=ExecSql($this->cn,$sql);
00696                    continue;
00697                  }
00698              }
00699          // TVA
00700            if ( $id == ATTR_DEF_TVA ) 
00701              {
00702                echo_debug("Modify ATTR_DEF_TVA");
00703                // Verify if the rate exists, if not then do not update
00704                if ( strlen(trim($value)) != 0 ) 
00705                  {
00706                    if ( CountSql($this->cn,"select * from tva_rate where tva_id=".$value) == 0) 
00707                      {
00708                        echo_debug("class_fiche",__LINE__,"Tva invalide $value");
00709                        continue;
00710                      }
00711                  }
00712              }
00713            if ( $id == ATTR_DEF_COMPANY ) 
00714              {
00715                $exist=CountSql($this->cn,"select f_id from fiche join fiche_def using (fd_id) ".
00716                                " join jnt_fic_att_value using (f_id) join attr_value using (jft_id) ".
00717                                " where frd_id in (8,9,14) and ad_id=".ATTR_DEF_QUICKCODE.
00718                                " and av_text='".FormatString($value)."'");
00719  
00720  
00721                if ( $exist == 0 ) 
00722                  {
00723                    $value="Attention : pas de société ";
00724                  }
00725              }
00726            
00727            // Normal traitement
00728            $value2=FormatString($value);
00729            $sql=sprintf("update attr_value set av_text='%s' where jft_id=%d",
00730                         $value2,$jft_id);
00731            ExecSql($this->cn,$sql);
00732          }
00733        return;
00734        
00735      }
00738    function remove() 
00739      {
00740        if ( $this->id==0 ) return;
00741        // verify if that card has not been used is a ledger
00742        $sql="select fiche_used(".$this->id.") as result";
00743        $Res=ExecSql($this->cn,$sql);
00744        
00745        // fetch the result
00746        list($result)=pg_fetch_row($Res,0);
00747  
00748        if ( $result == 0 ) 
00749          {
00750            $sql="select fiche_delete (".$this->id.")";
00751            $Res=ExecSql($this->cn,$sql);
00752            return 0;
00753          }
00754        return 1;
00755      }
00758    function getName() 
00759      {
00760        $sql="select av_text from jnt_fic_att_value join attr_value 
00761             using (jft_id)  where ad_id=1 and f_id=".$this->id;
00762        $Res=ExecSql($this->cn,$sql);
00763        $r=pg_fetch_all($Res);
00764        if ( sizeof($r) == 0 ) 
00765          return 1;
00766        return $r[0]['av_text'];
00767      }
00770    function Get() 
00771      {
00772        echo_debug('class_client',__LINE__,'Get');
00773        fiche::getAttribut();
00774      }
00780    function GetAll($p_offset=-1,$p_search="") 
00781      {
00782        return fiche::GetByDef($this->fiche_def_ref,$p_offset,$p_search);
00783     }
00784 }
00785 ?>