noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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 }
71
72 /**
73 * @param Database $db
74 */
75 public function set_db($db)
76 {
77 $this->db=$db;
78 return $this;
79 }
80
81 /**
82 * @return mixed
83 */
84 public function get_date_start()
85 {
86 return $this->date_start;
87 }
88
89 /**
90 * @param mixed $date_start
91 */
92 public function set_date_start($date_start)
93 {
94 if (isDate($date_start)==NULL)
95 throw new Exception(_("Format date invalide").$date_start);
96 $this->date_start=$date_start;
97 return $this;
98 }
99
100 /**
101 * @return mixed
102 */
103 public function get_date_end()
104 {
105 return $this->date_end;
106
107 }
108
109 /**
110 * @param mixed $date_end
111 */
112 public function set_date_end($date_end)
113 {
114 if (isDate($date_end)==NULL)
115 throw new Exception(_("Format date invalide").$date_end);
116 $this->date_end=$date_end;
117 return $this;
118 }
119
120 /**
121 * @brief depends of quant_* table, so we must check first that everything
122 * is in these tables
123 */
124 function check()
125 {
126 /* -------------SALE --------------------------------- */
127 $sql="select count(*)
128 from
129 quant_sold
130 where
131 j_id in (select j_id from jrnx
132 where
133 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'VEN')
134 and j_date >= to_date($1,'DD.MM.YYYY')
135 and j_date <= to_date($2,'DD.MM.YYYY')
136 )
137 ";
138 $cnt=$this->db->get_value($sql, [$this->date_start, $this->date_end]);
139 $cnt_ledger=$this->db->get_value("
140 select count(*) from jrnx
141 where
142 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'VEN')
143 and j_date >= to_date($1,'DD.MM.YYYY')
144 and j_date <= to_date($2,'DD.MM.YYYY')
145 ", [$this->date_start, $this->date_end]);
146 if ($cnt==0 && $cnt_ledger !=0)
147 {
148 throw new Exception('TX148:'._("Données manquantes"),100);
149 }
150 /* -------------Purchase --------------------------------- */
151
152 $sql="select count(*)
153 from
154 quant_purchase
155 where
156 j_id in (select j_id from jrnx
157 where
158 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'ACH')
159 and j_date >= to_date($1,'DD.MM.YYYY')
160 and j_date <= to_date($2,'DD.MM.YYYY')
161 )
162 ";
163 $cnt=$this->db->get_value($sql, [$this->date_start, $this->date_end]);
164
165 $cnt_ledger=$this->db->get_value("
166 select count(*) from jrnx
167 where
168 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'ACH')
169 and j_date >= to_date($1,'DD.MM.YYYY')
170 and j_date <= to_date($2,'DD.MM.YYYY')
171 ", [$this->date_start, $this->date_end]);
172
173 if ($cnt ==0 && $cnt_ledger !=0)
174 {
175 throw new Exception('TX175'._("Données manquantes"),100);
176 }
177 }
178 private function build_exigibility()
179 {
180 global $g_user;
181 $sql_ledger=$g_user->get_ledger_sql('ALL', 3);
182
183 }
184 /**
185 * Build the SQL for sale vat
186 * @return string
187 *
188 */
189 private function build_sql_purchase($p_group_ledger)
190 {
191 global $g_user;
192 $sql_ledger=$g_user->get_ledger_sql('ACH', 3);
193 $group_ledger="";
194 if ( $p_group_ledger )
195 {
196 $group_ledger='j_jrn_def,';
197 }
198
199 $sql="with detail_tva as (
200 select
201 sum(qp_vat) as amount_vat,
202 sum(qp_vat_sided) as amount_sided,
203 sum(qp_price) as amount_wovat,
204 sum(qp_nd_amount) as amount_noded_amount,
205 sum(qp_nd_tva) as amount_noded_tax,
206 sum(qp_nd_tva_recup) as amount_noded_return,
207 sum(qp_dep_priv) as amount_private,
208 {$group_ledger}
209 qp_vat_code
210 from
211 quant_purchase
212 join tva_rate on (qp_vat_code=tva_rate.tva_id)
213 join jrnx on (quant_purchase.j_id=jrnx.j_id)
214 join jrn_def on (jrn_def.jrn_def_id=jrnx.j_jrn_def)
215 join jrn on (jrn.jr_grpt_id=jrnx.j_grpt)
216 ";
217 if ( $this->tva_type=="O")
218 {
219 $sql=$sql."
220 where
221 j_date >= to_date($1,'DD.MM.YYYY')
222 and j_date <= to_date($2,'DD.MM.YYYY')
223 and {$sql_ledger}
224 group by {$group_ledger} qp_vat_code) ";
225 } elseif ($this->tva_type=="P") {
226 $sql=$sql."
227 where
228 coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
229 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
230 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
231 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
232 and {$sql_ledger}
233 group by {$group_ledger} qp_vat_code) ";
234 }elseif ($this->tva_type=="T") {
235 $sql=$sql." where
236 ( tva_rate.tva_payment_purchase='P'
237 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
238 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
239 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
240 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
241 )
242 or
243 ( tva_rate.tva_payment_purchase='O'
244 and coalesce(to_char(jr_date,'YYYYMMDD'),'00000000') >=
245 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
246 and coalesce(to_char(jr_date,'YYYYMMDD'),'99999999') <=
247 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
248 )
249 and {$sql_ledger}
250 group by {$group_ledger} qp_vat_code) ";
251
252 } else {
253 throw new Exception(_("Exig TVA invalide"),1001);
254 }
255 return $sql;
256 }
257 /**
258 * @brief Build the SQL for sale vat
259 *
260 * @param $p_group_ledger bool true group by ledgers
261 *
262 * @return string
263 *
264 */
265 private function build_sql_sale($p_group_ledger=TRUE)
266 {
267 global $g_user;
268 $sql_ledger=$g_user->get_ledger_sql('VEN', 3);
269 $group_ledger="";
270 if ( $p_group_ledger )
271 {
272 $group_ledger='j_jrn_def,';
273 }
274 $sql="with detail_tva as (
275 select
276 sum(qs_vat) as amount_vat,
277 sum(qs_vat_sided) as amount_sided,
278 sum(qs_price) as amount_wovat,
279 {$group_ledger}
280 qs_vat_code
281 from
282 quant_sold
283 join tva_rate on (qs_vat_code=tva_rate.tva_id)
284 join jrnx on (quant_sold.j_id=jrnx.j_id)
285 join jrn_def on (jrn_def.jrn_def_id=jrnx.j_jrn_def)
286 join jrn on (jrn.jr_grpt_id=jrnx.j_grpt)";
287 if ( $this->tva_type=="O")
288 {
289 $sql=$sql."
290 where
291 j_date >= to_date($1,'DD.MM.YYYY')
292 and j_date <= to_date($2,'DD.MM.YYYY')
293 and {$sql_ledger}
294 group by {$group_ledger} qs_vat_code) ";
295
296 } elseif ($this->tva_type=="P") {
297 $sql=$sql."
298 where
299 coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
300 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
301 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
302 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
303 and {$sql_ledger}
304 group by {$group_ledger} qs_vat_code) ";
305
306 } elseif ($this->tva_type=="T") {
307 $sql=$sql."
308 where
309 ( tva_rate.tva_payment_sale='P'
310 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
311 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
312 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
313 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
314 )
315 or
316 ( tva_rate.tva_payment_sale='O'
317 and coalesce(to_char(jr_date,'YYYYMMDD'),'00000000') >=
318 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
319 and coalesce(to_char(jr_date,'YYYYMMDD'),'99999999') <=
320 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
321 )
322 and {$sql_ledger}
323 group by {$group_ledger} qs_vat_code)
324 ";
325 }
326 else {
327 throw new Exception(_("Exig TVA invalide"),1001);
328 }
329 return $sql;
330 }
331 /**
332 * Total for each sales ledger
333 * @return array
334 */
335 function get_row_sale()
336 {
337 $sql=$this->build_sql_sale(TRUE);
338 $sql.="
339 select jrn_def_name,
340 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
341 qs_vat_code,
342 tva_rate,
343 tva_both_side,
344 amount_vat,
345 amount_wovat,
346 amount_sided,
347 tva_payment_sale as tva_type,
348 jrn_def.jrn_def_id
349 from
350 detail_tva
351 join tva_rate on (tva_rate.tva_id=qs_vat_code)
352 join jrn_def on (jrn_def.jrn_def_id=j_jrn_def)
353 order by jrn_def.jrn_def_id,jrn_def_name, tva_code ||' ('||tva_rate.tva_label||')'";
354
355 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
356 return $array;
357 }
358
359 /**
360 * Total for each purchase ledger
361 * @return array
362 */
364 {
365 $sql=$this->build_sql_purchase(TRUE)."
366 select jrn_def_name,
367 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
368 tva_rate,
369 tva_both_side,
370 qp_vat_code,
371 amount_vat,
372 amount_wovat,
373 amount_sided,
374 amount_noded_amount,
375 amount_noded_tax,
376 amount_noded_return,
377 amount_private,
378 jrn_def.jrn_def_id
379 from
380 detail_tva
381 join tva_rate on (tva_rate.tva_id=qp_vat_code)
382 join jrn_def on (jrn_def.jrn_def_id=j_jrn_def)
383 order by jrn_def.jrn_def_id,jrn_def_name, tva_code ||' ('||tva_rate.tva_label||')'";
384 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
385 return $array;
386 }
387
388 /**
389 * Summary for all sales ledger
390 */
392 {
393 $sql=$this->build_sql_sale(FALSE);
394 $sql.="select
395 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
396 qs_vat_code,
397 tva_rate,
398 tva_both_side,
399 amount_vat,
400 amount_wovat,
401 amount_sided,
402 tva_rate.tva_payment_sale
403 from
404 detail_tva
405 join tva_rate on (tva_rate.tva_id=qs_vat_code)
406 order by tva_code ||' ('||tva_rate.tva_label||')'";
407 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
408 return $array;
409 }
410
411 /**
412 * @brief Summary for all purchase ledgers
413 */
415 {
416
417 $sql=$this->build_sql_purchase(FALSE)."
418 select
419 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
420 tva_rate,
421 tva_both_side,
422 qp_vat_code,
423 amount_vat,
424 amount_wovat,
425 amount_sided,
426 amount_noded_amount,
427 amount_noded_tax,
428 amount_noded_return,
429 amount_private
430 from
431 detail_tva
432 join tva_rate on (tva_rate.tva_id=qp_vat_code)
433 order by tva_code ||' ('||tva_rate.tva_label||')'";
434 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
435
436 return $array;
437 }
438
439 /**
440 * @brief display the summary of VAT in the range of date
441 */
442 function display()
443 {
444 require_once NOALYSS_INCLUDE."/template/tax_summary_display.php";
445 }
446
447 /**
448 * @brief display a form to export in CSV
449 * @see export_printtva_csv.php
450 */
452 {
453 $id=uniqid("export_");
454 echo '<form method="GET" action="export.php" ';
455 printf( 'id="%s" onsubmit="download_document_form(\'%s\')">',$id,$id);
456 echo Dossier::hidden();
457 echo HtmlInput::hidden("act", 'CSV:printtva');
458 echo HtmlInput::hidden("date_start", $this->date_start);
459 echo HtmlInput::hidden("date_end", $this->date_end);
460 echo HtmlInput::hidden("tva_type", $this->tva_type);
461
462 echo HtmlInput::submit("CSV:printtva", _("Export CSV"));
463 echo '</form>';
464 }
465
466 /**
467 * @brief display a form to export in PDF
468 * @see export_printtva_pdf.php
469 */
471 {
472 $id=uniqid("export_");
473 echo '<form method="GET" action="export.php" ';
474 printf( 'id="%s" onsubmit="download_document_form(\'%s\')">',$id,$id);
475 echo Dossier::hidden();
476 echo HtmlInput::hidden("act", 'PDF:printtva');
477 echo HtmlInput::hidden("date_start", $this->date_start);
478 echo HtmlInput::hidden("date_end", $this->date_end);
479 echo HtmlInput::hidden("tva_type", $this->tva_type);
480 echo HtmlInput::submit("PDF:printtva", _("Export PDF"));
481 echo '</form>';
482 }
483
484 /**
485 * @brief Build a link to show the detail of a VAT ID
486 * @param $dateStart date from format 'DD.MM.YYYY'
487 * @param $DateeEd date to format 'DD.MM.YYYY'
488 * @param $nLedger_id integer JRN_DEF.JRN_DEF_ID
489 * @param $nVAT_id integer TVA_RATE.TVA_ID
490 * @return javascript string
491 */
492 function build_link_detail($dossier_id,$dateStart,$DateeEd,$nLedger_id,$nVAT_id)
493 {
494
495
496 $js=sprintf("tax_detail_view('%s','%s','%s','%s','%s')",
497 $dossier_id,$this->date_start,$this->date_end,$nLedger_id,$nVAT_id);
498
499 return $js;
500
501
502 }
503
504}
isDate($p_date)
global $g_user
if no group available , then stop
$dossier_id
_("actif, passif,charge,...")
contains the class for connecting to Noalyss
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)
build_link_detail($dossier_id, $dateStart, $DateeEd, $nLedger_id, $nVAT_id)
Build a link to show the detail of a VAT ID.
__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 ledgers.
get_row_purchase()
Total for each purchase ledger.
form_export_csv()
display a form to export in CSV
$SecUser db
if( $delta< 0) elseif( $delta==0)