Plugins  LAST
 All Data Structures Files Functions Variables Pages
class_am_card.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  /* $Revision$ */
20 
21  // Copyright (c) 2002 Author Dany De Bontridder dany@alchimerys.be
22 
23  /*!\file
24  * \brief let you add all the materiel you need to amortize
25  */
26 require_once('class_amortissement_sql.php');
27 require_once('class_amortissement_detail_sql.php');
28 require_once('class_amortissement_histo_sql.php');
29 
30 class Am_Card
31 {
32 
33  private $amortissement; //<! Amortissement_SQL
34 
35  private $amortissement_detail; //<! child records Amortissement_Detail_Sql
36  //contains details per year
37 
38  private $amortissement_histo; //<! child records Amortissement_Histo_Sql,
39  // contains histo info : concerned, amount...
40  function __construct()
41  {
42  }
43  /**
44  *@brief display the list of material
45  */
46  public function listing($p_all=false)
47  {
48  global $cn;
49  $amort=new Amortissement_Sql($cn);
50  if ( $p_all==true)
51  $ret=$amort->seek();
52  else
53  $ret=$amort->seek(" join fiche_detail using(f_id) where a_visible = 'Y' and ad_id=1 order by ad_value");
54 
55  require_once('template/material_listing.php');
56 
57  }
58  /**
59  *@brief display a button to add a material
60  */
61  public function add_card()
62  {
63  $add=new IButton('add_card');
64  $add->label=_("Ajout d'un bien à amortir");
65  $add->javascript=sprintf("add_material(%d,'%s','bxmat')",
66  dossier::id(),
67  $_REQUEST['plugin_code']
68  );
69  return $add;
70  }
71  /**
72  * all the data are in the array : we construct an object with data from the array
73  */
74  public function from_array($p_array)
75  {
76  global $cn;
77 
78  $this->amortissement=new Amortissement_Sql($cn);
79  $fiche=new Fiche($cn);
80  $fiche->get_by_qcode($p_array['p_card']);
81  $this->amortissement->a_id=(isset($p_array['a_id']))?$p_array['a_id']:-1;
82  $this->amortissement->f_id=$fiche->id;
83  $this->amortissement->account_deb=$p_array['p_deb'];
84  $this->amortissement->account_cred=$p_array['p_cred'];
85  $this->amortissement->a_start=$p_array['p_year'];
86  $this->amortissement->a_amount=$p_array['p_amount'];
87  $this->amortissement->a_nb_year=$p_array['p_number'];
88  $this->amortissement->a_date=$p_array['p_date'];
89  $this->amortissement->a_visible=(isset($p_array['p_visible']))?$p_array['p_visible']:'Y';
90 
91  if ( $p_array['type'] == 1 ) {
92  $fiche_deb=new Fiche($cn);
93  $fiche_deb->get_by_qcode($p_array['p_card_deb'],false);
94  $this->amortissement->card_deb=($fiche_deb->id != 0 ) ? $fiche_deb->id:null;
95 
96  $fiche_cred=new Fiche($cn);
97  $fiche_cred->get_by_qcode($p_array['p_card_cred'],false);
98  $this->amortissement->card_cred=($fiche_cred->id != 0 ) ? $fiche_cred->id:null;
99  $this->amortissement->account_deb=null;
100  $this->amortissement->account_cred=null;
101  } else {
102  $this->amortissement->account_deb=$p_array['p_deb'];
103  $this->amortissement->account_cred=$p_array['p_cred'];
104  $this->amortissement->card_cred=null;
105  $this->amortissement->card_deb=null;
106  }
107  /*
108  * if details then load them
109  */
110  if ( isset($p_array['ad_id']))
111  {
112  for ($i=0;$i<count($p_array['ad_id']);$i++)
113  {
114  $am=new Amortissement_Detail_Sql($cn);
115  $am->ad_id=$p_array['ad_id'][$i];
116  $am->ad_amount=$p_array['amount'][$i];
117  $am->ad_amount=$p_array['amount'][$i];
118  $am->a_id=$this->amortissement->a_id;
119  $am->ad_percentage=$p_array['pct'][$i];
120  $am->ad_year=$p_array['ad_year'][$i];
121 
122  $this->amortissement_detail[]=clone($am);
123  }
124  }
125  /* save detail from amortissement_histo
126  * p_pj[]
127  * p_histo[]
128  * h[]
129  */
130  if ( isset ($p_array['h']))
131  {
132  for ($i=0;$i<count($p_array['h']);$i++)
133  {
134  $id=$p_array['h'][$i];
135  $am=new Amortissement_Histo_Sql($cn,$p_array['h'][$i]);
136  $am->load();
137  $am->h_amount=$p_array['p_histo'][$i];
138  $am->h_pj=$p_array['p_pj'][$i];
139  $am->jr_id=0;
140  if ( isset ($p_array['op_concerne'][$id])) {
141  $am->jr_id=$p_array['op_concerne'][$id];
142  $am->jr_internal="";
143  } else {
144  $am->jr_id=0;
145  }
146  $this->amortissement_histo[]=clone($am);
147  }
148  }
149  }
150  /**
151  * show a form to modify data
152  */
153  public function input()
154  {
155  global $cn;
156  $this->amortissement->load();
157  $this->amortissement_detail=new Amortissement_Detail_Sql($cn);
158  $array=$this->amortissement_detail->seek(' where a_id=$1 order by ad_year asc',array($this->amortissement->a_id));
159 
160 
161  $a_id=HtmlInput::hidden('a_id',$this->amortissement->a_id);
162  $value_a_id=$this->amortissement->a_id;
163 
164  $p_year=new INum('p_year');
165  $p_year->value=$this->amortissement->a_start;
166  $p_number=new INum('p_number');
167  $p_number->value=$this->amortissement->a_nb_year;
168 
169  $p_date=new IDate('p_date');
170  $p_date->value=$this->amortissement->a_date;
171 
172  $p_visible=new IText('p_visible');
173  $p_visible->size=2;
174  $p_visible->value=$this->amortissement->a_visible;
175  $card=new Fiche($cn,$this->amortissement->f_id);
176 
177  $p_card=HtmlInput::hidden('p_card',$card->strAttribut(ATTR_DEF_QUICKCODE));
178 
179 
180  $p_deb=new IPoste('p_deb');
181  $p_deb->set_attribute('jrn',0);
182  $p_deb->set_attribute('account','p_deb');
183  $p_deb->set_attribute('label','p_deb_label');
184  $p_deb->value=$this->amortissement->account_deb;
185  $deb_span=new ISpan('p_deb_label');
186 
187  $p_cred=new IPoste('p_cred');
188  $p_cred->set_attribute('jrn',0);
189  $p_cred->set_attribute('account','p_cred');
190  $p_cred->set_attribute('label','p_cred_label');
191  $p_cred->value=$this->amortissement->account_cred;
192 
193  $cred_span=new ISpan('p_cred_label');
194 
195  $p_amount=new INum('p_amount');
196  $p_amount->value= $this->amortissement->a_amount;
197  $select_type=new ISelect('type');
198  $select_type->id='select_type_id';
199  $select_type->value=array(array('label'=>'--Faites un choix --','value'=>-1),
200  array('label'=>'Poste comptable','value'=>'0'),
201  array('label'=>'Fiche','value'=>'1')
202  );
203 
204  $select_type->selected=HtmlInput::default_value_post('type','-1');
205  $select_type->javascript=' onchange = "show_selected_material(this);"';
206  $select_type->selected=-1;
207  if ( $this->amortissement->card_deb != '') $select_type->selected=1;
208  if ( $this->amortissement->account_deb != '') $select_type->selected=0;
209 
210  $fiche_deb=new Fiche($cn);
211 
212  $p_card_deb=new ICard('p_card_deb');
213  $p_card_deb->typecard='all';
214  $p_card_cred=new ICard('p_card_cred');
215  $p_card_cred->typecard='all';
216  if ( $this->amortissement->card_deb != '' )
217  {
218  $fiche_deb=new Fiche($cn,$this->amortissement->card_deb);
219  $p_card_deb->value=$fiche_deb->get_quick_code();
220  }
221  if ( $this->amortissement->card_cred != '' )
222  {
223  $fiche_cred=new Fiche($cn,$this->amortissement->card_cred);
224  $p_card_cred->value=$fiche_cred->get_quick_code();
225  }
226  require_once('template/material_detail.php');
227  }
228  /**
229  *Verify that all data are correct
230  */
231  function verify_post()
232  {
233  global $cn;
234  $error_msg='';
235  if ( isNumber($_POST['p_year']) == null || $_POST['p_year']<1900||$_POST['p_year'] > 3000 ) $error_msg.=_('Année invalide')."\n";
236  if ( isNumber($_POST['p_number']) == null )$error_msg.=_ ('Nombre annuités invalide')."\n";
237  if ( isNumber($_POST['p_amount']) == null || $_POST['p_amount']==0) $error_msg.=_ ('Montant invalide')."\n";
238  $p_card = HtmlInput::default_value_post('p_card', '');
239  $visible=HtmlInput::default_value_post('p_visible','Y');
240  if ( $visible != 'Y' && $visible != 'N') $error_msg.=_("Visible Y ou N\n");
241  $_POST['p_visible']=$visible;
242  switch ( $_POST['type'] )
243  {
244  case -1:
245  $error_msg .= _('Choisissez poste comptable ou fiche');
246  break;
247  case 0:
248  if ( $cn->get_value('select count(*) from tmp_pcmn where pcm_val=$1',array($_POST['p_deb'])) == 0) $error_msg.=" Poste de charge incorrect"."\n";
249  if ( $cn->get_value('select count(*) from tmp_pcmn where pcm_val=$1',array($_POST['p_cred'])) == 0) $error_msg.=" Poste à créditer incorrect"."\n";
250  break;
251  case 1:
252  if ( $cn->get_value('select j_poste from vw_poste_qcode where j_qcode=trim(upper($1))',array($_POST['p_card_deb'])) == "") $error_msg.=" Fiche de charge incorrect"."\n";
253  if ( $cn->get_value('select j_poste from vw_poste_qcode where j_qcode=trim(upper($1))',array($_POST['p_card_cred'])) == "") $error_msg.=" Fiche contrepartie incorrect"."\n";
254  break;
255  }
256  $p_new=HtmlInput::default_value_post('p_new',-1);
257  /*
258  * Check duplicate : check that the material (p_card) is not already in the material to amortize
259  */
260  if ( $p_new != -1 ) {
261  $f_id=$cn->get_value('select f_id from vw_poste_qcode where j_qcode=trim(upper($1))',array($p_card));
262  if ( $f_id != "") {
263  if ( $cn->get_value('select count(*) from amortissement.amortissement where f_id = $1',array($f_id)) > 0 ) $error_msg.=_('Matériel déjà dans la liste');
264  }
265  }
266  /**
267  * Check that op_concerned has an internal
268  * and only one
269  */
270  $nb_histo=count($this->amortissement_histo);
271  for ($i=0;$i<$nb_histo;$i++)
272  {
273  $jr_id=$this->amortissement_histo[$i]->jr_id;
274  if (isNumber($jr_id) == 1 && $jr_id != 0 )
275  {
276  $this->amortissement_histo[$i]->jr_internal=$cn->get_value('select jr_internal from jrn where jr_id=$1',
277  array( $jr_id));
278  }
279  }
280  return $error_msg;
281  }
282 
283  /**
284  * we save into the two tables
285  * amortissement and amortissement_detail
286  *@see from_array
287  */
288  public function update()
289  {
290  global $cn;
291 
292  try
293  {
294  $this->amortissement->update();
295  if (isset($this->amortissement_detail))
296  {
297  for ($i=0; $i<count($this->amortissement_detail); $i++)
298  {
299  $this->amortissement_detail[$i]->update();
300  if ( $this->amortissement_histo[$i]->jr_internal == "")
301  $this->amortissement_histo[$i]->jr_internal=null;
302  $this->amortissement_histo[$i]->update();
303  }
304  } /*
305  * remove row from amortissement_detail if ad_amount=0
306  */
307  $cn->exec_sql('delete from amortissement.amortissement_detail where ad_amount=0');
308  }
309  catch (Exception $e)
310  {
311  echo $e->getMessage();
312  }
313  }
314 
315  /**
316  * we save into the two tables
317  * amortissement and amortissement_detail, the table amortissement_detail
318  * is filled via a trigger
319  *@see from_array
320  */
321  public function add()
322  {
323  $this->amortissement->save();
324  }
325  public function set_material($f)
326  {
327  global $cn;
328  $this->amortissement=new Amortissement_Sql($cn);
329  $this->amortissement_detail=new Amortissement_Detail_Sql($cn);
330  $this->amortissement->a_id=$cn->get_value("select a_id from amortissement.amortissement where f_id=$1",
331  array($f));
332  }
333  /**
334  *Display the detail of a card
335  */
336  public function print_detail($p_code)
337  {
338  global $cn;
339  $card=new Fiche($cn);
340  $card->get_by_qcode($p_code);
341  $amort=new Amortissement_Sql($cn);
342  $amort->a_id=$cn->get_value('select a_id from amortissement.amortissement where f_id=$1',array($card->id));
343 
344  if ( $amort->a_id =='')
345  {
346  echo '<h2 class="error">Non trouvé</h2>';
347  exit();
348  }
349  $amort->load();
350  $p_amount=$amort->a_amount;
351  $p_year=$amort->a_start;
352  $p_deb=$amort->account_deb;
353  $deb_span=$cn->get_value('select pcm_lib from tmp_pcmn where pcm_val=$1',
354  array($p_deb));
355  $p_cred=$amort->account_cred;
356  $cred_span=$cn->get_value('select pcm_lib from tmp_pcmn where pcm_val=$1',
357  array($p_cred));
358  $p_number=$amort->a_nb_year;
359  $a=new Amortissement_Detail_Sql($cn);
360  $p_date=$amort->a_date;
361  $array=$a->seek(' where a_id=$1 order by ad_year asc',array($amort->a_id));
362 
363  $fiche_deb=new Fiche($cn);
364 
365  $p_card_deb=new ICard('p_card_deb');
366  $p_card_deb->setReadOnly(true);
367  $p_card_cred=new ICard('p_card_cred');
368  $p_card_cred->typecard='all';
369  $p_card_cred->setReadOnly(true);
370  if ( $amort->card_deb != '' )
371  {
372  $fiche_deb=new Fiche($cn,$amort->card_deb);
373  $p_card_deb->value=$fiche_deb->get_quick_code();
374  }
375  if ( $amort->card_cred != '' )
376  {
377  $fiche_cred=new Fiche($cn,$amort->card_cred);
378  $p_card_cred->value=$fiche_cred->get_quick_code();
379  }
380 
381  require_once('template/material_display.php');
382  }
383 }
$f
Definition: show_field.php:4
$amortissement_histo
Manage the table amortissement.amortissement_detail.
$select_type id
$ret
input()
show a form to modify data
$select_type
print_detail($p_code)
Display the detail of a card.
$p_card_deb
$cred_span
add()
we save into the two tables amortissement and amortissement_detail, the table amortissement_detail is...
if(isset($_POST['remove'])) $array
listing($p_all=false)
display the list of material
add_card()
display a button to add a material
$amortissement_detail
Manage the table amortissement.amortissement_histo.
update()
we save into the two tables amortissement and amortissement_detail
set_material($f)
if(isset($_POST['generate'])) if(isset($_POST['save']))
$_REQUEST['sb']
Definition: am_print.php:39
$p_card_cred
Manage the table amortissement.amortissement.
from_array($p_array)
all the data are in the array : we construct an object with data from the array
if($p_number->value==0) l les annuités seront recalculées et l pct[] ad_year[] select jr_internal
verify_post()
Verify that all data are correct.
global $cn