noalyss Version-10
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
acc_ledger_history_purchase.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * PhpCompta 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 PhpCompta; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20// Copyright (2018) Author Dany De Bontridder <dany@alchimerys.be>
21
22/**
23 * @file
24 * @brief class Acc_Ledger_History_Purchase , list of operations
25 * * display or export operations in HTML , PDF or CSV
26 */
27
28/**
29 * @brief Display the operations for Purchase
30 * @see acc_ledger_historyTest.php
31 */
33{
34
35 /**
36 * @param mixed $data
37 */
38 public function set_data($data)
39 {
40 //!< Contains rows from SQL
41 $this->data = $data;
42 return $this;
43 }
44
45 public function __construct(\Database $cn, $pa_ledger, $p_from, $p_to,
46 $p_mode)
47 {
48 parent::__construct($cn, $pa_ledger, $p_from, $p_to, $p_mode);
49 $this->filter_operation='all';
50 $this->ledger_type='ACH';
51 }
52 public function get_filter_operation()
53 {
55 }
56
57
58 /**
59 * @brief display the accounting
60 */
61 public function export_accounting_html()
62 {
63 $ledger_history=new Acc_Ledger_History_Generic($this->db,
64 $this->ma_ledger, $this->m_from, $this->m_to, $this->m_mode);
65 $ledger_history->export_accounting_html();
66 }
67
68 public function export_detail_html()
69 {
71
72 $this->get_row();
73 $this->add_vat_info();
75 include NOALYSS_TEMPLATE."/acc_ledger_history_purchase_detail.php";
76 }
77
78 public function export_extended_html()
79 {
80 $this->get_row();
81 $this->add_vat_info();
83 $this->prepare_detail();
85 include NOALYSS_TEMPLATE."/acc_ledger_history_purchase_extended.php";
86 }
87
88 /**
89 * @brief display in HTML following the mode
90 */
91 function export_html()
92 {
93 switch ($this->m_mode)
94 {
95 case "E":
96 $this->export_extended_html();
97 break;
98 case "D":
99 $this->export_detail_html();
100 break;
101 case "L":
102 $this->export_oneline_html();
103 break;
104 case "A":
105 $this->export_accounting_html();
106 break;
107 default:
108 break;
109 }
110 }
111
112 /**
113 * display in HTML one operation by line
114 */
115 public function export_oneline_html()
116 {
117 $this->get_row();
118 $this->prepare_reconcile_date();
119 $nb_other_tax=$this->has_other_tax();
120 require_once NOALYSS_TEMPLATE.'/acc_ledger_history_purchase_oneline.php';
121 }
122
123 /**
124 * Get the rows from jrnx and quant* tables
125 * @param int $p_limit max of rows to returns
126 * @param int $p_offset the number of rows to skip
127 */
128 public function get_row($p_limit=-1, $p_offset="")
129 {
130 $periode=sql_filter_per($this->db, $this->m_from, $this->m_to, 'p_id',
131 'jr_tech_per');
132
133 $cond_limite=($p_limit!=-1)?" limit ".$p_limit." offset ".$p_offset:"";
135 $ledger_list=join(",", $this->ma_ledger);
136 $sql="
137 with row_purchase as
138 (select qp_internal,
139 qp_supplier,sum(qp_price) as novat,
140 sum(qp_vat) as vat ,
141 sum(qp_vat_sided) as tva_sided ,
142 sum(qp_nd_amount) as noded_amount,
143 sum(qp_nd_tva) as noded_vat,
144 sum(qp_dep_priv) as private_amount
145 from
146 quant_purchase group by qp_supplier,qp_internal),
147 supplier_detail as (
148 select x.f_id as f_id,
149 (select ad_value from fiche_detail where ad_id=1 and f_id=x.f_id) as name,
150 (select ad_value from fiche_detail where ad_id=32 and f_id=x.f_id) as first_name,
151 (select ad_value from fiche_detail where ad_id=23 and f_id=x.f_id) as qcode
152 from
153 fiche as x) ,
154 row_currency as (
155 select sum(oc_amount) as sum_oc_amount,sum(oc_vat_amount) as sum_oc_vat_amount,jrnx.j_grpt
156 from
157 operation_currency
158 join jrnx using (j_id)
159 join quant_purchase qp using (j_id)
160 group by j_grpt
161 ),
162 other_tax as (select sum(case when j_debit is true
163 then j_montant else 0-j_montant end) other_tax_amount
164 ,j_grpt
165 from jrnx j1
166 join jrn_tax jt2 on (j1.j_id=jt2.j_id) group by j_grpt)
167 select
168 name,
169 first_name,
170 qcode,
171 jrn.jr_id,
172 jr_pj_number,
173 to_char(jr_date,'DD.MM.YYYY') as str_date,
174 to_char(jr_date,'DDMMYY') as str_date_short,
175 to_char(jr_date_paid,'DD.MM.YYYY') as str_date_paid,
176 jr_internal,
177 qp_supplier,
178 jrn.jr_comment,
179 jr_pj_name,
180 vat,
181 tva_sided,
182 novat,
183 noded_amount,
184 noded_vat,
185 private_amount,
186 novat+vat-tva_sided as tvac,
187 n_text,
188 jr_grpt_id,
189 jrn.currency_id,
190 jrn.currency_rate,
191 jrn.currency_rate_ref,
192 sum_oc_amount,
193 sum_oc_vat_amount,
194 cr_code_iso,
195 coalesce (other_tax_amount,0) other_tax_amount
196 from
197 jrn
198 join row_purchase on (qp_internal=jr_internal)
199 join supplier_detail on (qp_supplier=f_id)
200 left join jrn_note using (jr_id)
201 left join row_currency as rc on (rc.j_grpt = jrn.jr_grpt_id)
202 left join currency as c on (c.id=jrn.currency_id)
203 left join other_tax as ot on (ot.j_grpt=jrn.jr_grpt_id)
204 where
205 jr_def_id in ({$ledger_list})
206 {$sql_filter}
207 and {$periode}
208 {$cond_limite}
209 order by jrn.jr_date, substring(jr_pj_number,'[0-9]+$')::numeric ";
210 $this->data=$this->db->get_array($sql);
211 }
212
213 /**
214 * @brief preprare the query for fetching the detailed VAT of an operation
215 */
216 private function add_vat_info()
217 {
218 $prepare=$this->db->is_prepare("vat_infop");
219 if ($prepare==FALSE)
220 {
221 $this->db->prepare("vat_infop",
222 "
223 select
224 sum(qp_vat) vat_amount ,
225 qp_vat_code
226 from
227 quant_purchase
228 where
229 qp_internal = $1
230 group by qp_vat_code order by qp_vat_code");
231 }
232
233 $nb_row=count($this->data);
234 for ($i=0; $i<$nb_row; $i++)
235 {
236 $ret=$this->db->execute("vat_infop",
237 array($this->data[$i]["jr_internal"]));
239 $this->data[$i]["detail_vat"]=$array;
240 }
241 }
242
243 /**
244 * Prepare the query for fetching detail of an operation
245 */
246 private function prepare_detail()
247 {
248
249 if ($this->db->is_prepare("detail_purchase")==FALSE)
250 {
251 $this->db->prepare("detail_purchase",
252 "
253 with card_name as
254 (select f_id,ad_value as name
255 from fiche_detail where ad_id=1),
256 card_qcode as
257 (select f_id,ad_value as qcode
258 from fiche_detail where ad_id=23)
259 select qp_price,qp_quantite,qp_vat,qp_vat_code,qp_unit,qp_vat_sided,name,qcode,tva_label,
260 qp_price+qp_vat-qp_vat_sided as tvac
261 from
262 quant_purchase
263 join jrnx using (j_id)
264 join card_name on (card_name.f_id=qp_fiche)
265 join card_qcode on (card_qcode.f_id=qp_fiche)
266 left join tva_rate on ( qp_vat_code=tva_id)
267 where
268 qp_internal=$1
269
270 ");
271 }
272 }
273 /**
274 * To get data
275 * @return array of rows
276 */
277 function get_data()
278 {
279 return $this->data;
280 }
281
282 /**
283 * @brief export Purchase in CSV
284 */
285 function export_csv()
286 {
287 // Prepare the query for reconcile date
288 $prepared_query=new Prepared_Query($this->db);
289 $prepared_query->prepare_reconcile_date();
290 $nb_other_tax=$this->has_other_tax();
291
292 $export=new Noalyss_Csv(_('journal'));
293 $export->send_header();
294
295 $this->get_row();
296 $this->prepare_reconcile_date();
297 $this->add_vat_info();
298
299 $own=new Noalyss_Parameter_Folder($this->db);
300 $title=array();
301 $title[]=_('Date');
302 $title[]=_("Paiement");
303 $title[]=_("operation");
304 $title[]=_("Pièce");
305 $title[]=_("Fournisseur");
306 $title[]=_("Note");
307 $title[]=_("interne");
308 $title[]=_("HTVA");
309 $title[]=_("privé");
310 $title[]=_("DNA");
311 $title[]=_("tva non ded.");
312 $title[]=_("TVA NP");
313
314
315
316 if ( $own->MY_TVA_USE=='Y')
317 {
318 $a_Tva=$this->db->get_array("select tva_id,tva_label from tva_rate order by tva_rate,tva_label,tva_id");
319 foreach($a_Tva as $line_tva)
320 {
321 $title[]="Tva ".$line_tva['tva_label'];
322 }
323 }
324 if ($nb_other_tax>0) {
325 $title[]=_("Autre taxe");
326 }
327 $title[]=_("TVAC/TTC");
328 $title[]=_("Devise");
329 $title[]=_("Devise HTVA");
330 $title[]=_("Devise TVA");
331 $title[]=_("Taux ref");
332 $title[]=_("Taux utilisé");
333 $title[]=_("Date paiement");
334 $title[]=_("Code paiement");
335 $title[]=_("Méthode paiement");
336 $title[]=_("Montant paiement");
337 $title[]=_("n° opération");
338 $export->write_header($title);
339
340 foreach ($this->data as $line)
341 {
342 $export->add($line['str_date']);
343 $export->add($line['str_date_paid']);
344 $export->add($line['jr_id']);
345 $export->add($line['jr_pj_number']);
346 $export->add($line['name']." ".
347 $line["first_name"]." ".
348 $line["qcode"]); // qp_supplier
349 $export->add($line['jr_comment']);
350 $export->add($line['jr_internal']);
351 $export->add($line['novat'],"number");
352 $export->add($line['private_amount'],"number");
353 $export->add($line['noded_amount'],"number");
354 $export->add($line['noded_vat'],"number");
355 $export->add($line['tva_sided'],"number");
356
357 $a_tva_amount=array();
358 if ($own->MY_TVA_USE=='Y')
359 {
360 //- set all TVA to 0
361 foreach ($a_Tva as $l)
362 {
363 $t_id=$l["tva_id"];
364 $a_tva_amount[$t_id]=0;
365 }
366 foreach ($line['detail_vat'] as $lineTVA)
367 {
368 $idx_tva=$lineTVA['qp_vat_code'];
369 $a_tva_amount[$idx_tva]=$lineTVA['vat_amount'];
370 }
371
372 foreach ($a_Tva as $line_tva)
373 {
374 $a=$line_tva['tva_id'];
375 $export->add($a_tva_amount[$a], "number");
376 }
377 }
378 if ( $nb_other_tax > 0)
379 {
380 $export->add($line['other_tax_amount'],"number");
381 }
382 $export->add(bcadd($line['other_tax_amount'],$line['tvac'],2),"number");
383 /**
384 * Add currency info
385 */
386 $export->add($line['cr_code_iso']);
387 $export->add($line['sum_oc_amount'],'number');
388 $export->add($line['sum_oc_vat_amount'],'number');
389 $export->add($line['currency_rate'],'number');
390 $export->add($line['currency_rate_ref'],'number');
391
392 /**
393 * Retrieve payment if any
394 */
395 $ret_reconcile=$this->db->execute('reconcile_date',array($line['jr_id']));
397 if ($max > 0) {
398 for ($e=0;$e<$max;$e++) {
400 $export->add($row['jr_date']);
401 $export->add($row['qcode_bank']);
402 $export->add($row['qcode_name']);
403 $export->add($row['jr_montant'],"number");
404 $export->add($row['jr_internal']);
405 }
406 }
407 $export->write();
408
409 }
410
411 }
412}
sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
Create the condition to filter on the j_tech_per thanks a from and to date.
_("actif, passif,charge,...")
manage the list of operation when we need several ledger with a different type or from Misceleaneous ...
Display the operations for Purchase.
export_oneline_html()
display in HTML one operation by line
export_detail_html()
display detail of operations m_mode=D
add_vat_info()
preprare the query for fetching the detailed VAT of an operation
get_row($p_limit=-1, $p_offset="")
Get the rows from jrnx and quant* tables.
__construct(\Database $cn, $pa_ledger, $p_from, $p_to, $p_mode)
export_html()
display in HTML following the mode
export_extended_html()
display extended details of operation m_mode=E
prepare_detail()
Prepare the query for fetching detail of an operation.
Display history of operation.
$data
Contains data see acc_ledger_history_financial->get_row.
has_other_tax()
count the number of addition tax for the ledger
build_filter_operation()
Build a SQL clause to filter operation depending if they are paid, unpaid or no filter.
$filter_operation
type of ledger VEN , ACH , ODS, FIN
prepare_reconcile_date()
Prepare the query for fetching the linked operation @staticvar int $prepare.
add_additional_tax_info()
add additional info about additional tax.
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
static fetch_all($ret, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_all
static num_row($ret)
wrapper for the function pg_num_rows
contains the class for connecting to Noalyss
Manage the CSV : manage files and write CSV record.
Class to manage the company parameter (address, name...)
contains prepared query used in different classes of the application
$SecUser db
$sql_filter
Definition preod.inc.php:43