class_acc_ledger_fin.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*
00004  *   This file is part of PhpCompta.
00005  *
00006  *   PhpCompta is free software; you can redistribute it and/or modify
00007  *   it under the terms of the GNU General Public License as published by
00008  *   the Free Software Foundation; either version 2 of the License, or
00009  *   (at your option) any later version.
00010  *
00011  *   PhpCompta is distributed in the hope that it will be useful,
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *   GNU General Public License for more details.
00015  *
00016  *   You should have received a copy of the GNU General Public License
00017  *   along with PhpCompta; if not, write to the Free Software
00018  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019  */
00020 /* $Revision: 4942 $ */
00021 
00022 // Copyright Author Dany De Bontridder ddebontridder@yahoo.fr
00023 
00024 /* !\file
00025  * \brief the class Acc_Ledger_Fin inherits from Acc_Ledger, this
00026  * object permit to manage the financial ledger
00027  */
00028 require_once("class_idate.php");
00029 require_once("class_icard.php");
00030 require_once("class_ispan.php");
00031 require_once("class_itext.php");
00032 require_once("class_iconcerned.php");
00033 require_once("class_ifile.php");
00034 require_once("class_ihidden.php");
00035 require_once("class_iselect.php");
00036 require_once('class_acc_ledger.php');
00037 require_once('ac_common.php');
00038 require_once('class_acc_reconciliation.php');
00039 
00040 class Acc_Ledger_Fin extends Acc_Ledger
00041 {
00042 
00043         function __construct($p_cn, $p_init)
00044         {
00045                 parent::__construct($p_cn, $p_init);
00046                 $this->type = 'FIN';
00047         }
00048 
00049         /* !\brief verify that the data are correct before inserting or confirming
00050          * \param an array (usually $_POST)
00051          * \return String
00052          * \throw Exception on error occurs
00053          */
00054 
00055         public function verify($p_array)
00056         {
00057                 global $g_user;
00058                 extract($p_array);
00059                 /* check for a double reload */
00060                 if (isset($mt) && $this->db->count_sql('select jr_mt from jrn where jr_mt=$1', array($mt)) != 0)
00061                         throw new Exception(_('Double Encodage'), 5);
00062 
00063                 /* check if we can write into this ledger */
00064                 if ($g_user->check_jrn($p_jrn) != 'W')
00065                         throw new Exception(_('Accès interdit'), 20);
00066 
00067                 /* check if there is a bank account linked to the ledger */
00068                 $bank_id = $this->get_bank();
00069 
00070                 if ($this->db->count() == 0)
00071                         throw new Exception("Ce journal n'a pas de compte en banque, allez dans paramètre->journal pour régler cela");
00072                 /* check if the accounting of the bank is correct */
00073                 $fBank = new Fiche($this->db, $bank_id);
00074                 $bank_accounting = $fBank->strAttribut(ATTR_DEF_ACCOUNT);
00075                 if (trim($bank_accounting) == '')
00076                         throw new Exception('Le poste comptable du compte en banque de ce journal est invalide');
00077 
00078                 /* check if the account exists */
00079                 $poste = new Acc_Account_Ledger($this->db, $bank_accounting);
00080                 if ($poste->load() == false)
00081                         throw new Exception('Le poste comptable du compte en banque de ce journal est invalide');
00082                 if ($chdate != 1 && $chdate != 2) throw new Exception ('Le choix de date est invalide');
00083                 if ( $chdate == 1 )
00084                 {
00085                         /*  check if the date is valid */
00086                         if (isDate($e_date) == null)
00087                         {
00088                                 throw new Exception('Date invalide', 2);
00089                         }
00090                         $oPeriode = new Periode($this->db);
00091                         if ($this->check_periode() == false)
00092                         {
00093                                 $periode = $oPeriode->find_periode($e_date);
00094                         }
00095                         else
00096                         {
00097                                 $oPeriode->p_id = $periode;
00098                                 list ($min, $max) = $oPeriode->get_date_limit();
00099                                 if (cmpDate($e_date, $min) < 0 ||
00100                                                 cmpDate($e_date, $max) > 0)
00101                                         throw new Exception(_('Date et periode ne correspondent pas'), 6);
00102                         }
00103 
00104                         /* check if the periode is closed */
00105                         if ($this->is_closed($periode) == 1)
00106                         {
00107                                 throw new Exception(_('Periode fermee'), 6);
00108                         }
00109 
00110                         /* check if we are using the strict mode */
00111                         if ($this->check_strict() == true)
00112                         {
00113                                 /* if we use the strict mode, we get the date of the last
00114                                 operation */
00115                                 $last_date = $this->get_last_date();
00116                                 if ($last_date != null && cmpDate($e_date, $last_date) < 0)
00117                                         throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ')
00118                                                         . $last_date . _(' vous ne pouvez pas encoder à une date antérieure'), 15);
00119                         }
00120                 }
00121 
00122                 $acc_pay = new Acc_Operation($this->db);
00123 
00124                 $nb = 0;
00125                 $tot_amount = 0;
00126                 //----------------------------------------
00127                 // foreach item
00128                 //----------------------------------------
00129                 for ($i = 0; $i < $nb_item; $i++)
00130                 {
00131                         if (strlen(trim(${'e_other' . $i})) == 0)
00132                                 continue;
00133                         /* check if amount are numeric and */
00134                         if (isNumber(${'e_other' . $i . '_amount'}) == 0)
00135                                 throw new Exception('La fiche ' . ${'e_other' . $i} . 'a un montant invalide [' . ${'e_other' . $i . '_amount'} . ']', 6);
00136 
00137                         /* compute the total */
00138                         $tot_amount+=round(${'e_other' . $i . '_amount'}, 2);
00139                         /* check if all card has a ATTR_DEF_ACCOUNT */
00140                         $fiche = new Fiche($this->db);
00141                         $fiche->get_by_qcode(${'e_other' . $i});
00142                         if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
00143                                 throw new Exception('La fiche ' . ${'e_other' . $i} . 'n\'a pas de poste comptable', 8);
00144 
00145                         $sposte = $fiche->strAttribut(ATTR_DEF_ACCOUNT);
00146                         // if 2 accounts, take only the debit one for customer
00147                         if (strpos($sposte, ',') != 0)
00148                         {
00149                                 $array = explode(',', $sposte);
00150                                 $poste_val = $array[1];
00151                         }
00152                         else
00153                         {
00154                                 $poste_val = $sposte;
00155                         }
00156                         /* The account exists */
00157                         $poste = new Acc_Account_Ledger($this->db, $poste_val);
00158                         if ($poste->load() == false)
00159                         {
00160                                 throw new Exception('Pour la fiche ' . ${'e_other' . $i} . ' le poste comptable [' . $poste->id . 'n\'existe pas', 9);
00161                         }
00162                         /* Check if the card belong to the ledger */
00163                         $fiche = new Fiche($this->db);
00164                         $fiche->get_by_qcode(${'e_other' . $i});
00165                         if ($fiche->belong_ledger($p_jrn, 'deb') != 1)
00166                                 throw new Exception('La fiche ' . ${'e_other' . $i} . 'n\'est pas accessible à ce journal', 10);
00167                         if ($chdate == 2)
00168                         {
00169                                 {/*  check if the date is valid */
00170                                         if (isDate(${'dateop' . $i}) == null)
00171                                         {
00172                                                 throw new Exception('Date invalide', 2);
00173                                         }
00174                                         $oPeriode = new Periode($this->db);
00175                                         if ($this->check_periode() == false)
00176                                         {
00177                                                 $periode = $oPeriode->find_periode(${'dateop' . $i});
00178                                         }
00179                                         else
00180                                         {
00181                                                 $oPeriode->p_id = $periode;
00182                                                 list ($min, $max) = $oPeriode->get_date_limit();
00183                                                 if (cmpDate(${'dateop' . $i}, $min) < 0 ||
00184                                                                 cmpDate(${'dateop' . $i}, $max) > 0)
00185                                                         throw new Exception(_('Date et periode ne correspondent pas'), 6);
00186                                         }
00187 
00188                                         /* check if the periode is closed */
00189                                         if ($this->is_closed($periode) == 1)
00190                                         {
00191                                                 throw new Exception(_('Periode fermee'), 6);
00192                                         }
00193 
00194                                         /* check if we are using the strict mode */
00195                                         if ($this->check_strict() == true)
00196                                         {
00197                                                 /* if we use the strict mode, we get the date of the last
00198                                                   operation */
00199                                                 $last_date = $this->get_last_date();
00200                                                 if ($last_date != null && cmpDate(${'dateop' . $i}, $last_date) < 0)
00201                                                         throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ')
00202                                                                         . $last_date . _(' vous ne pouvez pas encoder à une date antérieure'), 15);
00203                                         }
00204                                 }
00205                         }
00206                         $nb++;
00207                 }
00208                 if ($nb == 0)
00209                         throw new Exception('Il n\'y a aucune opération', 12);
00210 
00211                 /* Check if the last_saldo and first_saldo are correct */
00212                 if (strlen(trim($last_sold)) != 0 && isNumber($last_sold) &&
00213                                 strlen(trim($first_sold)) != 0 && isNumber($first_sold))
00214                 {
00215                         $diff = $last_sold - $first_sold;
00216                         $diff = round($diff, 2) - round($tot_amount, 2);
00217                         if ($first_sold != 0 && $last_sold != 0)
00218                         {
00219                                 if ($diff != 0)
00220                                         throw new Exception('Le montant de l\'extrait est incorrect' .
00221                                                         $tot_amount . ' extrait ' . $diff, 13);
00222                         }
00223                 }
00224         }
00225 
00226         /* !\brief
00227          * \param $p_array contains the value usually it is $_POST
00228          * \return string with html code
00229          * \note the form tag are not  set here
00230          */
00231 
00232         function input($p_array = null, $notused = 0)
00233         {
00234                 global $g_parameter, $g_user;
00235                 if ($p_array != null)
00236                         extract($p_array);
00237 
00238                 $pview_only = false;
00239 
00240                 $f_add_button = new IButton('add_card');
00241                 $f_add_button->label = _('Créer une nouvelle fiche');
00242                 $f_add_button->set_attribute('ipopup', 'ipop_newcard');
00243                 $f_add_button->set_attribute('jrn', $this->id);
00244                 $f_add_button->javascript = " this.jrn=\$('p_jrn').value;select_card_type(this);";
00245                 $str_add_button = ($g_user->check_action(FICADD) == 1) ? $f_add_button->input() : "";
00246 
00247                 // The first day of the periode
00248                 $pPeriode = new Periode($this->db);
00249                 list ($l_date_start, $l_date_end) = $pPeriode->get_date_limit($g_user->get_periode());
00250                 if ($g_parameter->MY_DATE_SUGGEST == 'Y')
00251                         $op_date = (!isset($e_date) ) ? $l_date_start : $e_date;
00252                 else
00253                         $op_date = (!isset($e_date) ) ? '' : $e_date;
00254 
00255                 $r = "";
00256 
00257                 $r.=dossier::hidden();
00258                 $f_legend = 'Banque, caisse';
00259                 //  Date
00260                 //--
00261                 $Date = new IDate("e_date", $op_date);
00262                 $Date->setReadOnly($pview_only);
00263                 $f_date = $Date->input();
00264                 $f_period = '';
00265                 if ($this->check_periode() == true)
00266                 {
00267                         // Periode
00268                         //--
00269                         $l_user_per = (isset($periode)) ? $periode : $g_user->get_periode();
00270                         $period = new IPeriod();
00271                         $period->cn = $this->db;
00272                         $period->type = OPEN;
00273                         $period->value = $l_user_per;
00274                         $period->user = $g_user;
00275                         $period->name = 'periode';
00276                         try
00277                         {
00278                                 $l_form_per = $period->input();
00279                         }
00280                         catch (Exception $e)
00281                         {
00282                                 if ($e->getCode() == 1)
00283                                 {
00284                                         echo "Aucune période ouverte";
00285                                         exit();
00286                                 }
00287                         }
00288                         $label = HtmlInput::infobulle(3);
00289                         $f_period = "Période comptable $label" . $l_form_per;
00290                 }
00291 
00292                 // Ledger (p_jrn)
00293                 //--
00294 
00295                 $add_js = 'onchange="update_pj();update_bank();get_last_date();ajax_saldo(\'first_sold\');update_name();"';
00296 
00297                 if ($g_parameter->MY_DATE_SUGGEST == 'Y')
00298                         $add_js = 'onchange="update_pj();update_bank();get_last_date();ajax_saldo(\'first_sold\')";';
00299 
00300                 $wLedger = $this->select_ledger('FIN', 2);
00301                 if ($wLedger == null)
00302                         exit('Pas de journal disponible');
00303 
00304                 $wLedger->javascript = $add_js;
00305 
00306                 $label = " Journal " . HtmlInput::infobulle(2);
00307                 $f_jrn = $label . $wLedger->input();
00308 
00309 
00310                 // retrieve bank name, code and account from the jrn_def.jrn_def_bank
00311 
00312                 $f_bank = '<span id="bkname">' . $this->get_bank_name() . '</span>';
00313                 if ($this->bank_id == "")
00314                 {
00315                         echo h2("Journal de banque non configuré " . $this->get_name(), ' class="error"');
00316                         echo '<span class="error"> vous devez donner à ce journal un compte en banque (fiche), modifiez dans CFGLED</span>';
00317                         alert("Journal de banque non configuré " . $this->get_name());
00318                 }
00319 
00320                 $f_legend_detail = 'Opérations financières';
00321                 //--------------------------------------------------
00322                 // Saldo begin end
00323                 //-------------------------------------------------
00324                 // Extrait
00325                 $default_pj = '';
00326                 if ($g_parameter->MY_PJ_SUGGEST == 'Y')
00327                 {
00328                         $default_pj = $this->guess_pj();
00329                 }
00330                 $wPJ = new IText('e_pj');
00331                 $wPJ->readonly = false;
00332                 $wPJ->size = 10;
00333                 $wPJ->value = (isset($e_pj)) ? $e_pj : $default_pj;
00334 
00335                 $f_extrait = $wPJ->input() . HtmlInput::hidden('e_pj_suggest', $default_pj);
00336                 $label = HtmlInput::infobulle(7);
00337 
00338                 $first_sold = (isset($first_sold)) ? $first_sold : "";
00339                 $wFirst = new INum('first_sold', $first_sold);
00340 
00341                 $last_sold = isset($last_sold) ? $last_sold : "";
00342                 $wLast = new INum('last_sold', $last_sold);
00343 
00344 
00345                 $max = (isset($nb_item)) ? $nb_item : MAX_ARTICLE;
00346 
00347                 $r.= HtmlInput::hidden('nb_item', $max);
00348                 //--------------------------------------------------
00349                 // financial operation
00350                 //-------------------------------------------------
00351 
00352                 $array = array();
00353                 // Parse each " tiers"
00354                 for ($i = 0; $i < $max; $i++)
00355                 {
00356                         $tiers = (isset(${"e_other" . $i})) ? ${"e_other" . $i} : "";
00357 
00358                         $tiers_amount = (isset(${"e_other$i" . "_amount"})) ? round(${"e_other$i" . "_amount"}, 2) : 0;
00359 
00360                         $tiers_comment = (isset(${"e_other$i" . "_comment"})) ? ${"e_other$i" . "_comment"} : "";
00361 
00362                         $operation_date=new IDate("dateop".$i);
00363                         $operation_date->value=(isset(${'dateop'.$i}))?${'dateop'.$i}:"";
00364                         $array[$i]['dateop']=$operation_date->input();
00365                         ${"e_other$i" . "_amount"} = (isset(${"e_other$i" . "_amount"})) ? ${"e_other$i" . "_amount"} : 0;
00366 
00367                         $W1 = new ICard();
00368                         $W1->label = "";
00369                         $W1->name = "e_other" . $i;
00370                         $W1->value = $tiers;
00371                         $W1->extra = 'deb';  // credits
00372                         $W1->typecard = 'deb';
00373                         $W1->set_dblclick("fill_ipopcard(this);");
00374                         $W1->set_attribute('ipopup', 'ipopcard');
00375 
00376                         // name of the field to update with the name of the card
00377                         $W1->set_attribute('label', 'e_other_name' . $i);
00378                         // name of the field to update with the name of the card
00379                         $W1->set_attribute('typecard', 'filter');
00380                         // Add the callback function to filter the card on the jrn
00381                         $W1->set_callback('filter_card');
00382                         $W1->set_function('fill_data');
00383                         $W1->javascript = sprintf(' onchange="fill_data_onchange(\'%s\');" ', $W1->name);
00384                         $W1->readonly = $pview_only;
00385                         $array[$i]['qcode'] = $W1->input();
00386                         $array[$i]['search'] = $W1->search();
00387 
00388                         // Card name
00389                         //
00390                          $card_name = "";
00391                         if ($tiers != "")
00392                         {
00393                                 $fiche = new Fiche($this->db);
00394                                 $fiche->get_by_qcode($tiers);
00395                                 $card_name = $this->db->get_value("Select ad_value from fiche_detail where ad_id=$1 and f_id=$2", array(ATTR_DEF_NAME, $fiche->id));
00396                         }
00397 
00398                         $wcard_name = new IText("e_other_name" . $i, $card_name);
00399                         $wcard_name->readOnly = true;
00400                         $array[$i]['cname'] = $wcard_name->input();
00401 
00402                         // Comment
00403                         $wComment = new IText("e_other$i" . "_comment", $tiers_comment);
00404 
00405                         $wComment->size = 35;
00406                         $wComment->setReadOnly($pview_only);
00407                         $array[$i]['comment'] = $wComment->input();
00408                         // amount
00409                         $wAmount = new INum("e_other$i" . "_amount", $tiers_amount);
00410 
00411                         $wAmount->size = 7;
00412                         $wAmount->setReadOnly($pview_only);
00413                         $array[$i]['amount'] = $wAmount->input();
00414                         // concerned
00415                         ${"e_concerned" . $i} = (isset(${"e_concerned" . $i})) ? ${"e_concerned" . $i} : ""
00416                         ;
00417                         $wConcerned = new IConcerned("e_concerned" . $i, ${"e_concerned" . $i});
00418                         $wConcerned->setReadOnly($pview_only);
00419                         $wConcerned->amount_id = "e_other" . $i . "_amount";
00420 
00421                         $wConcerned->paid = 'paid';
00422                         $array[$i]['concerned'] = $wConcerned->input();
00423                 }
00424 
00425                 ob_start();
00426                 require_once('template/form_ledger_fin.php');
00427                 $r.=ob_get_contents();
00428                 ob_clean();
00429 
00430 
00431                 return $r;
00432         }
00433 
00434         /* !\brief show the summary before inserting into the database, it
00435          * calls the function for adding a attachment. The function verify
00436          * should be called before
00437          * \param $p_array an array usually is $_POST
00438          * \return string with code html
00439          */
00440 
00441         public function confirm($p_array, $p_nothing = 0)
00442         {
00443                 global $g_parameter;
00444                 $r = "";
00445                 bcscale(2);
00446                 extract($p_array);
00447                 $pPeriode = new Periode($this->db);
00448                 if ($this->check_periode() == true)
00449                 {
00450                         $pPeriode->p_id = $periode;
00451                 }
00452                 else
00453                 {
00454                         $pPeriode->find_periode($e_date);
00455                 }
00456 
00457                 list ($l_date_start, $l_date_end) = $pPeriode->get_date_limit();
00458                 $exercice = $pPeriode->get_exercice();
00459                 $r.='';
00460                 $r.='<fieldset><legend>Banque, caisse </legend>';
00461                 $r.= '<div id="jrn_name_div">';
00462                 $r.='<h2 id="jrn_name" style="display:inline">' . $this->get_name() . '</h2>';
00463                 $r.= '</div>';
00464                 $r.='<TABLE  width="100%">';
00465                 //  Date
00466                 //--
00467                 $r.="<tr>";
00468                 if ( $chdate == 1 ) $r.='<td> Date : </td><td>' . $e_date;
00469                 // Periode
00470                 //--
00471                 $r.="<td>";
00472                 $r.="Période comptable </td><td>";
00473                 $r.=$l_date_start . ' - ' . $l_date_end;
00474                 $r.="</td>";
00475                 $r.="</tr>";
00476                 // Ledger (p_jrn)
00477                 //--
00478                 $r.='<tr>';
00479                 $r.='<td> Journal </td>';
00480                 $this->id = $p_jrn;
00481                 $r.='<td>';
00482                 $r.=h($this->get_name());
00483                 $r.='</td>';
00484                 $r.='</tr>';
00485 
00486                 //retrieve bank name
00487                 $bk_id = $this->get_bank();
00488 
00489                 $fBank = new Fiche($this->db, $bk_id);
00490                 $e_bank_account_label = $this->get_bank_name();
00491 
00492                 $filter_year = "  j_tech_per in (select p_id from parm_periode where  p_exercice='" . $exercice . "')";
00493 
00494                 $acc_account = new Acc_Account_Ledger($this->db, $fBank->strAttribut(ATTR_DEF_ACCOUNT));
00495                 $solde = $acc_account->get_solde($filter_year);
00496                 $new_solde = $solde;
00497 
00498                 $r.="<TR><td colspan=\"4\"> Banque ";
00499                 $r.=$e_bank_account_label;
00500 
00501                 $r.="</TABLE>";
00502 
00503                 $r.='</fieldset>';
00504 
00505                 $r.='<fieldset><legend>Opérations financières</legend>';
00506                 //--------------------------------------------------
00507                 // Saldo begin end
00508                 //-------------------------------------------------
00509                 $r.='<fieldset><legend>Extrait de compte</legend>';
00510                 $r.='<table>';
00511                 $r.='<tr>';
00512                 // Extrait
00513                 //--
00514                 $r.='<td> Numéro d\'extrait</td>' . h($e_pj);
00515                 $r.='<td >Solde début extrait </td>';
00516                 $r.='<td>' . $first_sold . '</td>';
00517                 $r.='<td>Solde fin extrait </td>';
00518                 $r.='<td>' . $last_sold . '</td>';
00519                 $r.='</table>';
00520                 $r.='</fieldset>';
00521 
00522                 //--------------------------------------------------
00523                 // financial operation
00524                 //-------------------------------------------------
00525                 $r.='<TABLE style="width:100%" id="fin_item">';
00526                 $r.="<TR>";
00527                 if ($chdate==2) $r.='<th>Date</th>';
00528                 $r.="<th style=\"width:auto;text-align:left\" colspan=\"2\">Nom</TH>";
00529                 $r.="<th style=\"text-align:left\" >Commentaire</TH>";
00530                 $r.="<th style=\"text-align:right\">Montant</TH>";
00531                 $r.='<th colspan="2"> Op. Concern&eacute;e(s)</th>';
00532 
00533                 /* if we use the AC */
00534                 if ($g_parameter->MY_ANALYTIC != 'nu')
00535                 {
00536                         $anc = new Anc_Plan($this->db);
00537                         $a_anc = $anc->get_list();
00538                         $x = count($a_anc);
00539                         /* set the width of the col */
00540                         $r.='<th colspan="' . $x . '">' . _('Compt. Analytique') . '</th>';
00541 
00542                         /* add hidden variables pa[] to hold the value of pa_id */
00543                         $r.=Anc_Plan::hidden($a_anc);
00544                 }
00545                 $r.="</TR>";
00546                 // Parse each " tiers"
00547                 $tot_amount = 0;
00548                 //--------------------------------------------------
00549                 // For each items
00550                 //--------------------------------------------------
00551                 for ($i = 0; $i < $nb_item; $i++)
00552                 {
00553 
00554                         $tiers = (isset(${"e_other" . $i})) ? ${"e_other" . $i} : ""
00555                         ;
00556 
00557                         if (strlen(trim($tiers)) == 0)
00558                                 continue;
00559                         $tiers_label = "";
00560                         $tiers_amount = round(${"e_other$i" . "_amount"}, 2);
00561                         $tot_amount = bcadd($tot_amount, $tiers_amount);
00562                         $tiers_comment = h(${"e_other$i" . "_comment"});
00563                         // If $tiers has a value
00564                         $fTiers = new Fiche($this->db);
00565                         $fTiers->get_by_qcode($tiers);
00566 
00567                         $tiers_label = $fTiers->strAttribut(ATTR_DEF_NAME);
00568 
00569                         $r.="<TR>";
00570                         if ($chdate==2) $r.=td(${"dateop".$i});
00571                         $r.="<td>" . ${'e_other' . $i} . "</TD>";
00572                         // label
00573                         $r.='<TD style="width:25%;border-bottom:1px dotted grey;">';
00574                         $r.=$fTiers->strAttribut(ATTR_DEF_NAME);
00575                         $r.='</td>';
00576                         // Comment
00577                         $r.='<td style="width:40%">' . $tiers_comment . '</td>';
00578                         // amount
00579                         $r.='<td class="num">' . nbm($tiers_amount) . '</td>';
00580                         // concerned
00581                         $r.='<td style="text-align:center">';
00582                         if (${"e_concerned" . $i} != '')
00583                         {
00584                                 $jr_internal = $this->db->get_array("select jr_internal from jrn where jr_id in (" . ${"e_concerned" . $i} . ")");
00585                                 for ($x = 0; $x < count($jr_internal); $x++)
00586                                 {
00587                                         $r.=HtmlInput::detail_op(${"e_concerned" . $i}, $jr_internal[$x]['jr_internal']);
00588                                 }
00589                         }
00590                         $r.='</td>';
00591                         // encode the pa
00592                         if ($g_parameter->MY_ANALYTIC != 'nu' && preg_match("/^[6,7]/", $fTiers->strAttribut(ATTR_DEF_ACCOUNT)) == 1) // use of AA
00593                         {
00594                                 // show form
00595                                 $anc_op = new Anc_Operation($this->db);
00596                                 $null = ($g_parameter->MY_ANALYTIC == 'op') ? 1 : 0;
00597                                 $r.='<td>';
00598                                 $p_mode = 1;
00599                                 $p_array['pa_id'] = $a_anc;
00600                                 /* op is the operation it contains either a sequence or a jrnx.j_id */
00601                                 $r.=HtmlInput::hidden('op[]=', $i);
00602                                 $r.=$anc_op->display_form_plan($p_array, $null, $p_mode, $i, $tiers_amount);
00603                                 $r.='</td>';
00604                         }
00605 
00606                         $r.='</TR>';
00607                 }
00608                 $r.="</TABLE>";
00609 
00610                 // saldo
00611                 $r.='<br>Ancien solde = ' . $solde;
00612                 $new_solde+=$tot_amount;
00613                 $r.='<br>Nouveau solde = ' . $new_solde;
00614                 $r.='<br>Difference =' . $tot_amount;
00615                 // check for upload piece
00616                 $file = new IFile();
00617 
00618                 $r.="<br>Ajoutez une pi&egrave;ce justificative ";
00619                 $r.=$file->input("pj", "");
00620 
00621                 $r.='</fieldset>';
00622                 //--------------------------------------------------
00623                 // Hidden variables
00624                 //--------------------------------------------------
00625                 $r.=dossier::hidden();
00626                 $r.=HtmlInput::hidden('p_jrn', $this->id);
00627                 $r.=HtmlInput::hidden('nb_item', $nb_item);
00628                 $r.=HtmlInput::hidden('last_sold', $last_sold);
00629                 $r.=HtmlInput::hidden('first_sold', $first_sold);
00630                 $r.=HtmlInput::hidden('e_pj', $e_pj);
00631                 $r.=HtmlInput::hidden('e_pj_suggest', $e_pj_suggest);
00632                 $r.=HtmlInput::hidden('e_date', $e_date);
00633                 $mt = microtime(true);
00634                 $r.=HtmlInput::hidden('mt', $mt);
00635 
00636                 if (isset($periode))
00637                         $r.=HtmlInput::hidden('periode', $periode);
00638                 $r.=dossier::hidden();
00639                 $r.=HtmlInput::hidden('sa', 'n','chdate');
00640                 for ($i = 0; $i < $nb_item; $i++)
00641                 {
00642                         $tiers = (isset(${"e_other" . $i})) ? ${"e_other" . $i} : ""                    ;
00643                         $r.=HtmlInput::hidden('e_other' . $i, $tiers);
00644                         $r.=HtmlInput::hidden('e_other' . $i, $tiers);
00645                         $r.=HtmlInput::hidden('e_other' . $i . '_comment', ${'e_other' . $i . '_comment'});
00646                         $r.=HtmlInput::hidden('e_other' . $i . '_amount', ${'e_other' . $i . '_amount'});
00647                         $r.=HtmlInput::hidden('e_concerned' . $i, ${'e_concerned' . $i});
00648                         $r.=HtmlInput::hidden('dateop' . $i, ${'dateop' . $i});
00649                         $r.=HtmlInput::hidden('chdate' , $chdate);
00650                 }
00651 
00652                 return $r;
00653         }
00654 
00655         /* !\brief save the data into the database, included the attachment,
00656          * and the reconciliations
00657          * \param $p_array usually $_POST
00658          * \return string with HTML code
00659          */
00660 
00661         public function insert($p_array = null)
00662         {
00663                 global $g_parameter;
00664                 bcscale(2);
00665                 $internal_code = "";
00666                 $oid = 0;
00667                 extract($p_array);
00668                 $ret = '';
00669                 // Debit = banque
00670                 $bank_id = $this->get_bank();
00671                 $fBank = new Fiche($this->db, $bank_id);
00672                 $e_bank_account = $fBank->strAttribut(ATTR_DEF_QUICKCODE);
00673                 // Get the saldo
00674                 $pPeriode = new Periode($this->db);
00675                 $sposte = $fBank->strAttribut(ATTR_DEF_ACCOUNT);
00676                 // if 2 accounts, take only the debit one for customer
00677                 if (strpos($sposte, ',') != 0)
00678                 {
00679                         $array = explode(',', $sposte);
00680                         $poste_val = $array[0];
00681                 }
00682                 else
00683                 {
00684                         $poste_val = $sposte;
00685                 }
00686 
00687                 $acc_account = new Acc_Account_Ledger($this->db, $poste_val);
00688 
00689                 // If date = deposit date
00690                 if ($chdate == 1 )
00691                 {
00692                         if ($this->check_periode() == true)
00693                         {
00694                                 $pPeriode->p_id = $periode;
00695                         }
00696                         else
00697                         {
00698                                 $pPeriode->find_periode($e_date);
00699                         }
00700                         $exercice = $pPeriode->get_exercice();
00701                         $filter_year = "  j_tech_per in (select p_id from parm_periode where  p_exercice='" . $exercice . "')";
00702                         $solde = $acc_account->get_solde($filter_year);
00703                         $new_solde = $solde;
00704                 }
00705 
00706 
00707 
00708 
00709 
00710                 try
00711                 {
00712                         $this->db->start();
00713                         $amount = 0.0;
00714                         $idx_operation = 0;
00715                         $ret = '<table class="result" style="width:75%">';
00716                         $ret.=tr(th('Date').th('n° interne') . th('Quick Code') . th('Nom') . th('Libellé') . th('Montant', ' style="text-align:right"'));
00717                         // Credit = goods
00718                         $get_solde=true;
00719                         for ($i = 0; $i < $nb_item; $i++)
00720                         {
00721                                 // if tiers is set and amount != 0 insert it into the database
00722                                 // and quit the loop ?
00723                                 if (strlen(trim(${"e_other$i"})) == 0)
00724                                         continue;
00725 
00726                                 if ( $chdate == 2 ) $e_date=${'dateop'.$i};
00727                                 // if date is date of operation
00728                                 if ($chdate == 2 && $get_solde )
00729                                 {
00730                                         $get_solde=false;
00731                                         if ($this->check_periode() == true)
00732                                         {
00733                                                 $pPeriode->p_id = $periode;
00734                                         }
00735                                         else
00736                                         {
00737                                                 $pPeriode->find_periode($e_date);
00738                                         }
00739                                         $exercice = $pPeriode->get_exercice();
00740                                         $filter_year = "  j_tech_per in (select p_id from parm_periode where  p_exercice='" . $exercice . "')";
00741                                         $solde = $acc_account->get_solde($filter_year);
00742                                         $new_solde = $solde;
00743                                 }
00744                                 $fPoste = new Fiche($this->db);
00745                                 $fPoste->get_by_qcode(${"e_other$i"});
00746 
00747                                 // round it
00748                                 ${"e_other$i" . "_amount"} = round(${"e_other$i" . "_amount"}, 2);
00749 
00750 
00751 
00752                                 $amount+=${"e_other$i" . "_amount"};
00753                                 // Record a line for the bank
00754                                 // Compute the j_grpt
00755                                 $seq = $this->db->get_next_seq('s_grpt');
00756 
00757                                 $acc_operation = new Acc_Operation($this->db);
00758                                 $acc_operation->date = $e_date;
00759                                 $sposte = $fPoste->strAttribut(ATTR_DEF_ACCOUNT);
00760                                 // if 2 accounts
00761                                 if (strpos($sposte, ',') != 0)
00762                                 {
00763                                         $array = explode(',', $sposte);
00764                                         if (${"e_other$i" . "_amount"} < 0)
00765                                                 $poste_val = $array[1];
00766                                         else
00767                                                 $poste_val = $array[0];
00768                                 }
00769                                 else
00770                                 {
00771                                         $poste_val = $sposte;
00772                                 }
00773 
00774 
00775                                 $acc_operation->poste = $poste_val;
00776                                 $acc_operation->amount = ${"e_other$i" . "_amount"} * (-1);
00777                                 $acc_operation->grpt = $seq;
00778                                 $acc_operation->jrn = $p_jrn;
00779                                 $acc_operation->type = 'd';
00780 
00781                                 if (isset($periode))
00782                                         $tperiode = $periode;
00783                                 else
00784                                 {
00785                                         $per = new Periode($this->db);
00786                                         $tperiode = $per->find_periode($e_date);
00787                                 }
00788                                 $acc_operation->periode = $tperiode;
00789                                 $acc_operation->qcode = ${"e_other" . $i};
00790                                 $j_id = $acc_operation->insert_jrnx();
00791 
00792                                 $acc_operation = new Acc_Operation($this->db);
00793                                 $acc_operation->date = $e_date;
00794                                 $sposte = $fBank->strAttribut(ATTR_DEF_ACCOUNT);
00795 
00796                                 // if 2 accounts
00797                                 if (strpos($sposte, ',') != 0)
00798                                 {
00799                                         $array = explode(',', $sposte);
00800                                         if (${"e_other$i" . "_amount"} < 0)
00801                                                 $poste_val = $array[1];
00802                                         else
00803                                                 $poste_val = $array[0];
00804                                 }
00805                                 else
00806                                 {
00807                                         $poste_val = $sposte;
00808                                 }
00809 
00810                                 $acc_operation->poste = $poste_val;
00811                                 $acc_operation->amount = ${"e_other$i" . "_amount"};
00812                                 $acc_operation->grpt = $seq;
00813                                 $acc_operation->jrn = $p_jrn;
00814                                 $acc_operation->type = 'd';
00815                                 $acc_operation->periode = $tperiode;
00816                                 $acc_operation->qcode = $e_bank_account;
00817                                 $acc_operation->insert_jrnx();
00818 
00819 
00820                                 if (sql_string(${"e_other$i" . "_comment"}) == null)
00821                                 {
00822                                         // if comment is blank set a default one
00823                                         $comment = "  compte : " . $fBank->strAttribut(ATTR_DEF_NAME) . ' a ' .
00824                                                         $fPoste->strAttribut(ATTR_DEF_NAME);
00825                                 }
00826                                 else
00827                                 {
00828                                         $comment = ${'e_other' . $i . '_comment'};
00829                                 }
00830 
00831 
00832                                 $acc_operation = new Acc_Operation($this->db);
00833                                 $acc_operation->jrn = $p_jrn;
00834                                 $acc_operation->amount = abs(${"e_other$i" . "_amount"});
00835                                 $acc_operation->date = $e_date;
00836                                 $acc_operation->desc = $comment;
00837                                 $acc_operation->grpt = $seq;
00838                                 $acc_operation->periode = $tperiode;
00839                                 $acc_operation->mt = $mt;
00840                                 $idx_operation++;
00841                                 $acc_operation->pj = '';
00842 
00843                                 if (trim($e_pj) != '' && $this->numb_operation() == true)
00844                                         $acc_operation->pj = $e_pj . str_pad($idx_operation, 3, 0, STR_PAD_LEFT);
00845 
00846                                 if (trim($e_pj) != '' && $this->numb_operation() == false)
00847                                         $acc_operation->pj = $e_pj;
00848 
00849                                 $jr_id = $acc_operation->insert_jrn();
00850                                 //        $acc_operation->set_pj();
00851                                 $this->db->exec_sql('update jrn set jr_pj_number=$1 where jr_id=$2', array($acc_operation->pj, $jr_id));
00852                                 $internal = $this->compute_internal_code($seq);
00853 
00854 
00855                                 if (trim(${"e_concerned" . $i}) != "")
00856                                 {
00857                                         if (strpos(${"e_concerned" . $i}, ',') != 0)
00858                                         {
00859                                                 $aRapt = explode(',', ${"e_concerned" . $i});
00860                                                 foreach ($aRapt as $rRapt)
00861                                                 {
00862                                                         // Add a "concerned operation to bound these op.together
00863                                                         //
00864                             $rec = new Acc_Reconciliation($this->db);
00865                                                         $rec->set_jr_id($jr_id);
00866 
00867                                                         if (isNumber($rRapt) == 1)
00868                                                         {
00869                                                                 $rec->insert($rRapt);
00870                                                         }
00871                                                 }
00872                                         }
00873                                         else
00874                                         if (isNumber(${"e_concerned" . $i}) == 1)
00875                                         {
00876                                                 $rec = new Acc_Reconciliation($this->db);
00877                                                 $rec->set_jr_id($jr_id);
00878                                                 $rec->insert(${"e_concerned$i"});
00879                                         }
00880                                 }
00881 
00882                                 // Set Internal code
00883                                 $this->grpt_id = $seq;
00884                                 /**
00885                                  * save also into quant_fin
00886                                  */
00887                                 $this->insert_quant_fin($fBank->id, $jr_id, $fPoste->id, ${"e_other$i" . "_amount"});
00888 
00889                                 if ($g_parameter->MY_ANALYTIC != "nu")
00890                                 {
00891                                         // for each item, insert into operation_analytique */
00892                                         $op = new Anc_Operation($this->db);
00893                                         $op->oa_group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
00894                                         $op->j_id = $j_id;
00895                                         $op->oa_date = $e_date;
00896                                         $op->oa_debit = ($amount < 0 ) ? 't' : 'f';
00897                                         $op->oa_description = sql_string($comment);
00898                                         $op->save_form_plan($_POST, $i, $j_id);
00899                                 }
00900 
00901 
00902                                 $this->update_internal_code($internal);
00903 
00904                                 $js_detail = HtmlInput::detail_op($jr_id, $internal);
00905                                 // Compute display
00906                                 $row = td($e_date).td($js_detail) . td(${"e_other$i"}) . td($fPoste->strAttribut(ATTR_DEF_NAME)) . td(${"e_other" . $i . "_comment"}) . td(nbm(${"e_other$i" . "_amount"}), 'class="num"');
00907 
00908                                 $ret.=tr($row);
00909 
00910 
00911                                 if ($i == 0)
00912                                 {
00913                                         // first record we upload the files and
00914                                         // keep variable to update other row of jrn
00915                                         if (isset($_FILES))
00916                                                 $oid = $this->db->save_upload_document($seq);
00917                                 }
00918                                 else
00919                                 {
00920                                         if ($oid != 0)
00921                                         {
00922                                                 $this->db->exec_sql("update jrn set jr_pj=" . $oid . ", jr_pj_name='" . $_FILES['pj']['name'] . "', " .
00923                                                                 "jr_pj_type='" . $_FILES['pj']['type'] . "'  where jr_grpt_id=$seq");
00924                                         }
00925                                 }
00926                         } // for nbitem
00927                         // increment pj
00928                         if (strlen(trim($e_pj)) != 0)
00929                         {
00930                                 $this->inc_seq_pj();
00931                         }
00932                         $ret.='</table>';
00933                 }
00934                 catch (Exception $e)
00935                 {
00936                         echo '<span class="error">' .
00937                         'Erreur dans l\'enregistrement ' .
00938                         __FILE__ . ':' . __LINE__ . ' ' .
00939                         $e->getMessage();
00940                         $this->db->rollback();
00941                         exit();
00942                 }
00943                 $this->db->commit();
00944                 $r = "";
00945                 $r.="<br>Ancien solde " . nbm($solde);
00946                 $new_solde = bcadd($new_solde, $amount);
00947                 $r.="<br>Nouveau solde " . nbm($new_solde);
00948                 $ret.=$r;
00949                 return $ret;
00950         }
00951 
00952         /* !\brief display operation of a FIN ledger
00953          * \return html code into a string
00954          */
00955 
00956         function show_ledger()
00957         {
00958                 global $g_user;
00959                 echo dossier::hidden();
00960                 $hid = new IHidden();
00961 
00962                 $hid->name = "p_action";
00963                 $hid->value = "bank";
00964                 echo $hid->input();
00965 
00966 
00967                 $hid->name = "sa";
00968                 $hid->value = "l";
00969                 echo $hid->input();
00970 
00971 
00972                 $w = new ISelect();
00973                 // filter on the current year
00974                 $filter_year = " where p_exercice='" . $g_user->get_exercice() . "'";
00975 
00976                 $periode_start = $this->db->make_array("select p_id,to_char(p_start,'DD-MM-YYYY') from parm_periode $filter_year order by p_start,p_end", 1);
00977                 // User is already set User=new User($this->db);
00978                 $current = (isset($_GET['p_periode'])) ? $_GET['p_periode'] : -1;
00979                 $w->selected = $current;
00980 
00981                 echo '<form>';
00982                 echo 'Période  ' . $w->input("p_periode", $periode_start);
00983                 $wLedger = $this->select_ledger('fin', 3);
00984 
00985                 if ($wLedger == null)
00986                         exit(_('Pas de journal disponible'));
00987 
00988                 if (count($wLedger->value) > 1)
00989                 {
00990                         $aValue = $wLedger->value;
00991                         $wLedger->value[0] = array('value' => -1, 'label' => _('Tous les journaux financiers'));
00992                         $idx = 1;
00993                         foreach ($aValue as $a)
00994                         {
00995                                 $wLedger->value[$idx] = $a;
00996                                 $idx++;
00997                         }
00998                 }
00999 
01000 
01001 
01002                 echo 'Journal ' . $wLedger->input();
01003                 $w = new ICard();
01004                 $w->noadd = 'no';
01005                 $w->jrn = $this->id;
01006                 $qcode = (isset($_GET['qcode'])) ? $_GET['qcode'] : "";
01007                 echo dossier::hidden();
01008                 echo HtmlInput::hidden('p_action', 'bank');
01009                 echo HtmlInput::hidden('sa', 'l');
01010                 $w->name = 'qcode';
01011                 $w->value = $qcode;
01012                 $w->label = '';
01013                 $this->type = 'FIN';
01014                 $all = $this->get_all_fiche_def();
01015                 $w->extra = $all;
01016                 $w->extra2 = 'QuickCode';
01017                 $sp = new ISpan();
01018                 echo $sp->input("qcode_label", "", $qcode);
01019                 echo $w->input();
01020 
01021                 echo HtmlInput::submit('gl_submit', _('Rechercher'));
01022                 echo '</form>';
01023 
01024                 // Show list of sell
01025                 // Date - date of payment - Customer - amount
01026                 if ($current != -1)
01027                 {
01028                         $filter_per = " and jr_tech_per=" . $current;
01029                 }
01030                 else
01031                 {
01032                         $filter_per = " and jr_tech_per in (select p_id from parm_periode where p_exercice::integer=" .
01033                                         $g_user->get_exercice() . ")";
01034                 }
01035                 /* security  */
01036                 if ($this->id != -1)
01037                         $available_ledger = " and jr_def_id= " . $this->id . " and " . $g_user->get_ledger_sql();
01038                 else
01039                         $available_ledger = " and " . $g_user->get_ledger_sql();
01040                 // Show list of sell
01041                 // Date - date of payment - Customer - amount
01042                 $sql = SQL_LIST_ALL_INVOICE . $filter_per . " and jr_def_type='FIN'" .
01043                                 " $available_ledger";
01044                 $step = $_SESSION['g_pagesize'];
01045                 $page = (isset($_GET['offset'])) ? $_GET['page'] : 1;
01046                 $offset = (isset($_GET['offset'])) ? $_GET['offset'] : 0;
01047 
01048                 $l = "";
01049 
01050                 // check if qcode contains something
01051                 if ($qcode != "")
01052                 {
01053                         // add a condition to filter on the quick code
01054                         $l = " and jr_grpt_id in (select j_grpt from jrnx where j_qcode=upper('$qcode')) ";
01055                 }
01056 
01057                 list($max_line, $list) = ListJrn($this->db, "where jrn_def_type='FIN' $filter_per $l $available_ledger "
01058                                 , null, $offset, 0);
01059                 $bar = navigation_bar($offset, $max_line, $step, $page);
01060 
01061                 echo "<hr> $bar";
01062                 echo $list;
01063                 echo "$bar <hr>";
01064         }
01065 
01066         /**
01067          * return a string with the bank account, name and quick_code
01068          */
01069         function get_bank_name()
01070         {
01071                 $this->bank_id = $this->db->get_value('select jrn_def_bank from jrn_def where jrn_def_id=$1', array($this->id));
01072                 $fBank = new Fiche($this->db, $this->bank_id);
01073                 $e_bank_account = " : " . $fBank->strAttribut(ATTR_DEF_BQ_NO);
01074                 $e_bank_name = " : " . $fBank->strAttribut(ATTR_DEF_NAME);
01075                 $e_bank_qcode = ": " . $fBank->strAttribut(ATTR_DEF_QUICKCODE);
01076                 return $e_bank_qcode . $e_bank_name . $e_bank_account;
01077         }
01078 
01079         /**
01080          * return the fiche_id of the bank
01081          */
01082         function get_bank()
01083         {
01084                 $bank_id = $this->db->get_value('select jrn_def_bank from jrn_def where jrn_def_id=$1', array($this->id));
01085                 return $bank_id;
01086         }
01087 
01088         /**
01089          * return true is we numbere each operation
01090          */
01091         function numb_operation()
01092         {
01093                 $a = $this->db->get_value('select jrn_def_num_op from jrn_def where jrn_def_id=$1', array($this->id));
01094                 if ($a == 1)
01095                         return true;
01096                 return false;
01097         }
01098 
01099         /**
01100          * insert into the quant_fin table
01101          * @param $bank_id is the f_id of the bank
01102          * @param $jr_id is the jrn.jr_id of the operation
01103          * @param $other is the f_id of the benefit
01104          * @param $amount is the amount
01105          */
01106         function insert_quant_fin($p_bankid, $p_jrid, $p_otherid, $p_amount)
01107         {
01108                 $sql = "INSERT INTO quant_fin(qf_bank, jr_id, qf_other, qf_amount)
01109                    VALUES ($1, $2, $3, $4);";
01110 
01111                 $this->db->exec_sql($sql, array($p_bankid, $p_jrid, $p_otherid, round($p_amount, 2)));
01112         }
01113 
01114 }