noalyss Version-9
acc_ledger_fin.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21// Copyright Author Dany De Bontridder danydb@aevalys.eu
22
23/**
24 * \file
25 * \brief the class Acc_Ledger_Fin inherits from Acc_Ledger, this
26 * object permit to manage the financial ledger
27 */
28 /**
29 * \class Acc_Ledger_Fin
30 * \brief the class Acc_Ledger_Fin inherits from Acc_Ledger, this
31 * object permit to manage the financial ledger
32 */
33require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
34
36{
37
38 function __construct($p_cn, $p_init)
39 {
40 parent::__construct($p_cn, $p_init);
41 $this->ledger_type='FIN';
42 }
43
44 /**
45 * @brief Verify that the data are correct before inserting or confirming
46 * @param an array (usually $_POST)
47 * @return String
48 * @throw Exception on error occurs
49 */
50 public function verify_operation($p_array)
51 {
52 global $g_user;
53 if (is_array($p_array)==false||empty($p_array))
54 throw new Exception("Array empty");
55 /*
56 * Check needed value
57 */
58 check_parameter($p_array, 'p_jrn');
59
60 extract($p_array, EXTR_SKIP);
61 /* check for a double reload */
62 if (isset($mt)&&$this->db->count_sql('select jr_mt from jrn where jr_mt=$1', array($mt))!=0)
63 throw new Exception(_('Double Encodage'), 5);
64
65 /* check if we can write into this ledger */
66 if ($g_user->check_jrn($p_jrn)!='W')
67 throw new Exception(_('Accès interdit'), 20);
68
69 /* check if there is a bank account linked to the ledger */
70 $bank_id=$this->get_bank();
71
72 if ($this->db->count()==0)
73 throw new Exception("Ce journal n'a pas de compte en banque, allez dans paramètre->journal pour régler cela");
74 /* check if the accounting of the bank is correct */
75 $fBank=new Fiche($this->db, $bank_id);
76 $bank_accounting=$fBank->strAttribut(ATTR_DEF_ACCOUNT);
77 if (trim($bank_accounting)=='')
78 throw new Exception('Le poste comptable du compte en banque de ce journal est invalide');
79
80 /* check if the account exists */
81 $poste=new Acc_Account_Ledger($this->db, $bank_accounting);
82 if ($poste->load()==false)
83 throw new Exception('Le poste comptable du compte en banque de ce journal est invalide');
84 if ($chdate!=1&&$chdate!=2)
85 throw new Exception('Le choix de date est invalide');
86 if ($chdate==1)
87 {
88 /* check if the date is valid */
89 if (isDate($e_date)==null)
90 {
91 throw new Exception('Date invalide', 2);
92 }
93 $oPeriode=new Periode($this->db);
94 if ($this->check_periode()==false)
95 {
96 $periode=$oPeriode->find_periode($e_date);
97 }
98 else
99 {
100 $oPeriode->p_id=$periode;
101 list ($min, $max)=$oPeriode->get_date_limit();
102 if (cmpDate($e_date, $min)<0||
103 cmpDate($e_date, $max)>0)
104 throw new Exception(_('Date et periode ne correspondent pas'), 6);
105 }
106
107 /* check if the periode is closed */
108 if ($this->is_closed($periode)==1)
109 {
110 throw new Exception(_('Periode fermee'), 6);
111 }
112
113 /* check if we are using the strict mode */
114 if ($this->check_strict()==true)
115 {
116 /* if we use the strict mode, we get the date of the last
117 operation */
118 $last_date=$this->get_last_date();
119 if ($last_date!=null&&cmpDate($e_date, $last_date)<0)
120 throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ')
121 .$last_date._(' vous ne pouvez pas encoder à une date antérieure'), 15);
122 }
123 }
124
125 $acc_pay=new Acc_Operation($this->db);
126
127 $nb=0;
128 $tot_amount=0;
129 //----------------------------------------
130 // foreach item
131 //----------------------------------------
132 for ($i=0; $i<$nb_item; $i++)
133 {
134 if (noalyss_strlentrim(${'e_other'.$i})==0)
135 continue;
136 /* check if amount are numeric and */
137 if (isNumber(${'e_other'.$i.'_amount'})==0)
138 throw new Exception('La fiche '.${'e_other'.$i}.'a un montant invalide ['.${'e_other'.$i.'_amount'}.']',
139 6);
140
141 /* compute the total */
142 $tot_amount+=round(${'e_other'.$i.'_amount'}, 2);
143 /* check if all card has a ATTR_DEF_ACCOUNT */
144 $fiche=new Fiche($this->db);
145 $fiche->get_by_qcode(${'e_other'.$i});
146 if ($fiche->empty_attribute(ATTR_DEF_ACCOUNT)==true)
147 throw new Exception('La fiche '.${'e_other'.$i}.'n\'a pas de poste comptable', 8);
148
149 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
150 // if 2 accounts, take only the debit one for customer
151 if (strpos($sposte, ',')!=0)
152 {
153 $array=explode(',', $sposte);
154 $poste_val=$array[1];
155 }
156 else
157 {
158 $poste_val=$sposte;
159 }
160 /* The account exists */
161 $poste=new Acc_Account_Ledger($this->db, $poste_val);
162 if ($poste->load()==false)
163 {
164 throw new Exception('Pour la fiche '.${'e_other'.$i}.' le poste comptable ['.$poste->id.'n\'existe pas',
165 9);
166 }
167 /* Check if the card belong to the ledger */
168 $fiche=new Fiche($this->db);
169 $fiche->get_by_qcode(${'e_other'.$i});
170 if ($fiche->belong_ledger($p_jrn, 'deb')!=1)
171 throw new Exception('La fiche '.${'e_other'.$i}.'n\'est pas accessible à ce journal', 10);
172 if ($chdate==2)
173 {
174 {/* check if the date is valid */
175 if (isDate(${'dateop'.$i})==null)
176 {
177 throw new Exception('Date invalide', 2);
178 }
179 $oPeriode=new Periode($this->db);
180 if ($this->check_periode()==false)
181 {
182 $periode=$oPeriode->find_periode(${'dateop'.$i});
183 }
184 else
185 {
186 $oPeriode->p_id=$periode;
187 list ($min, $max)=$oPeriode->get_date_limit();
188 if (cmpDate(${'dateop'.$i}, $min)<0||
189 cmpDate(${'dateop'.$i}, $max)>0)
190 throw new Exception(_('Date et periode ne correspondent pas'), 6);
191 }
192
193 /* check if the periode is closed */
194 if ($this->is_closed($periode)==1)
195 {
196 throw new Exception(_('Periode fermee'), 6);
197 }
198
199 /* check if we are using the strict mode */
200 if ($this->check_strict()==true)
201 {
202 /* if we use the strict mode, we get the date of the last
203 operation */
204 $last_date=$this->get_last_date();
205 if ($last_date!=null&&cmpDate(${'dateop'.$i}, $last_date)<0)
206 throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ')
207 .$last_date._(' vous ne pouvez pas encoder à une date antérieure'), 15);
208 }
209 }
210 }
211 $nb++;
212 }
213 if ($nb==0)
214 throw new Exception('Il n\'y a aucune opération', 12);
215
216 /* Check if the last_saldo and first_saldo are correct */
217 if (noalyss_strlentrim($last_sold)!=0&&isNumber($last_sold)&&noalyss_strlentrim($first_sold)!=0&&isNumber($first_sold)
218 )
219 {
220 $diff=bcsub($last_sold,$first_sold,2);
221 $diff=bcsub($diff,$tot_amount,2);
222 $calc=bcadd($first_sold, $tot_amount, 2);
223 if ($first_sold!=0||$last_sold!=0)
224 {
225 if ($diff!=0)
226 {
227 throw new Exception(sprintf(_('Le montant de l\'extrait est incorrect,'.
228 " solde donné [ %s ]".
229 " solde calculé [%s] , différence de [%s]"), $last_sold, $calc,
230 $diff), 13);
231 }
232 }
233 }
234 }
235
236 /**
237 * \brief display a form to enter an FIN operation
238 * \param $p_array contains the value usually it is $_POST
239 * \return string with html code
240 * \note the form tag are not set here
241 */
242
243 function input($p_array=null, $notused=0)
244 {
245 global $g_parameter, $g_user;
246 if ($p_array!=null)
247 extract($p_array, EXTR_SKIP);
248
249 $pview_only=false;
250
251 $min_article=$this->get_min_row();
252
253 $str_add_button_tiers="";
254 $add_card=FALSE;
255 if ($g_user->check_action(FICADD)==1)
256 {
257 $add_card=TRUE;
258 }
259
260 // The first day of the periode
261 $pPeriode=new Periode($this->db);
262 list ($l_date_start, $l_date_end)=$pPeriode->get_date_limit($g_user->get_periode());
263 if ($g_parameter->MY_DATE_SUGGEST=='Y')
264 $op_date=(!isset($e_date) )?$l_date_start:$e_date;
265 else
266 $op_date=(!isset($e_date) )?'':$e_date;
267
268 $r="";
269
270 $r.=dossier::hidden();
271 $f_legend='Banque, caisse';
272 // Date
273 //--
274 $Date=new IDate("e_date", $op_date);
275 $Date->setReadOnly($pview_only);
276 $f_date=$Date->input();
277 $f_period='';
278 if ($this->check_periode()==true)
279 {
280 // Periode
281 //--
282 $l_user_per=(isset($periode))?$periode:$g_user->get_periode();
283 $period=new IPeriod();
284 $period->cn=$this->db;
285 $period->type=OPEN;
286 $period->value=$l_user_per;
287 $period->user=$g_user;
288 $period->name='periode';
289 try
290 {
291 $l_form_per=$period->input();
292 }
293 catch (Exception $e)
294 {
295 if ($e->getCode()==1)
296 {
297 throw new Exception(_("Aucune période ouverte"));
298 }
299 }
301 $f_period="Période comptable $label".$l_form_per;
302 }
303
304 // Ledger (p_jrn)
305 //--
306 $onchange="update_bank();ajax_saldo('first_sold');update_name();update_row('fin_item');show_ledger_fin_currency();";
307
308 if ($g_parameter->MY_DATE_SUGGEST=='Y')
309 $onchange.='get_last_date();';
310 if ($g_parameter->MY_PJ_SUGGEST=='Y')
311 $onchange.='update_pj();';
312
313 $add_js='onchange="'.$onchange.'"';
314 $wLedger=$this->select_ledger('FIN', 2, FALSE);
315 if ($wLedger==null)
316 throw new Exception(_('Pas de journal disponible'));
317
318 $wLedger->javascript=$add_js;
319
320 $label_ledger=_("Journal")." ".Icon_Action::infobulle(2);
321
322 // retrieve bank name, code and account from the jrn_def.jrn_def_bank
323
324 $f_bank='<span id="bkname">'.$this->get_bank_name().'</span>';
325 if ($this->bank_id=="")
326 {
327 echo h2("Journal de banque non configuré ".$this->get_name(), ' class="error"');
328 echo '<span class="error"> vous devez donner à ce journal un compte en banque (fiche), modifiez dans CFGLED</span>';
329 alert("Journal de banque non configuré ".$this->get_name());
330 }
331
332 $f_legend_detail='Opérations financières';
333 //--------------------------------------------------
334 // Saldo begin end
335 //-------------------------------------------------
336 // Extrait
337 $default_pj='';
338 if ($g_parameter->MY_PJ_SUGGEST=='Y')
339 {
340 $default_pj=$this->guess_pj();
341 }
342 $wPJ=new IText('e_pj');
343 $wPJ->readonly=false;
344 $wPJ->size=10;
345 $wPJ->value=(isset($e_pj))?$e_pj:$default_pj;
346
347 $f_extrait=$wPJ->input().HtmlInput::hidden('e_pj_suggest', $default_pj);
349
350 $first_sold=(isset($first_sold))?$first_sold:"";
351 $wFirst=new INum('first_sold', $first_sold);
352
353 $last_sold=isset($last_sold)?$last_sold:"";
354 $wLast=new INum('last_sold', $last_sold);
355
356 $max=(isset($nb_item))?$nb_item:$min_article;
357
358 $r.=HtmlInput::hidden('nb_item', $max);
359 //--------------------------------------------------
360 // financial operation
361 //-------------------------------------------------
362
363 $array=array();
364 // Parse each " tiers"
365 for ($i=0; $i<$max; $i++)
366 {
367 $tiers=(isset(${"e_other".$i}))?${"e_other".$i}:"";
368
369 $tiers_amount=(isset(${"e_other$i"."_amount"}))?round(${"e_other$i"."_amount"}, 2):0;
370
371 $tiers_comment=(isset(${"e_other$i"."_comment"}))?${"e_other$i"."_comment"}:"";
372
373 $operation_date=new IDate("dateop".$i);
374 $operation_date->value=(isset(${'dateop'.$i}))?${'dateop'.$i}:"";
375 $array[$i]['dateop']=$operation_date->input();
376 ${"e_other$i"."_amount"}=(isset(${"e_other$i"."_amount"}))?${"e_other$i"."_amount"}:0;
377
378 $W1=new ICard();
379 $W1->label="";
380 $W1->name="e_other".$i;
381 $W1->id="e_other".$i;
382 $W1->value=$tiers;
383 $W1->extra='deb'; // credits
384 $W1->typecard='deb';
385 $W1->set_dblclick("fill_ipopcard(this);");
386 $W1->set_attribute('ipopup', 'ipopcard');
387
388 // name of the field to update with the name of the card
389 $W1->set_attribute('label', 'e_other_name'.$i);
390 // name of the field to update with the name of the card
391 $W1->set_attribute('typecard', 'filter');
392 // Add the callback function to filter the card on the jrn
393 $W1->set_callback('filter_card');
394 $W1->set_function('fill_data');
395 $W1->javascript=sprintf(' onchange="fill_data_onchange(\'%s\');" ', $W1->name);
396 $W1->readonly=$pview_only;
397 $array[$i]['qcode']=$W1->input();
398 $array[$i]['search']=$W1->search();
399 $array[$i]['card_add']=($add_card==TRUE)?$this->add_card("deb", $W1->id):"";
400 // Card name
401 //
402 $card_name="";
403 if ($tiers!="")
404 {
405 $fiche=new Fiche($this->db);
406 $fiche->get_by_qcode($tiers);
407 $card_name=$this->db->get_value("Select ad_value from fiche_detail where ad_id=$1 and f_id=$2",
408 array(ATTR_DEF_NAME, $fiche->id));
409 }
410
411 $wcard_name=new IText("e_other_name".$i, $card_name);
412 $wcard_name->id=$wcard_name->name;
413 $wcard_name->readOnly=true;
414 $array[$i]['cname']=$wcard_name->input();
415
416 // Comment
417 $wComment=new IText("e_other$i"."_comment", $tiers_comment);
418 $wComment->style='class="input_text label_item"';
419
420 $wComment->setReadOnly($pview_only);
421 $array[$i]['comment']=$wComment->input();
422 // amount
423 $wAmount=new INum("e_other$i"."_amount", $tiers_amount);
424
425 $wAmount->size=7;
426 $wAmount->setReadOnly($pview_only);
427 $array[$i]['amount']=$wAmount->input();
428 // concerned
429 ${"e_concerned".$i}=(isset(${"e_concerned".$i}))?${"e_concerned".$i}:""
430 ;
431 $wConcerned=new IConcerned("e_concerned".$i, ${"e_concerned".$i});
432 $wConcerned->tiers="e_other".$i;
433 $wConcerned->setReadOnly($pview_only);
434 $wConcerned->amount_id="e_other".$i."_amount";
435
436 $wConcerned->paid='paid';
437 $array[$i]['concerned']=$wConcerned->input();
438 }
439
440 ob_start();
441 require_once NOALYSS_TEMPLATE.'/form_ledger_fin.php';
442 $r.=ob_get_contents();
443 ob_end_clean();
444 $r.=create_script("$('".$Date->id."').focus()");
445
446 return $r;
447 }
448
449 /* * \brief show the summary before inserting into the database, it
450 * calls the function for adding a attachment. The function verify
451 * should be called before
452 * \param $p_array an array usually is $_POST
453 * \return string with code html
454 */
455
456 public function confirm($p_array, $p_nothing=0)
457 {
458 global $g_parameter, $g_user;
459 $r="";
460 if ( ! $this->get_is_loaded()) {
461 $this->load();
462 }
463 bcscale(2);
464 extract($p_array, EXTR_SKIP);
465 $pPeriode=new Periode($this->db);
466 if ($this->check_periode()==true)
467 {
468 $pPeriode->p_id=$periode;
469 }
470 else
471 {
472 if (isDate($e_date)!=null)
473 {
474 $pPeriode->find_periode($e_date);
475 }
476 else
477 {
478 $pPeriode->p_id=$g_user->get_periode();
479 }
480 }
481
482 list ($l_date_start, $l_date_end)=$pPeriode->get_date_limit();
483 $exercice=$pPeriode->get_exercice();
484 $r.='';
485 $r.='<fieldset><legend>'._("Banque, caisse").' </legend>';
486 $r.='<div id="jrn_name_div">';
487 $r.='<h1 id="jrn_name" style="display:inline">'.$this->get_name().'</h1>';
488 $r.='</div>';
489 $r.='<TABLE width="100%">';
490 // Date
491 //--
492 $r.="<tr>";
493 if ($chdate==1)
494 $r.='<td> Date : </td><td>'.$e_date;
495 // Periode
496 //--
497 $r.="<td>";
498 $r.="Période comptable </td><td>";
499 $r.=$l_date_start.' - '.$l_date_end;
500 $r.="</td>";
501 $r.="</tr>";
502 // Ledger (p_jrn)
503 //--
504 $r.='<tr>';
505 $r.='<td> Journal </td>';
506 $this->id=$p_jrn;
507 $r.='<td>';
508 $r.=h($this->get_name());
509 $r.='</td>';
510 $r.='</tr>';
511
512 //retrieve bank name
513 $bk_id=$this->get_bank();
514
515 $fBank=new Fiche($this->db, $bk_id);
516 $e_bank_account_label=$this->get_bank_name();
517
518 $filter_year=" j_tech_per in (select p_id from parm_periode where p_exercice='".$exercice."')";
519
520 $acc_account=new Acc_Account_Ledger($this->db, $fBank->strAttribut(ATTR_DEF_ACCOUNT));
521 $asolde=$acc_account->get_solde_detail($filter_year);
522 $deb=$asolde['debit'];
523 $cred=$asolde['credit'];
524 $solde=bcsub($deb, $cred);
525 $new_solde=$solde;
526
527 $r.="<TR><td colspan=\"4\"> Banque ";
528 $r.=$e_bank_account_label;
529
530 $r.="</TABLE>";
531
532 $r.='</fieldset>';
533
534 $r.='<div class="myfieldset"><h1 class="legend">'._("Extrait de compte").'</h1>';
535 //--------------------------------------------------
536 // Saldo begin end
537 //-------------------------------------------------
538 $r.='<table>';
539 $r.='<tr>';
540 // Extrait
541 //--
542 $r.=tr('<td>'._("Numéro d'extrait").' </td>'.td(h($e_pj)));
543 $r.='<tr><td >'._("Solde début extrait").' </td>';
544 $r.='<td style="num">'.nbm($first_sold).'</td></tr>';
545 $r.='<tr><td>'._("Solde fin extrait").' </td>';
546 $r.='<td style="num">'.nbm($last_sold).'</td></tr>';
547 $r.='</table>';
548
549 $r.='<h1 class="legend">Opérations financières</h1>';
550 //--------------------------------------------------
551 // financial operation
552 //-------------------------------------------------
553 $r.='<TABLE style="width:100%" id="fin_item">';
554 $r.="<TR>";
555 if ($chdate==2)
556 $r.='<th>'._("Date").'</th>';
557 $r.="<th style=\"width:auto;text-align:left\" colspan=\"2\">"._("Nom")."</th>";
558 $r.="<th style=\"text-align:left\" >"._("Commentaire")."</th>";
559 $r.="<th style=\"text-align:right\">"._("Montant")."</th>";
560 $r.='<th colspan="2">'._("Op. Concernée(s)").'</th>';
561
562 /* if we use the AC */
563 if ($g_parameter->MY_ANALYTIC!='nu')
564 {
565 $anc=new Anc_Plan($this->db);
566 $a_anc=$anc->get_list();
567 $x=count($a_anc);
568 /* set the width of the col */
569 $r.='<th colspan="'.$x.'">'._('Compt. Analytique').'</th>';
570
571 /* add hidden variables pa[] to hold the value of pa_id */
572 $r.=Anc_Plan::hidden($a_anc);
573 }
574 $r.="</TR>";
575 // Parse each " tiers"
576 $tot_amount=0;
577 //--------------------------------------------------
578 // For each items
579 //--------------------------------------------------
580 for ($i=0; $i<$nb_item; $i++)
581 {
582
583 $tiers=(isset(${"e_other".$i}))?${"e_other".$i}:""
584 ;
585
587 continue;
588 $tiers_label="";
589 $tiers_amount=round(${"e_other$i"."_amount"}, 2);
590 $tot_amount=bcadd($tot_amount, $tiers_amount);
591 $tiers_comment=h(${"e_other$i"."_comment"});
592 // If $tiers has a value
593 $fTiers=new Fiche($this->db);
594 $fTiers->get_by_qcode($tiers);
595
596 $tiers_label=$fTiers->strAttribut(ATTR_DEF_NAME);
597
598 $r.="<TR>";
599 if ($chdate==2)
600 $r.=td(${"dateop".$i});
601 $r.="<td>".${'e_other'.$i}."</TD>";
602 // label
603 $r.='<TD style="width:25%;border-bottom:1px dotted grey;">';
604 $r.=$fTiers->strAttribut(ATTR_DEF_NAME);
605 $r.='</td>';
606 // Comment
607 $r.='<td style="width:40%">'.$tiers_comment.'</td>';
608 // amount
609 $r.='<td class="num">'.nbm($tiers_amount).'</td>';
610 // concerned
611 $r.='<td style="text-align:center">';
612 if (${"e_concerned".$i}!='')
613 {
614 $jr_internal=$this->db->get_array("select jr_internal from jrn where jr_id in (".${"e_concerned".$i}.")");
615 $comma="";
616 for ($x=0; $x<count($jr_internal); $x++)
617 {
618 $r.=$comma.HtmlInput::detail_op(${"e_concerned".$i}, $jr_internal[$x]['jr_internal']);
619 $comma=" , ";
620 }
621 }
622 $r.='</td>';
623 // encode the pa
624 if ($g_parameter->MY_ANALYTIC!='nu'&&$g_parameter->match_analytic($fTiers->strAttribut(ATTR_DEF_ACCOUNT))==1) // use of AA
625 {
626 // show form
627 $anc_op=new Anc_Operation($this->db);
628 $null=($g_parameter->MY_ANALYTIC=='op')?1:0;
629 $r.='<td>';
630 $p_mode=1;
631 $p_array['pa_id']=$a_anc;
632 /* op is the operation it contains either a sequence or a jrnx.j_id */
633 $r.=HtmlInput::hidden('op[]=', $i);
634 $r.=$anc_op->display_form_plan($p_array, $null, $p_mode, $i, $tiers_amount);
635 $r.='</td>';
636 }
637
638 $r.='</TR>';
639 }
640 $r.="</TABLE>";
641 $acc_currency=$this->get_currency();
642
643 // If currency is not the default one
644 if ($acc_currency->get_id()!=0)
645 {
646
647 $solde=$fBank->get_bk_balance_currency();
648 $cur=$acc_currency->get_code();
649 $cur_rate=$acc_currency->get_rate_date($e_date);
650 $default_currency=new Acc_Currency($this->db, 0);
651 // saldo
652 $r.="<table>";
653 $r.=tr(
654 td(_("Ancien solde")).
655 td(nbm($solde).$cur, 'class="num"')
656 );
657 $new_solde=bcadd($solde??0, $tot_amount??0);
658 $r.=tr(
659 td(_("Nouveau solde")).
660 td(nbm($new_solde).$cur, ' class="num"')
661 );
662 $r.=tr(
663 td(_("Difference")).
664 td(nbm($tot_amount).$cur, ' class="num"')
665 );
666 $r.=tr(
667 td(_("Taux")).
668 td($cur_rate)
669 );
670 $r.=tr(
671 td(_("Nouveau solde")).
672 td(bcdiv($new_solde, $cur_rate).$default_currency->get_code(), ' class="num"')
673 );
674 $r.='</table>';
675 }
676 else
677 {
678
679 $cur=$acc_currency->get_code();
680 // saldo
681 $r.="<table>";
682 $r.=tr(
683 td(_("Ancien solde")).
684 td(nbm($solde).$cur, 'class="num"')
685 );
686 $new_solde=bcadd($solde, $tot_amount);
687 $r.=tr(
688 td(_("Nouveau solde")).
689 td(nbm($new_solde).$cur, ' class="num"')
690 );
691 $r.=tr(
692 td(_("Difference")).
693 td(nbm($tot_amount).$cur, ' class="num"')
694 );
695
696 $r.='</table>';
697 }
698 // check for upload piece
699 $file=new IFile();
700 $file->setAlertOnSize(true);
701 $r.="<br>"._("Ajoutez une pièce justificative")." ";
702 $r.=$file->input("pj", "");
703
704 $r.='</div>';
705 //--------------------------------------------------
706 // Hidden variables
707 //--------------------------------------------------
708 $r.=dossier::hidden();
709 $r.=HtmlInput::hidden('p_jrn', $this->id);
710 $r.=HtmlInput::hidden('nb_item', $nb_item);
711 $r.=HtmlInput::hidden('last_sold', $last_sold);
712 $r.=HtmlInput::hidden('first_sold', $first_sold);
713 $r.=HtmlInput::hidden('e_pj', $e_pj);
714 $r.=HtmlInput::hidden('e_pj_suggest', $e_pj_suggest);
715 $r.=HtmlInput::hidden('e_date', $e_date);
716 $mt=microtime(true);
717 $r.=HtmlInput::hidden('mt', $mt);
718
719 if (isset($periode))
720 $r.=HtmlInput::hidden('periode', $periode);
721 $r.=dossier::hidden();
722 $r.=HtmlInput::hidden('sa', 'n', 'chdate');
723 for ($i=0; $i<$nb_item; $i++)
724 {
725 $tiers=(isset(${"e_other".$i}))?${"e_other".$i}:"";
726 $r.=HtmlInput::hidden('e_other'.$i, $tiers);
727 $r.=HtmlInput::hidden('e_other'.$i, $tiers);
728 $r.=HtmlInput::hidden('e_other'.$i.'_comment', ${'e_other'.$i.'_comment'});
729 $r.=HtmlInput::hidden('e_other'.$i.'_amount', ${'e_other'.$i.'_amount'});
730 $r.=HtmlInput::hidden('e_concerned'.$i, ${'e_concerned'.$i});
731 $r.=HtmlInput::hidden('dateop'.$i, ${'dateop'.$i});
732 $r.=HtmlInput::hidden('chdate', $chdate);
733 }
734
735 return $r;
736 }
737
738 /* * \brief save the data into the database, included the attachment,
739 * and the reconciliations
740 * \param $p_array usually $_POST
741 * \return string with HTML code
742 */
743
744 public function insert($p_array=null)
745 {
746 global $g_parameter;
747
748 if ( ! $this->get_is_loaded()) {
749 $this->load();
750 }
751 bcscale(2);
752 $internal_code="";
753 $oid=0;
754 extract($p_array, EXTR_SKIP);
755 $ret='';
756 // Debit = banque
757 $bank_id=$this->get_bank();
758 $fBank=new Fiche($this->db, $bank_id);
759 $e_bank_account=$fBank->strAttribut(ATTR_DEF_QUICKCODE);
760 // Get the saldo
761 $pPeriode=new Periode($this->db);
762 $sposte=$fBank->strAttribut(ATTR_DEF_ACCOUNT);
763 // if 2 accounts, take only the debit one for customer
764 if (strpos($sposte, ',')!=0)
765 {
766 $array=explode(',', $sposte);
767 $poste_val=$array[0];
768 }
769 else
770 {
771 $poste_val=$sposte;
772 }
773
774 $acc_account=new Acc_Account_Ledger($this->db, $poste_val);
775
776 // If date = deposit date
777 if ($chdate==1)
778 {
779 if ($this->check_periode()==true)
780 {
781 $pPeriode->p_id=$periode;
782 }
783 else
784 {
785 $pPeriode->find_periode($e_date);
786 }
787 $exercice=$pPeriode->get_exercice();
788 $filter_year=" j_tech_per in (select p_id from parm_periode where p_exercice='".$exercice."')";
789 $asolde=$acc_account->get_solde_detail($filter_year);
790 $deb=$asolde['debit'];
791 $cred=$asolde['credit'];
792 $solde=bcsub($deb, $cred);
793 $new_solde=$solde;
794 }
795
796
797
798
799 try
800 {
801 $this->db->start();
802 $amount=0.0;
803 $idx_operation=0;
804 $ret='<table class="result" >';
805 $ret.=tr(th(_('Date')).th(_('n° interne')).th(_('Quick Code'))
806 .th(_('Nom')).th(_('Libellé')).th(_('Montant'), ' style="text-align:right"'));
807 // Credit = goods
808 $get_solde=true;
809
810 $acc_currency=new Acc_Currency($this->db, $this->currency_id);
811
812 // get the currency_rate when we have one date for all the operations
813 if ($chdate!=2)
814 {
815 $currency_rate=$acc_currency->get_rate_date($e_date);
816 }
817 if (DEBUGNOALYSS>1)
818 {
819 printf("<p> currency_id %s </p>",$this->currency_id);
820 printf("<p> rate %s </p>", $currency_rate);
821 }
822 // for each item
823 for ($i=0; $i<$nb_item; $i++)
824 {
825 // insert it into the database
826 // and quit the loop ?
827 if (noalyss_strlentrim(${"e_other$i"})==0)
828 continue;
829
830 // get the currency_rate when each operation has its own date
831 if ($chdate==2)
832 {
833 $e_date=${'dateop'.$i};
834 $currency_rate=$acc_currency->get_rate_date($e_date);
835 }
836 // if date is date of operation
837 if ($chdate==2&&$get_solde)
838 {
839 $get_solde=false;
840 if ($this->check_periode()==true&&isset($p_array['periode']))
841 {
842 $pPeriode->p_id=$periode;
843 }
844 else
845 {
846 $pPeriode->find_periode($e_date);
847 }
848 $exercice=$pPeriode->get_exercice();
849 $filter_year=" j_tech_per in (select p_id from parm_periode where p_exercice='".
850 sql_string($exercice)."')";
851 $solde=$acc_account->get_solde($filter_year);
852 $new_solde=$solde;
853 }
854 $fPoste=new Fiche($this->db);
855 $fPoste->get_by_qcode(${"e_other$i"});
856
857 // convert to EUR if needed and round it
858 $amount_input=${"e_other$i"."_amount"}=round(${"e_other$i"."_amount"}, 2);
859 $amount_eur=bcdiv($amount_input, $currency_rate);
860
861 $amount=bcadd($amount, $amount_input);
862
863 // Record a line for the bank
864 // Compute the j_grpt
865 $seq=$this->db->get_next_seq('s_grpt');
866
867 $acc_operation=new Acc_Operation($this->db);
868 $acc_operation->date=$e_date;
869 $sposte=$fPoste->strAttribut(ATTR_DEF_ACCOUNT);
870 // if 2 accounts
871 if (strpos($sposte, ',')!=0)
872 {
873 $array=explode(',', $sposte);
874 if (${"e_other$i"."_amount"}<0)
875 $poste_val=$array[1];
876 else
877 $poste_val=$array[0];
878 }
879 else
880 {
881 $poste_val=$sposte;
882 }
883
884
885 $acc_operation->poste=$poste_val;
886 $acc_operation->amount=bcmul($amount_eur, -1);
887 $acc_operation->grpt=$seq;
888 $acc_operation->jrn=$p_jrn;
889 $acc_operation->type='d';
890
891 if (isset($periode))
892 $tperiode=$periode;
893 else
894 {
895 $per=new Periode($this->db);
896 $tperiode=$per->find_periode($e_date);
897 }
898 $acc_operation->periode=$tperiode;
899 $acc_operation->qcode=${"e_other".$i};
900 $j_id_currency=$acc_operation->insert_jrnx();
901
902 // -- Insert into Operation Currency
903 $operation_currency=new Operation_currency_SQL($this->db);
904 $operation_currency->oc_amount=$amount_input;
905 $operation_currency->oc_vat_amount=0;
906 $operation_currency->oc_price_unit=0;
907 $operation_currency->j_id=$j_id_currency;
908 $operation_currency->insert();
909
910 $acc_operation=new Acc_Operation($this->db);
911 $acc_operation->date=$e_date;
912 $sposte=$fBank->strAttribut(ATTR_DEF_ACCOUNT);
913
914 // if 2 accounts, use the first one if DEB otherwise the second one
915 if (strpos($sposte, ',')!=0)
916 {
917 $array=explode(',', $sposte);
918 if (${"e_other$i"."_amount"}<0)
919 $poste_val=$array[1];
920 else
921 $poste_val=$array[0];
922 }
923 else
924 {
925 $poste_val=$sposte;
926 }
927
928 $acc_operation->poste=$poste_val;
929 $acc_operation->amount=$amount_eur;
930 $acc_operation->grpt=$seq;
931 $acc_operation->jrn=$p_jrn;
932 $acc_operation->type='d';
933 $acc_operation->periode=$tperiode;
934 $acc_operation->qcode=$e_bank_account;
935 $j_id=$acc_operation->insert_jrnx();
936
937 // -- Insert into Operation Currency
938 $operation_currency=new Operation_currency_SQL($this->db);
939 $operation_currency->oc_amount=$amount_input;
940 $operation_currency->oc_vat_amount=0;
941 $operation_currency->oc_price_unit=0;
942 $operation_currency->j_id=$j_id;
943 $operation_currency->insert();
944
945 if (sql_string(${"e_other$i"."_comment"})==null)
946 {
947 // if comment is blank set a default one
948 $comment=sprintf(_(" compte : %s a %s "), $fBank->strAttribut(ATTR_DEF_NAME),
949 $fPoste->strAttribut(ATTR_DEF_NAME)
950 );
951 }
952 else
953 {
954 $comment=strip_tags(${'e_other'.$i.'_comment'});
955 }
956
957
958 $acc_operation=new Acc_Operation($this->db);
959 $acc_operation->jrn=$p_jrn;
960 $acc_operation->amount=abs($amount_eur);
961 $acc_operation->date=$e_date;
962 $acc_operation->desc=$comment;
963 $acc_operation->grpt=$seq;
964 $acc_operation->periode=$tperiode;
965 $acc_operation->mt=$mt;
966 $idx_operation++;
967 $acc_operation->pj='';
968 $acc_operation->currency_id=$this->currency_id;
969 $acc_operation->currency_rate=$currency_rate;
970 $acc_operation->currency_rate_ref=$currency_rate;
971
972 if (trim($e_pj)!=''&&$this->numb_operation()==true)
973 $acc_operation->pj=$e_pj.str_pad($idx_operation, 3, 0, STR_PAD_LEFT);
974
975 if (trim($e_pj)!=''&&$this->numb_operation()==false)
976 $acc_operation->pj=$e_pj;
977
978 $jr_id=$acc_operation->insert_jrn();
979 // $acc_operation->set_pj();
980 $this->db->exec_sql('update jrn set jr_pj_number=$1 where jr_id=$2', array($acc_operation->pj, $jr_id));
981 $internal=$this->compute_internal_code($seq);
982
983 if (trim(${"e_concerned".$i})!="")
984 {
985 if (strpos(${"e_concerned".$i}, ',')!=0)
986 {
987 $aRapt=explode(',', ${"e_concerned".$i});
988 foreach ($aRapt as $rRapt)
989 {
990 // Add a "concerned operation to bound these op.together
991 //
992 $rec=new Acc_Reconciliation($this->db);
993 $rec->set_jr_id($jr_id);
994
995 if (isNumber($rRapt)==1)
996 {
997 $rec->insert($rRapt);
998 try
999 {
1000 $oppaid=new Acc_Operation($this->db);
1001 $oppaid->set_id($rRapt);
1002 $oppaid->set_paid();
1003 }
1004 catch (Exception $ex)
1005 {
1006 record_log($ex->getTraceAsString());
1007 echo _('Attention , erreur Acc_Ledger_Fin::insert , coche paiement');
1008 }
1009 }
1010 }
1011 }
1012 else
1013 if (isNumber(${"e_concerned".$i})==1)
1014 {
1015 $rec=new Acc_Reconciliation($this->db);
1016 $rec->set_jr_id($jr_id);
1017 $rec->insert(${"e_concerned$i"});
1018 try
1019 {
1020 $oppaid=new Acc_Operation($this->db);
1021 $oppaid->set_id(${"e_concerned".$i});
1022 $conc_amount=$oppaid->get_amount();
1023 if ($conc_amount==$acc_operation->amount)
1024 {
1025 $oppaid->set_paid();
1026 }
1027 }
1028 catch (Exception $ex)
1029 {
1030 record_log($ex->getTraceAsString());
1031 echo _('Attention , erreur Acc_Ledger_Fin::insert , coche paiement');
1032 }
1033 }
1034 }
1035
1036 // Set Internal code
1037 $this->grpt_id=$seq;
1038 /**
1039 * save also into quant_fin
1040 */
1041 $this->insert_quant_fin($fBank->id, $jr_id, $fPoste->id, $amount_eur, $j_id_currency);
1042
1043 if ($g_parameter->MY_ANALYTIC!="nu")
1044 {
1045 // for each item, insert into operation_analytique */
1046 $op=new Anc_Operation($this->db);
1047 $op->set_currency_rate($currency_rate);
1048 $op->oa_group=$this->db->get_next_seq("s_oa_group"); /* for analytic */
1049 $op->j_id=$j_id_currency;
1050 $op->oa_date=$e_date;
1051 $op->oa_debit='f';
1052 $op->oa_description=sql_string($comment);
1053 $op->save_form_plan($_POST, $i, $j_id_currency);
1054 }
1055
1056
1057 $this->update_internal_code($internal);
1058
1059 $js_detail=HtmlInput::detail_op($jr_id, $internal);
1060 // Compute display
1061 $row=td($e_date)
1062 .td($js_detail)
1063 .td(${"e_other$i"})
1064 .td($fPoste->strAttribut(ATTR_DEF_NAME))
1065 .td(${"e_other".$i."_comment"})
1066 .td(nbm(${"e_other$i"."_amount"}), 'class="num"');
1067 $class=($i%2==0)?' class="even" ':' class="odd" ';
1068 $ret.=tr($row, $class);
1069
1070 if ($i==0)
1071 {
1072 // first record we upload the files and
1073 // keep variable to update other row of jrn
1074 if (isset($_FILES['pj']))
1075 $oid=$this->db->save_receipt($seq);
1076 }
1077 else
1078 {
1079 if ($oid!=0)
1080 {
1081 $this->db->exec_sql("update jrn set jr_pj=$1 , jr_pj_name=$2,
1082 jr_pj_type=$3 where jr_grpt_id=$4",
1083 array($oid, $_FILES['pj']['name'], $_FILES['pj']['type'], $seq));
1084 }
1085 }
1086 } // for nbitem
1087 // increment pj
1088 if (noalyss_strlentrim($e_pj)!=0)
1089 {
1090 $this->inc_seq_pj();
1091 }
1092 $ret.='</table>';
1093 }
1094 catch (Exception $e)
1095 {
1096 $r='<span class="error">'.
1097 'Erreur dans l\'enregistrement '.
1098 __FILE__.':'.__LINE__.' '.
1099 $e->getMessage();
1100 $this->db->rollback();
1101 record_log($e);
1102 throw new Exception($r);
1103 }
1104 $this->db->commit();
1105 if ($acc_currency->get_id()==0)
1106 {
1107 $r="";
1108 $r.=sprintf("<br>"._("Ancien solde %s %s"), nbm($solde), $acc_currency->get_code());
1109 $new_solde=bcadd($new_solde, $amount);
1110 $r.=sprintf("<br>"._("Nouveau solde %s %s"), nbm($new_solde), $acc_currency->get_code());
1111 $ret.=$r;
1112 }
1113 else
1114 {
1115 $solde_cur=$fBank->get_bk_balance_currency();
1116 $r="";
1117 $r.=sprintf("<br>"._("Ancien solde %s %s"), nbm($solde_cur), $acc_currency->get_code());
1118 $new_solde=bcadd($solde_cur, $amount);
1119 $r.=sprintf("<br>"._("Nouveau solde %s %s"), nbm($new_solde), $acc_currency->get_code());
1120 $ret.=$r;
1121 }
1122 return $ret;
1123 }
1124
1125 /**
1126 * return a string with the bank account, name and quick_code
1127 */
1128 function get_bank_name()
1129 {
1130 $this->bank_id=$this->db->get_value('select jrn_def_bank from jrn_def where jrn_def_id=$1', array($this->id));
1131 $fBank=new Fiche($this->db, $this->bank_id);
1132 $e_bank_account=" : ".$fBank->strAttribut(ATTR_DEF_BQ_NO);
1133 $e_bank_name=" : ".$fBank->strAttribut(ATTR_DEF_NAME);
1134 $e_bank_qcode=": ".$fBank->strAttribut(ATTR_DEF_QUICKCODE);
1135 return $e_bank_qcode.$e_bank_name.$e_bank_account;
1136 }
1137
1138 /**
1139 * @brief find and FICHE.F_ID of the bank or -1 if not found
1140 */
1141 function get_bank()
1142 {
1143 $bank_id=$this->db->get_value('select jrn_def_bank from jrn_def where jrn_def_id=$1', array($this->id));
1144 if (empty($bank_id)) return -1;
1145 return $bank_id;
1146 }
1147
1148 /**
1149 * return true is we numbere each operation
1150 */
1152 {
1153 $a=$this->db->get_value('select jrn_def_num_op from jrn_def where jrn_def_id=$1', array($this->id));
1154 if ($a==1)
1155 return true;
1156 return false;
1157 }
1158
1159 /**
1160 * insert into the quant_fin table
1161 * @param integer $bank_id is the f_id of the bank
1162 * @param integer $jr_id is the jrn.jr_id of the operation
1163 * @param integer $other is the f_id of the benefit
1164 * @param integer $amount is the amount
1165 * @param integer $p_j_id is the j_id of the operation
1166 */
1167 function insert_quant_fin($p_bankid, $p_jrid, $p_otherid, $p_amount, $p_j_id_currency)
1168 {
1169 $sql="INSERT INTO quant_fin(qf_bank, jr_id, qf_other, qf_amount,j_id)
1170 VALUES ($1, $2, $3, $4,$5);";
1171
1172 $this->db->exec_sql($sql, array($p_bankid, $p_jrid, $p_otherid, round($p_amount, 2), $p_j_id_currency));
1173 }
1174
1175}
h2($p_string, $p_class="", $raw="")
Definition: ac_common.php:68
isNumber($p_int)
Definition: ac_common.php:215
th($p_string, $p_extra='', $raw='')
Definition: ac_common.php:58
isDate($p_date)
Definition: ac_common.php:236
noalyss_strlentrim($p_string)
Definition: ac_common.php:1549
tr($p_string, $p_extra='')
Definition: ac_common.php:88
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
sql_string($p_string)
Fix the problem with the quote char for the database.
Definition: ac_common.php:511
td($p_string='', $p_extra='')
surround the string with td
Definition: ac_common.php:83
nbm($p_number, $p_dec=2)
format the number with a sep.
Definition: ac_common.php:137
cmpDate($p_date, $p_date_oth)
Compare 2 dates.
Definition: ac_common.php:188
alert($p_msg, $buffer=false)
alert in javascript
Definition: ac_common.php:738
global $g_parameter
global $g_user
if no group available , then stop
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$op
Definition: ajax_admin.php:38
h( $row[ 'oa_description'])
catch(Exception $e) $exercice
$jr_id
Definition: ajax_ledger.php:44
$ex
Definition: balance.inc.php:45
$class
Manage the account from the table jrn, jrnx or tmp_pcmn.
display currency , convert to euro , and save them if used.
the class Acc_Ledger_Fin inherits from Acc_Ledger, this object permit to manage the financial ledger
input($p_array=null, $notused=0)
display a form to enter an FIN operation
verify_operation($p_array)
Verify that the data are correct before inserting or confirming.
insert($p_array=null)
get_bank_name()
return a string with the bank account, name and quick_code
__construct($p_cn, $p_init)
insert_quant_fin($p_bankid, $p_jrid, $p_otherid, $p_amount, $p_j_id_currency)
insert into the quant_fin table
numb_operation()
return true is we numbere each operation
get_bank()
find and FICHE.F_ID of the bank or -1 if not found
confirm($p_array, $p_nothing=0)
show the result of the array to confirm before inserting
$currency_id
!< default number of rows by default 10
get_currency()
returns the code iso of the default currency for this ledger
$nb
!< type of the ledger ACH ODS FIN VEN or GL
inc_seq_pj()
increment the sequence for the pj
select_ledger($p_type="ALL", $p_access=3, $enable=TRUE)
Show a select list of the ledgers you can access in writing, reading or simply accessing.
get_name()
Return the name of a ledger.
compute_internal_code($p_grpt)
compute the internal code of the saved operation and set the $this->jr_internal to the computed value
$row
!< database connextion
is_closed($p_periode)
check if the current ledger is closed
add_card($p_filter, $p_id_update)
Return a button to create new card, depending of the ledger.
update_internal_code($p_internal)
$db
!< jrn_def.jrn_def_id
get_last_date()
get the date of the last operation
check_periode()
Check if a Dossier is using the check on the periode, if true than the user has to enter the date and...
check_strict()
Check if a Dossier is using the strict mode or not.
guess_pj()
guess what the next pj should be
this file match the tables jrn & jrnx the purpose is to remove or save accountant writing to these ta...
new class for managing the reconciliation it must be used instead of the function InsertRapt,...
this class is used to show the form for entering an operation only FOR analytic operation to save it,...
Concerns the Analytic plan (table plan_analytique)
static hidden($p_array)
return an HTML string containing hidden input type to hold the differant PA_ID
define Class fiche and fiche def, those class are using class attribut. When adding or modifing new c...
Definition: fiche.class.php:38
static detail_op($p_jr_id, $p_mesg)
return a string containing the html code for calling the modifyOperation
setReadOnly($p_read)
static hidden($p_name, $p_value, $p_id="")
Input HTML for the card show buttons, in the file, you have to add card.js How to use :
Html Input.
Html Input : Input a date format dd.mm.yyyy The property title should be set to indicate what it is e...
Definition: idate.class.php:34
Html Input for uploading file, must be in a form with enctype="multipart/form-data".
Definition: ifile.class.php:31
This class handles only the numeric input, the input will call a javascript to change comma to period...
Definition: inum.class.php:42
Generate the form for the periode Data Members.
Html Input.
Definition: itext.class.php:30
static infobulle($p_comment)
Display a info in a bubble, text is in message_javascript.
ORM abstract of the table public.operation_currency.
For the periode tables parm_periode and jrn_periode.
if( $g_parameter->MY_PJ_SUGGEST=='Y') $e_date
const OPEN
Definition: constant.php:201
const ATTR_DEF_NAME
Definition: constant.php:216
const ATTR_DEF_BQ_NO
Definition: constant.php:217
const ATTR_DEF_QUICKCODE
Definition: constant.php:237
const ATTR_DEF_ACCOUNT
Definition: constant.php:215
const FICADD
$_POST['ac']
Definition: do.php:310
$oPeriode
Definition: do.php:154
if(sizeof($array)==0) $pPeriode
$SecUser db
create_script($p_string)
create the HTML for adding the script tags around of the script
check_parameter($p_array, $p_needed)
Check that all the index are in the array, used by function to check if the array contains the needed...
for($i=0;$i< $nb_jrn;$i++) $deb