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