EOL formatting cleanup.
[fa-stable.git] / reporting / rep114.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_TAXREP';
13 // ----------------------------------------------------------------
14 // $ Revision:  2.0 $
15 // Creator:     Joe Hunt
16 // date_:       2005-05-19
17 // Title:       Sales Summary Report
18 // ----------------------------------------------------------------
19 $path_to_root="..";
20
21 include_once($path_to_root . "/includes/session.inc");
22 include_once($path_to_root . "/includes/date_functions.inc");
23 include_once($path_to_root . "/includes/data_checks.inc");
24 include_once($path_to_root . "/gl/includes/gl_db.inc");
25
26 //------------------------------------------------------------------
27
28
29 print_sales_summary_report();
30
31 function getTaxTransactions($from, $to, $tax_id)
32 {
33         $fromdate = date2sql($from);
34         $todate = date2sql($to);
35
36         $sql = "SELECT d.debtor_no, d.name AS cust_name, d.tax_id, 
37                 CASE WHEN net_amount IS NULL OR amount=0 THEN 
38                         SUM(CASE WHEN dt.type=".ST_CUSTCREDIT." THEN (ov_amount+ov_freight+ov_discount)*-1 
39                         ELSE (ov_amount+ov_freight+ov_discount) END *dt.rate) ELSE 
40                         SUM(CASE WHEN dt.type=".ST_CUSTCREDIT." THEN -net_amount ELSE net_amount END *ex_rate) END AS total,
41                 SUM(CASE WHEN dt.type=".ST_CUSTCREDIT." THEN -amount ELSE amount END *ex_rate) AS tax
42                 FROM ".TB_PREF."debtor_trans dt
43                         LEFT JOIN ".TB_PREF."debtors_master d ON d.debtor_no=dt.debtor_no
44                         LEFT JOIN ".TB_PREF."trans_tax_details t ON (t.trans_type=dt.type AND t.trans_no=dt.trans_no)
45                 WHERE (dt.type=".ST_SALESINVOICE." OR dt.type=".ST_CUSTCREDIT.") ";
46         if ($tax_id)
47                 $sql .= "AND tax_id<>'' ";
48         $sql .= "AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
49                 GROUP BY d.debtor_no, d.name, d.tax_id ORDER BY d.name"; 
50     return db_query($sql,"No transactions were returned");
51 }
52
53 //----------------------------------------------------------------------------------------------------
54
55 function print_sales_summary_report()
56 {
57         global $path_to_root;
58         
59         $from = $_POST['PARAM_0'];
60         $to = $_POST['PARAM_1'];
61         $tax_id = $_POST['PARAM_2'];
62         $comments = $_POST['PARAM_3'];
63         $destination = $_POST['PARAM_4'];
64         if ($tax_id == 0)
65                 $tid = _('No');
66         else
67                 $tid = _('Yes');
68
69
70         if ($destination)
71                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
72         else
73                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
74
75         $dec = user_price_dec();
76
77         $rep = new FrontReport(_('Sales Summary Report'), "SalesSummaryReport", user_pagesize());
78
79         $params =   array(      0 => $comments,
80                                                 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
81                                                 2 => array(  'text' => _('Tax Id Only'),'from' => $tid,'to' => ''));
82
83         $cols = array(0, 130, 180, 270, 350, 500);
84
85         $headers = array(_('Customer'), _('Tax Id'), _('Total ex. Tax'), _('Tax'));
86         $aligns = array('left', 'left', 'right', 'right');
87         $rep->Font();
88         $rep->Info($params, $cols, $headers, $aligns);
89         $rep->NewPage();
90         
91         $totalnet = 0.0;
92         $totaltax = 0.0;
93         $transactions = getTaxTransactions($from, $to, $tax_id);
94
95         $rep->TextCol(0, 4, _("Values in domestic currency"));
96         $rep->NewLine(2);
97         
98         while ($trans=db_fetch($transactions))
99         {
100                 $rep->TextCol(0, 1, $trans['cust_name']);
101                 $rep->TextCol(1, 2,     $trans['tax_id']);
102                 $rep->AmountCol(2, 3,   $trans['total'], $dec);
103                 $rep->AmountCol(3, 4,   $trans['tax'], $dec);
104                 $totalnet += $trans['total'];
105                 $totaltax += $trans['tax'];
106
107                 $rep->NewLine();
108
109                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
110                 {
111                         $rep->Line($rep->row - 2);
112                         $rep->NewPage();
113                 }
114         }
115
116         $rep->Font('bold');
117         $rep->NewLine();
118         $rep->Line($rep->row + $rep->lineHeight);
119         $rep->TextCol(0, 2,     _("Total"));
120         $rep->AmountCol(2, 3, $totalnet, $dec);
121         $rep->AmountCol(3, 4, $totaltax, $dec);
122         $rep->Line($rep->row - 5);
123         $rep->Font();
124
125         $rep->End();
126 }
127
128 ?>