[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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, dt.type, dt.trans_no,  
37                         CASE WHEN dt.type=".ST_CUSTCREDIT." THEN (ov_amount+ov_freight+ov_discount)*-1 
38                         ELSE (ov_amount+ov_freight+ov_discount) END *dt.rate AS total
39                 FROM ".TB_PREF."debtor_trans dt
40                         LEFT JOIN ".TB_PREF."debtors_master d ON d.debtor_no=dt.debtor_no
41                 WHERE (dt.type=".ST_SALESINVOICE." OR dt.type=".ST_CUSTCREDIT.") ";
42         if ($tax_id)
43                 $sql .= "AND tax_id<>'' ";
44         $sql .= "AND dt.tran_date >=".db_escape($fromdate)." AND dt.tran_date<=".db_escape($todate)."
45                 ORDER BY d.debtor_no"; 
46     return db_query($sql,"No transactions were returned");
47 }
48
49 function getTaxes($type, $trans_no)
50 {
51         $sql = "SELECT included_in_price, SUM(CASE WHEN trans_type=".ST_CUSTCREDIT." THEN -amount ELSE amount END * ex_rate) AS tax
52                 FROM ".TB_PREF."trans_tax_details WHERE trans_type=$type AND trans_no=$trans_no GROUP BY included_in_price";
53
54     $result = db_query($sql,"No transactions were returned");
55     if ($result !== false)
56         return db_fetch($result);
57     else
58         return null;
59 }       
60
61 //----------------------------------------------------------------------------------------------------
62
63 function print_sales_summary_report()
64 {
65         global $path_to_root;
66         
67         $from = $_POST['PARAM_0'];
68         $to = $_POST['PARAM_1'];
69         $tax_id = $_POST['PARAM_2'];
70         $comments = $_POST['PARAM_3'];
71         $orientation = $_POST['PARAM_4'];
72         $destination = $_POST['PARAM_5'];
73         if ($tax_id == 0)
74                 $tid = _('No');
75         else
76                 $tid = _('Yes');
77
78
79         if ($destination)
80                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
81         else
82                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
83         $orientation = ($orientation ? 'L' : 'P');
84
85         $dec = user_price_dec();
86
87         $rep = new FrontReport(_('Sales Summary Report'), "SalesSummaryReport", user_pagesize(), 9, $orientation);
88
89         $params =   array(      0 => $comments,
90                                                 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
91                                                 2 => array(  'text' => _('Tax Id Only'),'from' => $tid,'to' => ''));
92
93         $cols = array(0, 130, 180, 270, 350, 500);
94
95         $headers = array(_('Customer'), _('Tax Id'), _('Total ex. Tax'), _('Tax'));
96         $aligns = array('left', 'left', 'right', 'right');
97     if ($orientation == 'L')
98         recalculate_cols($cols);
99
100         $rep->Font();
101         $rep->Info($params, $cols, $headers, $aligns);
102         $rep->NewPage();
103         
104         $totalnet = 0.0;
105         $totaltax = 0.0;
106         $transactions = getTaxTransactions($from, $to, $tax_id);
107
108         $rep->TextCol(0, 4, _('Balances in Home Currency'));
109         $rep->NewLine(2);
110         
111         $custno = 0;
112         $tax = $total = 0;
113         $custname = $tax_id = "";
114         while ($trans=db_fetch($transactions))
115         {
116                 if ($custno != $trans['debtor_no'])
117                 {
118                         if ($custno != 0)
119                         {
120                                 $rep->TextCol(0, 1, $custname);
121                                 $rep->TextCol(1, 2,     $tax_id);
122                                 $rep->AmountCol(2, 3, $total, $dec);
123                                 $rep->AmountCol(3, 4, $tax, $dec);
124                                 $totalnet += $total;
125                                 $totaltax += $tax;
126                                 $total = $tax = 0;
127                                 $rep->NewLine();
128
129                                 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
130                                 {
131                                         $rep->Line($rep->row - 2);
132                                         $rep->NewPage();
133                                 }
134                         }
135                         $custno = $trans['debtor_no'];
136                         $custname = $trans['cust_name'];
137                         $tax_id = $trans['tax_id'];
138                 }       
139                 $taxes = getTaxes($trans['type'], $trans['trans_no']);
140                 if ($taxes != null)
141                 {
142                         if ($taxes['included_in_price'])
143                                 $trans['total'] -= $taxes['tax'];
144                         $tax += $taxes['tax'];
145                 }       
146                 $total += $trans['total']; 
147         }
148         if ($custno != 0)
149         {
150                 $rep->TextCol(0, 1, $custname);
151                 $rep->TextCol(1, 2,     $tax_id);
152                 $rep->AmountCol(2, 3, $total, $dec);
153                 $rep->AmountCol(3, 4, $tax, $dec);
154                 $totalnet += $total;
155                 $totaltax += $tax;
156                 $rep->NewLine();
157         }
158         $rep->Font('bold');
159         $rep->NewLine();
160         $rep->Line($rep->row + $rep->lineHeight);
161         $rep->TextCol(0, 2,     _("Total"));
162         $rep->AmountCol(2, 3, $totalnet, $dec);
163         $rep->AmountCol(3, 4, $totaltax, $dec);
164         $rep->Line($rep->row - 5);
165         $rep->Font();
166
167         $rep->End();
168 }
169