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

Generated on Sun Jun 25 23:05:35 2006 for phpcompta by  doxygen 1.4.4