noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
anc_key.class.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
21// Copyright (2014) Author Dany De Bontridder danydb@aevalys.eu
22
23/**
24 * @file
25 * @brief Class to manage distribution keys for Analytic accountancy
26 *
27 */
28
29
30/**
31 * @class Anc_Key
32 * @brief Class to manage distribution keys for Analytic accountancy
33 *
34 */
35
36require_once NOALYSS_INCLUDE.'/database/anc_key_sql.class.php';
37
39{
40
41 private $key; /*! the distribution key */
42 protected $a_ledger ; /*!< array of ledger (JRN_DEF.JRN_DEF_ID) for
43 * which the key is available*/
44 protected $a_activity; /*!< array of activity for
45 * which the key is available*/
46 protected $a_row; /*!< array of row from key_distribution_detail
47 * , detail of the key*/
48
49 /**
50 *@brief Return the number of keys available.
51 * Return the number of keys available for the ledger given in parameter
52 *
53 * @global $cn database connection
54 * @param $p_jrn number of the ledger (jrn_def.jrn_def_id
55 * @return number of available keys
56 */
57 static function key_available($p_jrn)
58 {
59 global $cn;
60 $count=$cn->get_value (' select count(*)
61 from key_distribution_ledger
62 join key_distribution using (kd_id)
63 where
64 jrn_def_id=$1', array($p_jrn));
65 return $count;
66 }
67 function __construct($p_id=-1)
68 {
69 global $cn;
70 $this->key=new Anc_Key_SQL($cn, $p_id);
71 $this->a_ledger=null;
72 $this->a_activity=null;
73 $this->a_row=null;
74 }
75 public function get_key()
76 {
77 return $this->key;
78 }
79
80 public function set_key($key)
81 {
82 $this->key=$key;
83 return $this;
84 }
85
86 /**
87 * @brief display list of available keys
88 * @param $p_amount amount to distribute
89 * @param $p_target target to update
90 * @param $p_ledger is the jrn_def_id
91 */
92 static function display_choice($p_amount, $p_target,$p_ledger)
93 {
94 global $cn;
95 $a_key=$cn->get_array(' select kd_id,
96 kd_name,
97 kd_description
98 from
99 key_distribution
100 join key_distribution_ledger using (kd_id)
101 where
102 jrn_def_id=$1',
103 array(
104 $p_ledger
105 ));
106 if (empty($a_key))
107 {
108 echo _('Aucune clef disponible');
109 echo _('Allez dans ANCKEY pour en ajouter pour ce journal');
110 }
111 include NOALYSS_TEMPLATE.'/anc_key_display_choice.php';
112 }
113
114 /**
115 * @brief display a list of keys, choose one to modify it
116 *
117 */
118 static function display_list()
119 {
120 global $cn;
121 $a_key=$cn->get_array('select b.kd_id,b.kd_name,b.kd_description,
122 (select sum(ke_percent) from key_distribution_detail as a where a.kd_id=b.kd_id) as distrib
123 from key_distribution as b order by b.kd_name');
124 if (empty($a_key))
125 {
126 echo _('Aucune clef disponible');
127 }
128 include NOALYSS_TEMPLATE.'/anc_key_display_list.php';
129 }
130
131 /**
132 * @brief Show the detail for a key distribution and let you change it
133 * for adding or update
134 */
135 function input()
136 {
137 global $cn;
138
139 $plan=$cn->get_array('
140 select
141 pa_id,
142 pa_name ,
143 pa_description
144 from
145 plan_analytique
146 order by pa_name');
147 $count_max=count($plan);
148
149 $a_row=$cn->get_array('select ke_id,ke_row,ke_percent from key_distribution_detail
150 where
151 kd_id=$1 order by ke_row', array($this->key->getp('id')));
152
153 require_once NOALYSS_TEMPLATE.'/anc_key_input.php';
154 }
155
156 /**
157 * @brief verify that data are ok
158 * @param type $p_array
159 */
160 function verify($p_array)
161 {
162 $http=new HttpInput();
163 $http->set_array($p_array);
164
165 $a_percent=$http->extract('percent',"array");
166
167 if (count($a_percent)==0)
168 {
169 throw new Exception(_('Aucune répartition'));
170 }
171 $tot_percent=0;
172 bcscale(4);
173 for ($i=0; $i<count($a_percent); $i++)
174 {
175 $tot_percent=bcadd($tot_percent, $a_percent[$i]);
176 }
177 if ($tot_percent >100)
178 {
179 throw new Exception(_('Le total ne vaut pas 100, total calculé = ').$tot_percent);
180 }
181
182 if (trim($http->extract('name_key'))=='') {
183 throw new Exception (_('Le nom ne peut être vide'));
184 }
185 }
186
187 /**
188 * @brief save the data of a repartition key.
189 * @param received an array
190 * index :
191 * - key_id : key_distribution.kd_id
192 * - row : array of key_distribution.ke_id (row
193 * - pa : array of plan_analytic.pa_id (column)
194 * - po_id : double array,
195 * first index is the row
196 * second index is the first plan, the second the second plan...(column)
197 * - percent array, one per row
198 * - jrn : array of available ledgers
199 * @note if po_id == -1 then it is replaced by null, this why the pa_id is needed : to identify
200 * the column
201 * @verbatim
202
203 'key_id' => string '1' (length=1)
204 "name_key" => "name"
205 "description_key" => "description"
206 'row' =>
207 array
208 0 => string '1' (length=1)
209 1 => string '2' (length=1)
210 2 => string '3' (length=1)
211 'pa' =>
212 array
213 0 => string '1' (length=1)
214 1 => string '2' (length=1)
215 'po_id' =>
216 array
217 0 =>
218 array
219 0 => string '1' (length=1)
220 1 => string '8' (length=1)
221 1 =>
222 array
223 0 => string '2' (length=1)
224 1 => string '-1' (length=2)
225 2 =>
226 array
227 0 => string '3' (length=1)
228 1 => string '8' (length=1)
229 'percent' =>
230 array
231 0 => string '50.0000' (length=7)
232 1 => string '20.0000' (length=7)
233 2 => string '30.0000' (length=7)
234 'jrn' =>
235 array
236 0 => string '3' (length=1)
237 1 => string '2' (length=1)
238 @endverbatim
239 *
240 */
241 function save($p_array)
242 {
243 global $cn;
244 $this->verify($p_array);
245 $cn->start();
246 // for each row
247 $http=new HttpInput();
248 $http->set_array($p_array);
249
250 $a_row=$http->extract('row',"array");
251 $a_ledger=$http->extract("jrn","string",array());
252 $a_percent=$http->extract('percent',"array",[]);
253 $a_po_id=$http->extract('po_id',"array",[]);
254 $a_plan=$http->extract('pa',"array",[]);
255 try
256 {
257 $this->key->setp('name',$http->extract('name_key'));
258 $this->key->setp('description',$http->extract('description_key'));
259 $this->key->save();
260 for ($i=0; $i<count($a_row); $i++)
261 {
262 //save key_distribution_row
263 $key_row=new Anc_Key_Detail_SQL($cn);
264 $key_row->setp('id', $a_row[$i]);
265 $key_row->setp('key', $this->key->getp('id'));
266 $key_row->setp('row', $i+1);
267 $key_row->setp('percent', $a_percent[$i]);
268 $key_row->save();
269 //
270 // Save each activity + percent
271 $cn->exec_sql('delete from key_distribution_activity where ke_id=$1', array($key_row->getp('id')));
272
273 // Don't save row with 0 %
274 if ($a_percent[$i]==0)
275 {
276 $key_row->delete();
277 continue;
278 }
279 for ($j=0; $j<count($a_po_id[$i]); $j++)
280 {
281 $activity=new Anc_Key_Activity_SQL($cn);
282 $activity->setp('detail', $key_row->ke_id);
283 $value=($a_po_id[$i][$j]==-1)?null:$a_po_id[$i][$j];
284 $activity->setp('activity', $value);
285 $activity->setp('plan',$a_plan[$j]);
286 $activity->save();
287 }
288 }
289 // delete all from key_distribution_ledger
290 $cn->exec_sql('delete from key_distribution_ledger where kd_id=$1', array($this->key->getp('id')));
291 for ($k=0; $k<count($a_ledger); $k++)
292 {
294 $ledger->kd_id=$this->key->getp('id');
295 $ledger->jrn_def_id=$a_ledger[$k];
296 $ledger->save();
297 }
298
299 $cn->commit();
300 }
301 catch (Exception $e)
302 {
303
304 if ( DEBUGNOALYSS > 0 ) { echo $e->getTraceAsString(); } else { echo _('erreur').$e->getMessage();}
305 record_log($e);
306 $cn->rollback();
307 }
308 }
309 /**
310 * @brief Call the Anc_Operation::display_form_plan with the right amounts.
311 * This function compute the array and amount to pass to the Anc_Operation::display_form_plan
312 * and replace the current table of activity with the value computed from the key.
313 *
314 * @global $cn database connection
315 * @param $p_target Table to be replaced
316 * @param $p_amount amount to distribute among activities
317 */
318 function fill_table($p_target,$p_amount)
319 {
320 global $cn;
321 /* number is the index of the plan, he's computed from p_target */
322 $number=preg_replace('/det[0-9]*/', '', $p_target);
323 $number=noalyss_str_replace('t', '', $number);
324 $number=noalyss_str_replace('popup', '', $number);
325
326 $op[$number]=$p_amount;
327 $array['op']=$op;
328 $a_plan=$cn->get_array('select pa_id from plan_analytique order by pa_id');
329 for ($i=0;$i < count($a_plan);$i++)
330 {
331 $array['pa_id'][$i]=$a_plan[$i]['pa_id'];
332 }
333
334 $a_poste=$cn->get_array('select po_id,ke_percent,pa_id,ke_row
335 from key_distribution_activity
336 join key_distribution_detail using (ke_id)
337 where
338 kd_id=$1
339 order by ke_row,pa_id',
340 array($this->key->getp('id')));
341
342 for ($i=0;$i< count($a_poste);$i++)
343 {
344 $hplan[$number][$i]=($a_poste[$i]['po_id']==null)?-1:$a_poste[$i]['po_id'];
345 }
346 $array['hplan']=$hplan;
347
348 $a_amount=$cn->get_array("select distinct ke_row,ke_percent
349 from key_distribution_activity
350 join key_distribution_detail using (ke_id)
351 where
352 kd_id=$1
353 and pa_id=$2
354 order by ke_row",
355 array($this->key->getp('id'),$a_plan[0]['pa_id']));
356 bcscale(2);
357 for ($i=0;$i< count($a_amount);$i++)
358 {
359 $val[$number][$i]=bcmul($p_amount,$a_amount[$i]['ke_percent']);
360 $val[$number][$i]=bcdiv($val[$number][$i],100);
361 $val[$number][$i]=abs($val[$number][$i]);
362 }
363 $array['val']=$val;
364
365 $anc_operation=new Anc_Operation($cn);
366 // find the div thanks the target which looks like "det4t0"
367 // det4 is the div id and t0 is the row
368 if (strpos($p_target, "et")){
369 // cut off t[0-9]
370 $pos=strrpos($p_target,"t");
371 $anc_operation->in_div=substr($p_target,0,$pos);
372
373 }
374 echo $anc_operation->display_form_plan($array, 1, 1, $number, $p_amount,'',false);
375
376 }
377 /**
378 *@brief show a form for adding a key + button to display it
379 *
380 */
381 static function key_add()
382 {
383 $key=new Anc_Key();
384 $key->key->setp('name',_('Nouvelle clef'));
385 $key->key->setp('description',_('Description de la nouvelle clef'));
386 ?>
387<input type="button" class="smallbutton" value="<?php echo _('Ajout')?>" onclick="$('key_add_div_id').show()">
388<div id="key_add_div_id" style="display: none">
389<?php
390 $key->input();
391 echo '</div>';
392
393 }
394 /**
395 *@brief delete the distribution key
396 */
397 function delete ()
398 {
399 $this->key->delete();
400 }
401}
noalyss_str_replace($search, $replace, $string)
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...
$op
$opd_description style
for($i=0; $i< count($plan); $i++)( $j==0) $a_poste
_("actif, passif,charge,...")
$input_from type
manage table key_distribution_activity
manage table key_distribution_detail
manage table key_distribution_ledger
Manage the table key_distribution.
Class to manage distribution keys for Analytic accountancy.
static display_list()
display a list of keys, choose one to modify it
static display_choice($p_amount, $p_target, $p_ledger)
display list of available keys
fill_table($p_target, $p_amount)
Call the Anc_Operation\display_form_plan with the right amounts.
input()
Show the detail for a key distribution and let you change it for adding or update.
verify($p_array)
verify that data are ok
static key_add()
show a form for adding a key + button to display it
save($p_array)
save the data of a repartition key.
static key_available($p_jrn)
Return the number of keys available.
__construct($p_id=-1)
set_key($key)
this class is used to show the form for entering an operation only FOR analytic operation to save it,...
manage the http input (get , post, request) and extract from an array
$count