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