noalyss  Version-6.9.1.8
 All Data Structures Namespaces Files Functions Variables Pages
class_anc_operation.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  */
25 require_once NOALYSS_INCLUDE.'/lib/class_ibutton.php';
26 require_once NOALYSS_INCLUDE.'/lib/class_ihidden.php';
27 require_once NOALYSS_INCLUDE.'/lib/class_iselect.php';
28 require_once NOALYSS_INCLUDE.'/lib/class_itext.php';
29 require_once NOALYSS_INCLUDE.'/class/class_anc_plan.php';
30 require_once NOALYSS_INCLUDE.'/lib/user_common.php';
31 
32 /*! \brief this class is used to show the form for entering an
33  * operation only FOR analytic operation
34  * to save it, to display or to get a list from a certain period
35  *
36  */
38 {
39  var $db; /*!< database connection */
40  var $row; /*!< array of row for one operation*/
41  var $list; /*!< array of all operation */
42  var $id; /*!< = oa_id (one row) */
43  var $po_id; /*!< poste analytique */
44  var $oa_amount; /*!< amount for one row */
45  var $oa_description; /*!< comment for one row */
46  var $oa_debit; /*!< true for debit or false */
47  var $j_id; /*!< foreign key to a jrnx operation
48  (or null if none */
49  var $oa_group; /*!< group of operation */
50  var $oa_date; /*!< equal to j_date if j_id is not null */
51  var $pa_id; /*!< the plan analytique id */
52  /**
53  * In the case, the amount comes from a ND VAT, the variable
54  * contents the jrnx.j_id of the source which was used to compute
55  * the amount
56  */
58  /**
59  * @brief signed of the amount
60  */
62  var $in_div; /*< name of the div if any, default empty string*/
63  /*!\brief constructor
64  *
65  */
66  function __construct ($p_cn,$p_id=0)
67  {
68  $this->db=$p_cn;
69  $this->id=$p_id;
70  $this->oa_jrnx_id_source=null;
71  $this->oa_positive='Y';
72  $this->has_data=0;
73  $this->in_div="";
74  }
75  /*!\brief add a row to the table operation_analytique
76  * \note if $this->oa_group if 0 then a sequence id will be computed for
77  * the oa_group, if $this->j_id=0 then it will be null
78  *
79  */
80  function add($p_seq=0)
81  {
82 
83  if ( $this->oa_group == 0)
84  {
85  $this->oa_group=$this->db->get_next_seq('s_oa_group');
86  }
87 
88  if ( $this->j_id == 0 )
89  {
90  $this->j_id=null;
91  } else {
92  // must be the same side than the operation
93  if ( $this->oa_jrnx_id_source == null)
94  {
95  $side=$this->db->get_value('select j_debit from jrnx where j_id=$1',
96  array($this->j_id));
97  } else
98  {
99  $side=$this->db->get_value('select j_debit from jrnx where j_id=$1',
100  array($this->oa_jrnx_id_source));
101  }
102  $this->oa_debit=$side;
103  }
104 
105 
106  // we don't save null operations
107  if ( $this->oa_amount == 0 || $this->po_id==-1)
108  return;
109 
110  if ( $this->oa_amount< 0)
111  {
112  // if negatif must be oa_positive='N'
113  $this->oa_positive='N';
114  $this->oa_debit=($this->oa_debit=='t')?'f':'t';
115  }
116 
117  $oa_row=(isset($this->oa_row))?$this->oa_row:null;
118  $sql="insert into operation_analytique (
119  po_id,
120  oa_amount,
121  oa_description,
122  oa_debit,
123  oa_group,
124  j_id,
125  oa_date,
126  oa_row,
127  oa_jrnx_id_source,
128  oa_positive
129  ) values ($1,$2,$3,$4,$5,$6,to_date($7,'DD.MM.YYYY'),$8,$9,$10)";
130 
131  $this->db->exec_sql($sql,array(
132  $this->po_id, // 1
133  abs($this->oa_amount), //2
134  $this->oa_description, //3
135  $this->oa_debit, //4
136  $this->oa_group, //5
137  $this->j_id, //6
138  $this->oa_date, //7
139  $oa_row, //8
140  $this->oa_jrnx_id_source, //8
141  $this->oa_positive
142  ));
143 
144  }
145  /*!\brief delete a row from the table operation_analytique
146  * \note be carefull : do not delete a row when we have a group
147  */
148  function delete()
149  {
150  $sql="delete from operation_analytique where oa_id=$1";
151 
152  $this->db->exec_sql($sql,array($this->oa_id));
153  }
154 
155  /*!\brief get a list of row from a certain periode
156  */
157  function get_list($p_from,$p_to,$p_from_poste="",$p_to_poste="")
158  {
159  $cond="";
160  $cond_poste="";
161 
162  if ($p_from!="")
163  $cond="and (jr_date >= to_date('$p_from','DD.MM.YYYY') or oa_date >= to_date('$p_from','DD.MM.YYYY') )";
164  if ( $p_to!="" )
165  $cond.="and (jr_date <=to_date('$p_to','DD.MM.YYYY') or oa_date <=to_date('$p_to','DD.MM.YYYY')) ";
166 
167  if ($p_from_poste != "" )
168  $cond_poste=" and upper(po_name) >= upper('".$p_from_poste."')";
169  if ($p_to_poste != "" )
170  $cond_poste.=" and upper(po_name) <= upper('".$p_to_poste."')";
171  $pa_id_cond="";
172  if ( isset ( $this->pa_id) && $this->pa_id !='')
173  $pa_id_cond= "pa_id=".$this->pa_id." and";
174  $sql="
175 
176  select oa_id,
177  po_name,
178  oa_description,
179  po_description,
180  oa_debit,
181  (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,
182  oa_amount,
183  oa_group,
184  j_id ,
185  jr_internal,
186  jr_id,
187  jr_comment,
188  j_poste,
189  jrnx.f_id,
190  ( select ad_value from fiche_Detail where f_id=jrnx.f_id and ad_id=23) as qcode,
191  jr_pj_number
192  from operation_analytique as B join poste_analytique using(po_id)
193  left join jrnx using (j_id)
194  left join jrn on (j_grpt=jr_grpt_id)
195  where $pa_id_cond oa_amount <> 0.0 $cond $cond_poste
196  order by jr_date,oa_group,oa_debit desc,oa_id";
197 
198  $RetSql=$this->db->exec_sql($sql);
199  $array=Database::fetch_all($RetSql);
200  return $array;
201  }
202 
203  /*\brief show the HTML table for the operation
204  */
205  function html_table($p_from)
206  {
207  if ($p_from=="")
208  {
209  $from="";
210  $to="";
211  }
212  else
213  {
214  $p=new Periode($this->db);
215  list($from,$to)=$p->get_date_limit($p_from);
216  }
217 
218  $array=$this->get_list($from,$to);
219  if ( empty($array) )
220  return "Pas d'enregistrement trouv&eacute;";
221 
222  // navigation_bar
223  $step=$_SESSION['g_pagesize'];
224  $page=(isset($_GET['offset']))?$_GET['page']:1;
225  $offset=(isset($_GET['offset']))?$_GET['offset']:0;
227 
228  if ( $step !=-1)
229  $view=array_splice($array,$offset,$step);
230  else
231  $view=$array;
232 
234  $ret="";
235  $ret.=$bar;
236 
237  $count=0;
238  $group=0;
239  $oldgroup=0;
240  $oldjrid=0;
241 
242  foreach ($view as $row)
243  {
244  $group=$row['oa_group'];
245  if ( $group !=$oldgroup )
246  {
247  if ( $oldgroup!=0 )
248  {
249 
250  $efface=new IButton();
251  $efface->javascript="anc_remove_operation(".$gDossier.",".$oldgroup.")";
252  $efface->name="Efface";
253  $efface->label="Efface";
254  $ret.="<td>".$efface->input()."</td>";
255 
256  $this->oa_group=$oldgroup;
257  $jr_id=$this->get_jrid();
258 
259  if ( $jr_id != 0)
260  {
261  // get the old jr_id
262  $detail=new IButton();
263  $detail->javascript="viewOperation($jr_id,$gDossier)";
264  $detail->name="Detail";
265  $detail->label="Detail";
266  $ret.="<td>".$detail->input()."</td>";
267  }
268  $ret.='</table>';
269 
270  }
271  $ret.='<table id="'.$row['oa_group'].'" class="result">';
272 
273  $ret.="<tr class=\"highlight\">".
274  td($row['oa_date']).
275  "<td>".
276  HtmlInput::detail_op($row['jr_id'], h($row['oa_description']." ".$row['jr_pj_number'])).
277  "</td>".
278  td();
279 
280  $ret.="<td>".
281  "Groupe id : ".$row['oa_group'].
282  "</td>".
283 
284  $oldgroup=$group;
285 
286  }
287 
288  $class=($count%2==0)?"odd":"even";
289  $count++;
290  $cred= ( $row['oa_debit'] == 'f')?"CREDIT":"DEBIT";
291  $ret.="<tr class=\"$class\">";
292  $ret.= "<td>".
293  h($row['po_name']).
294  "</td>";
295 
296  $ret.=td(h($row['po_description']));
297 
298  $ret.='<td class="num">'. nbm($row['oa_amount']).
299  "</td>".
300  "<td>".
301  $cred.
302  "</td>".
303 
304  "</tr>";
305  }
306 
307 
308  $efface=new IButton();
309  $efface->javascript="anc_remove_operation("."$gDossier,".$oldgroup.")";
310  $efface->name="Efface";
311  $efface->label="Efface";
312  $ret.="<td>".$efface->input()."</td>";
313  // get the old jr_id
314  $this->oa_group=$oldgroup;
315  $jr_id=$this->get_jrid();
316  if ( $jr_id != 0 )
317  {
318  $detail=new IButton();
319  $detail->javascript="modifyOperation($jr_id,'".$gDossier."')";
320  $detail->name="Detail";
321  $detail->label="Detail";
322  $ret.="<td>".$detail->input()."</td>";
323  }
324  $ret.='</table>';
325  $ret.=$bar;
326  return $ret;
327  }
328  /*!\brief retrieve an operation thanks a jrnx.j_id
329  * \param the jrnx.j_id
330  * \return null if nothing is found other an array
331  */
332  function get_by_jid($p_jid)
333  {
334  $array=array();
335  $a_plan=$this->db->get_array('select pa_id from plan_analytique order by pa_id');
336  $res=array();
337  /*
338  * For one oa_row
339  */
340  $a_rowcount=$this->db->get_array("select distinct oa_row "
341  ." from operation_analytique where j_id=$1 order by oa_row", array($p_jid));
342 
343  for ($i=0; $i<count($a_rowcount); $i++)
344  {
345  /*
346  * Get one row as template for filling the missing
347  */
348  $a_existing=$this->db->get_array('
349  select distinct oa_id,
350  po_id,
351  oa_amount,
352  oa_description,
353  oa_debit,
354  j_id,
355  oa_group,
356  oa_date,
357  pa_id,
358  oa_row,
359  oa_positive
360  from operation_analytique join poste_analytique using (po_id)
361  where
362  j_id=$1 and oa_row = $2
363  order by j_id,oa_row',
364  array($p_jid, $a_rowcount[$i]['oa_row']));
365 
366  // the first row we found will be the template
367  $template=$a_existing[0];
368  /*
369  * For each plan
370  */
371  for ($j=0; $j<count($a_plan); $j++)
372  {
373  /*
374  * Fetch the row with this pa_id, oa_row, max : 1 row
375  */
376  $a_fetch=$this->db->get_array('
377  select distinct oa_id,
378  po_id,
379  oa_amount,
380  oa_description,
381  oa_debit,
382  j_id,
383  oa_group,
384  oa_date,
385  pa_id,
386  oa_row,
387  oa_positive
388  from operation_analytique join poste_analytique using (po_id)
389  where
390  j_id=$1 and oa_row = $2 and pa_id=$3', array($p_jid,
391  $a_rowcount[$i]['oa_row'],
392  $a_plan[$j]['pa_id']
393  )
394  );
395  if (count($a_fetch)==0)
396  {
397  $a_fetch=$template;
398  $a_fetch['pa_id']=$a_plan[$j]['pa_id'];
399  $a_fetch['po_id']=-1;
400  $a_fetch['oa_id']='';
401  $res[]=$a_fetch;
402  }
403  else
404  if (count($a_fetch)==1)
405  {
406  $res[]=$a_fetch[0];
407  }
408  }
409  }
410 
411  foreach ($res as $row)
412  {
413  $a=new Anc_Operation($this->db);
414  foreach ($row as $attr=> $value)
415  {
416  $a->$attr=$row[$attr];
417  }
418  $array[]=clone $a;
419  }
420 
421 
422  return $array;
423  }
424 
425  /*!\brief modify an op from modify_op.php
426  *
427  */
428  function update_from_jrnx($p_po_id)
429  {
430  $a=$this->get_by_jid($this->j_id);
431  if ( $a == null )
432  {
433  // retrieve data from jrnx
434  $sql="select jr_date,j_montant,j_debit from jrnx ".
435  " join jrn on (jr_grpt_id = j_grpt) ".
436  "where j_id=".$this->j_id;
437  $res=$this->db->exec_sql($sql);
438  if (Database::num_row($res) == 0 ) return;
440  $this->oa_amount=$row['j_amount'];
441  $this->oa_date=$row['jr_date'];
442  $this->oa_debit=$row['j_debit'];
443  $this->oa_description=$row['jr_comment'];
444  $this->add();
445  }
446  else
447  {
448  foreach ($a as $row )
449  {
450  if ( $row->pa_id == $this->pa_id )
451  {
452  $row->po_id=$p_po_id;
453  $row->update();
454  }
455  }
456  }
457  }
458  /*!\brief retrieve the jr_id thanks the oa_group */
459  function get_jrid()
460  {
461  $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;
462  $res=$this->db->exec_sql($sql);
463  if ( Database::num_row($res) == 0 ) return 0;
465  return $ret[0]['jr_id'];
466  }
467  /*\brief this function get the balance for a certain period
468  *\param $p_from from date (accountancy period)
469  *\param $p_to to dat (accountancy period)
470  *\param $p_plan_id the plan id
471  */
472  function get_balance($p_from,$p_to,$p_plan_id)
473  {
474  // for the operation connected to jrnx
475  $cond=sql_filter_per($this->db,$p_from,$p_to,'p_id','j_date');
476  $sql="select oa_id, po_id, oa_amount, oa_debit, j_date from jrnx join operation_analytique using (j_id)
477  join poste_analytique using (po_id)
478  where
479  $cond and j_id is not null and pa_id=$p_plan_id";
480 
481  // OD
482  $cond=sql_filter_per($this->db,$p_from,$p_to,'p_id','oa_date');
483  $sql="union select oa_id, po_id, oa_amount, oa_debit,oa_date from
484  operation_analytique
485  join poste_analytique using (po_id)
486  where j_id is null and
487  $cond and pa_id=$p_plan_id ";
488  try
489  {
490  $res=$this->db->exec_sql($sql);
492  }
493  catch (Exception $e)
494  {
495  var_dump($e);
496  }
497  }
498  /*!\brief display the form for PA
499  *\param $p_array contains POST (or GET) data (val[] hplan[][] op[])
500  * \param $p_null = 1 if PA optional otherwise 0 mandatory
501  * \param $p_mode == form 1 ==> read/write otherwise 0==>readonly
502  * \param $p_seq number of the row
503  * \param $p_amount amount
504  * \param $p_id operation is detailled in a HTML popup, if several
505  * are opened, the tableid MUST be different. So we need to use a new parameter
506  * \param $p_add_button true, show the button, false don't display them
507  * \see save_form_plan
508  @note
509  - op is an array containing the line number
510  - pa_id is an array of the existing array
511  - hplan is an array of the POSTE ANALYTIQUE id used, the size of hplan from 0 to x,
512  x can be bigger than the number of plan id
513  - val contains the amount by row inside the table. One operation (j_id) you can have several rows
514  @code
515  0 =>
516  array
517  'op' => int 200
518  'pa_id' =>
519  array
520  0 => string '14' (length=2)
521  1 => string '15' (length=2)
522  'hplan' =>
523  array
524  1 =>
525  array
526  0 => string '25' (length=2)
527  1 => string '26' (length=2)
528  'val' =>
529  array
530  1 =>
531  array
532  0 => string '100.0000' (length=8)
533 
534  @endcode
535  */
536  function display_form_plan($p_array,$p_null,$p_mode,$p_seq,$p_amount,$p_id='',$p_add_button=true)
537  {
538  if ( $p_array != null)
539  extract ($p_array, EXTR_SKIP);
540  $result="";
541  $plan=new Anc_Plan($this->db);
542  $a_plan=$plan->get_list(" order by pa_id ");
543  if ( empty ($a_plan) ) return "";
544  $table_id="t".$p_seq;
545  $hidden=new IHidden();
546 
547  $readonly=($p_mode==1)?false:true;
548 
549  $result.=$hidden->input('amount_'.$table_id,$p_amount);
550  if ( $p_mode==1 )
551  $result.='<table id="'.$p_id.$table_id.'">';
552  else
553  $result.='<table>';
554  $result.="<tr>".$plan->header()."<th>montant</th></tr>";
555 
556  /* compute the number of rows */
557  $nb_row=(isset($val[$p_seq]))?count($val[$p_seq]):1;
558  $count=0;
559 
560  $remain=abs($p_amount);
561  $ctrl_remain="remain".$this->in_div.$table_id;
562 
563  for ( $i=0; $i < $nb_row;$i++)
564  {
565  $result.='<tr>';
566 
567  foreach ($a_plan as $r_plan)
568  {
569 
570  $array=$this->db->make_array(
571  "select po_id as value,".
572  " html_quote(po_name) as label from poste_analytique ".
573  " where pa_id = ".$r_plan['id'].
574  " order by po_name",$p_null);
575  $select =new ISelect("hplan[".$p_seq."][]",$array);
576  $select->table=0;
577  // view only or editables
578  if ( $p_mode==1 )
579  {
580  // editable
581  $select->readonly=false;
582  if ( isset($hplan) && isset($hplan[$p_seq][$count]) ){
583  $select->selected=$hplan[$p_seq][$count];
584 
585  }
586  }
587  else
588  {
589  if ( isset($hplan) && isset($hplan[$p_seq][$count]) ){
590  $select->selected=$hplan[$p_seq][$count];
591  }
592  // view only
593  $select->readOnly=true;
594  }
595  if ($p_mode==1)
596  $result.='<td>'.$select->input().'</td>';
597  else
598  $result.='<td>'.$select->display().'</td>';
599  $count++;
600 
601 
602  }
603  $value=new INum();
604  $value->javascript='onchange="format_number(this);anc_refresh_remain(\''.$this->in_div.$table_id.'\',\''.$p_seq.'\')"';
605  $value->name="val[".$p_seq."][]";
606  $value->size=6;
607  $value->value=(isset($val[$p_seq][$i]))?$val[$p_seq][$i]:abs($p_amount);
608  $value->value=round($value->value,2);
609  $value->readOnly=($p_mode==1)?false:true;
610  $remain=bcsub($remain,$value->value);
611  $result.='<td>'.$value->input().'</td>';
612 
613  $result.="</tr>";
614  }
615 
616  $result.="</table>";
617 
618  if ($p_add_button && $p_mode == 1)
619  {
620  $style_remain=($remain==0)?'style="color:green"':' style="color:red"';
621  $result.=" "._("Reste")." = ".
622  '<span class="remain" '.$style_remain.' id="'.$ctrl_remain.'">'.
623  $remain.'</span>';
624  // add a button to add a row
625  $button=new IButton();
626  $button->javascript="add_row('".$p_id."$table_id',$p_seq);";
627  $button->name="js".$p_id.$p_seq;
628  $button->label=_("Nouvelle ligne");
629 
630  $result.="<br>".$button->input();
631  /**
632  * Add a button for distribution key
633  *
634  */
635  $ledger=HtmlInput::default_value_post("p_jrn", 0);
636  if ($ledger==0) {
637  $ledger=$this->db->get_value('select j_jrn_def from jrnx where j_id=$1',array($this->j_id));
638  }
639  $gDossier=Dossier::id();
640  $button_key=new IButton();
641  $button_key->javascript="anc_key_choice(".$gDossier.",'".$p_id."$table_id',$p_amount,'".$ledger."');";
642  $button_key->name="js".$p_id.$p_seq;
643  $button_key->label=_("Clef");
644  $result .= $button_key->input();
645 
646  }
647 
648  return $result;
649  }
650  /**
651  * Save the ND VAT with prorata
652  *
653  * @param $p_array usually $_POST
654  * @param $p_item item of the form
655  * @param $p_j_id jrnx.j_id concerned writing
656  * @param $p_nd amount nd vat
657  * @see Anc_Operation::save_form_plan_vat_nd
658  * @return type
659  */
660  function save_form_plan_vat_nd($p_array,$p_item,$p_j_id,$p_nd)
661  {
662  bcscale(4);
663  extract($p_array, EXTR_SKIP);
664  if (! isset ($hplan) ) return;
665 
666  if ( ! isset(${'amount_t'.$p_item}) )
667  throw new Exception ('amount not set');
668 
669  $tot=0;
670  /* variable for in array
671  pa_id array of existing pa_id
672  hplan double array with the pa_id (column)
673  val double array by row with amount
674  op contains sequence
675  p_item is used to identify what op is concerned
676  */
677  /* echo "j_id = $j_id p_item = $p_item hplan=".var_export($hplan[$p_item],true)." val = ".var_export($val[$p_item],true).'<br>'; */
678  /* for each row */
679  // for ($i=0;$i<count($val[$p_item]);$i++) {
680  $idx_pa_id=0;
681  $row=0;
682  $a_Anc_Operation=array();
683  // foreach col PA
684  for ($e=0;$e<count($hplan[$p_item]);$e++)
685  {
686  if ( $idx_pa_id == count($pa_id))
687  {
688  $idx_pa_id=0;
689  $row++;
690  }
691  if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
692  {
693  $op=new Anc_Operation($this->db);
694  $op->po_id=$hplan[$p_item][$e];
695  $op->oa_group=$this->oa_group;
696  $op->j_id=$p_j_id;
697  $ratio=bcdiv($val[$p_item][$row],${"amount_t".$p_item});
698  $amount= bcmul($p_nd, $ratio);
699  $op->oa_amount=round($amount,2);
700  $op->oa_debit=$this->oa_debit;
701  $op->oa_date=$this->oa_date;
702 
703  $op->oa_description=$this->oa_description;
704  $op->oa_row=$row;
705  $op->oa_jrnx_id_source=$this->oa_jrnx_id_source;
706  $a_Anc_Operation[]=clone $op;
707  }
708  $idx_pa_id++;
709  }
710  $nb_op=count($a_Anc_Operation);
711  bcscale(4);
712  for ($i=0;$i<$nb_op;$i++)
713  {
714  $tot=bcadd($tot,$a_Anc_Operation[$i]->oa_amount);
715  }
716  if ( $tot != $p_nd && count($a_Anc_Operation) > 0 )
717  {
718  $diff= bcsub($tot, $p_nd);
719  $a_Anc_Operation[0]->oa_amount=bcsub($a_Anc_Operation[0]->oa_amount,$diff);
720  }
721  for ($i=0;$i<$nb_op;$i++)
722  {
723  $a_Anc_Operation[$i]->add();
724  }
725  }
726  /*!\brief it called for each item, the data are taken from $p_array
727  * data and set before in this.
728  * \param $p_item if the item nb for each item (purchase or selling
729  * merchandise)
730  * \param $p_array structure
731  * \verbatim
732  nb_tA A is the number of the item it contains the number of
733  rows of CA for this card
734  valAlR amount for the CA (item A row R)
735  ta_AoCrow_R contains the value of the pa_id and po_id for this
736  row with the form pa_id_po_id %d_%d
737  *\endverbatim
738  * \attention The idea is one j_id matches several oa_id,
739  * serveral data are set before the call :
740  * -j_id
741  * -oa_debit
742  * -oa_group
743  * -oa_date
744  * -oa_description
745  *
746  */
747  function save_form_plan($p_array,$p_item,$p_j_id)
748  {
749  extract($p_array, EXTR_SKIP);
750  if (! isset ($hplan) ) return;
751  /* variable for in array
752  pa_id array of existing pa_id
753  hplan double array with the pa_id (column)
754  val double array by row with amount
755  op contains sequence
756  p_item is used to identify what op is concerned
757  */
758  /* for each row */
759  $idx_pa_id=0;
760  $row=0;
761 
762  if ( ! isset ($hplan[$p_item])) return;
763  // foreach col PA
764  for ($e=0;$e<count($hplan[$p_item]);$e++)
765  {
766  if ( $idx_pa_id == count($pa_id))
767  {
768  $idx_pa_id=0;
769  $row++;
770  }
771  if ($hplan[$p_item][$e] != -1 && $val[$p_item][$row] != '')
772  {
773  $op=new Anc_Operation($this->db);
774  $op->po_id=$hplan[$p_item][$e];
775  $op->oa_group=$this->oa_group;
776  $op->j_id=$p_j_id;
777  $op->oa_amount=$val[$p_item][$row];
778  $op->oa_debit=$this->oa_debit;
779  $op->oa_date=$this->oa_date;
780 
781  $op->oa_description=$this->oa_description;
782  $op->oa_row=$row;
783  $op->add();
784  }
785  $idx_pa_id++;
786  }
787  // }
788  }
789 
790  /**
791  *@brief save a whole form from a update box
792  *@param $p_array for ALL j_id
793  *@return
794  *@note
795  *@see save_form_plan to_request
796  @code
797 
798  @endcode
799  */
800  function save_update_form($p_array)
801  {
802  extract($p_array, EXTR_SKIP);
803  if ( ! isset($opanc)) return;
804  for ($i = 0; $i < count($opanc); $i++)
805  {
806  /* clean operation_analytique */
807  $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($opanc[$i]));
808 
809  /* get missing data for adding */
810  $a_missing = $this->db->get_array("select to_char(jr_date,'DD.MM.YYYY')
811  as mdate,j_montant,j_debit,jr_comment ,j_poste
812  from jrnx join jrn on (j_grpt=jr_grpt_id) where j_id=$1", array($opanc[$i]));
813  $missing = $a_missing[0];
814 
815  $this->oa_description = $missing['jr_comment'];
816  $this->j_id = $opanc[$i];
817  $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
818  $this->oa_group = $group;
819  $this->oa_date = $missing['mdate'];
820  $this->save_form_plan($p_array, $i, $opanc[$i]);
821 
822  // There is ND VAT amount
823  $a_nd = $this->db->get_array('select j_id from operation_analytique
824  where oa_jrnx_id_source=$1', array($opanc[$i]));
825  if (count($a_nd) > 0)
826  {
827  // for each ND VAT amount
828  for ($e=0;$e<count($a_nd);$e++)
829  {
830  $this->db->exec_sql('delete from operation_analytique where j_id=$1', array($a_nd[$e]['j_id']));
831  /* get missing data for adding */
832  $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']));
833  $missing_vat = $a_missing_vat[0];
834  $this->oa_debit = 't';
835  $this->oa_description = $missing_vat['jr_comment'];
836  $this->j_id = $opanc[$i];
837  $group = $this->db->get_next_seq("s_oa_group"); /* for analytic */
838  $this->oa_group = $group;
839  $this->oa_date = $missing_vat['mdate'];
840  $this->oa_jrnx_id_source=$opanc[$i];
841  $p_array['amount_t'.$i]=$missing['j_montant'];
842  $this->save_form_plan_vat_nd($p_array, $i, $a_nd[$e]['j_id'],$missing_vat['j_montant']);
843  }
844  }
845  }
846  }
847 
848  /*\brief transform a array of operation into a array usage by
849  *display_form_plan & save_form_plan
850  *\param $p_array array of operation
851  *\param $p_line line
852  *\return an array complying with \see save_form_plan
853  */
854  function to_request ($p_array,$p_line)
855  {
856  $result=array();
857  $result[]=array('op'=>$this->j_id);
858  $pa_id=array();
859 
860  /* First add the pa_id */
861  for ($i=0;$i < count($p_array);$i++)
862  {
863  if ( in_array($p_array[$i]->pa_id,$pa_id)==false)
864  $pa_id[]=$p_array[$i]->pa_id;
865  }
866  $result['pa_id']=$pa_id;
867 
868  /* add the hplan */
869  $seq=0;
870  for ($i=0;$i < count($p_array);$i++)
871  {
872  $hplan[$p_line][$i]=$p_array[$i]->po_id;
873  }
874  $result['hplan']=$hplan;
875  /* Add the amount */
876  $idx_pa=0;
877  $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));
878  for ($i=0;$i < count($p_array);$i++)
879  {
880 
881  /*
882  * For the bank, negatif are always on the debit and positif on the credit
883  */
884  if ( $jrn_def != 'FIN')
885  {
886  $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));
887  }
888  else
889  {
890  $val[$p_line][$p_array[$i]->oa_row]=$p_array[$i]->oa_amount;
891  }
892  }
893  $result['val']=$val;
894  return $result;
895  }
896  /*!
897  * \brief delete from operation_analytique
898  * \param $p_jid the operation_analytique.j_id field
899  *
900  * \return none
901  */
902  function delete_by_jid($p_jid)
903  {
904  $sql="delete from operation_analytique where j_id=$1";
905  $this->db->exec_sql($sql,array($p_jid));
906  }
907  /*!
908  *\brief Display a table with analytic accounting in
909  * detail of operation
910  *@note $this->j_id must be set
911  *\param $p_mode 0 = readonly or 1=writable
912  *\param $p_amount amount
913  *\param $p_id unique id
914  *@see display_form_plan
915  *\return string to display
916  */
917  function display_table($p_mode,$p_amount,$p_id)
918  {
919  static $seq=-1; /* first seq == 0 */
920  $seq++;
921 
922  $array=$this->get_by_jid($this->j_id) ;
923  if ( $array != null )
924  {
925  $request=$this->to_request($array,$seq);
926  return "<td>".$this->display_form_plan($request,1,$p_mode,$seq,$p_amount,$p_id)."</td>";
927  }
928  else
929  {
930  return '<td>'.$this->display_form_plan(null,1,$p_mode,$seq,$p_amount,$p_id)."</TD>";
931  }
932  return "";
933 
934  }
935 ///////////////////////////////////////////////////////////////////////////
936 // TEST
937 ///////////////////////////////////////////////////////////////////////////
938  /*\brief test the class
939  *\param
940  *\param
941  *\return
942  */
943  function test_me()
944  {
946  $anco=new Anc_Operation($cn);
947  $j_id=200;
948  $anco->j_id=$j_id;
949  $array=$anco->get_by_jid($j_id);
950  $a=$anco->to_request($array,1);
951  echo '<form>';
952  echo dossier::hidden();
953  echo HtmlInput::hidden('j_id',$j_id);
954  echo HtmlInput::hidden('test_select',$_REQUEST['test_select']);
955  echo $anco->display_table(1,15002,0);
956  echo '<input type="submit" name="save">';
957  echo '</form>';
958  if ( isset($_REQUEST['save']))
959  {
960  echo "to_save";
961  var_dump($_REQUEST);
962  }
963  var_dump($a);
964 
965  }
966 
967 }
get_list($p_from, $p_to, $p_from_poste="", $p_to_poste="")
get a list of row from a certain periode
$_GET['qcode']
$select
static fetch_all($ret)
wrapper for the function pg_fetch_all
get_jrid()
retrieve the jr_id thanks the oa_group
this class is used to show the form for entering an operation only FOR analytic operation to save it...
$bal jrn
td($p_string='', $p_extra='')
surround the string with td
Definition: ac_common.php:83
get_balance($p_from, $p_to, $p_plan_id)
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.
$side
static num_row($ret)
wrapper for the function pg_NumRows
$class
update_from_jrnx($p_po_id)
modify an op from modify_op.php
__construct($p_cn, $p_id=0)
constructor
nbm($p_number, $p_dec=2)
format the number with a sep.
Definition: ac_common.php:121
Concerns the Analytic plan (table plan_analytique)
add($p_seq=0)
add a row to the table operation_analytique
$value
if(!defined('ALLOWED'))
display_table($p_mode, $p_amount, $p_id)
Display a table with analytic accounting in detail of operation.
static fetch_array($ret, $p_indice=0)
wrapper for the function pg_fetch_array
$oa_positive
signed of the amount
navigation_bar($p_offset, $p_line, $p_size=0, $p_page=1, $p_javascript="")
Create a navigation_bar (pagesize)
Definition: user_common.php:81
For the periode tables parm_periode and jrn_periode.
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:622
$cond_poste
function clone(object)
save_form_plan_vat_nd($p_array, $p_item, $p_j_id, $p_nd)
Save the ND VAT with prorata.
$_REQUEST['ac']
$bilan from
function anc_key_choice(p_dossier, p_table, p_amount, p_ledger)
choose the distribution key in ajax, a window let you choose what key you want to use ...
Definition: anc_script.js:449
if(!isset($_REQUEST['p_jrn'])) else $Ledger id
delete_by_jid($p_jid)
delete from operation_analytique
$to
Definition: balance.inc.php:77
h($p_string)
to protect again bad characters which can lead to a cross scripting attack the string to be diplayed ...
Definition: ac_common.php:38
$from
Definition: balance.inc.php:67
$count
Definition: modele.inc.php:255
static connect()
display_form_plan($p_array, $p_null, $p_mode, $p_seq, $p_amount, $p_id='', $p_add_button=true)
display the form for PA
background color
Definition: index.php:328
$jr_id
Definition: ajax_ledger.php:56
tr($p_string, $p_extra='')
Definition: ac_common.php:88
to_request($p_array, $p_line)
$SecUser db
function add_row(p_table, p_seq)
add a row for the CA
Definition: anc_script.js:31
$select_type table
static detail_op($p_jr_id, $p_mesg)
return a string containing the html code for calling the modifyOperation
$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 ...
static hidden($p_name, $p_value, $p_id="")
$p
Definition: array.php:34
get_by_jid($p_jid)
retrieve an operation thanks a jrnx.j_id
This class handles only the numeric input, the input will call a javascript to change comma to period...
Definition: class_inum.php:40