Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_am_generate.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of NOALYSS.
5  *
6  * NOALYSS is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * NOALYSS is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with NOALYSS; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  */
20 /* $Revision$ */
21 
22 // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
23 
24 /**
25  * @file
26  * \brief generate writing of amortize
27  */
28 require_once('class_amortissement_histo_sql.php');
29 
31 {
32 
33  /**
34  * Show the form to generate the writing
35  * select ledger
36  * year
37  * date of operation
38  * list of material
39  */
40  function input($p_array)
41  {
42  global $cn, $g_user;
43  /*
44  * select ledger
45  */
46 
47  $ledger=new Acc_Ledger($cn, 0);
48  $sel_ledger=$ledger->select_ledger('ODS', 2);
49  $sel_ledger->selected=(isset($p_array['p_jrn']))?$p_array['p_jrn']:'';
50  /*
51  * Operation Date
52  */
53  $p_date=new IDate('p_date');
54  $p_date->value=(isset($p_array['p_date']))?$p_array['p_date']:date('d.m.Y');
55 
56  /*
57  * select year
58  */
59  $year=new INum('p_year');
60  $year->size=4;
61  $year->value=(isset($p_array['p_year']))?$p_array['p_year']:date('Y');
62 
63  /*
64  * PJ
65  */
66  $pj=new IText('p_pj');
67  $pj->size=10;
68  /*
69  * If we use the periode
70  */
71  if ($ledger->check_periode()==true)
72  {
73  $l_user_per=$g_user->get_periode();
74  $def=(isset($periode))?$periode:$l_user_per;
75  $period=new IPeriod("period");
76  $period->user=$g_user;
77  $period->cn=$cn;
78  $period->value=$def;
79  $period->type=OPEN;
80  try
81  {
82  $l_form_per=$period->input();
83  }
84  catch (Exception $e)
85  {
86  if ($e->getCode()==1)
87  {
88  echo _("Aucune période ouverte");
89  exit();
90  }
91  }
92  $label=HtmlInput::infobulle(3);
93  $f_periode=_("Période comptable")." $label ";
94  }
95  /*
96  * show all the visible material
97  */
98  require_once('template/util_generate.php');
99  }
100 
101  /**
102  * Propose to save the writing in the selected ledger
103  @code
104  array
105  'plugin_code' => string 'AMORT' (length=5)
106  'sa' => string 'util' (length=4)
107  'gDossier' => string '48' (length=2)
108  'p_year' => string '' (length=0)
109  'p_jrn' => string '4' (length=1)
110  'p_date' => string '' (length=0)
111  'a_id' =>
112  array
113  0 => string '86' (length=2)
114  1 => string '85' (length=2)
115  'p_ck1' => string '' (length=0)
116 
117  @endcode
118  */
119  function propose_writing($p_array)
120  {
121  global $cn;
122  $msg='';
123  if (isNumber($p_array['p_year'])==0||$p_array['p_year']<1900||$p_array['p_year']>2100)
124  $msg="Année invalide\n";
125  if (isDate($p_array['p_date'])==null)
126  $msg.="Date invalide ";
127  if ($msg!='')
128  {
129  echo alert($msg);
130  return false;
131  }
132 
133  $array=array(
134  'p_jrn'=>$p_array['p_jrn'],
135  'e_date'=>$p_array['p_date'],
136  'periode'=>0,
137  'desc'=>'Amortissement '.$p_array['p_year'],
138  'e_pj'=>$p_array['p_pj'],
139  'e_pj_suggest'=>$p_array['p_pj'],
140  );
141  if (isset($p_array['period']))
142  {
143  $array['period']=$p_array['period'];
144  }
145  $idx=0;
146  $a_material=array();
147  for ($i=0; $i<count($p_array['a_id']); $i++)
148  {
149  if (isset($p_array['p_ck'.$i]))
150  {
151 
152  /*
153  * If selected the add it to array
154  */
155  $n=new Amortissement_Sql($cn, $p_array['a_id'][$i]);
156  $fiche=new Fiche($cn, $n->f_id);
157  $val=$cn->get_value("select ad_amount from amortissement.amortissement_detail ".
158  " where a_id = $1 and ad_year=$2", array($n->a_id, $p_array['p_year']));
159  $val=($val=='')?0:$val;
160  // retrieve quick code card deb
161  $card_deb="";
162  if ($n->card_deb!="")
163  {
164  $fiche_card_deb=new Fiche($cn, $n->card_deb);
165  $card_deb=$fiche_card_deb->get_quick_code();
166  }
167 
168  $card_cred="";
169  if ($n->card_cred!="")
170  {
171  // retrieve quick code card cred
172  $fiche_card_cred=new Fiche($cn, $n->card_cred);
173  $card_cred=$fiche_card_cred->get_quick_code();
174  }
175  $mat=array('poste'.$idx=>$n->account_deb,
176  'amount'.$idx=>$val,
177  'ld'.$idx=>'Dotation à '.$fiche->strAttribut(ATTR_DEF_QUICKCODE),
178  'ck'.$idx=>1,
179  'qc_'.$idx=>$card_deb);
180  $a_material+=array('request_a'.$i=>$idx);
181  $array+=$mat;
182  $idx++;
183  $mat=array('poste'.$idx=>$n->account_cred,
184  'amount'.$idx=>$val,
185  'ld'.$idx=>'Amortissement '.$fiche->strAttribut(ATTR_DEF_QUICKCODE),
186  'qc_'.$idx=>$card_cred);
187  $array+=$mat;
188  $idx++;
189  }
190  }
191  $array+=array('nb_item'=>$idx);
192  $ledger=new Acc_Ledger($cn, $p_array['p_jrn']);
193  $ledger->with_concerned=false;
194  $list=new ISelect('grouped');
195  $list->value=array(
196  array('label'=>_('En une opération'), 'value'=>1),
197  array('label'=>_('En plusieurs opérations'), 'value'=>0),
198  array('label'=>_('-- choix --'), 'value'=>-1)
199  );
200  $list->selected=-1;
201  echo '<div style="width:80%;margin-left:10%">';
202  echo '<form method="POST" style="display:inline">';
203  try
204  {
205  echo $ledger->input($array, true);
206  foreach ($a_material as $key=> $value)
207  echo HtmlInput::hidden($key, $value);
208  }
209  catch (Exception $e)
210  {
211  echo alert($e->getMessage());
212  return false;
213  }
214  echo $list->input();
215  echo HtmlInput::submit('save',_('Sauver'));
216  echo HtmlInput::hidden('sa', $p_array['sa']);
217  echo HtmlInput::hidden('sb', $p_array['sb']);
218  echo HtmlInput::hidden('p_year', $p_array['p_year']);
219  echo HtmlInput::hidden('p_date', $p_array['p_date']);
220  echo HtmlInput::hidden('p_jrn', $p_array['p_jrn']);
221  echo HtmlInput::hidden('plugin_code', $p_array['plugin_code']);
222  for ($i=0; $i<count($p_array['a_id']); $i++)
223  {
224  echo HtmlInput::hidden('a_id[]', $p_array['a_id'][$i]);
225  if (isset($p_array['p_ck'.$i]))
226  echo HtmlInput::hidden('p_ck'.$i, '1');
227  }
228 
229 
230  echo '</form>';
231  /*
232  * correct
233  */
234  echo '<form method="POST" style="display:inline">';
235  echo dossier::hidden();
236  echo HtmlInput::hidden('sa', $p_array['sa']);
237  echo HtmlInput::hidden('sb', $p_array['sb']);
238  echo HtmlInput::hidden('p_year', $p_array['p_year']);
239  echo HtmlInput::hidden('p_date', $p_array['p_date']);
240  echo HtmlInput::hidden('p_jrn', $p_array['p_jrn']);
241  echo HtmlInput::hidden('plugin_code', $p_array['plugin_code']);
242  for ($i=0; $i<count($p_array['a_id']); $i++)
243  {
244  echo HtmlInput::hidden('a_id[]', $p_array['a_id'][$i]);
245  if (isset($p_array['p_ck'.$i]))
246  echo HtmlInput::hidden('p_ck'.$i, '1');
247  }
248  echo HtmlInput::submit('correct', 'Corriger');
249  echo '</form>';
250  echo '</div>';
251  return true;
252  }
253 
254  /**
255  * save into amortissement_histo
256  * @param $p_array contains the data to insert
257  * @param $p_group boolean Save in one operation if TRUE otherwise in
258  * several ones
259  @code
260  array
261  'plugin_code' => string 'AMORT' (length=5)
262  'sa' => string 'util' (length=4)
263  'gDossier' => string '48' (length=2)
264  'p_year' => string '' (length=0)
265  'p_jrn' => string '4' (length=1)
266  'p_date' => string '' (length=0)
267  'a_id' =>
268  array
269  0 => string '86' (length=2)
270  1 => string '85' (length=2)
271  'p_ck1' => string '' (length=0)
272 
273  @endcode
274  */
275  function save($p_array, $p_group)
276  {
277  if ($p_group == true)
278  {
279  return $this->save_grouped($p_array);
280  }
281  else if ($p_group == false)
282  {
283  return $this->save_separated($p_array);
284  }
285  }
286 
287  /**
288  * @brief save the amortized material into only one writing
289  * @param $p_array contains the data
290  * @see save
291  */
292 
293  private function save_separated($p_array)
294  {
295  global $cn;
296  $ledger=new Acc_Ledger($cn, $p_array['p_jrn']);
297  $this->saved_operation=array();
298  $msg='';
299  $oPeriode = new Periode($cn);
300  $period=$oPeriode->find_periode($p_array['e_date']);
301  for ($i=0; $i<count($p_array['a_id']); $i++)
302  {
303  if (isset($p_array['p_ck'.$i]))
304  {
305  /* * *
306  * corresponding
307  * if 'p_ck5' => '1',
308  * then
309  * if a_request5 = 4 operation 4 = deb 5 = cred
310  */
311  $idx=$p_array['request_a'.$i];
312  $cred=$idx+1;
313 
314  /**
315  * Save into the ledger
316  */
317  $p_post=array();
318  $p_post['p_jrn']=$p_array['p_jrn'];
319  $p_post['jrn_type']=$p_array['jrn_type'];
320  $p_post['e_date']=$p_array['e_date'];
321  $p_post['e_pj']=$p_array['e_pj'];
322  $p_post['e_pj_suggest']=$p_array['e_pj_suggest'];
323  $msg_operation=$cn->get_value('select vw_name from vw_fiche_attr join amortissement.amortissement using (f_id) where a_id=$1', array($p_array['a_id'][$i]));
324  $p_post['desc']=$p_array['e_comm']."-".$msg_operation;
325  $p_post['period']=$period;
326  $this->saved_operation['desc'][]=$p_post['desc'];
327  $p_post['mt']=microtime(false);
328  $p_post['nb_item']=$cred*2;
329 
330  // Debit
331  if (isset($p_array['ck'.$idx]))
332  $p_post['ck'.$idx]=1;
333  if (isset($p_array['poste'.$idx]))
334  $p_post['poste'.$idx]=$p_array['poste'.$idx];
335  if (isset($p_array['qc_'.$idx]))
336  $p_post['qc_'.$idx]=$p_array['qc_'.$idx];
337  if (isset($p_array['ld'.$idx]))
338  $p_post['ld'.$idx]=$p_array['ld'.$idx];
339  $p_post['amount'.$idx]=$p_array['amount'.$idx];
340  // crédit
341  if (isset($p_array['poste'.$cred]))
342  $p_post['poste'.$cred]=$p_array['poste'.$cred];
343  if (isset($p_array['qc_'.$cred]))
344  $p_post['qc_'.$cred]=$p_array['qc_'.$cred];
345  if (isset($p_array['ld'.$cred]))
346  $p_post['ld'.$cred]=$p_array['ld'.$cred];
347  $p_post['amount'.$cred]=$p_array['amount'.$cred];
348  $ledger->save($p_post);
349  $this->saved_operation["internal"][]=$ledger->internal;
350  $this->saved_operation["jr_id"][]=$ledger->jr_id;
351  /*
352  * Do not exist we insert into amortissement.amortissement_histo
353  */
354  $n=new Amortissement_Histo_Sql($cn);
355  $val=$cn->get_value("select ad_amount from amortissement.amortissement_detail ".
356  " where a_id = $1 and ad_year=$2", array($p_array['a_id'][$i], $p_array['p_year']));
357  $val=($val=='')?0:$val;
358  $h=$cn->get_value('select ha_id from amortissement.amortissement_histo where a_id=$1 and h_year=$2', array($p_array['a_id'][$i], $p_array['p_year']));
359  if ($cn->count()==0)
360  continue;
361  $n->ha_id=$h;
362  $n->load();
363  $n->h_amount=$val;
364  $n->h_year=$p_array['p_year'];
365  $n->jr_internal=$ledger->internal;
366  $n->update();
367  }
368  }
369  return $msg;
370  }
371 
372  /**
373  * @brief save the amortized material into several writings
374  * @param $p_array contains the data
375  * @see save
376  */
377  private function save_grouped($p_array)
378  {
379  global $cn;
380  $ledger=new Acc_Ledger($cn, $p_array['p_jrn']);
381  $this->saved_operation=array();
382  /**
383  * Save into the ledger
384  */
385  $p_post=array();
386  $p_post['p_jrn']=$p_array['p_jrn'];
387  $p_post['jrn_type']=$p_array['jrn_type'];
388  $p_post['e_date']=$p_array['e_date'];
389  $p_post['e_pj']=$p_array['e_pj'];
390  $p_post['e_pj_suggest']=$p_array['e_pj_suggest'];
391  $p_post['desc']='Amortissement ';
392  $this->saved_operation['desc'][0]=$p_post['desc'];
393  $p_post['mt']=microtime(false);
394  $oPeriode = new Periode($cn);
395  $p_post['period']=$oPeriode->find_periode($p_array['e_date']);
396  $msg='';
397  for ($i=0; $i<count($p_array['a_id']); $i++)
398  {
399  if (isset($p_array['p_ck'.$i]))
400  {
401  /* * *
402  * corresponding
403  * if 'p_ck5' => '1',
404  * then
405  * if a_request5 = 4 operation 4 = deb 5 = cred
406  */
407  $idx=$p_array['request_a'.$i];
408  $cred=$idx+1;
409 
410 
411  $p_post['nb_item']=$cred*2;
412 
413  // Debit
414  if (isset($p_array['ck'.$idx]))
415  $p_post['ck'.$idx]=1;
416  if (isset($p_array['poste'.$idx]))
417  $p_post['poste'.$idx]=$p_array['poste'.$idx];
418  if (isset($p_array['qc_'.$idx]))
419  $p_post['qc_'.$idx]=$p_array['qc_'.$idx];
420  if (isset($p_array['ld'.$idx]))
421  $p_post['ld'.$idx]=$p_array['ld'.$idx];
422  $p_post['amount'.$idx]=$p_array['amount'.$idx];
423  // crédit
424  if (isset($p_array['poste'.$cred]))
425  $p_post['poste'.$cred]=$p_array['poste'.$cred];
426  if (isset($p_array['qc_'.$cred]))
427  $p_post['qc_'.$cred]=$p_array['qc_'.$cred];
428  if (isset($p_array['ld'.$cred]))
429  $p_post['ld'.$cred]=$p_array['ld'.$cred];
430  $p_post['amount'.$cred]=$p_array['amount'.$cred];
431  }
432  }
433  $ledger->save($p_post);
434  $this->saved_operation["internal"][0]=$ledger->internal;
435  $this->saved_operation["jr_id"][0]=$ledger->jr_id;
436  for ($i=0; $i<count($p_array['a_id']); $i++)
437  {
438  if (isset($p_array['p_ck'.$i]))
439  {
440  /*
441  * Do not exist we insert into amortissement.amortissement_histo
442  */
443  $n=new Amortissement_Histo_Sql($cn);
444  $val=$cn->get_value("select ad_amount from amortissement.amortissement_detail ".
445  " where a_id = $1 and ad_year=$2", array($p_array['a_id'][$i], $p_array['p_year']));
446  $val=($val=='')?0:$val;
447  $h=$cn->get_value('select ha_id from amortissement.amortissement_histo where a_id=$1 and h_year=$2', array($p_array['a_id'][$i], $p_array['p_year']));
448  if ($cn->count()==0)
449  continue;
450  $n->ha_id=$h;
451  $n->load();
452  $n->h_amount=$val;
453  $n->h_year=$p_array['p_year'];
454  $n->jr_internal=$ledger->internal;
455  $n->update();
456  }
457  }
458  return $msg;
459  }
460 
461 }
$msg
if($cn->count()==0) $oPeriode
$periode
propose_writing($p_array)
Propose to save the writing in the selected ledger.
save($p_array, $p_group)
save into amortissement_histo
save_grouped($p_array)
save the amortized material into several writings
input($p_array)
Show the form to generate the writing select ledger year date of operation list of material...
if(isset($_POST['remove'])) $array
Manage the table amortissement.amortissement_histo.
$key
save_separated($p_array)
save the amortized material into only one writing
Manage the table amortissement.amortissement.
global $cn
$def
Definition: am_print.php:40