noalyss Version-9
anc_operation.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 definition of Anc_Operation
24 */
25require_once NOALYSS_INCLUDE.'/lib/user_common.php';
26
27/*! \brief this class is used to show the form for entering an
28 * operation only FOR analytic operation
29 * to save it, to display or to get a list from a certain period
30 *
31 */
33{
34 var $db; /*!< database connection */
35 var $row; /*!< array of row for one operation*/
36 var $list; /*!< array of all operation */
37 var $id; /*!< = oa_id (one row) */
38 var $po_id; /*!< poste analytique */
39 var $oa_amount; /*!< amount for one row */
40 var $oa_description; /*!< comment for one row */
41 var $oa_debit; /*!< true for debit or false */
42 var $j_id; /*!< foreign key to a jrnx operation
43 (or null if none */
44 var $oa_group; /*!< group of operation */
45 var $oa_date; /*!< equal to j_date if j_id is not null */
46 var $pa_id; /*!< the plan analytique id */
47 var $card; /*!< Card linked to the operation */
48 private $currency_rate; /*!< currency rate */
49 /**
50 * In the case, the amount comes from a ND VAT, the variable
51 * contents the jrnx.j_id of the source which was used to compute
52 * the amount
53 */
56 var $oa_id;
58 public function get_currency_rate()
59 {
61 }
62
63 public function __toString(): string
64 {
65 $r=<<<EOF
66Object Anc_Operation [
67 \$row=>$this->row,
68 \$list=> $this->list,
69 \$id => $this->id,
70 \$po_id => $this->po_id,
71 \$oa_amount $this->oa_amount,
72 \$oa_description => $this->oa_description,
73 \$oa_debit => $this->oa_debit,
74 \$j_id => $this->j_id,
75 \$oa_group => $this->oa_group,
76 \$oa_date => $this->oa_date,
77 \$pa_id => $this->pa_id,
78 \$card => $this->card ,
79 private \$currency_rate => $this->currency_rate,
80 ]
81EOF;
82 return $r;
83
84 }
85
87 {
88 $this->currency_rate=$currency_rate;
89 return $this;
90 }
91
92 /**
93 * @brief signed of the amount
94 */
96 var $in_div; /*< name of the div if any, default empty string*/
97 /*!\brief constructor
98 *
99 */
100 function __construct ($p_cn,$p_id=0)
101 {
102 $this->db=$p_cn;
103 $this->id=$p_id;
104 $this->oa_jrnx_id_source=null;
105 $this->oa_positive='Y';
106 $this->has_data=0;
107 $this->in_div="";
108 $this->card="";
109 $this->currency_rate=1;
110 }
111 /*!\brief add a row to the table operation_analytique
112 * \note if $this->oa_group == 0 then a sequence id will be computed for
113 * the oa_group, if $this->j_id=0 then it will be null
114 *
115 */
116 function add($p_seq=0)
117 {
118
119 if ( $this->oa_group == 0)
120 {
121 $this->oa_group=$this->db->get_next_seq('s_oa_group');
122 }
123
124 if ( $this->j_id == 0 )
125 {
126 $this->j_id=null;
127 } else {
128 // must be the same side than the operation
129 if ( $this->oa_jrnx_id_source == null)
130 {
131 $side=$this->db->get_value('select j_debit from jrnx where j_id=$1',
132 array($this->j_id));
133 } else
134 {
135 $side=$this->db->get_value('select j_debit from jrnx where j_id=$1',
136 array($this->oa_jrnx_id_source));
137 }
138 $this->oa_debit=$side;
139 }
140
141
142 // we don't save null operations
143 if ( $this->oa_amount == 0 || $this->po_id==-1)
144 return;
145
146 if ( $this->oa_amount< 0)
147 {
148 // if negatif must be oa_positive='N'
149 $this->oa_positive='N';
150 $this->oa_debit=($this->oa_debit=='t')?'f':'t';
151 }
152
153 // Retrieve the f_id of the card
154 $n_fid=0;
155 if ( $this->card != "") {
156 $fiche=new Fiche($this->db);
157 $fiche->get_by_qcode($this->card);
158 $n_fid=$fiche->id;
159 }
160 $n_fid=($n_fid!=0)?$n_fid:NULL;
161 $oa_row=(isset($this->oa_row))?$this->oa_row:null;
162 $sql="insert into operation_analytique (
163 po_id,
164 oa_amount,
165 oa_description,
166 oa_debit,
167 oa_group,
168 j_id,
169 oa_date,
170 oa_row,
171 oa_jrnx_id_source,
172 oa_positive,
173 f_id
174 ) values ($1,$2,$3,$4,$5,$6,to_date($7,'DD.MM.YYYY'),$8,$9,$10,$11)";
175
176 $this->db->exec_sql($sql,array(
177 $this->po_id, // 1
178 abs($this->oa_amount), //2
179 $this->oa_description, //3
180 $this->oa_debit, //4
181 $this->oa_group, //5
182 $this->j_id, //6
183 $this->oa_date, //7
184 $oa_row, //8
185 $this->oa_jrnx_id_source, //8
186 $this->oa_positive,
187 $n_fid // fiche.f_id , can be null
188 ));
189
190 }
191 /*!\brief delete a row from the table operation_analytique
192 * \note be carefull : do not delete a row when we have a group
193 */
194 function delete()
195 {
196 $sql="delete from operation_analytique where oa_id=$1";
197
198 $this->db->exec_sql($sql,array($this->oa_id));
199 }
200
201 function get_list_simple($p_from,$p_to)
202 {
203 $cond="";
204 $where=" where ";
205
206 if ($p_from!="") {
207 $cond="$where (oa_date >= to_date('$p_from','DD.MM.YYYY') or oa_date >= to_date('$p_from','DD.MM.YYYY') )";
208 $where=" and ";
209 }
210 if ( $p_to!="" ) {
211 $cond.="$where (oa_date <=to_date('$p_to','DD.MM.YYYY') or oa_date <=to_date('$p_to','DD.MM.YYYY')) ";
212 $where=" and ";
213 }
214
215 $cond .= $where." j_id is null ";
216
217 $sql="
218 select distinct oa_group,
219 to_char(oa_date,'DD.MM.YYYY') as str_date ,
220 oa_date,
221 oa_description
222 from
223 operation_analytique as oa
224 $cond
225 order by oa_date ";
226 return $this->db->get_array($sql);
227
228 }
229
230 /*!\brief get a list of row from a certain periode
231 */
232 function get_list($p_from,$p_to,$p_from_poste="",$p_to_poste="")
233 {
234 $cond="";
235 $cond_poste="";
236
237 if ($p_from!="")
238 $cond="and (jr_date >= to_date('$p_from','DD.MM.YYYY') or oa_date >= to_date('$p_from','DD.MM.YYYY') )";
239 if ( $p_to!="" )
240 $cond.="and (jr_date <=to_date('$p_to','DD.MM.YYYY') or oa_date <=to_date('$p_to','DD.MM.YYYY')) ";
241
242 if ($p_from_poste != "" )
243 $cond_poste=" and upper(po_name) >= upper('".$p_from_poste."')";
244 if ($p_to_poste != "" )
245 $cond_poste.=" and upper(po_name) <= upper('".$p_to_poste."')";
246 $pa_id_cond="";
247 if ( isset ( $this->pa_id) && $this->pa_id !='')
248 $pa_id_cond= "pa_id=".$this->pa_id." and";
249 $sql="
250
251 select B.po_id as po_id,
252 oa_id,
253 po_name,
254 oa_description,
255 po_description,
256 oa_debit,
257 (case when jr_date is not null then to_char(jr_date,'DD.MM.YYYY') else to_char(oa_date,'DD.MM.YYYY') end ) as oa_date,
258 (case when jr_date is not null then to_char(jr_date,'YYYYMMDD') else to_char(oa_date,'YYYYMMDD') end ) as str_order_date,
259 oa_amount,
260 oa_group,
261 j_id ,
262 jr_internal,
263 jr_pj_number ,
264 jr_id,
265 coalesce(jr_comment,b.oa_description) as jr_comment,
266 case when j_poste is null and b.f_id is not null then
267 (select ad_value from fiche_detail where fiche_detail.f_id=b.f_id and ad_id=".ATTR_DEF_ACCOUNT.")
268 when j_poste is not null then
269 j_poste
270 end as j_poste
271 ,
272 coalesce(jrnx.f_id,b.f_id) as f_id,
273 case when jrnx.f_id is not null then
274 (select ad_value from fiche_Detail where f_id=jrnx.f_id and ad_id=23)
275 when b.f_id is not null then
276 (select ad_value from fiche_Detail where f_id=b.f_id and ad_id=23)
277 end
278 as qcode,
279 jr_pj_number
280 from operation_analytique as B join poste_analytique using(po_id)
281 left join jrnx using (j_id)
282 left join jrn on (j_grpt=jr_grpt_id)
283 where $pa_id_cond oa_amount <> 0.0 $cond $cond_poste
284 order by jr_date,oa_group,oa_debit desc,oa_id";
285
286 $RetSql=$this->db->exec_sql($sql);
288 return $array;
289 }
290
291 /*\brief show the HTML table for the operation
292 */
293 function html_table($p_from)
294 {
295 if ($p_from=="")
296 {
297 $from="";
298 $to="";
299 }
300 else
301 {
302 $p=new Periode($this->db);
303 list($from,$to)=$p->get_date_limit($p_from);
304 }
305
307
308 if ( empty($array) )
309 return _("Pas d'enregistrement trouvé");
310
311 // navigation_bar
312 $step=$_SESSION[SESSION_KEY.'g_pagesize'];
313 $page=(isset($_GET['offset']))?$_GET['page']:1;
314 $offset=(isset($_GET['offset']))?$_GET['offset']:0;
316
317 if ( $step !=-1)
318 $view=array_splice($array,$offset,$step);
319 else
320 $view=$array;
321
323 $ret="";
324 $ret.=$bar;
325
326 $count=0;
327 $group=0;
328 $oldgroup=0;
329 $oldjrid=0;
330 $ret.=_("Chercher")." ".HtmlInput::filter_table("anc_operation_list_tb", '0,1,2', 1);
331 $ret.= "<table id=\"anc_operation_list_tb\"class=\"result\">";
332 $ret.=th(_("Date"));
333 $ret.=th(_("Libellé"));
334 $ret.=th("");
335 $ret.=th("");
336 $i=0;
337 foreach ($view as $row)
338 {
339 $class=($i%2 == 0)?'class="even"':' class="odd"';
340 $i++;
341
342 if ( $oldgroup <> $row['oa_group']) {
343 $oldgroup=$row['oa_group'];
344 }
345 $row_id=sprintf('id="tr%s"',$oldgroup);
346
347 $ret.="<tr $row_id $class>";
348 $ret.=td($row['str_date']);
349 $ret.=td(h($row['oa_description']));
350 $js="anc_remove_operation(".$gDossier.",".$oldgroup.")";
351
352 $ret.="<td>".Icon_Action::trash(uniqid(), $js) ."</td>";
353 $js="anc_detail_op({$row['oa_group']},{$gDossier})";
354 $ret .= "<td>". Icon_Action::modify(uniqid(), $js)."</td>";
355 $ret.="</tr>";
356 }
357 $ret.='</table>';
358 $ret.=$bar;
359
360 return $ret;
361 }
362 /*!\brief retrieve an operation thanks a jrnx.j_id
363 * \param the jrnx.j_id
364 * \return null if nothing is found other an array
365 */
366 function get_by_jid($p_jid)
367 {
368 $array=array();
369 $a_plan=$this->db->get_array('select pa_id from plan_analytique order by pa_id');
370 $res=array();
371 /*
372 * For one oa_row
373 */
374 $a_rowcount=$this->db->get_array("select distinct oa_row "
375 ." from operation_analytique where j_id=$1 order by oa_row", array($p_jid));
376
377 for ($i=0; $i<count($a_rowcount); $i++)
378 {
379 /*
380 * Get one row as template for filling the missing
381 */
382 $a_existing=$this->db->get_array('
383 select distinct oa_id,
384 po_id,
385 oa_amount,
386 oa_description,
387 oa_debit,
388 j_id,
389 oa_group,
390 oa_date,
391 pa_id,
392 oa_row,
393 oa_positive
394 from operation_analytique join poste_analytique using (po_id)
395 where
396 j_id=$1 and oa_row = $2
397 order by j_id,oa_row',
398 array($p_jid, $a_rowcount[$i]['oa_row']));
399
400 // the first row we found will be the template
401 $template=$a_existing[0];
402 /*
403 * For each plan
404 */
405 for ($j=0; $j<count($a_plan); $j++)
406 {
407 /*
408 * Fetch the row with this pa_id, oa_row, max : 1 row
409 */
410 $a_fetch=$this->db->get_array('
411 select distinct oa_id,
412 po_id,
413 oa_amount,
414 oa_description,
415 oa_debit,
416 j_id,
417 oa_group,
418 oa_date,
419 pa_id,
420 oa_row,
421 oa_positive
422 from operation_analytique join poste_analytique using (po_id)
423 where
424 j_id=$1 and oa_row = $2 and pa_id=$3', array($p_jid,
425 $a_rowcount[$i]['oa_row'],
426 $a_plan[$j]['pa_id']
427 )
428 );
429 if (count($a_fetch)==0)
430 {
431 $a_fetch=$template;
432 $a_fetch['pa_id']=$a_plan[$j]['pa_id'];
433 $a_fetch['po_id']=-1;
434 $a_fetch['oa_id']='';
435 $res[]=$a_fetch;
436 }
437 else
438 if (count($a_fetch)==1)
439 {
440 $res[]=$a_fetch[0];
441 }
442 }
443 }
444
445 foreach ($res as $row)
446 {
447 $a=new Anc_Operation($this->db);
448 foreach ($row as $attr=> $value)
449 {
450 $a->$attr=$row[$attr];
451 }
452 $array[]=clone $a;
453 }
454
455
456 return $array;
457 }
458
459 /*!\brief modify an op from modify_op.php
460 *
461 */
462 function update_from_jrnx($p_po_id)
463 {
464 $a=$this->get_by_jid($this->j_id);
465 if ( $a == null )
466 {
467 // retrieve data from jrnx
468 $sql="select jr_date,j_montant,j_debit from jrnx ".
469 " join jrn on (jr_grpt_id = j_grpt) ".
470 "where j_id=".$this->j_id;
471 $res=$this->db->exec_sql($sql);
472 if (Database::num_row($res) == 0 ) return;
474 $this->oa_amount=$row['j_amount'];
475 $this->oa_date=$row['jr_date'];
476 $this->oa_debit=$row['j_debit'];
477 $this->oa_description=$row['jr_comment'];
478 $this->add();
479 }
480 else
481 {
482 foreach ($a as $row )
483 {
484 if ( $row->pa_id == $this->pa_id )
485 {
486 $row->po_id=$p_po_id;
487 $row->update();
488 }
489 }
490 }
491 }
492 /*!\brief retrieve the jr_id thanks the oa_group */
493 function get_jrid()
494 {
495 $sql="select distinct jr_id from jrn join jrnx on (j_grpt=jr_grpt_id) join operation_analytique using (j_id) where j_id is not null and oa_group=".$this->oa_group;
496 $res=$this->db->exec_sql($sql);
497 if ( Database::num_row($res) == 0 ) return 0;
499 return $ret[0]['jr_id'];
500 }
501 /*\brief this function get the balance for a certain period
502 *\param $p_from from date (accountancy period)
503 *\param $p_to to dat (accountancy period)
504 *\param $p_plan_id the plan id
505 */
506 function get_balance($p_from,$p_to,$p_plan_id)
507 {
508 // for the operation connected to jrnx
509 $cond=sql_filter_per($this->db,$p_from,$p_to,'p_id','j_date');
510 $sql="select oa_id, po_id, oa_amount, oa_debit, j_date from jrnx join operation_analytique using (j_id)
511 join poste_analytique using (po_id)
512 where
513 $cond and j_id is not null and pa_id=$p_plan_id";
514
515 // OD
516 $cond=sql_filter_per($this->db,$p_from,$p_to,'p_id','oa_date');
517 $sql="union select oa_id, po_id, oa_amount, oa_debit,oa_date from
518 operation_analytique
519 join poste_analytique using (po_id)
520 where j_id is null and
521 $cond and pa_id=$p_plan_id ";
522 try
523 {
524 $res=$this->db->exec_sql($sql);
526 }
527 catch (Exception $e)
528 {
529 record_log($e);
530 }
531 }
532 /*!
533 * \brief display the form for PA
534 *\param $p_array contains POST (or GET) data (val[] hplan[][] op[])
535 * \param $p_null = 1 if PA optional otherwise 0 mandatory
536 * \param $p_mode == form 1 ==> read/write otherwise 0==>readonly
537 * \param $p_seq number of the row
538 * \param $p_amount amount
539 * \param $p_id operation is detailled in a HTML popup, if several
540 * are opened, the tableid MUST be different. So we need to use a new parameter
541 * \param $p_add_button true, show the button, false don't display them
542 * \see save_form_plan
543 @note
544 - op is an array containing the line number
545 - pa_id is an array of the existing array
546 - hplan is an array of the POSTE ANALYTIQUE id used, the size of hplan from 0 to x,
547 x can be bigger than the number of plan id
548 - val contains the amount by row inside the table. One operation (j_id) you can have several rows
549 @code
550 0 =>
551 array
552 'op' => int 200
553 'pa_id' =>
554 array
555 0 => string '14' (length=2)
556 1 => string '15' (length=2)
557 'hplan' =>
558 array
559 1 =>
560 array
561 0 => string '25' (length=2)
562 1 => string '26' (length=2)
563 'val' =>
564 array
565 1 =>
566 array
567 0 => string '100.0000' (length=8)
568
569 @endcode
570 */
571 function display_form_plan($p_array,$p_null,$p_mode,$p_seq,$p_amount,$p_id='',$p_add_button=true)
572 {
573 if ( $p_array != null)
574 extract ($p_array, EXTR_SKIP);
575 $result="";
576 $plan=new Anc_Plan($this->db);
577 $a_plan=$plan->get_list(" order by pa_id ");
578 if ( empty ($a_plan) ) return "";
579 $table_id="t".$p_seq;
580 $hidden=new IHidden();
581
582 $readonly=($p_mode==1)?false:true;
583
584 if ($p_mode==1)
585 {
586 $result .= sprintf('<input type="HIDDEN" id="amount_%s" class="%s%s-amount" name="amount_%s" value="%s">',
587 $table_id,
588 $this->in_div,
589 $table_id,
590 $table_id,
591 $p_amount
592 );
593
594 $result.='<table id="'.$p_id.$table_id.'">';
595 }
596 else
597 {
598 $result.='<table>';
599 }
600 $result.="<tr>".$plan->header()."<th>"._("montant")."</th></tr>";
601
602 /* compute the number of rows */
603 $nb_row=(isset($val[$p_seq]))?count($val[$p_seq]):1;
604 $count=0;
605
606 $remain=abs($p_amount);
607 $ctrl_remain="remain".$this->in_div.$table_id;
608
609 for ( $i=0; $i < $nb_row;$i++)
610 {
611 $result.='<tr>';
612
613 foreach ($a_plan as $r_plan)
614 {
615 $existing=(isset($hplan[$p_seq][$count]))?$hplan[$p_seq][$count]:-1;
616 $array=$this->db->make_array(
617 "select po_id as value,".
618 " html_quote(po_name) as label from poste_analytique ".
619 " where pa_id = $1 ".
620 " and ( po_state = 1 or po_id = $2) ".
621 " order by po_name",$p_null,[$r_plan['id'],$existing]);
622 $select =new ISelect("hplan[".$p_seq."][]",$array);
623 $select->table=0;
624 // view only or editables
625 if ( $p_mode==1 )
626 {
627 // editable
628 $select->readonly=false;
629 if ( isset($hplan) && isset($hplan[$p_seq][$count]) ){
630 $select->selected=$hplan[$p_seq][$count];
631
632 }
633 }
634 else
635 {
636 if ( isset($hplan) && isset($hplan[$p_seq][$count]) ){
637 $select->selected=$hplan[$p_seq][$count];
638 }
639 // view only
640 $select->readOnly=true;
641 }
642 if ($p_mode==1)
643 {
644 $result.='<td>'.$select->input().'</td>';
645 }
646 else
647 {
648 $result.='<td>'.$select->display().'</td>';
649 }
650 $count++;
651
652
653 }
654 $value=new INum();
655 $value->javascript='onchange="format_number(this);anc_refresh_remain(\''.$this->in_div.$table_id.'\',\''.$p_seq.'\')"';
656 $value->name=($readonly)?"ro"."val[".$p_seq."][]":"val[".$p_seq."][]";
657 $value->size=6;
658 $value->value=(isset($val[$p_seq][$i]))?$val[$p_seq][$i]:abs($p_amount);
659 $value->value=round($value->value,2);
660 $value->style='class="inum '.$this->in_div.$table_id.'-value-'.$p_seq.'"';
661 $value->readOnly=($p_mode==1)?false:true;
662 $remain=bcsub($remain,$value->value);
663 $result.='<td>'.$value->input().'</td>';
664
665 $result.="</tr>";
666 }
667
668 $result.="</table>";
669
670 if ($p_add_button && $p_mode == 1)
671 {
672 $style_remain=($remain==0)?'style="color:green"':' style="color:red"';
673 $result.=" "._("Reste")." = ".
674 '<span class="remain" '.$style_remain.' id="'.$ctrl_remain.'">'.
675 $remain.'</span>';
676 // add a button to add a row
677 $button=new IButton();
678 $button->javascript="add_row('".$p_id."$table_id',$p_seq);";
679 $button->name="js".$p_id.$p_seq;
680 $button->label=_("Nouvelle ligne");
681
682 $result.="<br>".$button->input();
683 /**
684 * Add a button for distribution key
685 *
686 */
687 $http=new HttpInput();
688 $ledger=$http->post("p_jrn", "string",0);
689 if ($ledger==0) {
690 $ledger=$this->db->get_value('select j_jrn_def from jrnx where j_id=$1',array($this->j_id));
691 }
692 $gDossier=Dossier::id();
693 $button_key=new IButton();
694 $button_key->javascript="anc_key_choice(".$gDossier.",'".$p_id."$table_id',$p_amount,'".$ledger."');";
695 $button_key->name="js".$p_id.$p_seq;
696 $button_key->label=_("Clef");
697 $result .= $button_key->input();
698 /**
699 * Add a button to clean the detail
700 */
701 $button_clean=new IButton();
702 $button_clean->javascript=sprintf("anc_key_clean('%s','%s','%s','%s','%s','%s');",
703 $gDossier,
704 $p_id,
705 $p_amount,
706 $ledger,
707 $this->j_id,
708 $p_seq
709 );
710 $button_clean->name=uniqid();
711 $button_clean->label=_("Efface détail");
712 $result.=$button_clean->input();
713
714
715 }
716
717 return $result;
718 }
719 /**
720 * Save the ND VAT with prorata
721 *
722 * @param $p_array usually $_POST
723 * @param $p_item item of the form
724 * @param $p_j_id jrnx.j_id concerned writing
725 * @param $p_nd amount nd vat
726 * @see Anc_Operation::save_form_plan_vat_nd
727 * @return type
728 */
729 function save_form_plan_vat_nd($p_array,$p_item,$p_j_id,$p_nd)
730 {
731 bcscale(4);
732 extract($p_array, EXTR_SKIP);
733 if (! isset ($hplan) ) return;
734
735 if ( ! isset(${'amount_t'.$p_item}) )
736 throw new Exception ('amount not set');
737
738 $tot=0;
739 /* variable for in array
740 pa_id array of existing pa_id
741 hplan double array with the pa_id (column)
742 val double array by row with amount
743 op contains sequence
744 p_item is used to identify what op is concerned
745 */
746 /* echo "j_id = $j_id p_item = $p_item hplan=".var_export($hplan[$p_item],true)." val = ".var_export($val[$p_item],true).'<br>'; */
747 /* for each row */
748 // for ($i=0;$i<count($val[$p_item]);$i++) {
749 $idx_pa_id=0;
750 $row=0;
751 $a_Anc_Operation=array();
752 // foreach col PA
753 for ($e=0;$e<count($hplan[$p_item]);$e++)
754 {
755 if ( $idx_pa_id == count($pa_id))
756 {
757 $idx_pa_id=0;
758 $row++;
759 }
760 if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
761 {
762 $op=new Anc_Operation($this->db);
763 $op->po_id=$hplan[$p_item][$e];
764 $op->oa_group=$this->oa_group;
765 $op->j_id=$p_j_id;
766 $ratio=0;
767 if (${"amount_t".$p_item} != 0 ) {
768 $ratio=bcdiv($val[$p_item][$row],${"amount_t".$p_item});
769 }
770 $amount= bcmul($p_nd, $ratio);
771 // convert to euro
772 $amount=bcmul($amount,$this->currency_rate);
773 $op->oa_amount=round($amount,2);
774 $op->oa_debit=$this->oa_debit;
775 $op->oa_date=$this->oa_date;
776
777 $op->oa_description=$this->oa_description;
778 $op->oa_row=$row;
779 $op->oa_jrnx_id_source=$this->oa_jrnx_id_source;
780 $a_Anc_Operation[]=clone $op;
781 }
782 $idx_pa_id++;
783 }
784 $nb_op=count($a_Anc_Operation);
785 bcscale(4);
786 for ($i=0;$i<$nb_op;$i++)
787 {
788 $tot=bcadd($tot,$a_Anc_Operation[$i]->oa_amount);
789 }
790// utilité ???
791// if ( $tot != $p_nd && count($a_Anc_Operation) > 0 )
792// {
793// $diff= bcsub($tot, $p_nd);
794// $a_Anc_Operation[0]->oa_amount=bcsub($a_Anc_Operation[0]->oa_amount,$diff);
795// }
796 for ($i=0;$i<$nb_op;$i++)
797 {
798 $a_Anc_Operation[$i]->add();
799 }
800 }
801 /*!\brief it called for each item, the data are taken from $p_array
802 * data and set before in this. Amount will be transformed thanks the $this->currency_rate;
803 * \param $p_item if the item nb for each item (purchase or selling
804 * merchandise)
805 * \param $p_array structure
806
807 nb_tx x is the number of the item it contains the number of
808 rows of CA for this card
809 <h1>val double array : amount for the CA row ACC => sub row ANC </h1>
810 here Operation 0 has 2 row for a total of 10.31 and 1
811 here Operation 1 has 1 row for a total of 430
812 \verbatim
813 [val] => Array
814 (
815 [0] => Array
816 (
817 [0] => 10.31
818 [1] => 1
819 )
820
821 [1] => Array
822 (
823 [0] => 430
824 )
825
826 )
827\endverbatim
828 <h1>amount_tx the amount for each row</h1>
829 It is the amount to split per accounting, it is possible that it is different from the sum of the given amount (<i>
830 see val</i>) per row
831 \verbatim
832 [amount_t0] => 11.31
833 [amount_t1] => 430
834 \endverbatim
835 ta_AoCrow_R contains the value of the pa_id and po_id for this
836 row with the form pa_id_po_id %d_%d
837 <h1> hplan = Double array operation</h1>
838 <p> Double array operation => all the amount but in linear form</p>
839
840 <p>Example if the amount of the operation 0 is splitted in 2 rows and 2 cols (because 2 analytic plan, means 2 cols)
841 we have an array of 4 values for operation 0, it gives the poste_analytique.po_id (from DB).</p>
842 <p> Here is the operation 0
843 (one row of the accounting) is splitted in po_id = 1 (pa_id see in tables or array pa_id) and po_id=5 (second pa_id)
844 for 10.31 (<i>see array val</i>) and for 1 is set for po_id 1 and 4, p
845 </p>
846 \verbatim
847 [hplan] => Array
848 (
849 [0] => Array
850 (
851 [0] => 1
852 [1] => 5
853 [2] => 1
854 [3] => 4
855 )
856
857 [1] => Array
858 (
859 [0] => 1
860 [1] => 6
861 )
862
863 )
864 \endverbatim
865
866 <h1> pa_id </h1>
867 pa_id array of plan_analytic.pa_id (1 per column), always ordered by pa_id
868
869 \verbatim
870 [pa_id] => Array
871 (
872 [0] => 1
873 [1] => 2
874 )
875
876 *\endverbatim
877 *
878 * \attention The idea is one j_id matches several oa_id,
879 * serveral data <b>must be </b>set before the call :
880 *
881 * <ul><li> j_id</li>
882 * <li>oa_debit</li>
883 * <li>oa_group</li>
884 * <li>oa_date</li>
885 * <li>oa_description</li>
886 * </ul>
887 *
888 */
889 function save_form_plan($p_array,$p_item,$p_j_id)
890 {
891 extract($p_array, EXTR_SKIP);
892 if (! isset ($hplan) ) return;
893 /* variable for in array
894 pa_id array of existing pa_id
895 hplan double array with the pa_id (column)
896 val double array by row with amount
897 op contains sequence
898 p_item is used to identify what op is concerned
899 */
900 /* for each row */
901 $idx_pa_id=0;
902 $row=0;
903
904 if ( ! isset ($hplan[$p_item])) return;
905 // foreach col PA
906 for ($e=0;$e<count($hplan[$p_item]);$e++)
907 {
908 if ( $idx_pa_id == count($pa_id))
909 {
910 $idx_pa_id=0;
911 $row++;
912 }
913 if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
914 {
915 $op=new Anc_Operation($this->db);
916 $op->po_id=$hplan[$p_item][$e];
917 $op->oa_group=$this->oa_group;
918 $op->j_id=$p_j_id;
919 // convert oa_amount to EUR
920 $op->oa_amount=bcdiv($val[$p_item][$row],$this->currency_rate,2);
921 $op->oa_debit=$this->oa_debit;
922 $op->oa_date=$this->oa_date;
923
924 $op->oa_description=$this->oa_description;
925 $op->oa_row=$row;
926 $op->add();
927 }
928 $idx_pa_id++;
929 }
930 // }
931 }
932
933 /**
934 *@brief save a whole form from a update box
935 *@param $p_array for ALL j_id
936 *@return
937 *@note
938 *@see save_form_plan to_request
939 @code
940
941 @endcode
942 */
943 function save_update_form($p_array)
944 {
945 extract($p_array, EXTR_SKIP);
946 if ( ! isset($opanc)) return;
947 for ($i = 0; $i < count($opanc); $i++)
948 {
949 /* clean operation_analytique */
950 $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($opanc[$i]));
951
952 /* get missing data for adding */
953 $a_missing = $this->db->get_array("select to_char(jr_date,'DD.MM.YYYY')
954 as mdate,j_montant,j_debit,jr_comment ,j_poste
955 from jrnx join jrn on (j_grpt=jr_grpt_id) where j_id=$1", array($opanc[$i]));
956 $missing = $a_missing[0];
957
958 $this->oa_description = $missing['jr_comment'];
959 $this->j_id = $opanc[$i];
960 $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
961 $this->oa_group = $group;
962 $this->oa_date = $missing['mdate'];
963 $this->save_form_plan($p_array, $i, $opanc[$i]);
964
965 // There is ND VAT amount
966 $a_nd = $this->db->get_array('select j_id from operation_analytique
967 where oa_jrnx_id_source=$1', array($opanc[$i]));
968 if (count($a_nd) > 0)
969 {
970 // for each ND VAT amount
971 for ($e=0;$e<count($a_nd);$e++)
972 {
973 $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($a_nd[$e]['j_id']));
974 /* get missing data for adding */
975 $a_missing_vat = $this->db->get_array("select to_char(jr_date,'DD.MM.YYYY') as mdate,j_montant,j_debit,jr_comment from jrnx join jrn on (j_grpt=jr_grpt_id) where j_id=$1", array($a_nd[$e]['j_id']));
976 $missing_vat = $a_missing_vat[0];
977 $this->oa_debit = 't';
978 $this->oa_description = $missing_vat['jr_comment'];
979 $this->j_id = $opanc[$i];
980 $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
981 $this->oa_group = $group;
982 $this->oa_date = $missing_vat['mdate'];
983 $this->oa_jrnx_id_source=$opanc[$i];
984 $p_array['amount_t'.$i]=$missing['j_montant'];
985 $this->save_form_plan_vat_nd($p_array, $i, $a_nd[$e]['j_id'],$missing_vat['j_montant']);
986 }
987 }
988 }
989 }
990
991 /*\brief transform a array of operation into a array usage by
992 *display_form_plan & save_form_plan
993 *\param $p_array array of operation
994 *\param $p_line line
995 *\return an array complying with \see save_form_plan
996 */
997 function to_request ($p_array,$p_line)
998 {
999 $result=array();
1000 $result[]=array('op'=>$this->j_id);
1001 $pa_id=array();
1002
1003 /* First add the pa_id */
1004 for ($i=0;$i < count($p_array);$i++)
1005 {
1006 if ( in_array($p_array[$i]->pa_id,$pa_id)==false)
1007 $pa_id[]=$p_array[$i]->pa_id;
1008 }
1009 $result['pa_id']=$pa_id;
1010
1011 /* add the hplan */
1012 $seq=0;
1013 for ($i=0;$i < count($p_array);$i++)
1014 {
1015 $hplan[$p_line][$i]=$p_array[$i]->po_id;
1016 }
1017 $result['hplan']=$hplan;
1018 /* Add the amount */
1019 $idx_pa=0;
1020 $jrn_def=$this->db->get_value('select jrn_def_type from jrnx join jrn_def on (j_jrn_def=jrn_def_id) where j_id=$1',array($this->j_id));
1021 for ($i=0;$i < count($p_array);$i++)
1022 {
1023
1024 /*
1025 * For the bank, negatif are always on the debit and positif on the credit
1026 */
1027 if ( $jrn_def != 'FIN')
1028 {
1029 $val[$p_line][$p_array[$i]->oa_row]=($p_array[$i]->oa_positive=='Y')?$p_array[$i]->oa_amount:($p_array[$i]->oa_amount*(-1));
1030 }
1031 else
1032 {
1033 $val[$p_line][$p_array[$i]->oa_row]=$p_array[$i]->oa_amount;
1034 }
1035 }
1036 $result['val']=$val;
1037 return $result;
1038 }
1039 /*!
1040 * \brief delete from operation_analytique
1041 * \param $p_jid the operation_analytique.j_id field
1042 *
1043 * \return none
1044 */
1045 function delete_by_jid($p_jid)
1046 {
1047 $sql="delete from operation_analytique where j_id=$1";
1048 $this->db->exec_sql($sql,array($p_jid));
1049 }
1050 /*!
1051 *\brief Display a table with analytic accounting in
1052 * detail of operation
1053 *@note $this->j_id must be set
1054 *\param $p_mode 0 = readonly or 1=writable
1055 *\param $p_amount amount
1056 *\param $p_id unique id
1057 *@see display_form_plan
1058 *\return string to display
1059 */
1060 function display_table($p_mode,$p_amount,$p_id)
1061 {
1062 static $seq=-1; /* first seq == 0 */
1063 $seq++;
1064
1065 $array=$this->get_by_jid($this->j_id) ;
1066 if ( $array != null )
1067 {
1068 $request=$this->to_request($array,$seq);
1069 return "<td>".$this->display_form_plan($request,1,$p_mode,$seq,$p_amount,$p_id)."</td>";
1070 }
1071 else
1072 {
1073 return '<td>'.$this->display_form_plan(null,1,$p_mode,$seq,$p_amount,$p_id)."</TD>";
1074 }
1075 return "";
1076
1077 }
1078///////////////////////////////////////////////////////////////////////////
1079// TEST
1080///////////////////////////////////////////////////////////////////////////
1081 /*\brief test the class
1082 *\param
1083 *\param
1084 *\return
1085 */
1086 function test_me()
1087 {
1089 $anco=new Anc_Operation($cn);
1090 $j_id=200;
1091 $anco->j_id=$j_id;
1092 $array=$anco->get_by_jid($j_id);
1093 $a=$anco->to_request($array,1);
1094 echo '<form>';
1095 echo dossier::hidden();
1096 echo HtmlInput::hidden('j_id',$j_id);
1097 echo HtmlInput::hidden('test_select',$_REQUEST['test_select']);
1098 echo $anco->display_table(1,15002,0);
1099 echo '<input type="submit" name="save">';
1100 echo '</form>';
1101 if ( isset($_REQUEST['save']))
1102 {
1103 echo "to_save";
1104 var_dump($_REQUEST);
1105 }
1106 var_dump($a);
1107
1108 }
1109
1110}
th($p_string, $p_extra='', $raw='')
Definition: ac_common.php:58
sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
Create the condition to filter on the j_tech_per thanks a from and to date.
Definition: ac_common.php:682
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
td($p_string='', $p_extra='')
surround the string with td
Definition: ac_common.php:83
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$anc pa_id
h( $row[ 'oa_description'])
if(!headers_sent())
– pour utiliser unoconv démarrer un server libreoffice commande libreoffice –headless –accept="socket...
$anc_grandlivre from
$_REQUEST['ac']
$p
Definition: array.php:34
$from
Definition: balance.inc.php:61
$to
Definition: balance.inc.php:75
$input_from id
Definition: balance.inc.php:63
$_GET['qcode']
$class
this class is used to show the form for entering an operation only FOR analytic operation to save it,...
save_form_plan($p_array, $p_item, $p_j_id)
it called for each item, the data are taken from $p_array data and set before in this....
get_jrid()
retrieve the jr_id thanks the oa_group
get_balance($p_from, $p_to, $p_plan_id)
set_currency_rate($currency_rate)
update_from_jrnx($p_po_id)
modify an op from modify_op.php
$oa_positive
signed of the amount
get_by_jid($p_jid)
retrieve an operation thanks a jrnx.j_id
get_list($p_from, $p_to, $p_from_poste="", $p_to_poste="")
get a list of row from a certain periode
save_form_plan_vat_nd($p_array, $p_item, $p_j_id, $p_nd)
Save the ND VAT with prorata.
get_list_simple($p_from, $p_to)
to_request($p_array, $p_line)
add($p_seq=0)
add a row to the table operation_analytique
display_table($p_mode, $p_amount, $p_id)
Display a table with analytic accounting in detail of operation.
delete_by_jid($p_jid)
delete from operation_analytique
display_form_plan($p_array, $p_null, $p_mode, $p_seq, $p_amount, $p_id='', $p_add_button=true)
display the form for PA
__construct($p_cn, $p_id=0)
constructor
$oa_jrnx_id_source
In the case, the amount comes from a ND VAT, the variable contents the jrnx.j_id of the source which ...
Concerns the Analytic plan (table plan_analytique)
static fetch_all($ret)
wrapper for the function pg_fetch_all
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
static num_row($ret)
wrapper for the function pg_num_rows
static connect()
define Class fiche and fiche def, those class are using class attribut. When adding or modifing new c...
Definition: fiche.class.php:38
static filter_table($p_table_id, $p_col, $start_row)
filter the rows in a table and keep the colored row in alternance
static hidden($p_name, $p_value, $p_id="")
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
Html Input , create a tag <SELECT> ... </SELECT> if readonly == true then display the label correspon...
static modify($p_id, $p_javascript)
Display the icon to modify a idem.
For the periode tables parm_periode and jrn_periode.
$all table
for($i=0;$i< $count;$i++) $template
$count
$bal jrn
$SecUser db
$side
navigation_bar($p_offset, $p_line, $p_size=0, $p_page=1, $p_javascript="")
Create a navigation_bar (pagesize)
Definition: user_common.php:76