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