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