noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
acc_ledger_history.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * Copyright (C) 2018 Dany De Bontridder <dany@alchimerys.be>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program 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 this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 */
20
21
22/**
23 * @file
24 * @brief display or export operations in HTML , PDF or CSV
25 *
26 */
27/**
28 * @class
29 * @brief Display history of operation
30 * @see acc_ledger_historyTest.php
31 */
32abstract class Acc_Ledger_History
33{
34
35 protected $m_from; //!< Starting Periode : periode.p_id
36 protected $m_to; //!< Ending Periode : periode.p_id
37 protected $ma_ledger; //!< Array of ledger id : jrn_def.jrn_def_id
38 protected $m_mode; //!< mode of export L : one line, E accounting writing , D : Detail
39 public $db; //!< database connx
40 protected $ledger_type; //! type of ledger VEN , ACH , ODS, FIN
41 protected $filter_operation; //!< to filter paid, unpaid or all operation
42
43 /**
44 *
45 * @param Database $cn
46 * @param array $pa_ledger array of jrn_def.jrn_def_id
47 * @param integer $p_from periode
48 * @param integer $p_to
49 * @param char $p_mode E D L or A , for Extended ,Detail , Listing , Accounting
50 * @throws Exception if $pa_ledger is not an array
51 */
52 function __construct(Database $cn, $pa_ledger, $p_from, $p_to, $p_mode)
53 {
54 if (is_array($pa_ledger) == FALSE) {
55 throw new Exception (_('pa_ledger doit être un tableau'),EXC_PARAM_VALUE);
56 }
57 $this->db=$cn;
58 $this->ma_ledger=$pa_ledger;
59 $this->m_from=$p_from;
60 $this->m_to=$p_to;
61 $this->m_mode=$p_mode;
62 $this->filter_operation='all';
63 }
64 /**
65 * setter / getter
66 * @returns m_from (periode id)
67 */
68 public function get_from()
69 {
70 return $this->m_from;
71 }
72 public function get_ledger_type()
73 {
74 return $this->ledger_type;
75 }
76
78 {
79 if (! in_array($ledger_type,['ACH','ODS','VEN','FIN'])) {
80 record_log('Acc_Ledger_History:set_ledger_type '.var_export($ledger_type,TRUE));
81 throw new Exception (_("Donnée invalide",EXC_PARAM_VALUE));
82 }
83
84 $this->ledger_type=$ledger_type;
85 return $this;
86 }
87
88 /**
89 * setter / getter
90 * @returns m_to (periode id)
91 */
92 public function get_to()
93 {
94 return $this->m_to;
95 }
96 /**
97 * setter / getter
98 * @returns ma_ledger (array)
99 */
100 public function get_ledger()
101 {
102 return $this->ma_ledger;
103 }
104 /**
105 * setter / getter
106 * @returns m_mode (A,L,E,D)
107 */
108 public function get_mode()
109 {
110 return $this->m_mode;
111 }
112 /**
113 * setter m_from (periode id)
114 */
115 public function set_from($m_from)
116 {
117 $this->m_from=$m_from;
118 return $this;
119 }
120 /**
121 * setter m_to (periode id)
122 */
123 public function set_to($m_to)
124 {
125 $this->m_to=$m_to;
126 return $this;
127 }
128 /**
129 * setter ma_ledger (array of jrn_def_id)
130 */
131 public function set_a_ledger($ma_ledger)
132 {
133 if (is_array($ma_ledger)==FALSE)
134 throw new Exception(_("invalid parameter"), EXC_PARAM_VALUE);
135 $this->ma_ledger=$ma_ledger;
136 return $this;
137 }
138 /**
139 * Setter
140 * @param $m_mode D,A,L,E
141 * @return $this
142 * @throws Exception
143 */
144 public function set_m_mode($m_mode)
145 {
146 if ($m_mode!='E'&&$m_mode!='D'&&$m_mode!='L'&&$m_mode!='A')
147 throw new Exception(_("invalid parameter"), EXC_PARAM_VALUE);
148 $this->m_mode=$m_mode;
149 return $this;
150 }
151
152 /**
153 * Build the right object
154 * @param Database $cn database conx
155 * @param array $pa_ledger ledger of array
156 * @param integer $p_from periode id
157 * @param integer $p_to periode id
158 * @param char $p_mode L (list operation) E (extended detail) A (accouting writing) D (Detailled VAT)
159 * @param $p_paid values are all means all operations, paid only paid operation, unpaid for only unpaid
160 * @return Acc_Ledger_History_Generic Acc_Ledger_History_Sale Acc_Ledger_History_Financial Acc_Ledger_History_Purchase
161 */
162 static function factory(Database $cn, $pa_ledger, $p_from, $p_to, $p_mode,$p_paid)
163 {
164 // For Accounting writing , we use Acc_Ledger_History
165 if ($p_mode=="A")
166 {
167 $ret=new Acc_Ledger_History_Generic($cn, $pa_ledger, $p_from, $p_to,
168 $p_mode);
169 return $ret;
170 }
171 $nb_ledger=count($pa_ledger);
172 $ledger=new Acc_Ledger($cn, $pa_ledger[0]);
173 $type=$ledger->get_type();
174
175 // If first one is ODS so Acc_Ledger_History
176 if ($type=="ODS")
177 {
178 $ret=new Acc_Ledger_History_Generic($cn, $pa_ledger, $p_from, $p_to,
179 $p_mode);
180 return $ret;
181 }
182 // If all of the same type then use the corresponding class
183
184 for ($i=0; $i<$nb_ledger; $i++)
185 {
186 $ledger=new Acc_Ledger($cn, $pa_ledger[$i]);
187 $type_next=$ledger->get_type();
188
189 // If type different then we go back to the generic
190 if ($type_next!=$type)
191 {
192 $ret=new Acc_Ledger_History_Generic($cn, $pa_ledger, $p_from,
193 $p_to, $p_mode);
194 return $ret;
195 }
196 }
197 switch ($type)
198 {
199 case "ACH":
200 $ret=new Acc_Ledger_History_Purchase($cn, $pa_ledger, $p_from,
201 $p_to, $p_mode);
202 $ret->set_filter_operation($p_paid);
203 return $ret;
204 break;
205 case "FIN":
206 $ret=new Acc_Ledger_History_Financial($cn, $pa_ledger, $p_from,
207 $p_to, $p_mode);
208 return $ret;
209 break;
210 case "VEN":
211 $ret=new Acc_Ledger_History_Sale($cn, $pa_ledger, $p_from,
212 $p_to, $p_mode);
213 $ret->set_filter_operation($p_paid);
214 return $ret;
215 break;
216
217 default:
218 break;
219 }
220 }
221
222 /**
223 * Retrieve the third : supplier for purchase, customer for sale, bank for fin,
224 * @param $p_jrn_type type of the ledger FIN, VEN ACH or ODS
225 * @param $jr_id jrn.jr_id
226 * @todo duplicate function , also in Acc_Ledger::get_tiers, remove one
227 */
228 function get_tiers($p_jrn_type, $jr_id)
229 {
230 if ($p_jrn_type=='ODS')
231 return ' ';
232 $tiers=$this->get_tiers_id($p_jrn_type, $jr_id);
233 if ($tiers==0)
234 return "";
235
236 $name=$this->db->get_value('select ad_value from fiche_detail where ad_id=1 and f_id=$1',
237 array($tiers));
238 $first_name=$this->db->get_value('select ad_value from fiche_detail where ad_id=32 and f_id=$1',
239 array($tiers));
240 return $name.' '.$first_name;
241 }
242
243 /**
244 * @brief Return the f_id of the tiers , called by get_tiers
245 * @param $p_jrn_type type of the ledger FIN, VEN ACH or ODS
246 * @param $jr_id jrn.jr_id
247 */
248 function get_tiers_id($p_jrn_type, $jr_id)
249 {
250 $tiers=0;
251 switch ($p_jrn_type)
252 {
253 case 'VEN':
254 $tiers=$this->db->get_value('select max(qs_client) '
255 . 'from quant_sold join jrnx using (j_id) '
256 . 'join jrn on (jr_grpt_id=j_grpt) where jrn.jr_id=$1',
257 array($jr_id));
258 break;
259 case 'ACH':
260 $tiers=$this->db->get_value('select max(qp_supplier) '
261 . ' from quant_purchase '
262 . ' join jrnx using (j_id) '
263 . ' join jrn on (jr_grpt_id=j_grpt) '
264 . ' where jrn.jr_id=$1',
265 array($jr_id));
266
267 break;
268 case 'FIN':
269 $tiers=$this->db->get_value('select qf_other from quant_fin where jr_id=$1',
270 array($jr_id));
271 break;
272 }
273 if ($this->db->count()==0)
274 return 0;
275 return $tiers;
276 }
277
278
279 /**
280 * Prepare the query for fetching the linked operation
281 * @staticvar int $prepare
282 */
283 protected function prepare_reconcile_date()
284 {
285 static $not_done=TRUE;
286 if ($not_done ) {
287 $prepare_query = new Prepared_Query($this->db);
288 $prepare_query->prepare_reconcile_date();
289 }
290 $not_done=FALSE;
291 }
292 /**
293 * display accounting of operations m_mode=A
294 */
295 abstract function export_accounting_html();
296 /**
297 * display detail of operations m_mode=D
298 */
299 abstract function export_detail_html();
300 /**
301 * display extended details of operation m_mode=E
302 */
303 abstract function export_extended_html();
304 /**
305 * display operation on one line m_mode=L
306 */
307 abstract function export_oneline_html();
308 /**
309 * call the right function , depending of m_mode
310 */
311 abstract function export_html();
312
313 abstract function get_row($p_limit, $p_offset);
314
315 /**
316 * Build a SQL clause to filter operation depending if they are paid, unpaid or no filter
317 * @return string SQL Clause
318 */
319 protected function build_filter_operation()
320 {
321 switch ( $this->get_filter_operation() )
322 {
323 case 'all':
324 $sql_filter="";
325 break;
326 case 'paid':
327 $sql_filter=" and (jr_date_paid is not null or jr_rapt ='paid' ) ";
328 break;
329 case 'unpaid':
330 $sql_filter=" and (jr_date_paid is null and coalesce(jr_rapt,'x') <> 'paid' ) ";
331 break;
332 default:
333 throw new Exception(_("Filtre invalide",5));
334
335 }
336 return $sql_filter;
337 }
338 /**
339 * Filter operation
340 */
342 {
344 }
345 /**
346 * Filter operation ,
347 * @param string $filter_operation, valid : all, paid, unpaid
348 */
349
351 {
352 if (in_array($filter_operation,['all','paid','unpaid']))
353 {
354 $this->filter_operation=$filter_operation;
355 return $this;
356 }
357 throw new Exception(_("Filter invalide ".$filter_operation),5);
358 }
359
360 /**
361 * @brief count the number of addition tax for the ledger
362 * @return integer
363 */
364 public function has_other_tax()
365 {
366 $str_ledger=join(',',$this->ma_ledger);
367 $count=$this->db->get_value("select count(*)
368 from jrn_tax
369 join jrnx using (j_id)
370 join jrn on (jr_grpt_id=j_grpt)
371 where jr_tech_per>=$1 and jr_tech_per <=$2
372 and jr_def_id in ($str_ledger) ",[$this->m_from,$this->m_to]);
373 return $count;
374 }
375 /**
376 * @brief add additional info about additional tax. Add to $this->data an array containing the
377 * info about a additional tax. Concerns only purchase and sales ledgers
378 * @verbatim
379 $this->data[$i]['supp_tax']['ac_id'] id in Acc_Other_Tax
380 $this->data[$i]['supp_tax']['j_montant'] Amount of this tax
381 $this->data[$i]['supp_tax']['ac_label'] Label of this tax
382 $this->data[$i]['supp_tax']['ac_rate'] Rate of this tax
383 $this->data[$i]['supp_tax']['j_poste'] Accounting
384 * @endverbatim
385 */
386 protected function add_additional_tax_info()
387 {
388 $prepare=$this->db->is_prepare("supp_tax_info");
389 if ( $prepare == false ){
390 $this->db->prepare("supp_tax_info","
391 select
392 case when j.j_debit is false and jd.jrn_def_type='ACH' then 0-j_montant
393 when j.j_debit is true and jd.jrn_def_type='VEN' then 0-j_montant
394 else j.j_montant end j_montant,
395 jt1.ac_id,
396 ac_label,
397 ac_rate,j_poste
398 from
399 jrn_tax jt1
400 join acc_other_tax using (ac_id)
401 join jrnx j using (j_id)
402 join jrn_def jd on (j.j_jrn_def=jd.jrn_def_id)
403 where j_grpt=$1
404 ");
405
406 }
407 $data=$this->get_data();
408 $nb_row=count($data);
409
410 for ($i=0;$i<$nb_row;$i++)
411 {
412 $ret=$this->db->execute("supp_tax_info",array($data[$i]["jr_grpt_id"]));
414 $array=($array==false)?array():$array;
415 $data[$i]["supp_tax"]=$array;
416 }
417 $this->set_data($data);
418 }
419}
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...
$jr_id
_("actif, passif,charge,...")
display financial operations in HTML , PDF or CSV
manage the list of operation when we need several ledger with a different type or from Misceleaneous ...
Display the operations for Purchase.
Acc_Ledger_History : Manage the list (history) of operations for display.
Display history of operation.
set_from($m_from)
setter m_from (periode id)
get_filter_operation()
Filter operation.
$m_to
Ending Periode : periode.p_id.
export_html()
call the right function , depending of m_mode
export_oneline_html()
display operation on one line m_mode=L
set_to($m_to)
setter m_to (periode id)
$m_from
Starting Periode : periode.p_id.
has_other_tax()
count the number of addition tax for the ledger
get_tiers($p_jrn_type, $jr_id)
Retrieve the third : supplier for purchase, customer for sale, bank for fin,.
export_accounting_html()
display accounting of operations m_mode=A
set_filter_operation($filter_operation)
Filter operation ,.
build_filter_operation()
Build a SQL clause to filter operation depending if they are paid, unpaid or no filter.
export_extended_html()
display extended details of operation m_mode=E
$filter_operation
type of ledger VEN , ACH , ODS, FIN
set_a_ledger($ma_ledger)
setter ma_ledger (array of jrn_def_id)
get_tiers_id($p_jrn_type, $jr_id)
Return the f_id of the tiers , called by get_tiers.
static factory(Database $cn, $pa_ledger, $p_from, $p_to, $p_mode, $p_paid)
Build the right object.
$m_mode
mode of export L : one line, E accounting writing , D : Detail
prepare_reconcile_date()
Prepare the query for fetching the linked operation @staticvar int $prepare.
__construct(Database $cn, $pa_ledger, $p_from, $p_to, $p_mode)
export_detail_html()
display detail of operations m_mode=D
add_additional_tax_info()
add additional info about additional tax.
$ma_ledger
Array of ledger id : jrn_def.jrn_def_id.
get_row($p_limit, $p_offset)
Class for jrn, class acc_ledger for manipulating the ledger AND some acc.
static fetch_all($ret)
wrapper for the function pg_fetch_all
contains the class for connecting to Noalyss
contains prepared query used in different classes of the application
const EXC_PARAM_VALUE
Definition constant.php:343
$count
$SecUser db
$sql_filter
Definition preod.inc.php:43