noalyss  Version-6.7.2
 All Data Structures Namespaces Files Functions Variables Enumerations
class_acc_account_ledger.php
Go to the documentation of this file.
00001 <?php
00002 /*
00003  *   This file is part of NOALYSS.
00004  *
00005  *   NOALYSS 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  *   NOALYSS 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 NOALYSS; if not, write to the Free Software
00017  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 // Copyright Author Dany De Bontridder danydb@aevalys.eu
00020 /*! \file
00021  * \brief Manage the account
00022  */
00023 /*!
00024  * \brief Manage the account from the table jrn, jrnx or tmp_pcmn
00025  */
00026 require_once("class_ihidden.php");
00027 require_once ('class_database.php');
00028 require_once ('class_dossier.php');
00029 
00030 class Acc_Account_Ledger
00031 {
00032     var $db;          /*!< $db database connection */
00033     var $id;          /*!< $id poste_id (pcm_val)*/
00034     var $label;       /*!< $label label of the poste */
00035     var $parent;      /*!< $parent parent account */
00036     var $row;         /*!< $row double array see get_row */
00037     var $tot_deb;    /*!< value set by  get_row */
00038     var $tot_cred;    /*!< value by  get_row */
00039     function __construct ($p_cn,$p_id)
00040     {
00041         $this->db=$p_cn;
00042         $this->id=$p_id;
00043     }
00044     /**
00045      *@brief get the row thanks the resource
00046      *@return double array (j_date,deb_montant,cred_montant,description,jrn_name,j_debit,jr_internal)
00047      *         (tot_deb,tot_credit)
00048      */
00049     private function get_row_sql($Res)
00050     {
00051         $array=array();
00052         $tot_cred=0.0;
00053         $tot_deb=0.0;
00054         $Max=Database::num_row($Res);
00055         if ( $Max == 0 ) return null;
00056         for ($i=0;$i<$Max;$i++)
00057         {
00058             $array[]=Database::fetch_array($Res,$i);
00059             if ($array[$i]['j_debit']=='t')
00060             {
00061                 $tot_deb+=$array[$i]['deb_montant'] ;
00062             }
00063             else
00064             {
00065                 $tot_cred+=$array[$i]['cred_montant'] ;
00066             }
00067         }
00068         $this->row=$array;
00069         $this->tot_deb=$tot_deb;
00070         $this->tot_cred=$tot_cred;
00071         return array($array,$tot_deb,$tot_cred);
00072 
00073     }
00074     /*!
00075      * \brief  Get data for accounting entry between 2 periode
00076      *
00077      * \param  $p_from periode from
00078      * \param  $p_to   end periode
00079      * \return double array (j_date,deb_montant,cred_montant,description,jrn_name,j_debit,jr_internal)
00080      *         (tot_deb,tot_credit
00081      *
00082      */
00083     function get_row($p_from,$p_to)
00084     {
00085         $periode=sql_filter_per($this->db,$p_from,$p_to,'p_id','jr_tech_per');
00086 
00087         $Res=$this->db->exec_sql("select distinct j_id,jr_id,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,j_date,".
00088                                  "case when j_debit='t' then j_montant else 0 end as deb_montant,".
00089                                  "case when j_debit='f' then j_montant else 0 end as cred_montant,".
00090                                  " jr_comment as description,jrn_def_name as jrn_name,".
00091                                  "j_debit, jr_internal,jr_pj_number ".
00092                                  " from jrnx left join jrn_def on jrn_def_id=j_jrn_def ".
00093                                  " left join jrn on jr_grpt_id=j_grpt".
00094                                  " where j_poste=".$this->id." and ".$periode.
00095                                  " order by j_date");
00096         return $this->get_row_sql($Res);
00097     }
00098     /*!
00099      * \brief  Get data for accounting entry between 2 date
00100      *
00101      * \param  $p_from date from
00102      * \param  $p_to   end date
00103      *\param $let 0 means all rows, 1 only lettered, 2 only unlettered
00104          * \param $solded 0 means all account, 1 means only accounts with a saldo <> 0
00105      *\note the data are filtered by the access of the current user
00106      * \return double array (j_date,deb_montant,cred_montant,description,jrn_name,j_debit,jr_internal)
00107      *         (tot_deb,tot_credit
00108      *
00109      */
00110     function get_row_date($p_from,$p_to,$let=0,$solded=0)
00111     {
00112         global $g_user;
00113         $filter_sql=$g_user->get_ledger_sql('ALL',3);
00114         $sql_let='';
00115         switch ($let)
00116         {
00117         case 0:
00118                 break;
00119         case 1:
00120             $sql_let=' and j_id in (select j_id from letter_cred union select j_id from letter_deb)';
00121             break;
00122         case '2':
00123             $sql_let=' and j_id not in (select j_id from letter_cred union select j_id from letter_deb) ';
00124             break;
00125         }
00126         if ( $solded == 1)
00127           {
00128             $filter=str_replace('jrn_def_id','jr_def_id',$filter_sql);
00129             $bal_sql="select sum(amount_deb) as s_deb,sum(amount_cred) as s_cred, j_poste
00130                                 from                                            (select case when j_debit='t' then j_montant else 0 end as amount_deb,
00131                                                                 case when j_debit='f' then j_montant else 0 end as amount_cred,
00132                                                                 j_poste
00133                                                                 from jrnx join jrn on (j_grpt = jr_grpt_id)
00134                                                                 where
00135                                                                 j_poste=$1 and
00136                                                                 $filter and
00137                                                                 ( to_date($2,'DD.MM.YYYY') <= j_date and
00138                                   to_date($3,'DD.MM.YYYY') >= j_date  )) as signed_amount
00139                                                 group by j_poste
00140                                                 ";
00141             $r=$this->db->get_array($bal_sql,array($this->id,$p_from,$p_to));
00142             if ( $this->db->count() == 0 ) return array();
00143             if ($r[0]['s_deb']==$r[0]['s_cred']) return array();
00144           }
00145         $Res=$this->db->exec_sql("select  jr_id,to_char(j_date,'DD.MM.YYYY') as j_date_fmt,j_date,".
00146                                  "case when j_debit='t' then j_montant else 0 end as deb_montant,".
00147                                  "case when j_debit='f' then j_montant else 0 end as cred_montant,".
00148                                  " case when j_text is null or j_text = '' then jr_comment 
00149                                    else jr_comment||' '||j_text  end
00150                 as description,jrn_def_name as jrn_name,".
00151                                  "j_debit, jr_internal,jr_pj_number,
00152                                                                  coalesce(comptaproc.get_letter_jnt(j_id),-1) as letter ".
00153                                  ",pcm_lib ".
00154                                  ",jr_tech_per,p_exercice,jrn_def_name,jrn_def_code".
00155                                  " from jrnx left join jrn_def on (jrn_def_id=j_jrn_def )".
00156                                  " left join jrn on (jr_grpt_id=j_grpt)".
00157                                  " left join tmp_pcmn on (j_poste=pcm_val)".
00158                                  " left join parm_periode on (p_id=jr_tech_per) ".
00159                                  " where j_poste=$1 and ".
00160                                  " ( to_date($2,'DD.MM.YYYY') <= j_date and ".
00161                                  "   to_date($3,'DD.MM.YYYY') >= j_date )".
00162                                  " and $filter_sql  $sql_let ".
00163                                  " order by j_date,substring(jr_pj_number,'[0-9]+$') asc",array($this->id,$p_from,$p_to));
00164         return $this->get_row_sql($Res);
00165     }
00166 
00167 
00168     /*!\brief Return the name of a account
00169      *        it doesn't change any data member
00170      * \return string with the pcm_lib
00171      */
00172     function get_name()
00173     {
00174         $ret=$this->db->exec_sql(
00175                  "select pcm_lib from tmp_pcmn where
00176                  pcm_val=$1",array($this->id));
00177         if ( Database::num_row($ret) != 0)
00178         {
00179             $r=Database::fetch_array($ret);
00180             $this->name=$r['pcm_lib'];
00181         }
00182         else
00183         {
00184             $this->name="Poste inconnu";
00185         }
00186         return $this->name;
00187     }
00188     /*!\brief check if the poste exist in the tmp_pcmn
00189      *\return the number of line (normally 1 or 0)
00190      */
00191     function do_exist()
00192     {
00193         $sql="select pcm_val from tmp_pcmn where pcm_val= $1";
00194         $ret=$this->db->exec_sql($sql,array($this->id));
00195         return Database::num_row($ret) ;
00196     }
00197     /*!\brief Get all the value for this object from the database
00198      *        the data member are set
00199      * \return false if this account doesn't exist otherwise true
00200      */
00201     function load()
00202     {
00203         $ret=$this->db->exec_sql("select pcm_lib,pcm_val_parent from
00204                                  tmp_pcmn where pcm_val=$1",array($this->id));
00205         $r=Database::fetch_all($ret);
00206 
00207         if ( ! $r ) return false;
00208         $this->label=$r[0]['pcm_lib'];
00209         $this->parent=$r[0]['pcm_val_parent'];
00210         return true;
00211 
00212     }
00213     /*!\brief Get all the value for this object from the database
00214      *        the data member are set
00215      * \return false if this account doesn't exist otherwise true
00216      */
00217     function get()
00218     {
00219         echo "OBSOLETE Acc_Account_Ledger->get(), a remplacer par Acc_Account_Ledger->load()";
00220         return $this->load();
00221     }
00222 
00223     /*!
00224      * \brief  give the balance of an account
00225      *
00226      * \return
00227      *      balance of the account
00228      *
00229      */
00230     function get_solde($p_cond=" true ")
00231     {
00232         $Res=$this->db->exec_sql("select sum(deb) as sum_deb, sum(cred) as sum_cred from
00233                                  ( select j_poste,
00234                                  case when j_debit='t' then j_montant else 0 end as deb,
00235                                  case when j_debit='f' then j_montant else 0 end as cred
00236                                  from jrnx join tmp_pcmn on j_poste=pcm_val
00237                                  where
00238                                  j_poste::text like ('$this->id'::text) and
00239                                  $p_cond
00240                                  ) as m  ");
00241         $Max=Database::num_row($Res);
00242         if ($Max==0) return 0;
00243         $r=Database::fetch_array($Res,0);
00244 
00245         return abs($r['sum_deb']-$r['sum_cred']);
00246     }
00247     /*!
00248      * \brief   give the balance of an account
00249      * \return
00250      *      balance of the account
00251      *
00252      */
00253     function get_solde_detail($p_cond="")
00254     {
00255 
00256         if ( $p_cond != "") $p_cond=" and ".$p_cond;
00257         $sql="select sum(deb) as sum_deb, sum(cred) as sum_cred from
00258              ( select j_poste,
00259              case when j_debit='t' then j_montant else 0 end as deb,
00260              case when j_debit='f' then j_montant else 0 end as cred
00261              from jrnx
00262              where
00263              j_poste::text like ('$this->id'::text)
00264              $p_cond
00265              ) as m  ";
00266 
00267         $Res=$this->db->exec_sql($sql);
00268         $Max=Database::num_row($Res);
00269 
00270         if ($Max==0)
00271         {
00272             return array('debit'=>0,
00273                          'credit'=>0,
00274                          'solde'=>0)     ;
00275         }
00276         $r=Database::fetch_array($Res,0);
00277 // if p_start is < p_end the query returns null to avoid any problem
00278 // we set it to 0
00279         if ($r['sum_deb']=='')
00280             $r['sum_deb']=0.0;
00281         if ($r['sum_cred']=='')
00282             $r['sum_cred']=0.0;
00283 
00284         return array('debit'=>$r['sum_deb'],
00285                      'credit'=>$r['sum_cred'],
00286                      'solde'=>abs(bcsub($r['sum_deb'],$r['sum_cred'])));
00287     }
00288     /*!
00289      * \brief isTva tell is a poste is used for VAT
00290      * \param none
00291      *
00292      *
00293      * \return 1 is Yes otherwise 0
00294      */
00295     function isTVA()
00296     {
00297         // Load TVA array
00298         $a_TVA=$this->db->get_array('select tva_poste
00299                                     from tva_rate');
00300         foreach ( $a_TVA as $line_tva)
00301         {
00302             if ( $line_tva['tva_poste']  == '' )
00303                 continue;
00304             list($tva_deb,$tva_cred)=explode(',',$line_tva['tva_poste']);
00305             if ( $this->id == $tva_deb ||
00306                     $this->id == $tva_cred )
00307             {
00308                 return 1;
00309             }
00310         }
00311         return 0;
00312 
00313     }
00314     /*!
00315      * \brief HtmlTable, display a HTML of a poste for the asked period
00316      * \param $p_array array for filter
00317      * \param $let lettering of operation 0
00318      * \return -1 if nothing is found otherwise 0
00319      */
00320     function HtmlTable($p_array=null,$let=0 , $from_div=0)
00321     {
00322         if ( $p_array==null)$p_array=$_REQUEST;
00323         $this->get_name();
00324         list($array,$tot_deb,$tot_cred)=$this->get_row_date( $p_array['from_periode'],
00325                                                              $p_array['to_periode'],$let
00326                                                            );
00327 
00328         if ( count($this->row ) == 0 )
00329             return -1;
00330 
00331         $rep="";
00332 
00333         echo '<h2 class="title">'.$this->id." ".$this->name.'</h2>';
00334         if ( $from_div == 0)
00335                         echo "<TABLE class=\"resultfooter\" style=\"border-collapse:separate;margin:1%;width:98%;;border-spacing:0px 5px\">";
00336                 else
00337                         echo "<TABLE class=\"resultfooter\" style=\"border-collapse:separate;margin:1%;width:98%;;border-spacing:0px 2px\">";
00338         echo '<tbody>';
00339         echo "<TR>".
00340         "<TH style=\"text-align:left\"> Date</TH>".
00341         "<TH style=\"text-align:left\"> n° de pièce </TH>".
00342         "<TH style=\"text-align:left\"> Code interne </TH>".
00343         "<TH style=\"text-align:left\"> Description </TH>".
00344         "<TH style=\"text-align:right\"> D&eacute;bit  </TH>".
00345         "<TH style=\"text-align:right\"> Cr&eacute;dit </TH>".
00346         th('Prog.','style="text-align:right"').
00347         th('Let.','style="text-align:right"');
00348         "</TR>"
00349         ;
00350         $progress=0;$sum_deb=0;$sum_cred=0;
00351         bcscale(2);
00352         $old_exercice="";
00353         $idx=0;
00354         foreach ( $this->row as $op )
00355         {
00356             $vw_operation = sprintf('<A class="detail" style="text-decoration:underline;color:red" HREF="javascript:modifyOperation(\'%s\',\'%s\')" >%s</A>', $op['jr_id'], dossier::id(), $op['jr_internal']);
00357             $let = '';
00358                         $html_let = "";
00359                         if ($op['letter'] != -1)
00360                         {
00361                                 $let = strtoupper(base_convert($op['letter'], 10, 36));
00362                                 $html_let = HtmlInput::show_reconcile($from_div, $let);
00363                         }
00364                         $tmp_diff=bcsub($op['deb_montant'],$op['cred_montant']);
00365 
00366             /*
00367              * reset prog. balance to zero if we change of exercice
00368              */
00369             if ( $old_exercice != $op['p_exercice'])
00370               {
00371                 if ( $old_exercice != '')
00372                   {
00373                     $progress=bcsub($sum_deb,$sum_cred);
00374                         $side="&nbsp;".$this->get_amount_side($progress);
00375                     echo "<TR style=\"font-weight:bold\">".
00376                       "<TD>$old_exercice</TD>".
00377                       td('').
00378                       "<TD></TD>".
00379                       "<TD>Totaux</TD>".
00380                       "<TD style=\"text-align:right\">".nbm($sum_deb)."</TD>".
00381                       "<TD style=\"text-align:right\">".nbm($sum_cred)."</TD>".
00382                       td(nbm(abs($progress)).$side,'style="text-align:right"').
00383                       td('').
00384                       "</TR>";
00385                     $sum_cred=0;
00386                     $sum_deb=0;
00387                     $progress=0;
00388 
00389                   }
00390               }
00391             $progress=bcadd($progress,$tmp_diff);
00392                 $side="&nbsp;".$this->get_amount_side($progress);
00393             $sum_cred=bcadd($sum_cred,$op['cred_montant']);
00394             $sum_deb=bcadd($sum_deb,$op['deb_montant']);
00395                 if ($idx%2 == 0) $class='class="odd"'; else $class=' class="even"';
00396                 $idx++;
00397 
00398             echo "<TR $class name=\"tr_" . $let . "_" . $from_div . "\">" .
00399                         "<TD>".smaller_date(format_date($op['j_date']))."</TD>".
00400               td(h($op['jr_pj_number'])).
00401               "<TD>".$vw_operation."</TD>".
00402               "<TD>".h($op['description'])."</TD>".
00403               "<TD style=\"text-align:right\">".nbm($op['deb_montant'])."</TD>".
00404               "<TD style=\"text-align:right\">".nbm($op['cred_montant'])."</TD>".
00405               td(nbm(abs($progress)).$side,'style="text-align:right"').
00406 
00407               td($html_let, ' style="color:red;text-align:right"') .
00408                         "</TR>";
00409             $old_exercice=$op['p_exercice'];
00410         }
00411         echo '<tfoot>';
00412         $solde_type=($sum_deb>$sum_cred)?"solde débiteur":"solde créditeur";
00413         $diff=bcsub($sum_deb,$sum_cred);
00414                 $side="&nbsp;".$this->get_amount_side($diff);
00415         echo "<TR style=\"font-weight:bold\">".
00416         "<TD >Totaux</TD><td></td>".
00417         "<TD ></TD>".
00418         "<TD></TD>".
00419           "<TD  style=\"text-align:right\">".nbm($sum_deb)."</TD>".
00420           "<TD  style=\"text-align:right\">".nbm($sum_cred)."</TD>".
00421           "<TD style=\"text-align:right\">".nbm(abs($diff)).$side."</TD>".
00422 
00423         "</TR>";
00424         echo   "<tr><TD>$solde_type</TD><td></td>".
00425           "<TD style=\"text-align:right\">".nbm(abs($diff))."</TD>".
00426         "</TR>";
00427         echo '</tfoot>';
00428         echo '</tbody>';
00429 
00430         echo "</table>";
00431 
00432         return;
00433     }
00434         /**
00435          * return the letter C if amount is > 0, D if < 0 or =
00436          * @param type $p_amount
00437          * @return string
00438          */
00439         function get_amount_side($p_amount)
00440                 {
00441                         if ($p_amount == 0)
00442                                 return "=";
00443                         if ($p_amount < 0)
00444                                 return "C";
00445                         if ($p_amount > 0)
00446                                 return "D";
00447                 }
00448     /*!
00449      * \brief Display HTML Table Header (button)
00450      *
00451      * \return none
00452      */
00453     static function HtmlTableHeader($actiontarget="poste")
00454     {
00455       switch($actiontarget)
00456         {
00457         case 'poste':
00458           $action_csv='CSV:postedetail';
00459           $action_pdf='PDF:postedetail';
00460           break;
00461         case 'gl_comptes':
00462           $action_csv='CSV:glcompte';
00463           $action_pdf='PDF:glcompte';
00464           break;
00465         default:
00466           throw new Exception(" Fonction HtmlTableHeader argument actiontarget invalid");
00467         }
00468         $hid=new IHidden();
00469 
00470         echo "<table  >";
00471         echo '<TR>';
00472         $str_ople=(isset($_REQUEST['ople']))?HtmlInput::hidden('ople',$_REQUEST['ople']):'';
00473         if ($actiontarget=='poste')
00474           {
00475             echo '<TD><form method="GET" ACTION="">'.
00476               dossier::hidden().
00477               HtmlInput::submit('bt_other',"Autre poste").
00478               $hid->input("type","poste").$hid->input('ac',$_REQUEST['ac'])."</form></TD>";
00479           }
00480 
00481 
00482         echo '<TD><form method="GET" ACTION="export.php">'.
00483         dossier::hidden().
00484         HtmlInput::submit('bt_pdf',"Export PDF").
00485         HtmlInput::hidden('act',$action_pdf).
00486         $hid->input("type","poste").$str_ople.
00487         $hid->input('p_action','impress').
00488         $hid->input("from_periode",$_REQUEST['from_periode']).
00489         $hid->input("to_periode",$_REQUEST['to_periode'])
00490           ;
00491 
00492         if ( isset($_REQUEST['letter'] )) echo HtmlInput::hidden('letter','2');
00493         if ( isset($_REQUEST['solded'] )) echo HtmlInput::hidden('solded','1');
00494 
00495         if (isset($_REQUEST['from_poste']))
00496           echo HtmlInput::hidden('from_poste',$_REQUEST['from_poste']);
00497 
00498         if (isset($_REQUEST['to_poste']))
00499           echo HtmlInput::hidden('to_poste',$_REQUEST['to_poste']);
00500 
00501         if (isset($_REQUEST['poste_id']))
00502           echo HtmlInput::hidden("poste_id",$_REQUEST['poste_id']);
00503 
00504         if (isset($_REQUEST['poste_fille']))
00505             echo $hid->input('poste_fille','on');
00506         if (isset($_REQUEST['oper_detail']))
00507             echo $hid->input('oper_detail','on');
00508 
00509         echo "</form></TD>";
00510 
00511         echo '<TD><form method="GET" ACTION="export.php">'.
00512         dossier::hidden().
00513         HtmlInput::submit('bt_csv',"Export CSV").
00514         HtmlInput::hidden('act',$action_csv).
00515         $hid->input("type","poste").$str_ople.
00516         $hid->input('p_action','impress').
00517         $hid->input("from_periode",$_REQUEST['from_periode']).
00518           $hid->input("to_periode",$_REQUEST['to_periode']);
00519 
00520         if (isset($_REQUEST['from_poste']))
00521           echo HtmlInput::hidden('from_poste',$_REQUEST['from_poste']);
00522 
00523         if (isset($_REQUEST['to_poste']))
00524           echo HtmlInput::hidden('to_poste',$_REQUEST['to_poste']);
00525 
00526         if (isset($_REQUEST['poste_id']))
00527           echo HtmlInput::hidden("poste_id",$_REQUEST['poste_id']);
00528 
00529         if ( isset($_REQUEST['letter'] )) echo HtmlInput::hidden('letter','2');
00530         if ( isset($_REQUEST['solded'] )) echo HtmlInput::hidden('solded','1');
00531 
00532         if (isset($_REQUEST['poste_fille']))
00533             echo $hid->input('poste_fille','on');
00534         if (isset($_REQUEST['oper_detail']))
00535             echo $hid->input('oper_detail','on');
00536         if (isset($_REQUEST['poste_id'])) echo $hid->input("poste_id",$_REQUEST['poste_id']);
00537 
00538         echo "</form></TD>";
00539         echo "</form></TD>";
00540         echo '<td style="vertical-align:top">';
00541         echo HtmlInput::print_window();
00542         echo '</td>';
00543         echo '</tr>';
00544         echo "</table>";
00545 
00546 
00547     }
00548     /*!
00549      * \brief verify that the poste belong to a ledger
00550      *
00551      * \return 0 ok,  -1 no
00552      */
00553     function belong_ledger($p_jrn)
00554     {
00555         $filter=$this->db->get_value("select jrn_def_class_cred from jrn_def where jrn_def_id=$p_jrn");
00556         if ( trim ($filter) == '')
00557             return 0;
00558 
00559         $valid_cred=explode(" ",$filter);
00560         $sql="select count(*) as poste from tmp_pcmn where ";
00561         // Creation query
00562         $or="";
00563         $SqlFilter="";
00564         foreach ( $valid_cred as $item_cred)
00565         {
00566             if ( strlen (trim($item_cred)))
00567             {
00568                 if ( strstr($item_cred,"*") == true )
00569                 {
00570                     $item_cred=strtr($item_cred,"*","%");
00571                     $SqlItem="$or pcm_val::text like '".sql_string($item_cred)."'";
00572                     $or="  or ";
00573                 }
00574                 else
00575                 {
00576                     $SqlItem="$or pcm_val::text = '".sql_string($item_cred)."' ";
00577                     $or="  or ";
00578                 }
00579                 $SqlFilter=$SqlFilter.$SqlItem;
00580             }
00581         }//foreach
00582         $sql.=$SqlFilter." and pcm_val::text='".sql_string($this->id)."'";
00583         $max=$this->db->get_value($sql);
00584         if ($max > 0 )
00585             return 0;
00586         else
00587             return -1;
00588     }
00589     /*!\brief With the id of the ledger, get the col jrn_def_class_deb
00590      *\param $p_jrn jrn_id
00591      *\return array of value, or an empty array if nothing is found
00592      *\note
00593      *\see
00594      */
00595     function get_account_ledger($p_jrn)
00596     {
00597         $l=new Acc_Ledger($this->db,$p_jrn);
00598         $row=$l->get_propertie();
00599         if ( strlen(trim($row['jrn_def_class_deb'])) == 0 ) return array();
00600         $valid_account=explode(" ",$row['jrn_def_class_deb']);
00601         return $valid_account;
00602     }
00603     /*!\brief build a sql statement thanks a array found with get_account_ledger
00604      *
00605      *\param $p_jrn jrn_id
00606      *\return an emty string if nothing is found or a valid SQL statement like
00607     \code
00608     pcm_val like ... or pcm_val like ...
00609     \endcode
00610      *\note
00611      *\see get_account_ledger
00612      */
00613     function build_sql_account($p_jrn)
00614     {
00615         $array=$this->get_account_ledger($p_jrn);
00616         if ( empty($array) ) return "";
00617         $sql="";
00618         foreach ( $array as $item_cred)
00619         {
00620             if ( strlen (trim($item_cred))>0 )
00621             {
00622                 if ( strstr($item_cred,"*") == true )
00623                 {
00624                     $item_cred=strtr($item_cred,"*","%");
00625                     $sql_tmp=" pcm_val::text like '$item_cred' or";
00626                 }
00627                 else
00628                 {
00629                     $sql_tmp=" pcm_val::text = '$item_cred' or";
00630                 }
00631                 $sql.=$sql_tmp;
00632             }
00633         }//foreach
00634         /* remove the last or */
00635         $sql=substr($sql,0,strlen($sql)-2);
00636         return $sql;
00637     }
00638         /**
00639          * Find the id of the cards which are using the current account
00640          * 
00641          * @return an array of f_id
00642          */
00643         function find_card()
00644         {
00645                 $sql="select f_id from fiche_detail where ad_id=$1 and ad_value=$2";
00646                 $account=$this->db->get_array($sql,array(ATTR_DEF_ACCOUNT,$this->id));
00647                 return $account;
00648         }
00649     static function test_me()
00650     {
00651         $cn=new Database(dossier::id());
00652         $a=new Acc_Account_Ledger($cn,550);
00653         echo ' Journal 4 '.$a->belong_ledger(4);
00654         return $a->belong_ledger(4);;
00655 
00656     }
00657 }
 All Data Structures Namespaces Files Functions Variables Enumerations