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 ( $r->ad_id == ATTR_DEF_ACCOUNT) 
00470             {
00471               $ret.=JS_SEARCH_POSTE;
00472               $w=new widget("js_search_poste");
00473               //  account created automatically
00474               $sql="select account_auto($this->fiche_def)";
00475               echo_debug("class_fiche",__LINE__,$sql);
00476               $ret_sql=ExecSql($this->cn,$sql);
00477               $a=pg_fetch_array($ret_sql,0);
00478 //            if ( $a['account_auto'] == 't' )
00479 //              $msg="<TD> <font color=\"red\">si vide le Poste sera créer automatiquement</font></TD> ";
00480              }
00481           elseif ( $r->ad_id == ATTR_DEF_TVA) 
00482             {
00483               $ret.=JS_TVA;
00484               $w=new widget("js_tva");
00485             }
00486           elseif ( $r->ad_id == ATTR_DEF_COMPANY )
00487             {
00488               $ret.=JS_SEARCH_CARD;
00489               $w=new widget("js_search");
00490               // filter on frd_id
00491               $w->extra=FICHE_TYPE_CLIENT.','.FICHE_TYPE_FOURNISSEUR.','.FICHE_TYPE_ADM_TAX; 
00492               $w->extra2=0;      // jrn = 0
00493               $label=new widget("span");
00494               $label->name="av_text".$r->ad_id."_label";
00495               $msg=$label->IOValue();
00496             }
00497           else 
00498             {
00499               $w=new widget("text");
00500             }
00501 
00502           $w->label=$r->ad_text;
00503           $w->value=$r->av_text;
00504           $w->name="av_text".$r->ad_id;
00505           $w->readonly=$p_readonly;
00506           $w->table=1;
00507 
00508           $ret.="<TR>".$w->IOValue()."$msg </TR>";
00509         }
00510       $ret.="</table>";
00511       return $ret;
00512     }
00519   function Save($p_fiche_def=0) 
00520     {
00521       // new card or only a update ?
00522       if ( $this->id == 0 ) 
00523         $this->insert($p_fiche_def);
00524       else
00525         $this->update();
00526     }
00533   function insert($p_fiche_def) 
00534     {
00535       $fiche_id=NextSequence($this->cn,'s_fiche');
00536       $this->id=$fiche_id;
00537       // first we create the card
00538       StartSql($this->cn);
00539       $sql=sprintf("insert into fiche(f_id,fd_id)". 
00540                    " values (%d,%d)",
00541                    $fiche_id,$p_fiche_def);
00542       $Ret=ExecSql($this->cn,$sql);
00543       // parse the $_POST array
00544       foreach ($_POST as $name=>$value ) 
00545         {
00546           echo_debug ("class_fiche",__LINE__,"Name = $name value $value") ;
00547           list ($id) = sscanf ($name,"av_text%d");
00548           if ( $id == null ) continue;
00549           echo_debug("class_fiche",__LINE__,"add $id");
00550           
00551           // Special traitement
00552           // quickcode
00553           if ( $id == ATTR_DEF_QUICKCODE) 
00554             {
00555               echo_debug("Modify ATTR_DEF_QUICKCODE");
00556               $sql=sprintf("select insert_quick_code(%d,'%s')",
00557                            $fiche_id,FormatString($value));
00558               ExecSql($this->cn,$sql);
00559               continue;
00560             }
00561           // name
00562           if ( $id == ATTR_DEF_NAME ) 
00563             {
00564               echo_debug("Modify ATTR_DEF_NAME");
00565               if ( strlen(trim($value)) == 0 )
00566                 $value="pas de nom";
00567               
00568             }
00569           // account
00570           if ( $id == ATTR_DEF_ACCOUNT ) 
00571             {
00572               echo_debug("Modify ATTR_DEF_ACCOUNT");
00573               $v=FormatString($value);
00574               if ( isNumber($v) == 1 )
00575                 {
00576                   $sql=sprintf("select account_insert(%d,%d)",
00577                                $this->id,$v);
00578                   $Ret=ExecSql($this->cn,$sql);
00579                 }
00580               if ( strlen(trim($v))==0  )
00581                 {
00582                   $sql=sprintf("select account_insert(%d,null)",
00583                                $this->id);
00584                   $Ret=ExecSql($this->cn,$sql);
00585                 }
00586               
00587               continue;
00588             }
00589         // TVA
00590           if ( $id == ATTR_DEF_TVA ) 
00591             {
00592               echo_debug("Modify ATTR_DEF_TVA");
00593               // Verify if the rate exists, if not then do not update
00594               if ( strlen(trim($value)) != 0 ) 
00595                 {
00596                   if ( CountSql($this->cn,"select * from tva_rate where tva_id=".$value) == 0) 
00597                     {
00598                       echo_debug("class_fiche",__LINE__,"Tva invalide $value");
00599                       continue;
00600                     }
00601                 }
00602             }
00603           // The contact has a company attribut
00604           if ( $id == ATTR_DEF_COMPANY ) 
00605             {
00606               $exist=CountSql($this->cn,"select f_id from fiche join fiche_def using (fd_id) ".
00607                               " join jnt_fic_att_value using (f_id) join attr_value using (jft_id) ".
00608                               " where frd_id in (8,9,14) and ad_id=".ATTR_DEF_QUICKCODE.
00609                               " and av_text='".FormatString($value)."'");
00610               if ( $exist == 0 ) 
00611                 {
00612                   $value="Attention : pas de société  ";
00613                 }
00614             }
00615           // Normal traitement
00616           $value2=FormatString($value);
00617 
00618           $sql=sprintf("select attribut_insert(%d,%d,'%s')",
00619                        $fiche_id,$id,$value2);
00620           ExecSql($this->cn,$sql);
00621         }
00622       Commit($this->cn);
00623       return;
00624       
00625 
00626     }
00627 
00628    
00629 
00633  function update() 
00634      {
00635        // parse the $_POST array
00636        foreach ($_POST as $name=>$value ) 
00637          {
00638            echo_debug ("class_fiche",__LINE__,"Name = $name value $value") ;
00639            list ($id) = sscanf ($name,"av_text%d");
00640            if ( $id == null ) continue;
00641            echo_debug("class_fiche",__LINE__,"modify $id");
00642            
00643            // retrieve jft_id to update table attr_value
00644            $sql=" select jft_id from jnt_fic_att_value where ad_id=$id and f_id=$this->id";
00645            $Ret=ExecSql($this->cn,$sql);
00646            if ( pg_NumRows($Ret) != 1 ) {
00647              echo_error ("class_fiche ".__LINE__." INVALID ID !!! ");
00648              return;
00649            }
00650            $tmp=pg_fetch_array($Ret,0);
00651            $jft_id=$tmp['jft_id'];
00652            
00653            // Special traitement
00654            // quickcode
00655            if ( $id == ATTR_DEF_QUICKCODE) 
00656              {
00657                echo_debug("Modify ATTR_DEF_QUICKCODE");
00658                $sql=sprintf("select update_quick_code(%d,'%s')",
00659                             $jft_id,FormatString($value));
00660                ExecSql($this->cn,$sql);
00661                continue;
00662              }
00663            // name
00664            if ( $id == ATTR_DEF_NAME ) 
00665              {
00666                echo_debug("Modify ATTR_DEF_NAME");
00667                if ( strlen(trim($value)) == 0 )
00668                  continue;
00669                
00670              }
00671            // account
00672            if ( $id == ATTR_DEF_ACCOUNT ) 
00673              {
00674                echo_debug("Modify ATTR_DEF_ACCOUNT");
00675                $v=FormatString($value);
00676                if ( isNumber($v) == 1 )
00677                  {
00678                    $sql=sprintf("select account_update(%d,%d)",
00679                                 $this->id,$v);
00680                    $Ret=ExecSql($this->cn,$sql);
00681                  }
00682                if ( strlen (trim($v)) == 0 ) 
00683                  {
00684                    $sql=sprintf("select account_update(%d,null)",
00685                                 $this->id);
00686                    $Ret=ExecSql($this->cn,$sql);
00687                    continue;
00688                  }
00689              }
00690          // TVA
00691            if ( $id == ATTR_DEF_TVA ) 
00692              {
00693                echo_debug("Modify ATTR_DEF_TVA");
00694                // Verify if the rate exists, if not then do not update
00695                if ( strlen(trim($value)) != 0 ) 
00696                  {
00697                    if ( CountSql($this->cn,"select * from tva_rate where tva_id=".$value) == 0) 
00698                      {
00699                        echo_debug("class_fiche",__LINE__,"Tva invalide $value");
00700                        continue;
00701                      }
00702                  }
00703              }
00704            if ( $id == ATTR_DEF_COMPANY ) 
00705              {
00706                $exist=CountSql($this->cn,"select f_id from fiche join fiche_def using (fd_id) ".
00707                                " join jnt_fic_att_value using (f_id) join attr_value using (jft_id) ".
00708                                " where frd_id in (8,9,14) and ad_id=".ATTR_DEF_QUICKCODE.
00709                                " and av_text='".FormatString($value)."'");
00710  
00711  
00712                if ( $exist == 0 ) 
00713                  {
00714                    $value="Attention : pas de société ";
00715                  }
00716              }
00717            
00718            // Normal traitement
00719            $value2=FormatString($value);
00720            $sql=sprintf("update attr_value set av_text='%s' where jft_id=%d",
00721                         $value2,$jft_id);
00722            ExecSql($this->cn,$sql);
00723          }
00724        return;
00725        
00726      }
00729    function remove() 
00730      {
00731        if ( $this->id==0 ) return;
00732        // verify if that card has not been used is a ledger
00733        $sql="select fiche_used(".$this->id.") as result";
00734        $Res=ExecSql($this->cn,$sql);
00735        
00736        // fetch the result
00737        list($result)=pg_fetch_row($Res,0);
00738  
00739        if ( $result == 0 ) 
00740          {
00741            $sql="select fiche_delete (".$this->id.")";
00742            $Res=ExecSql($this->cn,$sql);
00743            return 0;
00744          }
00745        return 1;
00746      }
00749    function getName() 
00750      {
00751        $sql="select av_text from jnt_fic_att_value join attr_value 
00752             using (jft_id)  where ad_id=1 and f_id=".$this->id;
00753        $Res=ExecSql($this->cn,$sql);
00754        $r=pg_fetch_all($Res);
00755        if ( sizeof($r) == 0 ) 
00756          return 1;
00757        return $r[0]['av_text'];
00758      }
00761    function Get() 
00762      {
00763        echo_debug('class_client',__LINE__,'Get');
00764        fiche::getAttribut();
00765      }
00771    function GetAll($p_offset=-1,$p_search="") 
00772      {
00773        return fiche::GetByDef($this->fiche_def_ref,$p_offset,$p_search);
00774     }
00775 }
00776 ?>

Generated on Fri Jun 23 00:12:45 2006 for phpcompta by  doxygen 1.4.4