noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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 [
69 \$id => $this->id,
74 \$j_id => $this->j_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;
315 $bar=navigation_bar($offset+1,count($array),$step,$page);
316
317 if ( $step !=-1)
318 $view=array_splice($array,$offset,$step);
319 else
320 $view=$array;
321
322 $gDossier=dossier::id();
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 * @brief Save the ND VAT in ANL, and distribute the amount among ANL Axis proportionnaly to the source
721 *
722 * @param $p_array array usually $_POST
723 * @param $p_item int nb of the item of the form
724 * @param $p_j_id int jrnx.j_id concerned writing
725 * @param $p_nd float amount nd vat
726 * @see Anc_Operation::save_form_plan()
727 */
728 function save_form_plan_vat_nd($p_array,$p_item,$p_j_id,$p_nd):void
729 {
730 bcscale(4);
731 extract($p_array, EXTR_SKIP);
732 if (! isset ($hplan) ) return;
733
734 if ( ! isset(${'amount_t'.$p_item}) )
735 throw new Exception ('amount not set');
736
737 /**
738 * variable for in array
739 * @var pa_id array of existing pa_id
740 @var hplan double array with the pa_id as key (column)
741 @var val double array by row with amount
742 * @var op contains sequence
743 @var p_item is used to identify what op is concerned
744 */
745
746 $idx_pa_id=0;
747 $row=0;
748 $a_Anc_Operation=array();
749 // foreach col PA
750 for ($e=0;$e<count($hplan[$p_item]);$e++)
751 {
752 if ( $idx_pa_id == count($pa_id))
753 {
754 $idx_pa_id=0;
755 $row++;
756 }
757 if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
758 {
759 $op=new Anc_Operation($this->db);
760 $op->po_id=$hplan[$p_item][$e];
761 $op->oa_group=$this->oa_group;
762 $op->j_id=$p_j_id;
763 $ratio=0;
764 if (${"amount_t".$p_item} != 0 ) {
765 $ratio=bcdiv($val[$p_item][$row],${"amount_t".$p_item});
766 }
767 $amount= bcmul($p_nd, $ratio);
768 // convert to euro
769 $amount=bcmul($amount,$this->currency_rate);
770 $op->oa_amount=round($amount,2);
771 $op->oa_debit=$this->oa_debit;
772 $op->oa_date=$this->oa_date;
773
774 $op->oa_description=$this->oa_description;
775 $op->oa_row=$row;
776 $op->oa_jrnx_id_source=$this->oa_jrnx_id_source;
777 $a_Anc_Operation[]=clone $op;
778 }
779 $idx_pa_id++;
780 }
781 $nb_op=count($a_Anc_Operation);
782
783 for ($i=0;$i<$nb_op;$i++)
784 {
785 $a_Anc_Operation[$i]->add();
786 }
787
788 }
789 /*!\brief it called for each item, the data are taken from $p_array
790 * data and set before in this. Amount will be transformed thanks the $this->currency_rate;
791 * \param $p_item if the item nb for each item (purchase or selling
792 * merchandise)
793 * \param $p_array structure
794
795 nb_tx x is the number of the item it contains the number of
796 rows of CA for this card
797 <h1>val double array : amount for the CA row ACC => sub row ANC </h1>
798 here Operation 0 has 2 row for a total of 10.31 and 1
799 here Operation 1 has 1 row for a total of 430
800 \verbatim
801 [val] => Array
802 (
803 [0] => Array
804 (
805 [0] => 10.31
806 [1] => 1
807 )
808
809 [1] => Array
810 (
811 [0] => 430
812 )
813
814 )
815\endverbatim
816 <h1>amount_tx the amount for each row</h1>
817 It is the amount to split per accounting, it is possible that it is different from the sum of the given amount (<i>
818 see val</i>) per row
819 \verbatim
820 [amount_t0] => 11.31
821 [amount_t1] => 430
822 \endverbatim
823 ta_AoCrow_R contains the value of the pa_id and po_id for this
824 row with the form pa_id_po_id %d_%d
825 <h1> hplan = Double array operation</h1>
826 <p> Double array operation => all the amount but in linear form</p>
827
828 <p>Example if the amount of the operation 0 is splitted in 2 rows and 2 cols (because 2 analytic plan, means 2 cols)
829 we have an array of 4 values for operation 0, it gives the poste_analytique.po_id (from DB).</p>
830 <p> Here is the operation 0
831 (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)
832 for 10.31 (<i>see array val</i>) and for 1 is set for po_id 1 and 4, p
833 </p>
834 \verbatim
835 [hplan] => Array
836 (
837 [0] => Array
838 (
839 [0] => 1
840 [1] => 5
841 [2] => 1
842 [3] => 4
843 )
844
845 [1] => Array
846 (
847 [0] => 1
848 [1] => 6
849 )
850
851 )
852 \endverbatim
853
854 <h1> pa_id </h1>
855 pa_id array of plan_analytic.pa_id (1 per column), always ordered by pa_id
856
857 \verbatim
858 [pa_id] => Array
859 (
860 [0] => 1
861 [1] => 2
862 )
863
864 *\endverbatim
865 *
866 * \attention The idea is one j_id matches several oa_id,
867 * serveral data <b>must be </b>set before the call :
868 *
869 * <ul><li> j_id</li>
870 * <li>oa_debit</li>
871 * <li>oa_group</li>
872 * <li>oa_date</li>
873 * <li>oa_description</li>
874 * </ul>
875 *
876 */
877 function save_form_plan($p_array,$p_item,$p_j_id)
878 {
879 extract($p_array, EXTR_SKIP);
880 if (! isset ($hplan) ) return;
881 /* variable for in array
882 pa_id array of existing pa_id
883 hplan double array with the pa_id (column)
884 val double array by row with amount
885 op contains sequence
886 p_item is used to identify what op is concerned
887 */
888 /* for each row */
889 $idx_pa_id=0;
890 $row=0;
891
892 if ( ! isset ($hplan[$p_item])) return;
893 // foreach col PA
894 for ($e=0;$e<count($hplan[$p_item]);$e++)
895 {
896 if ( $idx_pa_id == count($pa_id))
897 {
898 $idx_pa_id=0;
899 $row++;
900 }
901 if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
902 {
903 $op=new Anc_Operation($this->db);
904 $op->po_id=$hplan[$p_item][$e];
905 $op->oa_group=$this->oa_group;
906 $op->j_id=$p_j_id;
907 // convert oa_amount to EUR
908 $op->oa_amount=bcdiv($val[$p_item][$row],$this->currency_rate,2);
909 $op->oa_debit=$this->oa_debit;
910 $op->oa_date=$this->oa_date;
911
912 $op->oa_description=$this->oa_description;
913 $op->oa_row=$row;
914 $op->add();
915 }
916 $idx_pa_id++;
917 }
918 // }
919 }
920
921 /**
922 *@brief save a whole form from a update box
923 *@param $p_array for ALL j_id
924 *@return
925 *@note
926 *@see save_form_plan to_request
927 @code
928
929 @endcode
930 */
931 function save_update_form($p_array)
932 {
933 extract($p_array, EXTR_SKIP);
934 if ( ! isset($opanc)) return;
935 for ($i = 0; $i < count($opanc); $i++)
936 {
937 /* clean operation_analytique */
938 $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($opanc[$i]));
939
940 /* get missing data for adding */
941 $a_missing = $this->db->get_array("select to_char(jr_date,'DD.MM.YYYY')
942 as mdate,j_montant,j_debit,jr_comment ,j_poste
943 from jrnx join jrn on (j_grpt=jr_grpt_id) where j_id=$1", array($opanc[$i]));
944 $missing = $a_missing[0];
945
946 $this->oa_description = $missing['jr_comment'];
947 $this->j_id = $opanc[$i];
948 $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
949 $this->oa_group = $group;
950 $this->oa_date = $missing['mdate'];
951 $this->save_form_plan($p_array, $i, $opanc[$i]);
952
953 // There is ND VAT amount
954 $a_nd = $this->db->get_array('select j_id from operation_analytique
955 where oa_jrnx_id_source=$1', array($opanc[$i]));
956 if (count($a_nd) > 0)
957 {
958 // for each ND VAT amount
959 for ($e=0;$e<count($a_nd);$e++)
960 {
961 $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($a_nd[$e]['j_id']));
962 /* get missing data for adding */
963 $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']));
964 $missing_vat = $a_missing_vat[0];
965 $this->oa_debit = 't';
966 $this->oa_description = $missing_vat['jr_comment'];
967 $this->j_id = $opanc[$i];
968 $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
969 $this->oa_group = $group;
970 $this->oa_date = $missing_vat['mdate'];
971 $this->oa_jrnx_id_source=$opanc[$i];
972 $p_array['amount_t'.$i]=$missing['j_montant'];
973 $this->save_form_plan_vat_nd($p_array, $i, $a_nd[$e]['j_id'],$missing_vat['j_montant']);
974 }
975 }
976 }
977 }
978
979 /*\brief transform a array of operation into a array usage by
980 *display_form_plan & save_form_plan
981 *\param $p_array array of operation
982 *\param $p_line line
983 *\return an array complying with \see save_form_plan
984 */
985 function to_request ($p_array,$p_line)
986 {
987 $result=array();
988 $result[]=array('op'=>$this->j_id);
989 $pa_id=array();
990
991 /* First add the pa_id */
992 for ($i=0;$i < count($p_array);$i++)
993 {
994 if ( in_array($p_array[$i]->pa_id,$pa_id)==false)
995 $pa_id[]=$p_array[$i]->pa_id;
996 }
997 $result['pa_id']=$pa_id;
998
999 /* add the hplan */
1000 $seq=0;
1001 for ($i=0;$i < count($p_array);$i++)
1002 {
1003 $hplan[$p_line][$i]=$p_array[$i]->po_id;
1004 }
1005 $result['hplan']=$hplan;
1006 /* Add the amount */
1007 $idx_pa=0;
1008 $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));
1009 for ($i=0;$i < count($p_array);$i++)
1010 {
1011
1012 /*
1013 * For the bank, negatif are always on the debit and positif on the credit
1014 */
1015 if ( $jrn_def != 'FIN')
1016 {
1017 $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));
1018 }
1019 else
1020 {
1021 $val[$p_line][$p_array[$i]->oa_row]=$p_array[$i]->oa_amount;
1022 }
1023 }
1024 $result['val']=$val;
1025 return $result;
1026 }
1027 /*!
1028 * \brief delete from operation_analytique
1029 * \param $p_jid the operation_analytique.j_id field
1030 *
1031 * \return none
1032 */
1033 function delete_by_jid($p_jid)
1034 {
1035 $sql="delete from operation_analytique where j_id=$1";
1036 $this->db->exec_sql($sql,array($p_jid));
1037 }
1038 /*!
1039 *\brief Display a table with analytic accounting in
1040 * detail of operation
1041 *@note $this->j_id must be set
1042 *\param $p_mode 0 = readonly or 1=writable
1043 *\param $p_amount amount
1044 *\param $p_id unique id
1045 *@see display_form_plan
1046 *\return string to display
1047 */
1048 function display_table($p_mode,$p_amount,$p_id)
1049 {
1050 static $seq=-1; /* first seq == 0 */
1051 $seq++;
1052
1053 $array=$this->get_by_jid($this->j_id) ;
1054 if ( $array != null )
1055 {
1056 $request=$this->to_request($array,$seq);
1057 return "<td>".$this->display_form_plan($request,1,$p_mode,$seq,$p_amount,$p_id)."</td>";
1058 }
1059 else
1060 {
1061 return '<td>'.$this->display_form_plan(null,1,$p_mode,$seq,$p_amount,$p_id)."</TD>";
1062 }
1063 return "";
1064
1065 }
1066///////////////////////////////////////////////////////////////////////////
1067// TEST
1068///////////////////////////////////////////////////////////////////////////
1069 /*\brief test the class
1070 *\param
1071 *\param
1072 *\return
1073 */
1074 function test_me()
1075 {
1076 $cn=Dossier::connect();
1077 $anco=new Anc_Operation($cn);
1078 $j_id=200;
1079 $anco->j_id=$j_id;
1080 $array=$anco->get_by_jid($j_id);
1081 $a=$anco->to_request($array,1);
1082 echo '<form>';
1083 echo dossier::hidden();
1084 echo HtmlInput::hidden('j_id',$j_id);
1085 echo HtmlInput::hidden('test_select',$_REQUEST['test_select']);
1086 echo $anco->display_table(1,15002,0);
1087 echo '<input type="submit" name="save">';
1088 echo '</form>';
1089 if ( isset($_REQUEST['save']))
1090 {
1091 echo "to_save";
1092 var_dump($_REQUEST);
1093 }
1094 var_dump($a);
1095
1096 }
1097
1098}
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.
tr($p_string, $p_extra='')
Definition ac_common.php:88
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
td($p_string='', $p_extra='')
surround the string with td
Definition ac_common.php:83
return false Description background color
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$anc pa_id
h( $row[ 'oa_description'])
$anc_grandlivre from
$from
$to
_("actif, passif,charge,...")
$p
Definition calendar.php:9
$_GET['qcode']
this class is used to show the form for entering an operation only FOR analytic operation to save it,...
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
get_list_simple($p_from, $p_to)
add($p_seq=0)
add a row to the table operation_analytique
delete()
delete a row from the table 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
define Class fiche and fiche def, those class are using class attribut. When adding or modifing new c...
Html Input.
This class handles only the numeric input, the input will call a javascript to change comma to period...
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.
$all table
for($i=0;$i< $count;$i++) $template
$count
$bal jrn
$SecUser db
catch(\Exception $e) $bar
$side
navigation_bar($p_offset, $p_line, $p_size=0, $p_page=1, $p_javascript="")
Create a navigation_bar (pagesize)