noalyss Version-9
acc_ledger_history_sale.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 Acc_Ledger_History : Manage the list (history) of operations for display
25 * display or export operations in HTML , PDF or CSV
26 */
27
28/**
29 * @brief Acc_Ledger_History : Manage the list (history) of operations for display
30 */
32{
33
34 private $data; //!< Contains rows from SQL
35
36
37 public function __construct(\Database $cn, $pa_ledger, $p_from, $p_to,
38 $p_mode)
39 {
40 parent::__construct($cn, $pa_ledger, $p_from, $p_to, $p_mode);
41 $this->filter_operation='all';
42 $this->ledger_type='VEN';
43 }
44 /**
45 * Display the operation of sales with detailled VAT
46 */
47 public function export_detail_html()
48 {
50
51 $this->get_row();
52 $this->add_vat_info();
54 include NOALYSS_TEMPLATE."/acc_ledger_history_sale_detail.php";
55 }
56 /**
57 * @brief display the accounting
58 */
59 public function export_accounting_html()
60 {
61 $ledger_history=new Acc_Ledger_History_Generic($this->db,
62 $this->ma_ledger, $this->m_from, $this->m_to, $this->m_mode);
63 $ledger_history->export_accounting_html();
64 }
65 /**
66 * display the operation with detailled vat per item
67 */
68 public function export_extended_html()
69 {
70 $this->get_row();
71 $this->add_vat_info();
73 $this->prepare_detail();
75 include NOALYSS_TEMPLATE."/acc_ledger_history_sale_extended.php";
76 }
77 /**
78 * Prepare the query for fetching detail of an operation
79 */
80 private function prepare_detail()
81 {
82
83 if ( $this->db->is_prepare("detail_sale")== FALSE)
84 {
85 $this->db->prepare("detail_sale","
86 with card_name as
87 (select f_id,ad_value as name
88 from fiche_detail where ad_id=1),
89 card_qcode as
90 (select f_id,ad_value as qcode
91 from fiche_detail where ad_id=23)
92 select qs_price,qs_quantite,qs_vat,qs_vat_code,qs_unit,qs_vat_sided,name,qcode,tva_label,
93 qs_price+qs_vat-qs_vat_sided as tvac,
94 oc_amount,
95 oc_vat_amount
96 from
97 quant_sold
98 join jrnx using (j_id)
99 join card_name on (card_name.f_id=qs_fiche)
100 join card_qcode on (card_qcode.f_id=qs_fiche)
101 left join tva_rate on ( qs_vat_code=tva_id)
102 left join operation_currency using (j_id)
103 where
104 qs_internal=$1
105
106 ");
107 }
108 }
109
110 /**
111 * Get the rows from jrnx and quant* tables
112 * @param int $p_limit max of rows to returns
113 * @param int $p_offset the number of rows to skip
114 */
115 public function get_row($p_limit=-1, $p_offset="")
116 {
117 $periode=sql_filter_per($this->db, $this->m_from, $this->m_to, 'p_id',
118 'jr_tech_per');
119
120 $cond_limite=($p_limit!=-1)?" limit ".$p_limit." offset ".$p_offset:"";
121
122 $sql_filter=$this->build_filter_operation();
123
124 $ledger_list=join(",", $this->ma_ledger);
125 $sql="
126 with row_sale as
127 (select qs_internal,
128 qs_client,sum(qs_price) as novat,
129 sum(qs_vat) as vat ,
130 sum(qs_vat_sided) as tva_sided
131 from
132 quant_sold group by qs_client,qs_internal),
133 client_detail as (
134 select x.f_id as f_id,
135 (select ad_value from fiche_detail where ad_id=1 and f_id=x.f_id) as name,
136 (select ad_value from fiche_detail where ad_id=32 and f_id=x.f_id) as first_name,
137 (select ad_value from fiche_detail where ad_id=23 and f_id=x.f_id) as qcode
138 from
139 fiche as x),
140 row_currency as (
141 select sum(oc_amount) as sum_oc_amount,sum(oc_vat_amount) as sum_oc_vat_amount,jrnx.j_grpt
142 from
143 operation_currency
144 join jrnx using (j_id)
145 join quant_sold using (j_id)
146 group by j_grpt
147 ),
148 other_tax as (select sum(case when j_debit is false
149 then j_montant else 0-j_montant end) as other_tax_amount,
150 j_grpt
151 from jrnx j1
152 join jrn_tax jt2 on (j1.j_id=jt2.j_id) group by j_grpt)
153 select
154 name,
155 first_name,
156 qcode,
157 jr_id,
158 jr_pj_number,
159 to_char(jr_date,'DD.MM.YYYY') as str_date,
160 to_char(jr_date_paid,'DD.MM.YYYY') as str_date_paid,
161 jr_internal,
162 qs_client,
163 jrn.jr_comment,
164 jr_pj_name,
165 vat,
166 tva_sided,
167 novat,
168 novat+vat-tva_sided as tvac,
169 to_char(jr_date,'DDMMYY') as str_date_short,
170 jr_grpt_id,
171 jrn.currency_id,
172 jrn.currency_rate,
173 jrn.currency_rate_ref,
174 sum_oc_amount,
175 sum_oc_vat_amount,
176 cr_code_iso,
177 coalesce (other_tax_amount,0) other_tax_amount
178 from
179 jrn
180 join row_sale on (qs_internal=jr_internal)
181 join client_detail on (qs_client=f_id)
182 left join row_currency as rc on (rc.j_grpt = jrn.jr_grpt_id)
183 left join currency as c on (c.id=jrn.currency_id)
184 left join other_tax as ot on (ot.j_grpt=jrn.jr_grpt_id)
185 where
186 jr_def_id in ({$ledger_list})
187 {$sql_filter}
188 and {$periode}
189 {$cond_limite}
190 order by jr_date, substring(jr_pj_number,'[0-9]+$')::numeric ";
191 $this->data=$this->db->get_array($sql);
192
193 }
194 /**
195 * @brief preprare the query for fetching the detailed VAT of an operation
196 * @staticvar int $prepare
197 */
198 private function add_vat_info()
199 {
200 $prepare=$this->db->is_prepare("vat_info");
201 if ( $prepare==FALSE) {
202 $this->db->prepare("vat_info","
203 select
204 sum(qs_vat) vat_amount ,
205 qs_vat_code
206 from
207 quant_sold
208 where
209 qs_internal = $1
210 group by qs_vat_code order by qs_vat_code");
211
212 }
213
214 $nb_row=count($this->data);
215 for ($i=0;$i<$nb_row;$i++)
216 {
217 $ret=$this->db->execute("vat_info",array($this->data[$i]["jr_internal"]));
219 $this->data[$i]["detail_vat"]=$array;
220 }
221 }
222
223 /**
224 * @brief display in HTML following the mode
225 */
226 function export_html()
227 {
228 switch ($this->m_mode)
229 {
230 case "E":
231 $this->export_extended_html();
232 break;
233 case "D":
234 $this->export_detail_html();
235 break;
236 case "L":
237 $this->export_oneline_html();
238 break;
239 case "A":
240 $this->export_accounting_html();
241 break;
242 default:
243 break;
244 }
245 }
246
247 /**
248 * display in HTML one operation by line
249 */
250 public function export_oneline_html()
251 {
252 $this->get_row();
253 $this->prepare_reconcile_date();
254 $nb_other_tax=$this->has_other_tax();
255 require_once NOALYSS_TEMPLATE.'/acc_ledger_history_sale_oneline.php';
256
257 }
258 /**
259 * To get data
260 * @return array of rows
261 */
262 function get_data()
263 {
264 return $this->data;
265 }
266 function set_data($p_data)
267 {
268 $this->data=$p_data;
269 return $this;
270 }
271 /**
272 * export in csv with detail VAT
273 */
274 function export_csv()
275 {
276 $export=new Noalyss_Csv(_('journal'));
277 $export->send_header();
278 $nb_other_tax=$this->has_other_tax();
279
280 $this->get_row();
281 $this->prepare_reconcile_date();
282 $this->add_vat_info();
283
284 $own=new Noalyss_Parameter_Folder($this->db);
285 $title=array();
286 $title[]=_('Date');
287 $title[]=_("Paiement");
288 $title[]=_("operation");
289 $title[]=_("Pièce");
290 $title[]=_("Fournisseur");
291 $title[]=_("Note");
292 $title[]=_("interne");
293 $title[]=_("HTVA");
294 $title[]=_("TVA");
295 $title[]=_("TVA annulée");
296
297 if ( $own->MY_TVA_USE=='Y')
298 {
299 $a_Tva=$this->db->get_array("select tva_id,tva_label from tva_rate order by tva_rate,tva_label,tva_id");
300 foreach($a_Tva as $line_tva)
301 {
302 $title[]="Tva ".$line_tva['tva_label'];
303 }
304 }
305 if ($nb_other_tax>0) {
306 $title[]=_("Autre tx");
307 }
308 $title[]=_("TVAC/TTC");
309 $title[]=_("Devise");
310 $title[]=_("Devise HTVA");
311 $title[]=_("Devise TVA");
312 $title[]=_("Taux ref");
313 $title[]=_("Taux utilisé");
314 $title[]=_("Date paiement");
315 $title[]=_("Code paiement");
316 $title[]=_("Montant paiement");
317 $title[]=_("n° opération");
318
319 $export->write_header($title);
320
321 foreach ($this->data as $line)
322 {
323 $export->add($line['str_date']);
324 $export->add($line['str_date_paid']);
325 $export->add($line['jr_id']);
326 $export->add($line['jr_pj_number']);
327 $export->add($line['name']." ".
328 $line["first_name"]." ".
329 $line["qcode"]); // qp_supplier
330 $export->add($line['jr_comment']);
331 $export->add($line['jr_internal']);
332 $export->add($line['novat'],"number");
333 $export->add($line['vat'],"number");
334 $export->add($line['tva_sided'],"number");
335
336 $a_tva_amount=array();
337
338 if ($own->MY_TVA_USE == 'Y' )
339 {
340 //- set all TVA to 0
341 foreach ($a_Tva as $l) {
342 $t_id=$l["tva_id"];
343 $a_tva_amount[$t_id]=0;
344 }
345 foreach ($line['detail_vat'] as $lineTVA)
346 {
347 $idx_tva=$lineTVA['qs_vat_code'];
348 $a_tva_amount[$idx_tva]=$lineTVA['vat_amount'];
349 }
350 foreach ($a_Tva as $line_tva)
351 {
352 $a=$line_tva['tva_id'];
353 $export->add($a_tva_amount[$a],"number");
354 }
355 }
356 if ( $nb_other_tax > 0)
357 {
358 $export->add($line['other_tax_amount'],"number");
359 }
360 $export->add(bcadd($line['other_tax_amount'],$line['tvac'],2),"number");
361 /**
362 * Add currency info
363 */
364 $export->add($line['cr_code_iso']);
365 $export->add($line['sum_oc_amount'],'number');
366 $export->add($line['sum_oc_vat_amount'],'number');
367 $export->add($line['currency_rate'],'number');
368 $export->add($line['currency_rate_ref'],'number');
369
370 /**
371 * Retrieve payment if any
372 */
373 $ret_reconcile=$this->db->execute('reconcile_date',array($line['jr_id']));
375 if ($max > 0) {
376 for ($e=0;$e<$max;$e++) {
378 $export->add($row['jr_date']);
379 $export->add($row['qcode_bank']);
380 $export->add($row['qcode_name']);
381 $export->add($row['jr_montant'],"number");
382 $export->add($row['jr_internal']);
383
384 }
385 }
386 $export->write();
387
388 }
389 }
390}
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.
Definition: ac_common.php:682
manage the list of operation when we need several ledger with a different type or from Misceleaneous ...
Acc_Ledger_History : Manage the list (history) of operations for display.
export_detail_html()
Display the operation of sales with detailled VAT.
export_extended_html()
display the operation with detailled vat per item
add_vat_info()
preprare the query for fetching the detailed VAT of an operation @staticvar int $prepare
get_row($p_limit=-1, $p_offset="")
Get the rows from jrnx and quant* tables.
export_csv()
export in csv with detail VAT
export_html()
display in HTML following the mode
prepare_detail()
Prepare the query for fetching detail of an operation.
export_oneline_html()
display in HTML one operation by line
export_accounting_html()
display the accounting
__construct(\Database $cn, $pa_ledger, $p_from, $p_to, $p_mode)
Display history of operation.
has_other_tax()
count the number of addition tax for the ledger
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_all($ret)
wrapper for the function pg_fetch_all
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
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...)
$SecUser db
$sql_filter
Definition: preod.inc.php:43