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