noalyss Version-9
acc_ledger_purchase.class.php
Go to the documentation of this file.
1<?php
2/*
3 * This file is part of NOALYSS.
4 *
5 * NOALYSS is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * NOALYSS is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with NOALYSS; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18*/
19
20// Copyright Author Dany De Bontridder danydb@aevalys.eu
21
22/*!\file
23 * \brief class for the purchase, herits from acc_ledger
24 */
25require_once NOALYSS_INCLUDE.'/lib/user_common.php';
26require_once NOALYSS_INCLUDE.'/lib/ac_common.php';
27
28
29/*!\brief Handle the ledger of purchase,
30 *
31 *
32 */
34{
35 private $payment_operation; /*<! id of the payment , set in insert */
36 function __construct ($p_cn,$p_init)
37 {
38 $this->ledger_type='ACH';
39 parent::__construct($p_cn,$p_init);
40 $this->payment_operation=-1;
41 }
42 /*!\brief verify that the data are correct before inserting or confirming
43 *\param an array (usually $_POST)
44 *\return String
45 *\throw Exception if an error occurs
46 */
47 public function verify_operation($p_array)
48 {
49 global $g_parameter,$g_user;
50
51 if (is_array($p_array ) == false || empty($p_array))
52 throw new Exception ("Array empty");
53 /*
54 * Check needed value
55 */
56 check_parameter($p_array,'p_jrn,e_date,e_client');
57
58 extract ($p_array, EXTR_SKIP);
59 /* check if we can write into this ledger */
60 if ( $g_user->check_jrn($p_jrn) != 'W' )
61 throw new Exception (_('Accès interdit'),20);
62
63
64 /* check for a double reload */
65 if ( isset($mt) && $this->db->count_sql('select jr_mt from jrn where jr_mt=$1',array($mt)) != 0 )
66 throw new Exception (_('Double Encodage'),5);
67
68 /* check if there is a customer */
69 if ( noalyss_strlentrim($e_client)== 0 )
70 throw new Exception(_('Vous n\'avez pas donné de fournisseur'),11);
71
72 /* check if the date is valid */
73 if ( isDate($e_date) == null )
74 {
75 throw new Exception(_('Date invalide'), 2);
76 }
77 $oPeriode=new Periode($this->db);
78 if ( $this->check_periode() == false || ! isset($p_array['period']))
79 {
80 $tperiode=$oPeriode->find_periode($e_date);
81 }
82 else
83 {
84 $tperiode=$period;
85 $oPeriode->p_id=$tperiode;
86 /* check that the datum is in the choosen periode */
87 list ($min,$max)=$oPeriode->get_date_limit($tperiode);
88 if ( cmpDate($e_date,$min) < 0 ||
89 cmpDate($e_date,$max) > 0)
90 throw new Exception(_('Date et periode ne correspondent pas'),6);
91 }
92 /* check if the periode is closed */
93 if ( $this->is_closed($tperiode)==1 )
94 {
95 throw new Exception(_('Periode fermee'),6);
96 }
97
98 /* check if we are using the strict mode */
99 if( $this->check_strict() == true)
100 {
101 /* if we use the strict mode, we get the date of the last
102 operation */
103 $last_date=$this->get_last_date();
104 if ( $last_date != null && cmpDate($e_date,$last_date) < 0 )
105 throw new Exception(_('Vous utilisez le mode strict la dernière operation est à la date du ')
106 .$last_date._(' vous ne pouvez pas encoder à une '.
107 ' date antérieure dans ce journal'),13);
108
109 }
110
111 /* check the account */
112 $fiche=new Fiche($this->db);
113 $fiche->get_by_qcode($e_client);
114
115 if ( $fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
116 throw new Exception(_('La fiche ').$e_client._('n\'a pas de poste comptable'),8);
117
118 /* get the account and explode if necessary */
119 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
120 // if 2 accounts, take only the credit one for supplier
121 if ( strpos($sposte,',') != 0 )
122 {
123 $array=explode(',',$sposte);
124 $poste_val=$array[1];
125 }
126 else
127 {
128 $poste_val=$sposte;
129 }
130
131 /* The account exists */
132 $poste=new Acc_Account_Ledger($this->db,$poste_val);
133 if ( $poste->load() == false )
134 {
135 throw new Exception(_('Pour la fiche ').$e_client._(' le poste comptable [').$poste->id.'] '._('n\'existe pas'),9);
136 }
137 /* Check if the card belong to the ledger */
138 $fiche=new Fiche ($this->db);
139 $fiche->get_by_qcode($e_client,'cred');
140 if ( $fiche->belong_ledger($p_jrn) !=1 )
141 throw new Exception(_('La fiche ').$e_client._('n\'est pas accessible à ce journal'),10);
142
143 $nb=0;
144 //------------------------------------------------------
145 // The "Paid By" check
146 //------------------------------------------------------
147 if ($e_mp != 0 ) {
148 $this->check_payment($e_mp,${"e_mp_qcode_".$e_mp});
149 // check for the currency , if we use a financial ledger and a card which is a bank account (with his own
150 // ledger , then the currency of the operation must be the same
151 $this->check_currency(${"e_mp_qcode_" . $e_mp},$p_currency_code);
152 }
153
154
155 //----------------------------------------
156 // foreach item
157 //----------------------------------------
158 for ($i=0;$i< $nb_item;$i++)
159 {
160 if ( noalyss_strlentrim(${'e_march'.$i})== 0) continue;
161 /* check if amount are numeric and */
162 if ( isNumber(${'e_march'.$i.'_price'}) == 0 )
163 throw new Exception(_('La fiche ').${'e_march'.$i}._('a un montant invalide').' ['.${'e_march'.$i}.']',6);
164 if ( isNumber(${'e_quant'.$i}) == 0 )
165 throw new Exception(_('La fiche ').${'e_march'.$i}._('a une quantité invalide').' ['.${'e_quant'.$i}.']',7);
166
167 // Check if the given tva id is valid
168 if ( $g_parameter->MY_TVA_USE=='Y')
169 {
170 if ( empty(${'e_march'.$i.'_tva_id'}) )
171 throw new Exception(_('La fiche ').${'e_march'.$i}._('a un code tva invalide').' ['.${'e_march'.$i.'_tva_id'}.']',13);
172 $tva_rate=new Acc_Tva($this->db);
173 $tva_rate->set_parameter('id',${'e_march'.$i.'_tva_id'});
174
175 if ( $tva_rate->load() != 0 )
176 throw new Exception(_('La fiche ').${'e_march'.$i}._('a un code tva invalide').' ['.${'e_march'.$i.'_tva_id'}.']',13);
177 /*
178 * check if the accounting for VAT are valid
179 */
180 $a_poste=explode(',',$tva_rate->tva_poste);
181
182 if (
183 $this->db->get_value('select count(*) from tmp_pcmn where pcm_val=$1',array($a_poste[0])) == 0 )
184 throw new Exception(_(" La TVA ".$tva_rate->tva_label." utilise des postes comptables inexistants"));
185
186 }
187 /* check if all card has a ATTR_DEF_ACCOUNT*/
188 $fiche=new Fiche($this->db);
189 $fiche->get_by_qcode(${'e_march'.$i});
190 if ( $fiche->empty_attribute(ATTR_DEF_ACCOUNT) == true)
191 throw new Exception(_('La fiche ').${'e_march'.$i}._('n\'a pas de poste comptable'),8);
192
193 /* get the account and explode if necessary */
194 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
195 // if 2 accounts, take only the debit
196 if ( strpos($sposte,',') != 0 )
197 {
198 $array=explode(',',$sposte);
199 $poste_val=$array[0];
200 }
201 else
202 {
203 $poste_val=$sposte;
204 }
205
206 /* The account exists */
207 $poste=new Acc_Account_Ledger($this->db,$poste_val);
208 if ( $poste->load() == false )
209 {
210 throw new Exception(_('Pour la fiche ').${'e_march'.$i}._(' le poste comptable').' ['.$poste->id._('n\'existe pas'),9);
211 }
212 /* Check if the card belong to the ledger */
213 $fiche=new Fiche ($this->db);
214 $fiche->get_by_qcode(${'e_march'.$i});
215 if ( $fiche->belong_ledger($p_jrn,'deb') !=1 )
216 throw new Exception(_('La fiche ').${'e_march'.$i}._('n\'est pas accessible à ce journal'),10);
217 /**
218 * we have to check also if the different accountings exist
219 "ATTR_DEF_DEP_PRIV"
220 "ATTR_DEF_DEPENSE_NON_DEDUCTIBLE"
221 "ATTR_DEF_TVA_NON_DEDUCTIBLE"
222 "ATTR_DEF_TVA_NON_DEDUCTIBLE_RECUP"
223 */
224 foreach (array(
229 {
230 if ( ! $fiche->empty_attribute($key[0]) && $fiche->empty_attribute($key[2]))
231 {
232 $a=new Acc_Parm_Code($this->db,$key[1]);
233 if ( $this->db->count_sql('select pcm_val from tmp_pcmn where pcm_val=$1',array($a->p_value))==0)
234 throw new Exception ($key[1]._("ce code n'a pas de poste comptable, créez ce poste : [".$a->p_value."]"));
235 }
236 if ( ! $fiche->empty_attribute($key[0]) && ! $fiche->empty_attribute($key[2]))
237 {
238 $nd_str=$fiche->strAttribut($key[2]);
239 if ( $nd_str != '')
240 {
241 $poste_nd=new Acc_Account_Ledger($this->db,$nd_str);
242 if ( $poste_nd->load() == false)
243 {
244 $nd_msg=sprintf(_("Pour la fiche %s, le compte contrepartie %s n'existe pas"),
245 $fiche->getName(),$poste_nd->id);
246 $nd_msg=h($nd_msg);
247 throw new Exception ($nd_msg);
248 }
249 }
250 }
251 }
252 if ( ${"e_quant".$i} != 0 && trim(${"e_quant".$i}) !="" ) {$nb++;}
253 }
254
255 if ( $nb == 0 )
256 throw new Exception(_('Il n\'y a aucune marchandise'),12);
257
258 // check payment date
259 if ( isset ($mp_date) && trim ($mp_date) != "" && isDate($mp_date) == null) {
260 throw new Exception(_('Date de paiement invalide'),13);
261
262 }
263 // check that MP is in a not closed and exists
264 if ( isset ($mp_date) && trim ($mp_date) != "" && isDate($mp_date) == $mp_date ) {
265 $periode=new Periode($this->cn);
266 $periode->find_periode($mp_date);
267 $periode->set_ledger($this->id);
268 if ( $periode->is_closed() ) {
269 throw new Exception(_("Période fermée")." $mp_date ");
270 }
271
272 }
273 // check limit date
274 if ( isset ($e_ech) && trim ($e_ech)!="" && isDate($e_ech) == null )
275 {
276 throw new Exception(_('Date échéance invalide'),14);
277
278 }
279 // Check currency_rate if valid
280 if ( isNumber($p_currency_rate) == 0 || $p_currency_rate <=0 ) {
281 throw new Exception(_('Taux devise invalide'),15);
282 }
283 $this->check_currency_setting($p_currency_code);
284 }
285 /**
286 * Compute the ND amount thanks the attribute of the concerned card. The object
287 * $p_nd_amount will changed
288 *
289 * @param Acc_Compute $p_nd_amount object with ND amount
290 * @param Fiche $p_fiche Concerned Card (purchase items)
291 * @param type $p_tva_bot 0 TVA on one side, 1 TVA on both side
292 */
293 private function compute_no_deductible(Acc_Compute $p_nd_amount, Fiche $p_fiche)
294 {
296 {
297 $p_nd_amount->amount_nd_rate = $p_fiche->strAttribut(ATTR_DEF_DEPENSE_NON_DEDUCTIBLE);
298 $p_nd_amount->compute_nd();
299 }
301 {
302 $p_nd_amount->nd_vat_rate = $p_fiche->strAttribut(ATTR_DEF_TVA_NON_DEDUCTIBLE);
303 $p_nd_amount->compute_nd_vat();
304 }
306 {
307 $p_nd_amount->nd_ded_vat_rate = $p_fiche->strAttribut(ATTR_DEF_TVA_NON_DEDUCTIBLE_RECUP);
308 $p_nd_amount->compute_ndded_vat();
309 }
310
311 if (!$p_fiche->empty_attribute(ATTR_DEF_DEP_PRIV))
312 {
313 $p_nd_amount->amount_perso_rate = $p_fiche->strAttribut(ATTR_DEF_DEP_PRIV);
314 $p_nd_amount->compute_perso();
315 }
316
317 }
318
319 /**
320 * Insert into JRNX the No Deductible amount and into Analytic Accountancy for the ND VAT
321 * @param Acc_Compute $p_nd_amount content ND amount
322 * @param Fiche $p_fiche Card of the Service
323 * @param type $p_tva_both 0 if TVA is normal or 1 if on both side
324 * @param type $p_tot_debit total debit
325 * @param $p_acc_operation Acc_Operation for inserting into jrnx
326 * @param $p_group group for AC
327 * @param $idx row number
328 *
329 * @see Acc_Ledger_Purchase::insert
330 */
331 private function insert_no_deductible(Acc_Compute $p_nd_amount, Fiche $p_fiche, $p_tva_both,&$p_tot_debit,Acc_Operation $p_acc_operation,$p_group,$idx)
332 {
333 global $g_parameter;
334 if ($p_acc_operation->jrnx_id == 0) {
335 throw new Exception(__FILE__.__LINE__.'invalid acc_operation.j_id');
336 }
337 $source_j_id=$p_acc_operation->jrnx_id ;
338 /*
339 * Save all the no deductible
340 * ATTR_DEF_ACCOUNT_ND_TVA,ATTR_DEF_ACCOUNT_ND_TVA_ND,ATTR_DEF_ACCOUNT_ND_PERSO,ATTR_DEF_ACCOUNT_ND
341 */
342 if ($p_nd_amount->amount_nd_rate != 0)
343 {
344 $dna_default = new Acc_Parm_Code($this->db, 'DNA');
345
346 /* save op. */
347 if (!$p_fiche->empty_attribute(ATTR_DEF_ACCOUNT_ND))
348 {
349 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND);
350 } else
351 {
352 $dna = $dna_default->p_value;
353 }
354 $dna = ($dna == '') ? $dna_default->p_value : $dna;
355
356 $p_acc_operation->type = 'd';
357 $p_acc_operation->amount = $p_nd_amount->amount_nd;
358 $p_acc_operation->poste = $dna;
359 $p_acc_operation->qcode = '';
360 $p_acc_operation->desc=$this->find_label($dna)." ND ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
361 if ($p_nd_amount->amount_nd > 0)
362 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->amount_nd );
363 $j_id = $p_acc_operation->insert_jrnx();
364 }
365 /*
366 * ATTR_DEF_ACCOUNT_ND_PERSO
367 */
368 if ($p_nd_amount->amount_perso != 0)
369 {
370 $dna_default = new Acc_Parm_Code($this->db, 'DEP_PRIV');
371
372 /* save op. */
373 $p_acc_operation->type = 'd';
375 {
376 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_PERSO);
377 } else
378 {
379 $dna = $dna_default->p_value;
380 }
381 $dna = ($dna == '') ? $dna_default->p_value : $dna;
382
383 $p_acc_operation->amount = $p_nd_amount->amount_perso ;
384 $p_acc_operation->poste = $dna;
385 $p_acc_operation->qcode = '';
386 $p_acc_operation->desc=$this->find_label($dna)." ND_PRIV ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
387 if ($p_nd_amount->amount_perso> 0)
388 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->amount_perso);
389 $j_id = $p_acc_operation->insert_jrnx();
390 }
391 if ($p_nd_amount->nd_vat != 0)
392 {
393 $dna_default = new Acc_Parm_Code($this->db, 'TVA_DNA');
394
395 /* save op. */
396 $p_acc_operation->type = 'd';
397 $p_acc_operation->qcode = '';
399 {
400 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_TVA_ND);
401 } else
402 {
403 $dna = $dna_default->p_value;
404 }
405 $dna = ($dna == '') ? $dna_default->p_value : $dna;
406
407 $p_acc_operation->amount = $p_nd_amount->nd_vat;
408 $p_acc_operation->poste = $dna;
409 $p_acc_operation->desc=$this->find_label($dna)." ND_TVA ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
410 $j_id = $p_acc_operation->insert_jrnx();
411 if ( $g_parameter->MY_ANALYTIC != "nu"
412 && $g_parameter->match_analytic($p_fiche->strAttribut(ATTR_DEF_ACCOUNT))
413 )
414 {
415 $op=new Anc_Operation($this->db);
416 $op->oa_group=$p_group;
417 $op->j_id=$j_id;
418 $op->oa_date=$p_acc_operation->date;
419
420 $op->oa_debit='t';
421 $op->oa_description=sql_string('ND_TVA');
422 $op->oa_jrnx_id_source=$source_j_id;
423 $op->save_form_plan_vat_nd($_POST,$idx,$j_id,$p_nd_amount->nd_vat,$p_acc_operation->jrnx_id);
424 }
425 if ($p_nd_amount->nd_vat> 0)
426 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->nd_vat);
427
428 }
429 if ($p_nd_amount->nd_ded_vat != 0)
430 {
431 $dna_default = new Acc_Parm_Code($this->db, 'TVA_DED_IMPOT');
432 /* save op. */
434 {
435 $dna = $p_fiche->strAttribut(ATTR_DEF_ACCOUNT_ND_TVA);
436 } else
437 {
438 $dna = $dna_default->p_value;
439 }
440 $dna = ($dna == '') ? $dna_default->value : $dna;
441
442
443
444 $p_acc_operation->type = 'd';
445 $p_acc_operation->qcode = '';
446 $p_acc_operation->amount = $p_nd_amount->nd_ded_vat;
447 $p_acc_operation->poste = $dna;
448 $p_acc_operation->desc=$this->find_label($dna)." DED_TVA ".$p_fiche->strAttribut(ATTR_DEF_QUICKCODE);
449 if ($p_nd_amount->nd_ded_vat > 0)
450 $p_tot_debit = bcadd($p_tot_debit, $p_nd_amount->nd_ded_vat);
451 $j_id = $p_acc_operation->insert_jrnx();
452 if ( $g_parameter->MY_ANALYTIC != "nu"
453 && $g_parameter->match_analytic($p_fiche->strAttribut(ATTR_DEF_ACCOUNT))
454 )
455 {
456 $op=new Anc_Operation($this->db);
457 $op->oa_group=$p_group;
458 $op->j_id=$j_id;
459 $op->oa_date=$p_acc_operation->date;
460
461 $op->oa_debit='t';
462 $op->oa_description=sql_string('DED_TVA ');
463 $op->oa_jrnx_id_source=$source_j_id;
464 $op->save_form_plan_vat_nd($_POST,$idx,$j_id,$p_nd_amount->nd_ded_vat);
465 }
466 }
467 }
468
469 /*!\brief insert into the database, it calls first the verify function
470 * change the value of this->jr_id and this->jr_internal.
471 * It generates the document and save the middle of payment, if 'gen_invoice is set
472 * and e_mp
473 *\param $p_array is usually $_POST or a predefined operation
474 \code
475 Array
476 (
477
478 [e_client] =>BELGACOM
479 [nb_item] =>9
480 [p_jrn] =>3
481 [period] =>117
482 [e_comm] =>Frais de téléphone
483 [e_date] =>01.09.2009
484 [e_ech] =>
485 [jrn_type] =>ACH
486 [e_pj] =>ACH53
487 [e_pj_suggest] =>ACH53
488 [mt] =>1265318941.39
489 [e_mp] =>0
490 [e_march0] =>TEL
491 [e_march0_price] =>63.6700
492 [e_march0_tva_id] =>1
493 [e_march0_tva_amount] =>13.3700
494 [e_quant0] =>1.000
495 ...
496 [bon_comm] =>
497 [other_info] =>
498 [record] =>Enregistrement
499 [p_currency_code]=> id currency
500 [p_currency_rate]=>rate used
501 )
502 \endcode
503 *\return string
504 *\note throw an Exception
505 */
506 public function insert($p_array=null)
507 {
508 global $g_parameter,$g_user;
509 extract ($p_array, EXTR_SKIP);
510 $this->verify($p_array) ;
511 if ( !isset($p_array['jrn_note_input'])) {$p_array['jrn_note_input']='';}
512 $group=$this->db->get_next_seq("s_oa_group"); /* for analytic */
513 $seq=$this->db->get_next_seq('s_grpt');
514 $this->id=$p_jrn;
515
516 $internal=$this->compute_internal_code($seq);
517 $this->internal=$internal;
518
519 $cust=new Fiche($this->db);
520 $cust->get_by_qcode($e_client);
521 $sposte=$cust->strAttribut(ATTR_DEF_ACCOUNT);
522 // if 2 accounts, take only the credit Supplier
523 if ( strpos($sposte,',') != 0 )
524 {
525 $array=explode(',',$sposte);
526 $poste=$array[1];
527 }
528 else
529 {
530 $poste=$sposte;
531 }
532
533 $oPeriode=new Periode($this->db);
535
536 if ( $check_periode == true && isset($p_array['period']) )
537 $tperiode=$period;
538 else
539 $tperiode=$oPeriode->find_periode($e_date);
540
541
542 try
543 {
544 bcscale(4);
545 // total amount of the purchase
546 $tot_amount=0;
547 $tot_tva=0;
548 $tot_debit=0;
549 $this->db->start();
550 $tot_nd=0;
551 $tot_perso=0;
552 $tot_tva_nd=0;
553 $tot_tva_ndded=0;
554 $tot_tva_reversed=0;
555 $tva=array();
556 $tot_amount_cur=0;
557 // find the currency from v_currency_last_value
558 $currency_rate_ref=new Acc_Currency($this->db, $p_currency_code);
559
560 /* Save all the items without vat and no deductible vat and expense*/
561 for ($i=0;$i< $nb_item;$i++)
562 {
563 if ( empty(${'e_march'.$i}) || empty(${'e_quant'.$i}) ) continue;
564
565 /* First we save all the items without vat */
566 $fiche=new Fiche($this->db);
567 $fiche->get_by_qcode(${"e_march".$i});
568 $tva_both=0;
569 /* tva */
570 if ($g_parameter->MY_TVA_USE=='Y')
571 {
572 $idx_tva=trim(${'e_march'.$i.'_tva_id'});
573 \Noalyss\Dbg::echo_var(1," idx_tva [$idx_tva]",);
574 $oTva=new Acc_Tva($this->db);
575 $oTva->set_parameter('id',$idx_tva);
576 $oTva->load();
577 $tva_both=$oTva->get_parameter("both_side");
578 }
579 /* -- Create acc_operation -- */
580 $acc_operation=new Acc_Operation($this->db);
581 $acc_operation->date=$e_date;
582 $acc_operation->grpt=$seq;
583 $acc_operation->jrn=$p_jrn;
584 $acc_operation->type='d';
585 $acc_operation->periode=$tperiode;
586 $acc_operation->qcode="";
587 $amount_4=bcmul(${'e_march'.$i.'_price'},${'e_quant'.$i});
588
589 /* We have to compute all the amount thanks Acc_Compute */
590
591 $acc_amount=new Acc_Compute();
592 $acc_amount->check=false;
593 $acc_amount->set_parameter('amount',$amount_4);
594 // Set the currency rate
595 $acc_amount->set_parameter("currency_rate", $p_currency_rate);
596 $acc_amount->convert_euro();
597 $amount_euro=$acc_amount->amount;
598
599 // Compute VAT or take the given one
600 if ( $g_parameter->MY_TVA_USE=='Y')
601 {
602 $acc_amount->set_parameter('amount_vat_rate',$oTva->get_parameter('rate'));
603 if ( noalyss_strlentrim(${'e_march'.$i.'_tva_amount'}) ==0 || ${'e_march'.$i.'_tva_amount'} == 0)
604 {
605 // vat must computed and the amount is already converted to EUR
606 $acc_amount->compute_vat();
607
608 }
609 else
610 {
611 // we convert the vat in euro
612 $acc_amount->set_parameter("amount_vat", ${'e_march'.$i.'_tva_amount'});
613 $acc_amount->convert_euro_vat();
614
615 }
616 // convert amount in eur
617 $tot_tva=bcadd($tot_tva,$acc_amount->amount_vat);
618 $tot_tva=round($tot_tva,2);
619 }
620
621
622 /* compute ND */
623 $save_amount_vat=$acc_amount->amount_vat;
624 $this->compute_no_deductible($acc_amount, $fiche);
625 $acc_amount->correct();
626 // TVA which avoid
627 $acc_amount->amount_unpaid=($tva_both == 1 ) ? $save_amount_vat :0 ;
628 $tot_tva_reversed=bcadd($tot_tva_reversed,$acc_amount->amount_unpaid);
629
630
631
632 $tot_amount=round(bcadd($tot_amount,$acc_amount->amount),2);
633 $tot_amount=round(bcadd($tot_amount,$acc_amount->amount_nd),2);
634 $tot_amount=round(bcadd($tot_amount,$acc_amount->amount_perso),2);
635
636 /* get the account and explode if necessary */
637 $sposte=$fiche->strAttribut(ATTR_DEF_ACCOUNT);
638 // if 2 accounts, take only the debit one for customer
639 if ( strpos($sposte,',') != 0 )
640 {
641 $array=explode(',',$sposte);
642 $poste_val=$array[0];
643 }
644 else
645 {
646 $poste_val=$sposte;
647 }
648 if ($g_parameter->MY_UPDLAB=='Y')
649 {
650 $acc_operation->desc=strip_tags(${"e_march".$i."_label"});
651 }
652 else
653 {
654 $acc_operation->desc=null;
655 }
656 $acc_operation->poste=$poste_val;
657 $acc_operation->amount=$acc_amount->amount;
658 $acc_operation->qcode=${"e_march".$i};
659 if ($acc_amount->amount>0)
660 {
661 $tot_debit=bcadd($tot_debit, $acc_amount->amount);
662 }
663 $j_id=$acc_operation->insert_jrnx();
664
665 /* insert ND */
666 $this->insert_no_deductible($acc_amount, $fiche, $tva_both, $tot_debit,$acc_operation,$group,$i);
667
668
669 /* Compute sum vat */
670 if ( $g_parameter->MY_TVA_USE=='Y')
671 {
672 $tva_item=$acc_amount->amount_vat;
673
674 if (isset($tva[$idx_tva]))
675 {
676 $tva[$idx_tva]=bcadd($tva[$idx_tva], $tva_item);
677 }
678 else
679 $tva[$idx_tva]=$tva_item;
680 }
681 /* Save the stock */
682 /* if the quantity is < 0 then the stock increase (return of
683 * material)
684 */
685 $nNeg=(${"e_quant" . $i}< 0) ? -1 : 1;
686
687 // always save quantity but in withStock we can find
688 // what card need a stock management
689 if ( $g_parameter->MY_STOCK='Y'&& isset ($repo))
690 {
691 $dir=(${'e_quant'.$i} < 0 ) ? 'c':'d';
692 Stock_Goods::insert_goods($this->db,array('j_id'=>$j_id,'goods'=>${'e_march'.$i},'quant'=>$nNeg*${'e_quant'.$i},'dir'=>$dir,'repo'=>$repo)) ;
693 }
694
695 if ( $g_parameter->MY_ANALYTIC != "nu" && $g_parameter->match_analytic($poste_val))
696 {
697 // for each item, insert into operation_analytique */
698 $op=new Anc_Operation($this->db);
699 $op->set_currency_rate($p_currency_rate);
700 $op->oa_group=$group;
701 $op->j_id=$j_id;
702 $op->oa_date=$e_date;
703 $op->oa_debit='t';
704 $op->oa_description=sql_string($e_comm);
705 $op->save_form_plan($p_array,$i,$j_id);
706 }
707 // insert into quant_purchase
708 //-----
709
710 if (empty( ${'e_march' . $i . '_price'} ) ) ${'e_march' . $i . '_price'} = 0;
711 if (empty( ${'e_march' . $i } ) ) ${'e_march' . $i } = 0;
712 if (empty( ${'e_quant' . $i } ) ) ${'e_quant' . $i } = 0;
713 $price_euro=bcdiv(${'e_march'.$i.'_price'}, $p_currency_rate);
714
715 if ( $g_parameter->MY_TVA_USE=='Y')
716 {
717
718 $r=$this->db->exec_sql("select insert_quant_purchase ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14)",
719 array(
720 null /*1*/
721 ,$j_id /* 2 */
722 ,${"e_march".$i} /* 3 */
723 ,${"e_quant".$i} /* 4 */
724 ,round($amount_euro,2) /* 5 */
725 ,$acc_amount->amount_vat /* 6 */
726 ,$oTva->get_parameter('id') /* 7 */
727 ,$acc_amount->amount_nd /* 8 */
728 ,$acc_amount->nd_vat /* 9 */
729 ,$acc_amount->nd_ded_vat /* 10 */
730 ,$acc_amount->amount_perso /* 11 */
731 ,$e_client /* 12 */
732 , $acc_amount->amount_unpaid /*13*/
733 ,$price_euro /* 14 */
734 ));
735
736
737 }
738 else
739 {
740 $acc_amount->amount_vat=0;
741 $r=$this->db->exec_sql("select insert_quant_purchase ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14)",
742 array(
743 null /*1*/
744 ,$j_id /* 2 */
745 ,${"e_march".$i} /* 3 */
746 ,${"e_quant".$i} /* 4 */
747 ,round($acc_amount->amount,2) /* 5 */
748 ,0 /* 6 */
749 ,null/* 7 */
750 ,$acc_amount->amount_nd /* 8 */
751 ,0 /* 9 */
752 ,$acc_amount->nd_ded_vat /* 10 */
753 ,$acc_amount->amount_perso /* 11 */
754 ,$e_client /* 12 */
755 , $acc_amount->amount_unpaid /*13*/
756 ,$price_euro /* 14 */
757 ));
758 }
759 /*
760 * Insert also in operation_currency
761 */
762 $operation_currency=new Operation_currency_SQL($this->db);
763 $operation_currency->oc_amount=$acc_amount->amount_currency;
764 $operation_currency->oc_vat_amount=($tva_both==0)?$acc_amount->amount_vat_currency:0;
765 $operation_currency->oc_price_unit=${'e_march'.$i.'_price'};
766 $operation_currency->j_id=$j_id;
767 $operation_currency->insert();
768 $tot_amount_cur=round(bcadd($tot_amount_cur,$acc_amount->amount_currency,4),4);
769 $tot_amount_cur=round(bcadd($tot_amount_cur,$acc_amount->amount_vat_currency,4),4);
770 if ( DEBUGNOALYSS > 1 ) {
771 echo __LINE__." insert into operation currency oc_amount:{$acc_amount->amount_currency} oc_vat_amount {$acc_amount->amount_vat_currency} <br>";
772 }
773 } // end loop : save all items
774
775
776 /*** save other tax ****/
777 if ( $this->has_other_tax() && isset($p_array['other_tax'])) {
778 $row=$this->db->get_row("select ac_id,ac_label,ac_accounting
779 from acc_other_tax
780 where ac_id=$1 ",
781 [$p_array['other_tax']]);
782 if ( ! empty ($row )) {
783 $other_tax_amount=bcmul($p_array['other_tax_amount'],$p_currency_rate);
784 $acc_operation=new Acc_Operation($this->db);
785 $acc_operation->date=$e_date;
786 $acc_operation->poste=$row['ac_accounting'];
787 $acc_operation->amount=$other_tax_amount;
788 $acc_operation->grpt=$seq;
789 $acc_operation->jrn=$p_jrn;
790 $acc_operation->type='d';
791 $acc_operation->periode=$tperiode;
792 $acc_operation->desc=$row['ac_label'];
793 $jrn_tax_sql=new Jrn_Tax_SQL($this->db);
794 $jrn_tax_sql->j_id=$acc_operation->insert_jrnx();
795 $jrn_tax_sql->ac_id=$row['ac_id'];
796 $jrn_tax_sql->pcm_val=$row['ac_accounting'];
797 $jrn_tax_sql->insert();
798 $operation_currency=new Operation_currency_SQL($this->db);
799 $operation_currency->oc_amount=$p_array['other_tax_amount'];
800 $operation_currency->oc_vat_amount=0;
801 $operation_currency->oc_price_unit=0;
802 $operation_currency->j_id=$jrn_tax_sql->j_id;
803 $operation_currency->insert();
804
805 $tot_debit=bcadd($tot_debit, abs($other_tax_amount));
807 }
808
809 }
810
811
812 /*** save total customer ***/
813 if ( DEBUGNOALYSS > 1 ) {
814 echo __LINE__." tot_amount $tot_amount<br>";
815 echo __LINE__." tot_tva $tot_tva<br>";
816
817 }
818 $cust_amount=round(bcadd($tot_amount,$tot_tva),2);
819 $acc_operation=new Acc_Operation($this->db);
820 $acc_operation->date=$e_date;
821 $acc_operation->poste=$poste;
822 $acc_operation->amount=bcsub($cust_amount,$tot_tva_reversed);
823 $acc_operation->grpt=$seq;
824 $acc_operation->jrn=$p_jrn;
825 $acc_operation->type='c';
826 $acc_operation->periode=$tperiode;
827 $acc_operation->qcode=${"e_client"};
828 if ($cust_amount<0)
829 {
830 $tot_debit=bcadd($tot_debit, abs($cust_amount));
831 }
832 $let_client=$acc_operation->insert_jrnx();
833
834 // --- insert also the currency amount for the customer
835 $operation_currency=new Operation_currency_SQL($this->db);
836 $operation_currency->oc_amount=$tot_amount_cur;
837 $operation_currency->oc_vat_amount=0;
838 $operation_currency->oc_price_unit=0;
839 $operation_currency->j_id=$let_client ;
840 $operation_currency->insert();
841
842 if ( $g_parameter->MY_TVA_USE=='Y')
843 {
844 /* save all vat
845 * $i contains the tva_id and value contains the vat amount
846 */
847 foreach ($tva as $i => $value)
848 {
849 $oTva=new Acc_Tva($this->db);
850 $oTva->set_parameter('id',$i);
851 $oTva->load();
852
853 $poste_vat=$oTva->get_side('d');
854
855 $cust_amount=round(bcadd($tot_amount,$tot_tva),2);
856 $acc_operation=new Acc_Operation($this->db);
857 $acc_operation->date=$e_date;
858 $acc_operation->poste=$poste_vat;
859 $acc_operation->amount=$value;
860 $acc_operation->grpt=$seq;
861 $acc_operation->jrn=$p_jrn;
862 $acc_operation->type='d';
863 $acc_operation->periode=$tperiode;
864 if ( $value > 0 ) $tot_debit=bcadd($tot_debit,abs($value));
865 $acc_operation->insert_jrnx();
866 // if TVA is on both side, we deduce it immediately
867
868 if ( $oTva->get_parameter("both_side")==1 )
869 {
870 $poste_vat=$oTva->get_side('c');
871 if ( $poste_vat == '#')
872 {
873 $poste_vat=$oTva->get_side('d');
874 }
875 $acc_operation=new Acc_Operation($this->db);
876 $acc_operation->date=$e_date;
877 $acc_operation->poste=$poste_vat;
878 $acc_operation->amount=$tot_tva_reversed;
879 $acc_operation->grpt=$seq;
880 $acc_operation->jrn=$p_jrn;
881 $acc_operation->type='c';
882 $acc_operation->periode=$tperiode;
883 $acc_operation->insert_jrnx();
884 if ( $value < 0 ) $tot_debit=bcadd($tot_debit,abs($value));
885 }
886
887 }
888 }
889
890 /* insert into jrn */
891 $acc_operation=new Acc_Operation($this->db);
892 $acc_operation->date=$e_date;
893 $acc_operation->echeance=$e_ech;
894 // Total DEB
895 $acc_operation->amount=$this->db->get_value("select sum(j_montant) from jrnx where j_grpt = $1 and j_debit='t'",
896 array($seq));
897 if ( DEBUGNOALYSS > 1 ) {
898 echo __LINE__." amount ".$acc_operation->amount."<br>";
899
900 }
901 $acc_operation->desc=$e_comm;
902 $acc_operation->grpt=$seq;
903 $acc_operation->jrn=$p_jrn;
904 $acc_operation->periode=$tperiode;
905 $acc_operation->pj=$e_pj;
906 $acc_operation->mt=$mt;
907 $acc_operation->currency_id=$p_currency_code;
908 $acc_operation->currency_rate=$p_currency_rate;
909 $acc_operation->currency_rate_ref=$currency_rate_ref->get_rate();
910
911 if ( ! $this->jr_id=$acc_operation->insert_jrn() ) {
912 throw new Exception (_("Erreur de balance"));
913 }
914 $this->pj=$acc_operation->set_pj();
915
916 // Set Internal code
917 $this->grpt_id=$seq;
918 $this->update_internal_code($internal);
919 /* update quant_purchase */
920 $this->db->exec_sql('update quant_purchase set qp_internal = $1 where j_id in (select j_id from jrnx where j_grpt=$2)',
921 array($internal,$seq));
922
923 /**= e_pj then do not increment sequence , if the given receipt number is equal to one computed then increment */
924 if ($e_pj == $this->pj && noalyss_strlentrim($e_pj) != 0)
925 {
926 $this->inc_seq_pj();
927 }
928
929 /* Save the attachment */
930 if ( isset ($_FILES))
931 {
932 if ( sizeof($_FILES) != 0 )
933 $this->db->save_receipt($seq);
934 }
935 $str_file="";
936 /* Generate an document and save it into the database (Note de frais only)
937 */
938 if ( isset($_POST['gen_invoice']) )
939 {
940 $ref_doc= $this->create_document($internal,$p_array);
941 $this->doc=HtmlInput::show_receipt_document($this->jr_id,h($ref_doc));
942 }
943
944 //----------------------------------------
945 // Save the payer
946 //----------------------------------------
947 if ( $e_mp != 0 )
948 {
949 /* mp */
950 $mp=new Acc_Payment($this->db,$e_mp);
951 $mp->load();
952
953 /* jrnx */
954 $acseq=$this->db->get_next_seq('s_grpt');
955 $acjrn=new Acc_Ledger($this->db,$mp->get_parameter('ledger_target'));
956 $acinternal=$acjrn->compute_internal_code($acseq);
957 /*
958 * for the use of the card of the bank
959 */
960 if ( $acjrn->get_type()=='FIN') {
961 $acjrn=new Acc_Ledger_Fin($this->db, $mp->get_parameter('ledger_target'));
962 $acfiche=new Fiche($this->db,$acjrn->get_bank());
963 $fqcode=$acfiche->strAttribut(ATTR_DEF_QUICKCODE);
964 } else {
965 $fqcode = ${'e_mp_qcode_' . $e_mp};
966 $acfiche = new Fiche($this->db);
967 $acfiche->get_by_qcode($fqcode);
968 }
969 /* Insert paid by */
970 $acc_pay=new Acc_Operation($this->db);
971 $acc_pay->date=$e_date;
972
973 /* get the account and explode if necessary */
974 $sposte=$acfiche->strAttribut(ATTR_DEF_ACCOUNT);
975 // if 2 accounts, take only the debit one for customer
976 if ( strpos($sposte,',') != 0 )
977 {
978 $array=explode(',',$sposte);
979 $poste_val=$array[1];
980 }
981 else
982 {
983 $poste_val=$sposte;
984 }
985 // remove the VAT autoliquidation
986 $cust_amount=bcsub($cust_amount, $tot_tva_reversed);
987
988 // Convert paid amount in EUR
989 $acompte_defcur=bcdiv($acompte, $p_currency_rate);
990
991 $famount=bcsub($cust_amount,$acompte_defcur);
992
993 $acc_pay->poste=$poste_val;
994 $acc_pay->qcode=$fqcode;
995 $acc_pay->amount=abs(round($famount,2));
996 $acc_pay->desc='';
997 $acc_pay->grpt=$acseq;
998 $acc_pay->jrn=$mp->get_parameter('ledger_target');
999 $acc_pay->periode=$tperiode;
1000 $acc_pay->type=($famount>=0)?'c':'d';
1001 $let_pay=$acc_pay->insert_jrnx();
1002
1003 /* Insert supplier */
1004 $acc_pay=new Acc_Operation($this->db);
1005 $acc_pay->date=empty($mp_date)?$e_date:$mp_date;
1006 $acc_pay->poste=$poste;
1007 $acc_pay->qcode=$e_client;
1008 $acc_pay->amount=abs(round($famount,2));
1009 $acc_pay->desc='';
1010 $acc_pay->grpt=$acseq;
1011 $acc_pay->jrn=$mp->get_parameter('ledger_target');
1012 $acc_pay->periode=$tperiode;
1013 $acc_pay->type=($famount>=0)?'d':'c';
1014 $let_other=$acc_pay->insert_jrnx();
1015
1016 // insert into operation_currency
1017 $operation_currency=new Operation_currency_SQL($this->db);
1018 $operation_currency->oc_amount=bcsub($tot_amount_cur,$acompte);
1019 $operation_currency->oc_vat_amount=0;
1020 $operation_currency->oc_price_unit=0;
1021 $operation_currency->j_id=$let_other;
1022 $operation_currency->insert();
1023
1024 // insert into operation_currency bank
1025 $operation_currency=new Operation_currency_SQL($this->db);
1026 $operation_currency->oc_amount=bcsub($tot_amount_cur,$acompte);
1027 $operation_currency->oc_vat_amount=0;
1028 $operation_currency->oc_price_unit=0;
1029 $operation_currency->j_id=$let_pay;
1030 $operation_currency->insert();
1031
1032 /* insert into jrn */
1033 $acc_pay->mt=$mt;
1034 $acc_pay->desc=(!isset($e_comm_paiement) || noalyss_strlentrim($e_comm_paiement) == 0) ?$e_comm:$e_comm_paiement;
1035
1036 // Add info for currency
1037 $acc_pay->currency_id=$p_currency_code;
1038 $acc_pay->currency_rate=$p_currency_rate;
1039 $acc_pay->currency_rate_ref=$currency_rate_ref->get_rate();
1040
1041
1042 // insert into the table JRN
1043 $mp_jr_id=$acc_pay->insert_jrn();
1044 $this->payment_operation=$mp_jr_id;
1045 $acjrn->grpt_id=$acseq;
1046 $acjrn->update_internal_code($acinternal);
1047 // add an automatic PJ if ODS
1048 if ($acjrn->get_type()=="ODS") {
1049 $acc_pay->pj=$acjrn->guess_pj();
1050 $acc_pay->set_pj();
1051 }
1052 $r1=$this->get_id($internal);
1053 $r2=$this->get_id($acinternal);
1054
1055 /*
1056 * add lettering
1057 */
1058 $oletter=new Lettering($this->db);
1059 $oletter->insert_couple($let_client,$let_other);
1060
1061 /* set the flag paid */
1062 $Res=$this->db->exec_sql("update jrn set jr_rapt='paid' where jr_id=$1",array($r1));
1063
1064 /* Reconcialiation */
1065 $rec=new Acc_Reconciliation($this->db);
1066 $rec->set_jr_id($r1);
1067 $rec->insert($r2);
1068 /*
1069 * save also into quant_fin
1070 */
1071
1072 /* get ledger property */
1073 $ledger=new Acc_Ledger_Fin($this->db,$acc_pay->jrn);
1074 $prop=$ledger->get_propertie();
1075
1076 /* if ledger is FIN then insert into quant_fin */
1077 if ( $prop['jrn_def_type'] == 'FIN' )
1078 {
1079 $ledger->insert_quant_fin($acfiche->id,$mp_jr_id,$cust->id,bcmul($famount,-1),$let_other);
1080 }
1081
1082
1083 }
1084 /*----------------------------------------------
1085 * Save the note
1086 ----------------------------------------------*/
1087 if (isset($p_array['jrn_note_input']) && !empty($p_array['jrn_note_input'])) {
1088 $acc_operation_note=Acc_Operation_Note::build_jrn_id(-1);
1089 $acc_operation_note->setNote($p_array['jrn_note_input']);
1090 $acc_operation_note->setOperation_id( $this->jr_id);
1091 $acc_operation_note->save();
1092 }
1093 }//end try
1094 catch (Exception $e)
1095 {
1096 record_log($e);
1097 echo '<span class="error">'.
1098 'Erreur dans l\'enregistrement '.
1099 __FILE__.':'.__LINE__.' '.
1100 $e->getMessage().$e->getMessage();
1101 record_log($e->getMessage());
1102 $this->db->rollback();
1103 throw new Exception($e);
1104 }
1105 $this->db->commit();
1106 return $internal;
1107 }
1108
1109 /*!\brief display the form for entering data for invoice
1110 *\param $p_array is null or you can put the predef operation or the $_POST
1111 \code
1112 array
1113 'sa' => string 'n' (length=1)
1114 'p_action' => string 'ach' (length=3)
1115 'gDossier' => string '28' (length=2)
1116 'e_client' => string 'ASEKURA' (length=7)
1117 'nb_item' => string '9' (length=1)
1118 'p_jrn' => string '3' (length=1)
1119 'period' => string '126' (length=3)
1120 'e_comm' => string 'descriptio' (length=10)
1121 'e_date' => string '01.05.2010' (length=10)
1122 'e_ech' => string '' (length=0)
1123 'jrn_type' => string 'ACH' (length=3)
1124 'e_pj' => string 'ACH37' (length=5)
1125 'e_pj_suggest' => string 'ACH37' (length=5)
1126 'mt' => string '1273759434.5701' (length=15)
1127 'e_mp' => string '0' (length=1)
1128 'e_march0' => string 'DOC' (length=3)
1129 'e_march0_price' => string '2000' (length=4)
1130 'e_march0_tva_id' => string '3' (length=1)
1131 'e_march0_tva_amount' => string '120' (length=3)
1132 'e_quant0' => string '1' (length=1)
1133 'gen_invoice' => string 'on' (length=2)
1134 'gen_doc' => string '7' (length=1)
1135 'bon_comm' => string '' (length=0)
1136 'other_info' => string '' (length=0)
1137 'correct' => string 'Corriger' (length=8)
1138 \endcode
1139 *\return HTML string
1140 */
1141 public function input($p_array=null,$p_readonly=0)
1142 {
1143 global $g_parameter,$g_user;
1144 // load ledger definition
1145 $this->load();
1146 $http=new HttpInput();
1147 if ( $p_array != null ) extract($p_array, EXTR_SKIP);
1148
1149 $flag_tva=$g_parameter->MY_TVA_USE;
1150
1151 /* Add button */
1152 $str_add_button_tiers = "";
1153 $add_card=FALSE;
1154 if ($g_user->check_action(FICADD) == 1) {
1155 $add_card=TRUE;
1156 $str_add_button_tiers = $this->add_card("cred", "e_client");
1157 }
1158 // The first day of the periode
1159 $oPeriode=new Periode($this->db);
1160 list ($l_date_start,$l_date_end)=$oPeriode->get_date_limit($g_user->get_periode());
1161 if ( $g_parameter->MY_DATE_SUGGEST=='Y' )
1162 $op_date=( ! isset($e_date) ) ?$l_date_start:$e_date;
1163 else
1164 $op_date=( ! isset($e_date) ) ?'':$e_date;
1165
1166 $e_ech=(isset($e_ech))?$e_ech:"";
1167 $e_comm=(isset($e_comm))?$e_comm:"";
1168
1169 $r="";
1170 $r.=dossier::hidden();
1171 $f_legend_detail=_("Détail articles achetés");
1172
1173 // Date
1174 //--
1175 $Date=new IDate();
1176 $Date->setReadOnly(false);
1177 $Date->table=1;
1178 $Date->tabindex=1;
1179 $f_date=$Date->input("e_date",$op_date);
1180 // Payment limit
1181 //--
1182 $Echeance=new IDate();
1183 $Echeance->setReadOnly(false);
1184 $Echeance->tabindex=2;
1186 $f_echeance=$Echeance->input('e_ech',$e_ech,'Echéance'.$label);
1187 $f_periode="";
1188 if ($this->check_periode() == true)
1189 {
1190 // Periode
1191 //--
1192 $l_user_per=$g_user->get_periode();
1193 $def=(isset($periode))?$periode:$l_user_per;
1194
1195 $period=new IPeriod("period");
1196 $period->user=$g_user;
1197 $period->cn=$this->db;
1198 $period->value=$def;
1199 $period->type=OPEN;
1200 try
1201 {
1202 $l_form_per=$period->input();
1203 }
1204 catch (Exception $e)
1205 {
1206 record_log($e);
1207 if ($e->getCode() == 1 )
1208 {
1209 throw new Exception( _("Aucune période ouverte"));
1210 }
1211 }
1212
1213 $r.="<td>";
1215 $f_periode=td(_("Période comptable")." $label ").td($l_form_per);
1216 }
1217 // Ledger (p_jrn)
1218 //--
1219 /* if we suggest the next pj, then we need a javascript */
1220 $add_js="";
1221 if ( $g_parameter->MY_PJ_SUGGEST !='N')
1222 {
1223 $add_js="update_pj();";
1224 }
1225 if ($g_parameter->MY_DATE_SUGGEST == 'Y')
1226 {
1227 $add_js.='get_last_date();';
1228 }
1229 $add_js.='update_name();';
1230 $add_js.='update_pay_method();';
1231 $add_js.='update_row("sold_item");';
1232 $add_js.='update_other_tax();';
1233 $add_js.='update_visibility_quantity();';
1234
1235 $wLedger=$this->select_ledger('ACH',2,FALSE);
1236
1237 if ($wLedger == null) throw new Exception(_('Pas de journal disponible'));
1238 $wLedger->javascript="onChange='update_predef(\"ach\",\"f\",\"".$http->request("ac")."\");$add_js'";
1239 $wLedger->table=0;
1240 $f_jrn=$wLedger->input();
1241
1242 // Comment
1243 //--
1244 $Commentaire=new IText();
1245 $Commentaire->table=0;
1246 $Commentaire->setReadOnly(false);
1247 $Commentaire->size=60;
1248 $Commentaire->tabindex=3;
1250 $f_desc=$Commentaire->input("e_comm",$e_comm);
1251
1252 // PJ
1253 //--
1254 /* suggest PJ ? */
1255 $default_pj='';
1256 if ( $g_parameter->MY_PJ_SUGGEST !='N')
1257 {
1258 $default_pj=$this->guess_pj();
1259 }
1260
1261 $pj=new IText();
1262 $pj->value=(isset($e_pj))?$e_pj:$default_pj;
1263
1264 if ( $g_parameter->MY_PJ_SUGGEST=='A' || $g_user->check_action(UPDRECEIPT)==0)
1265 {
1266 $pj->setReadOnly(true);
1267 $pj->id="e_pj";
1268 }
1269 $pj->table=0;
1270 $pj->name="e_pj";
1271 $pj->size=10;
1272 $pj->readonly=false;
1273
1274 $f_pj=$pj->input().HtmlInput::hidden('e_pj_suggest',$default_pj);
1275
1276 // Display the customer
1277 //--
1278 $fiche='cred';
1279
1280 // Save old value and set a new one
1281 //--
1282 $e_client=( isset ($e_client) )?$e_client:"";
1283 $e_client_label="&nbsp;";//str_pad("",100,".");
1284
1285
1286 // retrieve e_client_label
1287 //--
1288
1289 if ( noalyss_strlentrim($e_client) != 0)
1290 {
1291 $fClient=new Fiche($this->db);
1292 $fClient->get_by_qcode($e_client);
1293 $e_client_label=$fClient->strAttribut(ATTR_DEF_NAME).' '.
1294 ' Adresse : '.$fClient->strAttribut(ATTR_DEF_ADRESS).' '.
1295 $fClient->strAttribut(ATTR_DEF_CP).' '.
1296 $fClient->strAttribut(ATTR_DEF_CITY).' ';
1297
1298
1299 }
1300
1301 $W1=new ICard();
1302 $W1->label=_("Fournisseur ").Icon_Action::infobulle(0) ;
1303 $W1->name="e_client";
1304 $W1->tabindex=3;
1305 $W1->value=$e_client;
1306 $W1->table=0;
1307 $W1->set_dblclick("fill_ipopcard(this);");
1308 $W1->set_attribute('ipopup','ipopcard');
1309
1310 // name of the field to update with the name of the card
1311 $W1->set_attribute('label','e_client_label');
1312 // name of the field to update with the name of the card
1313 $W1->set_attribute('typecard','cred');
1314
1315 // Add the callback function to filter the card on the jrn
1316 $W1->set_callback('filter_card');
1317 $W1->set_function('fill_data');
1318 $W1->javascript=sprintf(' onchange="fill_data_onchange(\'%s\');" ',
1319 $W1->name);
1320 $f_client_qcode=$W1->input();
1321 $client_label=new ISpan();
1322 $client_label->style="vertical-align:top";
1323 $client_label->table=0;
1324 $f_client=$client_label->input("e_client_label",$e_client_label);
1325 $f_client_bt=$W1->search();
1326
1327
1328 // Record the current number of article
1329
1330 $e_comment=(isset($e_comment))?$e_comment:"";
1331 $p_article= ( isset ($nb_item))?$nb_item:$this->get_min_row();
1332 $p_article=($p_article < $this->get_min_row())?$this->get_min_row():$p_article;
1333
1334 $Hid=new IHidden();
1335 $r.=$Hid->input("nb_item",$p_article);
1336
1337 // For each article
1338 //--
1339 for ($i=0;$i< $p_article ;$i++)
1340 {
1341 // Code id, price & vat code
1342 //--
1343 $march=(isset(${"e_march$i"}))?${"e_march$i"}:"" ;
1344 $march_price=(isset(${"e_march".$i."_price"}))?${"e_march".$i."_price"}:""
1345 ;
1346 /* use vat */
1347 if ( $g_parameter->MY_TVA_USE=='Y')
1348 {
1349 $march_tva_id=(isset(${"e_march$i"."_tva_id"}))?${"e_march$i"."_tva_id"}:"";
1350 $march_tva_amount=(isset(${"e_march$i"."_tva_amount"}))?${"e_march$i"."_tva_amount"}:"";
1351 }
1352
1353
1354
1355 $march_label=(isset(${"e_march".$i."_label"}))?${"e_march".$i."_label"}:"";
1356 // retrieve the tva label and name
1357 //--
1358 if ( noalyss_strlentrim($march)!=0 && noalyss_strlentrim($march_label)==0 )
1359 {
1360 $fMarch=new Fiche($this->db);
1361 $fMarch->get_by_qcode($march);
1362 $march_label=$fMarch->strAttribut(ATTR_DEF_NAME);
1363 /* vat use */
1364 if ( ! isset($march_tva_id) && $g_parameter->MY_TVA_USE=='Y' )
1365 $march_tva_id=$fMarch->strAttribut(ATTR_DEF_TVA);
1366 }
1367 // Show input
1368 //--
1369 $W1=new ICard();
1370 $W1->label="";
1371 $W1->name="e_march".$i;
1372 $W1->value=$march;
1373 $W1->table=1;
1374 $W1->set_dblclick("fill_ipopcard(this);");
1375 $W1->set_attribute('ipopup','ipopcard');
1376
1377 $W1->set_attribute('typecard','deb');
1378
1379 // name of the field to update with the name of the card
1380 $W1->set_attribute('label','e_march'.$i.'_label');
1381 // name of the field with the price
1382 $W1->set_attribute('purchase','e_march'.$i.'_price'); /* autocomplete */
1383 $W1->set_attribute('price','e_march'.$i.'_price'); /* via search */
1384
1385 // name of the field with the TVA_ID
1386 $W1->set_attribute('tvaid','e_march'.$i.'_tva_id');
1387 // Add the callback function to filter the card on the jrn
1388 $W1->set_callback('filter_card');
1389 $W1->set_function('fill_data');
1390 $W1->javascript=sprintf(' onchange="fill_data_onchange(\'%s\');" ',
1391 $W1->name);
1392 $W1->readonly=false;
1393 $array[$i]['quick_code']=$W1->input();
1394 $array[$i]['bt']=$W1->search();
1395 $array[$i]['card_add']=($add_card==TRUE)?$this->add_card("deb", $W1->id):"";
1396
1397 $array[$i]['hidden']='';
1398 // For computing we need some hidden field for holding the value
1399 if ( $g_parameter->MY_TVA_USE=='Y')
1400 {
1401 $array[$i]['hidden'].=HtmlInput::hidden('tva_march'.$i,0);
1402 }
1403
1404 if ( $g_parameter->MY_TVA_USE=='Y')
1405 $tvac=new INum('tvac_march'.$i);
1406 else
1407 $tvac=new IHidden('tvac_march'.$i);
1408
1409 $tvac->readOnly=1;
1410 $tvac->value=0;
1411 $array[$i]['tvac']=$tvac->input();
1412
1413 $htva=new INum('htva_march'.$i);
1414 $htva->readOnly=1;
1415
1416 $htva->value=0;
1417 $array[$i]['htva']=$htva->input();
1418
1419 if ( $g_parameter->MY_UPDLAB == 'Y')
1420 {
1421 $Span=new IText("e_march".$i."_label");
1422 $Span->style='class="input_text label_item"';
1423 } else
1424 {
1425 $Span=new ISpan("e_march".$i."_label");
1426 $Span->extra='class="label_item"';
1427 }
1428 $Span->value=$march_label;
1429 $Span->setReadOnly(false);
1430 // card's name, price
1431 //--
1432 $array[$i]['denom']=$Span->input("e_march".$i."_label",$march_label);
1433 // price
1434 $Price=new INum();
1435 $Price->setReadOnly(false);
1436 $Price->size=9;
1437 $Price->javascript="onblur=\"format_number(this,4);clean_tva($i);compute_ledger($i)\"";
1438 $array[$i]['pu']=$Price->input("e_march".$i."_price",$march_price);
1439 if ( $g_parameter->MY_TVA_USE=='Y')
1440 {
1441
1442 // vat label
1443 //--
1444 $Tva=new ITva_Popup($this->db);
1445 $Tva->js="onblur=\"clean_tva($i);compute_ledger($i)\"";
1446 $Tva->in_table=true;
1447 $Tva->set_attribute('compute',$i);
1448 $Tva->set_filter("purchase");
1449 $Tva->value=$march_tva_id;
1450 $array[$i]['tva']=$Tva->input("e_march$i"."_tva_id");
1451
1452 // Tva_amount
1453
1454 // price
1455 $Tva_amount=new INum();
1456 $Tva_amount->setReadOnly(false);
1457 $Tva_amount->size=9;
1458 $Tva_amount->javascript="onblur=\"format_number(this);compute_ledger($i)\"";
1459 $array[$i]['amount_tva']=$Tva_amount->input("e_march".$i."_tva_amount",$march_tva_amount);
1460 }
1461 // quantity
1462 //--
1463 $quant=(isset(${"e_quant$i"}))?${"e_quant$i"}:"1"
1464 ;
1465 $Quantity=new INum();
1466 $Quantity->setReadOnly(false);
1467 $Quantity->size=9;
1468 $Quantity->javascript="onchange=\"format_number(this,2);clean_tva($i);compute_ledger($i)\"";
1469 $array[$i]['quantity']=$Quantity->input("e_quant".$i,$quant);
1470
1471 }
1472 $f_type=_('Fournisseur');
1473
1474 // Currency
1475 $currency_select = $this->CurrencyInput("currency_code", "p_currency_rate" , "p_currency_euro");
1476 $currency_select->selected=$http->request('p_currency_code','string',0);
1477
1478 $currency_input=new INum("p_currency_rate");
1479 $currency_input->prec=8;
1480 $currency_input->id="p_currency_rate";
1481 $currency_input->value=$http->request('p_currency_rate','string',1);
1482 $currency_input->javascript='onchange="format_number(this,6);CurrencyCompute(\'p_currency_rate\',\'p_currency_euro\');"';
1483
1484 $currency=new Acc_Currency($this->db,0);
1485
1486 //
1487 // Button for template operation
1488 //
1489 ob_start();
1490 echo '<div id="predef_form">';
1491 echo HtmlInput::hidden('p_jrn_predef', $this->id);
1492 $op = new Pre_operation($this->db);
1493 $op->set_p_jrn($this->id);
1494 $op->set_jrn_type("ACH");
1495 $op->set_od_direct('f');
1496 $url=http_build_query(array('p_jrn_predef'=>$this->id,'ac'=>$http->request('ac'),'gDossier'=>dossier::id()));
1497 echo $op->form_get('do.php?'.$url);
1498 echo '</div>';
1499 $str_op_template=ob_get_contents();
1500 ob_end_clean();
1501
1502 ob_start();
1503 require_once NOALYSS_TEMPLATE.'/form_ledger_detail.php';
1504 $r.=ob_get_contents();
1505 ob_end_clean();
1506
1507 // Set correctly the REQUEST param for jrn_type
1508 $r.= HtmlInput::hidden('jrn_type','ACH');
1510
1511
1512
1513 /* if we suggest the pj n# the run the script */
1514 if ( $g_parameter->MY_PJ_SUGGEST !='N')
1515 {
1516 $r.='<script> update_pj();</script>';
1517 }
1518 // set focus on date
1519 $r.= create_script("$('".$Date->id."').focus()");
1520 $r.='<div id="additional_tax_div">';
1521 $r.=$this->input_additional_tax();
1522 $r.='</div>';
1523 return $r;
1524 }
1525
1526 /*!@brief show the summary of the operation and propose to save it
1527 *@param array contains normally $_POST. It proposes also to save
1528 * the Analytic accountancy
1529 * @param $p_summary true to confirm false, show only the result in RO
1530 *@return string
1531 */
1532 function confirm($p_array,$p_summary=false)
1533 {
1534 global $g_parameter,$g_user;
1535 extract ($p_array,EXTR_SKIP);
1536 if ( !isset($p_array['jrn_note_input'])) {$p_array['jrn_note_input']='';}
1537 // we don't need to verify if we need only a feedback
1538 if ( ! $p_summary ){$this->verify($p_array) ;}
1539
1540 $anc=null;
1541 // to show a select list for the analytic
1542 // if analytic is op (optionnel) there is a blank line
1543
1544 bcscale(4);
1545 $client=new Fiche($this->db);
1546 $client->get_by_qcode($e_client,true);
1547
1548 $client_name=h($client->getName().
1549 ' '.$client->strAttribut(ATTR_DEF_ADRESS).' '.
1550 $client->strAttribut(ATTR_DEF_CP).' '.
1551 $client->strAttribut(ATTR_DEF_CITY));
1552 $lPeriode=new Periode($this->db);
1553 if ($this->check_periode() == true)
1554 {
1555 $lPeriode->p_id=$period;
1556 }
1557 else
1558 {
1559 $lPeriode->find_periode($e_date);
1560 }
1561 $date_limit=$lPeriode->get_date_limit();
1562 $r="";
1563 $r .= '<div id="summary_op1">';
1564 $r.='<TABLE>';
1565 if ( $p_summary ) {
1566 $jr_id=$this->db->get_value('select jr_id from jrn where jr_internal=$1',array($this->internal));
1567 $r.="<tr>";
1568 $r.='<td>';
1569 $r.=_('Détail opération ');
1570 $r.='</td>';
1571 $r.='<td>';
1572 $r.=sprintf ('<a class="line" style="display:inline" href="javascript:modifyOperation(%d,%d)">%s</a>',
1573 $jr_id,dossier::id(),$this->internal);
1574 $r.='</td>';
1575 $r.="</tr>";
1576 }
1577 $r.='<tr>';
1578
1579 $span=$this->warn_manual_receipt($p_array);
1580 if ( ! $p_summary) {
1581 $r.='<td>' . _('Numéro Pièce') .$span.'</td><td>'. hb($e_pj) . '</td>';
1582 } else {
1583 if ( $g_parameter->MY_PJ_SUGGEST=="A" || $g_user->check_action(UPDRECEIPT)==0) $e_pj=$this->pj;
1584
1585 if ( strcmp($this->pj,$e_pj) != 0 )
1586 {
1587 $r.='<td>' . _('Numéro Pièce').$span .'</td><td>'. hb($this->pj) .
1588 '<span class="notice"> '._('Attention numéro pièce existante, elle a du être adaptée').'</span></td>';
1589 } else {
1590 $r.='<td>' . _('Numéro Pièce') .$span.'</td><td>'. hb($this->pj) . '</td>';
1591 }
1592 }
1593 $r.='</tr>';
1594 $r.='<td> ' . _('Date') . '</td><td> ' . hb($e_date) . '</td>';
1595 $r.='</tr>';
1596 $r.='<tr>';
1597 $r.='<td>' . _('Echeance') . '</td><td> ' . hb($e_ech) . '</td>';
1598 $r.='</tr>';
1599
1600
1601 $r.='<tr>';
1602 $r.='<td> ' . _('Période Comptable') . '</td><td> ' .hb( $date_limit['p_start'] . '-' . $date_limit['p_end']) . '</td>';
1603 $r.='</tr>';
1604 $r.='</table>';
1605 $r.='</div>';
1606 $r .= '<div id="summary_op2">';
1607 $r.='<table>';
1608 $r.='<tr>';
1609 $r.='<td> ' . _('Journal') . '</td><td> ' . hb($this->get_name()) . '</td>';
1610 $r.='</tr>';
1611 $r.='<tr>';
1612 $r.='<td> ' . _('Libellé') . '</td><td> ' . hb($e_comm) . '</td>';
1613 $r.='</tr>';
1614 $r.='<tr>';
1615
1616 $r.='<tr>';
1617 $r.='<td> ' . _('Fournisseur') . '</td><td> ' . hb($e_client . ':' . $client_name) . '</td>';
1618 $r.='</tr>';
1619 $r.='</table>';
1620 $r.='<pre>'._('Note').' '.h($p_array['jrn_note_input']).'</pre>';
1621 $r.='</div>';
1622 $r.='<div style="position:float;clear:both">';
1623 $r.='</div>';
1624 $r.='<h2>' . _('Détail articles achetés') . '</h2>';
1625 $r.='<p class="decale">';
1626 $r.='<table class="result" >';
1627 $r.='<TR>';
1628 $r.="<th>" . _('Code') . "</th>";
1629 $r.="<th>" . _('Dénomination') . "</th>";
1630 $r.="<th style=\"text-align:right\">" . _('prix') . "</th>";
1631 $r.="<th style=\"text-align:right\">" . _('quantité') . "</th>";
1632
1633
1634 if ($g_parameter->MY_TVA_USE == 'Y') {
1635 $r.="<th style=\"text-align:right\">" . _('tva') . "</th>";
1636 $r.='<th style="text-align:right"> ' . _('Montant TVA') . '</th>';
1637 $r.='<th style="text-align:right">' . _('Montant HTVA') . '</th>';
1638 $r.='<th style="text-align:right">' . _('Montant TVAC') . '</th>';
1639 } else {
1640 $r.='<th style="text-align:right">' . _('Montant') . '</th>';
1641 }
1642
1643 /* if we use the AC */
1644 if ($g_parameter->MY_ANALYTIC!='nu')
1645 {
1646 $anc=new Anc_Plan($this->db);
1647 $a_anc=$anc->get_list();
1648 $x=count($a_anc);
1649 /* set the width of the col */
1650 $r.='<th colspan="'.$x.'">'._('Compt. Analytique').'</th>';
1651
1652 /* add hidden variables pa[] to hold the value of pa_id */
1653 $r.=Anc_Plan::hidden($a_anc);
1654 }
1655
1656 $r.='</tr>';
1657 $tot_amount=0.0;
1658 $tot_tva=0.0;
1659 //--
1660 // For each item
1661 //--
1662 for ($i = 0; $i < $nb_item;$i++)
1663 {
1664 $tot_row=0;
1665 if ( noalyss_strlentrim(${"e_march".$i}) == 0 ) continue;
1666
1667 /* retrieve information for card */
1668 $fiche=new Fiche($this->db);
1669 $fiche->get_by_qcode(${"e_march".$i});
1670 if ( $g_parameter->MY_UPDLAB=='Y')
1671 $fiche_name=h(${"e_march".$i."_label"});
1672 else
1673 $fiche_name=$fiche->strAttribut (ATTR_DEF_NAME);
1674 $amount=bcmul(${"e_march".$i."_price"},${'e_quant'.$i});
1675 if ( $g_parameter->MY_TVA_USE=='Y')
1676 {
1677 $idx_tva=${"e_march".$i."_tva_id"};
1678 $oTva=new Acc_Tva($this->db);
1679 $oTva->set_parameter('id',$idx_tva);
1680 $oTva->load();
1681 $op=new Acc_Compute();
1682
1683 $op->set_parameter("amount",$amount);
1684 $op->set_parameter('amount_vat_rate',$oTva->get_parameter('rate'));
1685 $op->compute_vat();
1686 $tva_computed=$op->get_parameter('amount_vat');
1687 //----- if tva_amount is not given we compute the vat ----
1688 if ( strlen (trim (${'e_march'.$i.'_tva_amount'})) == 0)
1689 {
1690 $tva_item=$op->get_parameter('amount_vat');
1691 }
1692 else
1693 $tva_item=round(${'e_march'.$i.'_tva_amount'},2);
1694
1695 if (isset($tva[$idx_tva] ) )
1696 $tva[$idx_tva]=bcadd($tva_item,$tva[$idx_tva]);
1697 else
1698 $tva[$idx_tva]=$tva_item;
1699
1700
1701
1702 }
1703 $tot_amount=round(bcadd($tot_amount,$amount),2);
1704 $tot_row=round(bcadd($tot_row,$amount),2);
1705 $r.='<tr>';
1706 $r.='<td>';
1707 $r.=${"e_march".$i};
1708 $r.='</td>';
1709 $r.='<TD style="border-bottom:1px dotted grey;">';
1710 $r.=$fiche_name;
1711 $r.='</td>';
1712 $r.='<td class="num">';
1713 $r.=nbm(${"e_march".$i."_price"},4);
1714 $r.='</td>';
1715 $r.='<td class="num">';
1716 $r.=nbm(${"e_quant".$i},4);
1717 $r.='</td>';
1718 $both_side=0;
1719 if ($g_parameter->MY_TVA_USE == 'Y')
1720 {
1721 $r.='<td class="num">';
1722 $r.=$oTva->get_parameter('label');
1723 $both_side=$oTva->get_parameter("both_side");
1724 if ( $both_side == 0) {
1725 $tot_row=bcadd($tot_row,$tva_item);
1726 $tot_tva=round(bcadd($tva_item,$tot_tva),2);
1727 }
1728 $r.='</td>';
1729 /* warning if tva_computed and given are not the
1730 same */
1731 $css_void_tva=($both_side == 1)?'style="text-decoration:line-through"':'';
1732 if ( bcsub($tva_item,$tva_computed) != 0 && ! ($tva_item == 0 && $both_side == 1))
1733 {
1734
1735 $r.='<td style="background-color:red" class="num" '.$css_void_tva.'>';
1737 $r.='<a href="#" class="error" style="display:inline" title="'. _("Attention Différence entre TVA calculée et donnée").'">'
1738 .nbm($tva_item).'<a>';
1739 }
1740 else{
1741 $r.='<td class="num" '.$css_void_tva.'>';
1742 $r.=nbm($tva_item);
1743 }
1744 $r.='</td>';
1745 $r.='<td class="num"> ';
1746 $r.=nbm(round($amount,2));
1747 $r.='</td>';
1748 }
1749 $r.='<td class="num">';
1750 $r.=nbm(round($tot_row,2));
1751 $r.='</td>';
1752 // encode the pa
1753 if ( $g_parameter->MY_ANALYTIC!='nu'
1754 && $g_parameter->match_analytic($fiche->strAttribut(ATTR_DEF_ACCOUNT))==TRUE
1755 ) // use of AA
1756 {
1757 // show form
1758 $anc_op=new Anc_Operation($this->db);
1759 $null=($g_parameter->MY_ANALYTIC=='op')?1:0;
1760 $r.='<td>';
1761 $p_mode=($p_summary==false)?1:0;
1762 $p_array['pa_id']=$a_anc;
1763 /* op is the operation it contains either a sequence or a jrnx.j_id */
1764 $r.=HtmlInput::hidden('op[]=',$i);
1765 $r.=$anc_op->display_form_plan($p_array,$null,$p_mode,$i,round($amount,2));
1766 $r.='</td>';
1767 }
1768
1769
1770 $r.='</tr>';
1771
1772 }
1773 // Add the sum
1774 $decalage=($g_parameter->MY_TVA_USE == 'Y')?'<td></td><td></td><td></td><td></td>':'<td></td>';
1775 $tot = round(bcadd($tot_amount, $tot_tva), 2);
1776 $str_tot=_('Totaux');
1777 $tot_eur=round(bcdiv($tot, $p_currency_rate),2);
1778
1779 // Get currency code
1780 $default_currency=new Acc_Currency($this->db,0);
1781 $str_code=$default_currency->get_code();
1782 if ( $p_currency_code != 0 ) {
1783 $acc_currency=new Acc_Currency($this->db);
1784 $acc_currency->set_id($p_currency_code);
1785 $str_code=$acc_currency->get_code();
1786 }
1787 // Format amount
1789 $tot_tva=nbm($tot_tva);
1790 $tot_str=nbm($tot);
1791
1792 if ( $g_parameter->MY_TVA_USE == 'Y') {
1793 $r.=<<<EOF
1794<tr class="highlight">
1795 {$decalage}
1796 <td>
1797 {$str_tot} {$str_code}
1798 </td>
1799 <td class="num">
1800 {$tot_tva}
1801 </td>
1802 <td class="num">
1803 {$tot_amount}
1804 </td>
1805 <td class="num">
1806 {$tot_str} {$str_code}
1807 </td>
1808</tr>
1809EOF;
1810 if ($p_currency_code !=0) {
1811 $rate=_("Taux ");
1812$r.=<<<EOF
1813<tr class="highlight">
1814 {$decalage}
1815 <td>
1816
1817 </td>
1818 <td class="num">
1819
1820 </td>
1821 <td class="num">
1822 {$rate} {$p_currency_rate}
1823 </td>
1824 <td class="num">
1825 {$tot_eur} EUR
1826 </td>
1827</tr>
1828EOF;
1829 } // if ($p_currency_code !=0
1830 }else { // if $g_parameter->MY_TVA_USE=='Y'
1831 $r.=<<<EOF
1832<tr class="highlight">
1833 {$decalage}
1834 <td>
1835 {$str_tot} {$str_code}
1836 </td>
1837 <td class="num">
1838 {$tot_amount}
1839 </td>
1840 <td class="num">
1841
1842 </td>
1843 <td class="num">
1844 {$tot_str} {$str_code}
1845 </td>
1846</tr>
1847<tr class="highlight">
1848 {$decalage}
1849 <td>
1850 </td>
1851 <td>
1852 </td>
1853 <td>
1854 </td>
1855 <td class="num">
1856 {$tot_str} {$str_code}
1857 </td>
1858</tr>
1859EOF;
1860
1861 }
1862 $r.='</table>';
1863 $r.='</p>';
1864 if ( $g_parameter->MY_ANALYTIC!='nu' && !$p_summary) // use of AA
1865 $r.='<input type="button" class="button" value="'._('Vérifiez imputation analytique').'" onClick="verify_ca(\'\');">';
1866
1867 $r.='<div id="total_div_id" >';
1868 $r.='<h2>Totaux</h2>';
1869 $other_tax_label="";
1871 if ( $this->has_other_tax() && isset($p_array['other_tax'])) {
1872 $other_tax_label=_("Autre taxe");
1873 $other_tax_amount=htmlspecialchars($p_array['other_tax_amount']);
1874 }
1875 /* use VAT */
1876 if ($g_parameter->MY_TVA_USE == 'Y') {
1877 $r.='<table>';
1878 $r.='<tr><td>Total HTVA</td>';
1879 $r.=td(hb($tot_amount ),'class="num"');
1880 foreach ($tva as $i => $value) {
1881 $oTva->set_parameter('id', $i);
1882 $oTva->load();
1883
1884 $r.='<tr><td> TVA ' . $oTva->get_parameter('label').'</td>';
1885 $r.=td(hb(nbm($tva[$i])),'class="num"');
1886 }
1887 $r.='<tr>'.td(_('Total TVA')).td(hb($tot_tva),'class="num"');
1888 if ( ! empty($other_tax_label) ) {
1889 $r.='<tr>'.td($other_tax_label).td(hb($other_tax_amount),'class="num"');
1890 }
1891 if ( $other_tax_amount!="") {$tot=bcadd($tot,$other_tax_amount,2);}
1892 $r.='<tr>'.td(_('Total TVAC')).td(hb($tot),'class="num"');
1893 $r.='</table>';
1894 } else {
1895 if ( ! empty($other_tax_label) ) {
1896 $r.='<tr>'.td($other_tax_label).td(hb($other_tax_amount),'class="num"');
1897 }
1898 if ( $other_tax_amount!="") {$tot=bcadd($tot,$other_tax_amount,2);}
1899 $r.='<br>Total '.hb($tot);
1900 }
1901 $r.='</div>';
1902 /* Add hidden */
1903 $r.=HtmlInput::hidden('e_client',$e_client);
1904 $r.=HtmlInput::hidden('nb_item',$nb_item);
1905 $r.=HtmlInput::hidden('p_jrn',$p_jrn);
1906 $r.=HtmlInput::hidden('jrn_note_input',h($p_array['jrn_note_input']));
1907
1908 if ( isset($period))
1909 $r.=HtmlInput::hidden('period',$period);
1910 $r.=HtmlInput::hidden('e_comm',$e_comm);
1911 $r.=HtmlInput::hidden('e_date',$e_date);
1912 $r.=HtmlInput::hidden('e_ech',$e_ech);
1913 $r.=HtmlInput::hidden('jrn_type',$jrn_type);
1914 $r.=HtmlInput::hidden('e_pj',$e_pj);
1915 $r.=HtmlInput::hidden('e_pj_suggest',$e_pj_suggest);
1916 $r.=HtmlInput::post_to_hidden(['p_currency_rate','p_currency_code']);
1917 if ( $this->has_other_tax() && isset($p_array["other_tax"])) {
1918 $r.=HtmlInput::hidden("other_tax",$p_array['other_tax']);
1919 $r.=HtmlInput::hidden("other_tax_amount",$p_array['other_tax_amount']);
1920 }
1921 $mt=microtime(true);
1922 $r.=HtmlInput::hidden('mt',$mt);
1923
1924 $e_mp=(isset($e_mp))?$e_mp:0;
1925 $r.=HtmlInput::hidden('e_mp',$e_mp);
1926 /* Paid by */
1927 /* if the paymethod is not 0 and if a quick code is given */
1928
1929
1930 for ($i=0;$i < $nb_item;$i++)
1931 {
1932 $r.=HtmlInput::hidden("e_march".$i,${"e_march".$i});
1933 if (isset (${"e_march".$i."_label"})) $r.=HtmlInput::hidden("e_march".$i."_label",${"e_march".$i."_label"});
1934 $r.=HtmlInput::hidden("e_march".$i."_price",${"e_march".$i."_price"});
1935 if ( $g_parameter->MY_TVA_USE=='Y' )
1936 {
1937 $r.=HtmlInput::hidden("e_march".$i."_tva_id",${"e_march".$i."_tva_id"});
1938 $r.=HtmlInput::hidden('e_march'.$i.'_tva_amount', ${'e_march'.$i.'_tva_amount'});
1939 }
1940 $r.=HtmlInput::hidden("e_quant".$i,${"e_quant".$i});
1941
1942 }
1943
1944 /**
1945 * Payment method
1946 */
1947 if ( $e_mp!=0 && strlen (trim (${'e_mp_qcode_'.$e_mp})) != 0 )
1948 {
1949 $r.="<p>";
1950 $r.=HtmlInput::hidden('e_mp_qcode_'.$e_mp,${'e_mp_qcode_'.$e_mp});
1951 $r.=HtmlInput::hidden('acompte',$acompte);
1952 $r.=HtmlInput::hidden('e_comm_paiement',$e_comm_paiement);
1953 $r.=HtmlInput::hidden('mp_date',$mp_date);
1954 /* needed for generating a invoice */
1955 $r.=HtmlInput::hidden('qcode_benef', ${'e_mp_qcode_' . $e_mp});
1956 $fname = new Fiche($this->db);
1957 $fname->get_by_qcode(${'e_mp_qcode_' . $e_mp});
1958 $detail_payment="";
1959 // payment operation
1960 if ($this->payment_operation != 1) {
1961 $pay_internal=$this->db->get_value("select jr_internal from jrn where jr_id=$1",
1962 [$this->payment_operation]);
1963 $detail_payment=HtmlInput::detail_op($this->payment_operation,$pay_internal);
1964 }
1965 $r.='<h2>' . _("Payé par")." " . ${'e_mp_qcode_' . $e_mp} .
1966 " " . $fname->getName() .$detail_payment. '</h2> ' ;
1967 $r.='<p class="decale">' . _('Déduction acompte ') . h($acompte) . '</p>' .
1968 _('Libellé :') . h($e_comm_paiement) ;
1969 $r.='<br>';
1970 $r.='<br>';
1971
1972 $r.="</p>";
1973 }
1974 // check for upload piece
1975 /*
1976 * warning if the amount is positive and expecting a negative one
1977 */
1978 $negative=$this->display_negative_warning($tot);
1979 if ( $negative != "") {
1980 $r.=span($negative,'class="warning" ');
1981 }
1982 return $r;
1983 }
1984
1985 /*!\brief the function extra info allows to
1986 * - add a attachment
1987 * - generate an invoice
1988 * - insert extra info
1989 *\return html string
1990 */
1991 public function extra_info()
1992 {
1993 $r="";
1994 $r = '<div id="facturation_div_id" style="height:185px;height:10rem">';
1995 $r.='<p class="decale">';
1996 // check for upload piece
1997 $file=new IFile();
1998 $file->setAlertOnSize(true);
1999 $file->table=0;
2000 $r.=_("Ajoutez une pièce justificative ");
2001 $r.=$file->input("pj","");
2002
2003 if ( $this->db->count_sql("select md_id,md_name from document_modele where md_affect='ACH'") > 0 )
2004 {
2005
2006 $r.=_('ou générer un document').' <input type="checkbox" name="gen_invoice" >';
2007 // We propose to generate the fee note
2008 $doc_gen=new ISelect();
2009 $doc_gen->name="gen_doc";
2010 $doc_gen->value=$this->db->make_array(
2011 "select md_id,md_name ".
2012 " from document_modele where md_affect='ACH' order by 2");
2013 $r.=$doc_gen->input().'<br>';
2014 }
2015 $r.='<br>';
2016 $obj=new IText();
2017 $r.=_('Numero de bon de commande : ').$obj->input('bon_comm').'<br>';
2018 $r.=_('Autre information : ').$obj->input('other_info').'<br>';
2019 $r.='</p>';
2020 $r.='</div>';
2021 return $r;
2022 }
2023
2024
2025 /**
2026 * @brief update the payment
2027 * @todo to remove, obsolete
2028 * @deprecated
2029 */
2031 {
2032 // Show list of unpaid sell
2033 // Date - date of payment - Customer - amount
2034 // Nav. bar
2035 $step=$_SESSION[SESSION_KEY.'g_pagesize'];
2036 $page=(isset($_GET['offset']))?$_GET['page']:1;
2037 $offset=(isset($_GET['offset']))?$_GET['offset']:0;
2038
2039
2041 list($max_line,$list)=$this->list_operation($sql,null,$offset,1);
2042 $sql=SQL_LIST_UNPAID_INVOICE." and jr_def_id=".$this->id ;
2043 list($max_line2,$list2)=$this->list_operation($sql,null,$offset,1);
2044
2045 // Get the max line
2046 $m=($max_line2>$max_line)?$max_line2:$max_line;
2048
2049 echo $bar2;
2050 echo '<h2 class="info"> '._('Echeance dépassée').' </h2>';
2051 echo $list;
2052 echo '<h2 class="info"> '._('Non Payée').' </h2>';
2053 echo $list2;
2054 echo $bar2;
2055 // Add hidden parameter
2056 $hid=new IHidden();
2057
2058 echo '<hr>';
2059
2060 if ( $m != 0 )
2061 echo HtmlInput::submit('paid',_('Mise à jour paiement'));
2062
2063
2064 }
2065 /**
2066 * Retrieve data from the view v_detail_purchase
2067 * @note $g_user connected user
2068 * @param $p_from jrn.jr_tech_per from
2069 * @param $p_end jrn.jr_tech_per to
2070 * @return handle to database result
2071 */
2072 function get_detail_purchase($p_from,$p_end,$p_filter_operation='all')
2073 {
2074 global $g_user;
2075 // Journal valide
2076 if ( $this->id == 0 ) die (__FILE__.":".__LINE__." Journal invalide");
2077 switch ( $p_filter_operation)
2078 {
2079 case 'all':
2080 $sql_filter="";
2081 break;
2082 case 'paid':
2083 $sql_filter=" and (jr_date_paid is not null or jr_rapt ='paid' ) ";
2084 break;
2085 case 'unpaid':
2086 $sql_filter=" and (jr_date_paid is null and coalesce(jr_rapt,'x') <> 'paid' ) ";
2087 break;
2088 default:
2089 throw new Exception(_("Filtre invalide",5));
2090
2091 }
2092
2093 // get the data from the view
2094 $sql = "select *
2095 from v_detail_purchase
2096 where
2097 jr_def_id = $1
2098 and jr_date >= (select p_start from parm_periode where p_id = $2)
2099 {$sql_filter}
2100 and jr_date <= (select p_end from parm_periode where p_id = $3) "
2101 .' order by jr_date,substring(jr_pj_number,\'[0-9]+$\')::numeric asc ';
2102 $ret = $this->db->exec_sql($sql, array($this->id,$p_from, $p_end));
2103 return $ret;
2104 }
2105 /**
2106 * @brief compute an array with the heading cells for the
2107 * details, used for the export in CSV
2108 * @return array
2109 */
2110 static function heading_detail_purchase()
2111 {
2112 $array['jr_id'] = _('Numéro opération');
2113 $array['jr_date'] = _('Date');
2114 $array['jr_date_paid'] = _('Date paiement');
2115 $array['jr_ech'] = _('Date échéance');
2116 $array['jr_tech_per'] = _('Période');
2117 $array['jr_comment'] = _('Libellé');
2118 $array['jr_pj_number'] = _('Pièce');
2119 $array['jr_internal'] = _('Interne');
2120 $array['jr_def_id'] = _('Code journal');
2121 $array['j_poste'] = _('Poste');
2122 $array['j_text'] = _('Commentaire');
2123 $array['j_qcode'] = _('Code Item');
2124 $array['jr_rapt'] = _('Payé');
2125 $array['item_card'] = _('N° fiche');
2126 $array['item_name'] = _('Nom fiche');
2127 $array['qp_supplier'] = _('N° fiche fournisseur');
2128 $array['tiers_name'] = _('Nom fournisseur');
2129 $array['quick_code'] = _('Code fournisseur');
2130 $array['tva_label'] = _('Nom TVA');
2131 $array['tva_comment'] = _('Commentaire TVA');
2132 $array['tva_both_side'] = _('TVA annulée');
2133 $array['vat_sided'] = _('TVA Non Payé');
2134 $array['vat_code'] = _('Code TVA');
2135 $array['vat'] = _('Montant TVA');
2136 $array['price'] = _('Total HTVA');
2137 $array['quantity'] = _('quantité');
2138 $array['price_per_unit'] = _('PU');
2139 $array['non_ded_amount'] = _('Montant ND');
2140 $array['non_ded_tva'] = _('Montant TVA ND');
2141 $array['non_ded_tva_recup'] = _('TVA récup.');
2142 $array['htva'] = _('HTVA Opération');
2143 $array['tot_vat'] = _('TVA Opération');
2144 $array['tot_tva_np'] = _('TVA NP opération');
2145 $array['other_tax'] = _("Autre taxe");
2146 $array['oc_amount'] = _('Mont. Devise');
2147 $array['oc_vat_amount'] = _('Mont. TVA Devise');
2148 $array['cr_code_iso'] = _('Devise');
2149
2150 return $array;
2151 }
2152
2153
2154}
2155
2156
2157
2158
2159
isNumber($p_int)
Definition: ac_common.php:215
hb($p_string)
Definition: ac_common.php:53
span($p_string, $p_extra='')
Definition: ac_common.php:43
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
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'])
$idx
$url
$jr_id
Definition: ajax_ledger.php:44
$op jr_id
Definition: ajax_ledger.php:83
if(isset($_REQUEST['gDossier']) && $http->request("gDossier","number", 0) !=0) $repo
catch(Exception $e) $tva_rate
for($i=0; $i< count($plan); $i++)( $j==0) $a_poste
$input_from cn
Definition: balance.inc.php:66
$input_from id
Definition: balance.inc.php:63
$_GET['qcode']
Manage the account from the table jrn, jrnx or tmp_pcmn.
this class aims to compute different amount
compute_nd()
Compute the no deductible part of the amount, it reduce also the vat.
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
Handle the ledger of purchase,.
insert($p_array=null)
insert into the database, it calls first the verify function change the value of this->jr_id and this...
static heading_detail_purchase()
compute an array with the heading cells for the details, used for the export in CSV
insert_no_deductible(Acc_Compute $p_nd_amount, Fiche $p_fiche, $p_tva_both, &$p_tot_debit, Acc_Operation $p_acc_operation, $p_group, $idx)
Insert into JRNX the No Deductible amount and into Analytic Accountancy for the ND VAT.
input($p_array=null, $p_readonly=0)
display the form for entering data for invoice
compute_no_deductible(Acc_Compute $p_nd_amount, Fiche $p_fiche)
Compute the ND amount thanks the attribute of the concerned card.
verify_operation($p_array)
verify that the data are correct before inserting or confirming
confirm($p_array, $p_summary=false)
show the summary of the operation and propose to save it
show_unpaid_deprecated()
update the payment
get_detail_purchase($p_from, $p_end, $p_filter_operation='all')
Retrieve data from the view v_detail_purchase.
extra_info()
the function extra info allows to
$nb
!< type of the ledger ACH ODS FIN VEN or GL
inc_seq_pj()
increment the sequence for the pj
compute_internal_code($p_grpt)
compute the internal code of the saved operation and set the $this->jr_internal to the computed value
check_currency($p_qcode_payment, $p_currency_id)
When we write a record for the payment at the same time as a sale or a purchase, to have a bank saldo...
$row
!< database connextion
create_document($internal, $p_array)
create the invoice and saved it as attachment to the operation,
is_closed($p_periode)
check if the current ledger is closed
has_other_tax()
returns true if the ledger has an additional tax
update_internal_code($p_internal)
check_payment($e_mp, $e_mp_qcode)
check if the payment method is valid
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.
get_id($p_internal)
retrieve the jr_id thanks the internal code, do not change anything to the current object
check_currency_setting($p_currency_code)
Check that the currency code does exist and the setting of the folder is correct.
this file match the tables jrn & jrnx the purpose is to remove or save accountant writing to these ta...
insert_jrnx()
Insert into the table Jrn The needed data are :
Manage the table parm_code which contains the custom parameter for the module accountancy.
Handle the table payment_method.
new class for managing the reconciliation it must be used instead of the function InsertRapt,...
Acc_Tva is used for to map the table tva_rate parameter are.
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
strAttribut($p_ad_id, $p_return=1)
empty_attribute($p_attr)
check if an attribute is empty
static ledger_add_item($p_ledger)
Build a HTML string for adding multiple rows.
static detail_op($p_jr_id, $p_mesg)
return a string containing the html code for calling the modifyOperation
static show_receipt_document($p_jr_id, $p_name="")
Returns HTML code for displaying a icon with a link to a receipt document from the ledger.
static hidden($p_name, $p_value, $p_id="")
static submit($p_name, $p_value, $p_javascript="", $p_class="smallbutton")
static post_to_hidden($array)
transform $_POST data to hidden
manage the http input (get , post, request) and extract from an array
Input HTML for the card show buttons, in the file, you have to add card.js How to use :
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
Html Input.
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 , create a tag <SELECT> ... </SELECT> if readonly == true then display the label correspon...
Html Input.
Definition: ispan.class.php:32
Html Input.
Definition: itext.class.php:30
let you choose a TVA in a popup
static infobulle($p_comment)
Display a info in a bubble, text is in message_javascript.
ORM of the table public.jrn_tax.
mother class for the lettering by account and by card use the tables jnt_letter, letter_deb and lette...
static echo_var($n_level, $msg, $print=true)
Display the value of a var if DEBUGNOALYSS is greater than $n_level, the debugging info has a certain...
Definition: dbg.php:45
ORM abstract of the table public.operation_currency.
For the periode tables parm_periode and jrn_periode.
manage the predefined operation, link to the table op_def and op_def_detail
static insert_goods(&$p_cn, $p_array)
Insert into stock_goods from ACH and VEN.
$check_periode
$def
show a form for quick_writing
$acompte
if( $g_parameter->MY_PJ_SUGGEST=='Y') $e_date
$other_tax_amount
Definition: compute.php:87
const ATTR_DEF_ADRESS
Definition: constant.php:223
const OPEN
Definition: constant.php:201
const ATTR_DEF_TVA_NON_DEDUCTIBLE_RECUP
Definition: constant.php:236
const ATTR_DEF_ACCOUNT_ND_TVA_ND
Definition: constant.php:242
const ATTR_DEF_NAME
Definition: constant.php:216
const SQL_LIST_UNPAID_INVOICE
Definition: constant.php:329
const ATTR_DEF_ACCOUNT_ND
Definition: constant.php:244
const ATTR_DEF_ACCOUNT_ND_TVA
Definition: constant.php:241
const ATTR_DEF_CP
Definition: constant.php:224
const ATTR_DEF_TVA
Definition: constant.php:221
const SQL_LIST_UNPAID_INVOICE_DATE_LIMIT
Definition: constant.php:333
const ATTR_DEF_ACCOUNT_ND_PERSO
Definition: constant.php:243
const ATTR_DEF_DEP_PRIV
Definition: constant.php:233
const ATTR_DEF_TVA_NON_DEDUCTIBLE
Definition: constant.php:235
const ATTR_DEF_CITY
Definition: constant.php:229
const ATTR_DEF_DEPENSE_NON_DEDUCTIBLE
Definition: constant.php:234
const ATTR_DEF_QUICKCODE
Definition: constant.php:237
const ATTR_DEF_ACCOUNT
Definition: constant.php:215
const FICADD
const UPDRECEIPT
$jrn_type[]
$_POST['ac']
Definition: do.php:310
$oPeriode
Definition: do.php:154
$Res
$p_end
$SecUser db
$dir
Definition: file-dir.php:3
create_script($p_string)
create the HTML for adding the script tags around of the script
$str_code
Definition: menu_detail.php:6
$str_file
Definition: menu_detail.php:11
$sql_filter
Definition: preod.inc.php:43
$date_limit
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...
navigation_bar($p_offset, $p_line, $p_size=0, $p_page=1, $p_javascript="")
Create a navigation_bar (pagesize)
Definition: user_common.php:76