noalyss Version-9
tax_summary.class.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS 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
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with NOALYSS; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22// Copyright Author Dany De Bontridder danydb@noalyss.eu
23
24/**
25 * @file
26 * @brief Compute , display and export the tax summary
27 *
28 */
29
30/**
31 * @class Tax_Summary
32 * @brief Compute , display and export the tax summary
33 *
34 */
36{
37
38 private $date_start;
39 private $date_end;
40 private $db;
41 private $tva_type; ///< exigibility of VAT : operation , payment date or depending of setting in tva_rate
42
44 {
45 $this->db=$db;
47 $this->set_date_end($p_end);
48
49 // By default the TVA is computed by parameter in tva_rate.tva_payment_purchase or tva_rate.tva_payment_sale
50 $this->set_tva_type("T");
51 }
52
53 public function get_tva_type()
54 {
55 return $this->tva_type;
56 }
57
58 public function set_tva_type($tva_type)
59 {
60 $this->tva_type=$tva_type;
61 return $this;
62 }
63
64 /**
65 * @return Database
66 */
67 public function get_db()
68 {
69 return $this->db;
70 return $this;
71 }
72
73 /**
74 * @param Database $db
75 */
76 public function set_db($db)
77 {
78 $this->db=$db;
79 return $this;
80 }
81
82 /**
83 * @return mixed
84 */
85 public function get_date_start()
86 {
87 return $this->date_start;
88 return $this;
89 }
90
91 /**
92 * @param mixed $date_start
93 */
94 public function set_date_start($date_start)
95 {
96 if (isDate($date_start)==NULL)
97 throw new Exception(_("Format date invalide").$date_start);
98 $this->date_start=$date_start;
99 return $this;
100 }
101
102 /**
103 * @return mixed
104 */
105 public function get_date_end()
106 {
107 return $this->date_end;
108 return $this;
109 }
110
111 /**
112 * @param mixed $date_end
113 */
114 public function set_date_end($date_end)
115 {
116 if (isDate($date_end)==NULL)
117 throw new Exception(_("Format date invalide").$date_end);
118 $this->date_end=$date_end;
119 return $this;
120 }
121
122 /**
123 * @brief depends of quant_* table, so we must check first that everything
124 * is in these tables
125 */
126 function check()
127 {
128 /* -------------SALE --------------------------------- */
129 $sql="select count(*)
130 from
131 quant_sold
132 where
133 j_id in (select j_id from jrnx
134 where
135 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'VEN')
136 and j_date >= to_date($1,'DD.MM.YYYY')
137 and j_date <= to_date($2,'DD.MM.YYYY')
138 )
139 ";
140 $cnt=$this->db->get_value($sql, [$this->date_start, $this->date_end]);
141 if ($cnt==0)
142 {
143 throw new Exception(_("Données manquantes"),100);
144 }
145 /* -------------Purchase --------------------------------- */
146 $sql="select count(*)
147 from
148 quant_purchase
149 where
150 j_id in (select j_id from jrnx
151 where
152 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'VEN')
153 and j_date >= to_date($1,'DD.MM.YYYY')
154 and j_date <= to_date($2,'DD.MM.YYYY')
155 )
156 ";
157 $cnt=$this->db->get_value($sql, [$this->date_start, $this->date_end]);
158 if ($cnt>0)
159 {
160 throw new Exception(_("Données manquantes"),100);
161 }
162 }
163 private function build_exigibility()
164 {
165 global $g_user;
166 $sql_ledger=$g_user->get_ledger_sql('ALL', 3);
167
168 }
169 /**
170 * Build the SQL for sale vat
171 * @return string
172 *
173 */
174 private function build_sql_purchase($p_group_ledger)
175 {
176 global $g_user;
177 $sql_ledger=$g_user->get_ledger_sql('ACH', 3);
178 $group_ledger="";
179 if ( $p_group_ledger )
180 {
181 $group_ledger='j_jrn_def,';
182 }
183
184 $sql="with detail_tva as (
185 select
186 sum(qp_vat) as amount_vat,
187 sum(qp_vat_sided) as amount_sided,
188 sum(qp_price) as amount_wovat,
189 sum(qp_nd_amount) as amount_noded_amount,
190 sum(qp_nd_tva) as amount_noded_tax,
191 sum(qp_nd_tva_recup) as amount_noded_return,
192 sum(qp_dep_priv) as amount_private,
193 {$group_ledger}
194 qp_vat_code
195 from
196 quant_purchase
197 join tva_rate on (qp_vat_code=tva_rate.tva_id)
198 join jrnx on (quant_purchase.j_id=jrnx.j_id)
199 join jrn_def on (jrn_def.jrn_def_id=jrnx.j_jrn_def)
200 join jrn on (jrn.jr_grpt_id=jrnx.j_grpt)
201 ";
202 if ( $this->tva_type=="O")
203 {
204 $sql=$sql."
205 where
206 j_date >= to_date($1,'DD.MM.YYYY')
207 and j_date <= to_date($2,'DD.MM.YYYY')
208 and {$sql_ledger}
209 group by {$group_ledger} qp_vat_code) ";
210 } elseif ($this->tva_type=="P") {
211 $sql=$sql."
212 where
213 coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
214 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
215 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
216 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
217 and {$sql_ledger}
218 group by {$group_ledger} qp_vat_code) ";
219 }elseif ($this->tva_type=="T") {
220 $sql=$sql." where
221 ( tva_rate.tva_payment_purchase='P'
222 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
223 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
224 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
225 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
226 )
227 or
228 ( tva_rate.tva_payment_purchase='O'
229 and coalesce(to_char(jr_date,'YYYYMMDD'),'00000000') >=
230 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
231 and coalesce(to_char(jr_date,'YYYYMMDD'),'99999999') <=
232 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
233 )
234 and {$sql_ledger}
235 group by {$group_ledger} qp_vat_code) ";
236
237 } else {
238 throw new Exception(_("Exig TVA invalide"),1001);
239 }
240 return $sql;
241 }
242 /**
243 * Build the SQL for sale vat
244 *
245 * @param group by ledger
246 *
247 * @return string
248 *
249 */
250 private function build_sql_sale($p_group_ledger=TRUE)
251 {
252 global $g_user;
253 $sql_ledger=$g_user->get_ledger_sql('VEN', 3);
254 $group_ledger="";
255 if ( $p_group_ledger )
256 {
257 $group_ledger='j_jrn_def,';
258 }
259 $sql="with detail_tva as (
260 select
261 sum(qs_vat) as amount_vat,
262 sum(qs_vat_sided) as amount_sided,
263 sum(qs_price) as amount_wovat,
264 {$group_ledger}
265 qs_vat_code
266 from
267 quant_sold
268 join tva_rate on (qs_vat_code=tva_rate.tva_id)
269 join jrnx on (quant_sold.j_id=jrnx.j_id)
270 join jrn_def on (jrn_def.jrn_def_id=jrnx.j_jrn_def)
271 join jrn on (jrn.jr_grpt_id=jrnx.j_grpt)";
272 if ( $this->tva_type=="O")
273 {
274 $sql=$sql."
275 where
276 j_date >= to_date($1,'DD.MM.YYYY')
277 and j_date <= to_date($2,'DD.MM.YYYY')
278 and {$sql_ledger}
279 group by {$group_ledger} qs_vat_code) ";
280
281 } elseif ($this->tva_type=="P") {
282 $sql=$sql."
283 where
284 coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
285 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
286 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
287 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
288 and {$sql_ledger}
289 group by {$group_ledger} qs_vat_code) ";
290
291 } elseif ($this->tva_type=="T") {
292 $sql=$sql."
293 where
294 ( tva_rate.tva_payment_sale='P'
295 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
296 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
297 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
298 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
299 )
300 or
301 ( tva_rate.tva_payment_sale='O'
302 and coalesce(to_char(jr_date,'YYYYMMDD'),'00000000') >=
303 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
304 and coalesce(to_char(jr_date,'YYYYMMDD'),'99999999') <=
305 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
306 )
307 and {$sql_ledger}
308 group by {$group_ledger} qs_vat_code)
309 ";
310 }
311 else {
312 throw new Exception(_("Exig TVA invalide"),1001);
313 }
314 return $sql;
315 }
316 /**
317 * Total for each sales ledger
318 * @return array
319 */
320 function get_row_sale()
321 {
322 $sql=$this->build_sql_sale(TRUE);
323 $sql.="
324 select jrn_def_name,
325 tva_label ,
326 qs_vat_code,
327 tva_rate,
328 tva_both_side,
329 amount_vat,
330 amount_wovat,
331 amount_sided,
332 tva_payment_sale as tva_type
333 from
334 detail_tva
335 join tva_rate on (tva_rate.tva_id=qs_vat_code)
336 join jrn_def on (jrn_def.jrn_def_id=j_jrn_def)
337 order by jrn_def_name,tva_label";
338
339 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
340 return $array;
341 }
342
343 /**
344 * Total for each purchase ledger
345 * @return array
346 */
348 {
349 $sql=$this->build_sql_purchase(TRUE)."
350 select jrn_def_name,
351 tva_label ,
352 tva_rate,
353 tva_both_side,
354 qp_vat_code,
355 amount_vat,
356 amount_wovat,
357 amount_sided,
358 amount_noded_amount,
359 amount_noded_tax,
360 amount_noded_return,
361 amount_private
362 from
363 detail_tva
364 join tva_rate on (tva_rate.tva_id=qp_vat_code)
365 join jrn_def on (jrn_def.jrn_def_id=j_jrn_def)
366 order by jrn_def_name,tva_label";
367 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
368 return $array;
369 }
370
371 /**
372 * Summary for all sales ledger
373 */
375 {
376 $sql=$this->build_sql_sale(FALSE);
377 $sql.="select
378 tva_label ,
379 qs_vat_code,
380 tva_rate,
381 tva_both_side,
382 amount_vat,
383 amount_wovat,
384 amount_sided,
385 tva_rate.tva_payment_sale
386 from
387 detail_tva
388 join tva_rate on (tva_rate.tva_id=qs_vat_code)
389 order by tva_label";
390 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
391 return $array;
392 }
393
394 /**
395 * Summary for all purchase ledger
396 */
398 {
399
400 $sql=$this->build_sql_purchase(FALSE)."
401 select
402 tva_label ,
403 tva_rate,
404 tva_both_side,
405 qp_vat_code,
406 amount_vat,
407 amount_wovat,
408 amount_sided,
409 amount_noded_amount,
410 amount_noded_tax,
411 amount_noded_return,
412 amount_private
413 from
414 detail_tva
415 join tva_rate on (tva_rate.tva_id=qp_vat_code)
416 order by tva_label";
417 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
418
419 return $array;
420 }
421
422 /**
423 * @brief display the summary of VAT in the range of date
424 */
425 function display()
426 {
427 require_once NOALYSS_INCLUDE."/template/tax_summary_display.php";
428 }
429
430 /**
431 * @brief display a form to export in CSV
432 * @see export_printtva_csv.php
433 */
435 {
436 $id=uniqid("export_");
437 echo '<form method="GET" action="export.php" ';
438 printf( 'id="%s" onsubmit="download_document_form(\'%s\')">',$id,$id);
439 echo Dossier::hidden();
440 echo HtmlInput::hidden("act", 'CSV:printtva');
441 echo HtmlInput::hidden("date_start", $this->date_start);
442 echo HtmlInput::hidden("date_end", $this->date_end);
443 echo HtmlInput::hidden("tva_type", $this->tva_type);
444
445 echo HtmlInput::submit("CSV:printtva", _("Export CSV"));
446 echo '</form>';
447 }
448
449 /**
450 * @brief display a form to export in PDF
451 * @see export_printtva_pdf.php
452 */
454 {
455 $id=uniqid("export_");
456 echo '<form method="GET" action="export.php" ';
457 printf( 'id="%s" onsubmit="download_document_form(\'%s\')">',$id,$id);
458 echo Dossier::hidden();
459 echo HtmlInput::hidden("act", 'PDF:printtva');
460 echo HtmlInput::hidden("date_start", $this->date_start);
461 echo HtmlInput::hidden("date_end", $this->date_end);
462 echo HtmlInput::hidden("tva_type", $this->tva_type);
463 echo HtmlInput::submit("PDF:printtva", _("Export PDF"));
464 echo '</form>';
465 }
466
467}
isDate($p_date)
Definition: ac_common.php:236
global $g_user
if no group available , then stop
contains the class for connecting to Noalyss
static hidden()
return a string to set gDossier into a FORM
static hidden($p_name, $p_value, $p_id="")
static submit($p_name, $p_value, $p_javascript="", $p_class="smallbutton")
Compute , display and export the tax summary.
check()
depends of quant_* table, so we must check first that everything is in these tables
form_export_pdf()
display a form to export in PDF
set_tva_type($tva_type)
__construct(Database $db, $p_start, $p_end)
$tva_type
exigibility of VAT : operation , payment date or depending of setting in tva_rate
get_row_sale()
Total for each sales ledger.
set_date_end($date_end)
display()
display the summary of VAT in the range of date
build_sql_sale($p_group_ledger=TRUE)
Build the SQL for sale vat.
build_sql_purchase($p_group_ledger)
Build the SQL for sale vat.
set_date_start($date_start)
get_summary_sale()
Summary for all sales ledger.
get_summary_purchase()
Summary for all purchase ledger.
get_row_purchase()
Total for each purchase ledger.
form_export_csv()
display a form to export in CSV
$p_end
$p_start
$SecUser db
if( $delta< 0) elseif( $delta==0)