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