noalyss Version-10
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
149 throw new Exception('TX148:'._("Données manquantes"),100);
150
151 }
152 /* -------------Purchase --------------------------------- */
153
154 $sql="select count(*)
155 from
156 quant_purchase
157 where
158 j_id in (select j_id from jrnx
159 where
160 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'ACH')
161 and j_date >= to_date($1,'DD.MM.YYYY')
162 and j_date <= to_date($2,'DD.MM.YYYY')
163 )
164 ";
165 $cnt=$this->db->get_value($sql, [$this->date_start, $this->date_end]);
166
167 $cnt_ledger=$this->db->get_value("
168 select count(*) from jrnx
169 where
170 jrnx.j_jrn_def in (select jrn_def_id from jrn_def where jrn_def_type = 'ACH')
171 and j_date >= to_date($1,'DD.MM.YYYY')
172 and j_date <= to_date($2,'DD.MM.YYYY')
173 ", [$this->date_start, $this->date_end]);
174
175 if ($cnt ==0 && $cnt_ledger !=0)
176 {
177 throw new Exception('TX175'._("Données manquantes"),100);
178 }
179 }
180 private function build_exigibility()
181 {
182 global $g_user;
183 $sql_ledger=$g_user->get_ledger_sql('ALL', 3);
184
185 }
186 /**
187 * Build the SQL for sale vat
188 * @return string
189 *
190 */
191 private function build_sql_purchase($p_group_ledger)
192 {
193 global $g_user;
194 $sql_ledger=$g_user->get_ledger_sql('ACH', 3);
195 $group_ledger="";
196 if ( $p_group_ledger )
197 {
198 $group_ledger='j_jrn_def,';
199 }
200
201 $sql="with detail_tva as (
202 select
203 sum(qp_vat) as amount_vat,
204 sum(qp_vat_sided) as amount_sided,
205 sum(qp_price) as amount_wovat,
206 sum(qp_nd_amount) as amount_noded_amount,
207 sum(qp_nd_tva) as amount_noded_tax,
208 sum(qp_nd_tva_recup) as amount_noded_return,
209 sum(qp_dep_priv) as amount_private,
210 {$group_ledger}
211 qp_vat_code
212 from
213 quant_purchase
214 join tva_rate on (qp_vat_code=tva_rate.tva_id)
215 join jrnx on (quant_purchase.j_id=jrnx.j_id)
216 join jrn_def on (jrn_def.jrn_def_id=jrnx.j_jrn_def)
217 join jrn on (jrn.jr_grpt_id=jrnx.j_grpt)
218 ";
219 if ( $this->tva_type=="O")
220 {
221 $sql=$sql."
222 where
223 j_date >= to_date($1,'DD.MM.YYYY')
224 and j_date <= to_date($2,'DD.MM.YYYY')
225 and {$sql_ledger}
226 group by {$group_ledger} qp_vat_code) ";
227 } elseif ($this->tva_type=="P") {
228 $sql=$sql."
229 where
230 coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
231 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
232 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
233 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
234 and {$sql_ledger}
235 group by {$group_ledger} qp_vat_code) ";
236 }elseif ($this->tva_type=="T") {
237 $sql=$sql." where
238 ( tva_rate.tva_payment_purchase='P'
239 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
240 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
241 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
242 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
243 )
244 or
245 ( tva_rate.tva_payment_purchase='O'
246 and coalesce(to_char(jr_date,'YYYYMMDD'),'00000000') >=
247 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
248 and coalesce(to_char(jr_date,'YYYYMMDD'),'99999999') <=
249 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
250 )
251 and {$sql_ledger}
252 group by {$group_ledger} qp_vat_code) ";
253
254 } else {
255 throw new Exception(_("Exig TVA invalide"),1001);
256 }
257 return $sql;
258 }
259 /**
260 * @brief Build the SQL for sale vat
261 *
262 * @param $p_group_ledger bool true group by ledgers
263 *
264 * @return string
265 *
266 */
267 private function build_sql_sale($p_group_ledger=TRUE)
268 {
269 global $g_user;
270 $sql_ledger=$g_user->get_ledger_sql('VEN', 3);
271 $group_ledger="";
272 if ( $p_group_ledger )
273 {
274 $group_ledger='j_jrn_def,';
275 }
276 $sql="with detail_tva as (
277 select
278 sum(qs_vat) as amount_vat,
279 sum(qs_vat_sided) as amount_sided,
280 sum(qs_price) as amount_wovat,
281 {$group_ledger}
282 qs_vat_code
283 from
284 quant_sold
285 join tva_rate on (qs_vat_code=tva_rate.tva_id)
286 join jrnx on (quant_sold.j_id=jrnx.j_id)
287 join jrn_def on (jrn_def.jrn_def_id=jrnx.j_jrn_def)
288 join jrn on (jrn.jr_grpt_id=jrnx.j_grpt)";
289 if ( $this->tva_type=="O")
290 {
291 $sql=$sql."
292 where
293 j_date >= to_date($1,'DD.MM.YYYY')
294 and j_date <= to_date($2,'DD.MM.YYYY')
295 and {$sql_ledger}
296 group by {$group_ledger} qs_vat_code) ";
297
298 } elseif ($this->tva_type=="P") {
299 $sql=$sql."
300 where
301 coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
302 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
303 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
304 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
305 and {$sql_ledger}
306 group by {$group_ledger} qs_vat_code) ";
307
308 } elseif ($this->tva_type=="T") {
309 $sql=$sql."
310 where
311 ( tva_rate.tva_payment_sale='P'
312 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'00000000') >=
313 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
314 and coalesce(to_char(jr_date_paid,'YYYYMMDD'),'99999999') <=
315 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
316 )
317 or
318 ( tva_rate.tva_payment_sale='O'
319 and coalesce(to_char(jr_date,'YYYYMMDD'),'00000000') >=
320 to_char(to_date($1,'DD.MM.YYYY'),'YYYYMMDD')
321 and coalesce(to_char(jr_date,'YYYYMMDD'),'99999999') <=
322 to_char(to_date($2,'DD.MM.YYYY'),'YYYYMMDD')
323 )
324 and {$sql_ledger}
325 group by {$group_ledger} qs_vat_code)
326 ";
327 }
328 else {
329 throw new Exception(_("Exig TVA invalide"),1001);
330 }
331 return $sql;
332 }
333 /**
334 * Total for each sales ledger
335 * @return array
336 */
337 function get_row_sale()
338 {
339 $sql=$this->build_sql_sale(TRUE);
340 $sql.="
341 select jrn_def_name,
342 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
343 qs_vat_code,
344 tva_rate,
345 tva_both_side,
346 amount_vat,
347 amount_wovat,
348 amount_sided,
349 tva_payment_sale as tva_type,
350 jrn_def.jrn_def_id
351 from
352 detail_tva
353 join tva_rate on (tva_rate.tva_id=qs_vat_code)
354 join jrn_def on (jrn_def.jrn_def_id=j_jrn_def)
355 order by jrn_def.jrn_def_id,jrn_def_name, tva_code ||' ('||tva_rate.tva_label||')'";
356
357 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
358 return $array;
359 }
360
361 /**
362 * Total for each purchase ledger
363 * @return array
364 */
366 {
367 $sql=$this->build_sql_purchase(TRUE)."
368 select jrn_def_name,
369 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
370 tva_rate,
371 tva_both_side,
372 qp_vat_code,
373 amount_vat,
374 amount_wovat,
375 amount_sided,
376 amount_noded_amount,
377 amount_noded_tax,
378 amount_noded_return,
379 amount_private,
380 jrn_def.jrn_def_id
381 from
382 detail_tva
383 join tva_rate on (tva_rate.tva_id=qp_vat_code)
384 join jrn_def on (jrn_def.jrn_def_id=j_jrn_def)
385 order by jrn_def.jrn_def_id,jrn_def_name, tva_code ||' ('||tva_rate.tva_label||')'";
386 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
387 return $array;
388 }
389
390 /**
391 * Summary for all sales ledger
392 */
394 {
395 $sql=$this->build_sql_sale(FALSE);
396 $sql.="select
397 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
398 qs_vat_code,
399 tva_rate,
400 tva_both_side,
401 amount_vat,
402 amount_wovat,
403 amount_sided,
404 tva_rate.tva_payment_sale
405 from
406 detail_tva
407 join tva_rate on (tva_rate.tva_id=qs_vat_code)
408 order by tva_code ||' ('||tva_rate.tva_label||')'";
409 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
410 return $array;
411 }
412
413 /**
414 * @brief Summary for all purchase ledgers
415 */
417 {
418
419 $sql=$this->build_sql_purchase(FALSE)."
420 select
421 tva_code ||' ('||tva_rate.tva_label||')' tva_label,
422 tva_rate,
423 tva_both_side,
424 qp_vat_code,
425 amount_vat,
426 amount_wovat,
427 amount_sided,
428 amount_noded_amount,
429 amount_noded_tax,
430 amount_noded_return,
431 amount_private
432 from
433 detail_tva
434 join tva_rate on (tva_rate.tva_id=qp_vat_code)
435 order by tva_code ||' ('||tva_rate.tva_label||')'";
436 $array=$this->db->get_array($sql, [$this->date_start, $this->date_end]);
437
438 return $array;
439 }
440
441 /**
442 * @brief display the summary of VAT in the range of date
443 */
444 function display()
445 {
446 require_once NOALYSS_INCLUDE."/template/tax_summary_display.php";
447 }
448
449 /**
450 * @brief display a form to export in CSV
451 * @see export_printtva_csv.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", 'CSV: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
464 echo HtmlInput::submit("CSV:printtva", _("Export CSV"));
465 echo '</form>';
466 }
467
468 /**
469 * @brief display a form to export in PDF
470 * @see export_printtva_pdf.php
471 */
473 {
474 $id=uniqid("export_");
475 echo '<form method="GET" action="export.php" ';
476 printf( 'id="%s" onsubmit="download_document_form(\'%s\')">',$id,$id);
477 echo Dossier::hidden();
478 echo HtmlInput::hidden("act", 'PDF:printtva');
479 echo HtmlInput::hidden("date_start", $this->date_start);
480 echo HtmlInput::hidden("date_end", $this->date_end);
481 echo HtmlInput::hidden("tva_type", $this->tva_type);
482 echo HtmlInput::submit("PDF:printtva", _("Export PDF"));
483 echo '</form>';
484 }
485
486 /**
487 * @brief Build a link to show the detail of a VAT ID
488 * @param $dateStart date from format 'DD.MM.YYYY'
489 * @param $DateeEd date to format 'DD.MM.YYYY'
490 * @param $nLedger_id integer JRN_DEF.JRN_DEF_ID
491 * @param $nVAT_id integer TVA_RATE.TVA_ID
492 * @return javascript string
493 */
494 function build_link_detail($dossier_id,$dateStart,$DateeEd,$nLedger_id,$nVAT_id)
495 {
496
497
498 $js=sprintf("tax_detail_view('%s','%s','%s','%s','%s')",
499 $dossier_id,$this->date_start,$this->date_end,$nLedger_id,$nVAT_id);
500
501 return $js;
502
503
504 }
505
506}
isDate($p_date)
Verifie qu'une date est bien formaté en d.m.y et est valable.
global $g_user
if no group available , then stop
for($i=0; $i< $nb_vatex_code; $i++)($i % 2==0) ? " odd " $cnt
$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)