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