Old hooks superseded by new hooks API.
[fa-stable.git] / reporting / rep709.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:       Tax 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_tax_report();
30
31 function getTaxTransactions($from, $to)
32 {
33         $fromdate = date2sql($from);
34         $todate = date2sql($to);
35
36         $sql = "SELECT taxrec.*, taxrec.amount*ex_rate AS amount,
37                     taxrec.net_amount*ex_rate AS net_amount,
38                                 IF(ISNULL(supp.supp_name), debt.name, supp.supp_name) as name,
39                                 branch.br_name
40                 FROM ".TB_PREF."trans_tax_details taxrec
41                 LEFT JOIN ".TB_PREF."supp_trans strans
42                         ON taxrec.trans_no=strans.trans_no AND taxrec.trans_type=strans.type
43                 LEFT JOIN ".TB_PREF."suppliers as supp ON strans.supplier_id=supp.supplier_id
44                 LEFT JOIN ".TB_PREF."debtor_trans dtrans
45                         ON taxrec.trans_no=dtrans.trans_no AND taxrec.trans_type=dtrans.type
46                 LEFT JOIN ".TB_PREF."debtors_master as debt ON dtrans.debtor_no=debt.debtor_no
47                 LEFT JOIN ".TB_PREF."cust_branch as branch ON dtrans.branch_code=branch.branch_code
48                 WHERE (taxrec.amount <> 0 OR taxrec.net_amount <> 0)
49                         AND taxrec.trans_type <> ".ST_CUSTDELIVERY."
50                         AND taxrec.tran_date >= '$fromdate'
51                         AND taxrec.tran_date <= '$todate'
52                 ORDER BY taxrec.tran_date";
53 //display_error($sql);
54     return db_query($sql,"No transactions were returned");
55 }
56
57 function getTaxTypes()
58 {
59         $sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
60     return db_query($sql,"No transactions were returned");
61 }
62
63 function getTaxInfo($id)
64 {
65         $sql = "SELECT * FROM ".TB_PREF."tax_types WHERE id=$id";
66     $result = db_query($sql,"No transactions were returned");
67     return db_fetch($result);
68 }
69
70 //----------------------------------------------------------------------------------------------------
71
72 function print_tax_report()
73 {
74         global $path_to_root, $trans_dir, $systypes_array;
75         
76         $from = $_POST['PARAM_0'];
77         $to = $_POST['PARAM_1'];
78         $summaryOnly = $_POST['PARAM_2'];
79         $comments = $_POST['PARAM_3'];
80         $destination = $_POST['PARAM_4'];
81
82         if ($destination)
83                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
84         else
85                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
86
87         $dec = user_price_dec();
88
89         $rep = new FrontReport(_('Tax Report'), "TaxReport", user_pagesize());
90         if ($summaryOnly == 1)
91                 $summary = _('Summary Only');
92         else
93                 $summary = _('Detailed Report');
94
95         $res = getTaxTypes();
96
97         $taxes = array();
98         while ($tax=db_fetch($res))
99                 $taxes[$tax['id']] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);
100
101         $params =   array(      0 => $comments,
102                                                 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
103                                                 2 => array('text' => _('Type'), 'from' => $summary, 'to' => ''));
104
105         $cols = array(0, 100, 130, 180, 290, 370, 420, 470, 520);
106
107         $headers = array(_('Trans Type'), _('Ref'), _('Date'), _('Name'), _('Branch Name'),
108                 _('Net'), _('Rate'), _('Tax'));
109         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
110         $rep->Font();
111         $rep->Info($params, $cols, $headers, $aligns);
112         if (!$summaryOnly)
113         {
114                 $rep->NewPage();
115         }
116         
117         $totalnet = 0.0;
118         $totaltax = 0.0;
119         $transactions = getTaxTransactions($from, $to);
120
121         while ($trans=db_fetch($transactions))
122         {
123                 if (in_array($trans['trans_type'], array(ST_CUSTCREDIT,ST_SUPPINVOICE))) {
124                         $trans['net_amount'] *= -1;
125                         $trans['amount'] *= -1;
126                 }
127                 
128                 if (!$summaryOnly)
129                 {
130                         $rep->TextCol(0, 1, $systypes_array[$trans['trans_type']]);
131                         if ($trans['memo'] == '')
132                                 $trans['memo'] = get_reference($trans['trans_type'], $trans['trans_no']);
133                         $rep->TextCol(1, 2,     $trans['memo']);
134                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
135                         $rep->TextCol(3, 4,     $trans['name']);
136                         $rep->TextCol(4, 5,     $trans['br_name']);
137
138                         $rep->AmountCol(5, 6, $trans['net_amount'], $dec);
139                         $rep->AmountCol(6, 7, $trans['rate'], $dec);
140                         $rep->AmountCol(7, 8, $trans['amount'], $dec);
141
142                         $rep->NewLine();
143
144                         if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
145                         {
146                                 $rep->Line($rep->row - 2);
147                                 $rep->NewPage();
148                         }
149                 }
150                 if ($trans['trans_type']==ST_JOURNAL && $trans['amount']<0) {
151                         $taxes[$trans['tax_type_id']]['taxout'] -= $trans['amount'];
152                         $taxes[$trans['tax_type_id']]['out'] -= $trans['net_amount'];
153                 }
154                 elseif (in_array($trans['trans_type'], array(ST_BANKDEPOSIT,ST_SALESINVOICE,ST_CUSTCREDIT))) {
155                         $taxes[$trans['tax_type_id']]['taxout'] += $trans['amount'];
156                         $taxes[$trans['tax_type_id']]['out'] += $trans['net_amount'];
157                 } else {
158                         $taxes[$trans['tax_type_id']]['taxin'] += $trans['amount'];
159                         $taxes[$trans['tax_type_id']]['in'] += $trans['net_amount'];
160                 }
161                 $totalnet += $trans['net_amount'];
162                 $totaltax += $trans['amount'];
163         }
164         
165         // Summary
166         $cols2 = array(0, 100, 180,     260, 340, 420, 500);
167
168         $headers2 = array(_('Tax Rate'), _('Outputs'), _('Output Tax'), _('Inputs'), _('Input Tax'), _('Net Tax'));
169
170         $aligns2 = array('left', 'right', 'right', 'right',     'right', 'right', 'right');
171
172         $rep->Info($params, $cols2, $headers2, $aligns2);
173
174         //for ($i = 0; $i < count($cols2); $i++)
175         //      $rep->cols[$i] = $rep->leftMargin + $cols2[$i];
176
177         $rep->headers = $headers2;
178         $rep->aligns = $aligns2;
179         $rep->NewPage();
180
181         $taxtotal = 0;
182         foreach( $taxes as $id=>$sum)
183         {
184                 $tx = getTaxInfo($id);
185                 
186                 $rep->TextCol(0, 1, $tx['name'] . " " . number_format2($tx['rate'], $dec) . "%");
187                 $rep->AmountCol(1, 2, $sum['out'], $dec);
188                 $rep->AmountCol(2, 3, $sum['taxout'], $dec);
189                 $rep->AmountCol(3, 4, $sum['in'], $dec);
190                 $rep->AmountCol(4, 5, $sum['taxin'], $dec); 
191                 $rep->AmountCol(5, 6, $sum['taxout']+$sum['taxin'], $dec);
192                 $taxtotal += $sum['taxout']+$sum['taxin'];
193                 $rep->NewLine();
194         }
195
196         $rep->Font('bold');
197         $rep->NewLine();
198         $rep->Line($rep->row + $rep->lineHeight);
199         $rep->TextCol(3, 5,     _("Total payable or refund"));
200         $rep->AmountCol(5, 6, $taxtotal, $dec);
201         $rep->Line($rep->row - 5);
202         $rep->Font();
203         $rep->NewLine();
204
205         hook_tax_report_done();
206
207         $rep->End();
208 }
209
210 ?>