Changes to committed tax report routines.
[fa-stable.git] / reporting / rep709.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 2;
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.*, IF(ISNULL(dtrans.rate),IF(ISNULL(strans.rate), taxrec.amount, taxrec.amount*strans.rate), 
37                                 taxrec.amount*dtrans.rate) AS amount,
38                     IF(ISNULL(dtrans.rate),IF(ISNULL(strans.rate), taxrec.net_amount,taxrec.net_amount*strans.rate),
39                     taxrec.net_amount*dtrans.rate) AS net_amount,
40                                 stype.type_name,
41                                 if(supp.supp_name is null, debt.name, supp.supp_name) as name,
42                                 branch.br_name
43                 FROM ".TB_PREF."trans_tax_details taxrec
44                 LEFT JOIN ".TB_PREF."supp_trans strans
45                         ON taxrec.trans_no=strans.trans_no AND taxrec.trans_type=strans.type
46                 LEFT JOIN ".TB_PREF."suppliers as supp ON strans.supplier_id=supp.supplier_id
47                 LEFT JOIN ".TB_PREF."debtor_trans dtrans
48                         ON taxrec.trans_no=dtrans.trans_no AND taxrec.trans_type=dtrans.type
49                 LEFT JOIN ".TB_PREF."debtors_master as debt ON dtrans.debtor_no=debt.debtor_no
50                 LEFT JOIN ".TB_PREF."cust_branch as branch ON dtrans.branch_code=branch.branch_code,
51                 ".TB_PREF."sys_types stype
52                 WHERE taxrec.trans_type=stype.type_id
53                         AND taxrec.trans_type != 13
54                         AND taxrec.tran_date >= '$fromdate'
55                         AND taxrec.tran_date <= '$todate'
56                 ORDER BY taxrec.tran_date";
57 //display_error($sql);
58     return db_query($sql,"No transactions were returned");
59 }
60
61 function getTaxTypes()
62 {
63         $sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
64     return db_query($sql,"No transactions were returned");
65 }
66
67 function getTaxInfo($id)
68 {
69         $sql = "SELECT * FROM ".TB_PREF."tax_types WHERE id=$id";
70     $result = db_query($sql,"No transactions were returned");
71     return db_fetch($result);
72 }
73
74 //----------------------------------------------------------------------------------------------------
75
76 function print_tax_report()
77 {
78         global $path_to_root, $trans_dir;
79         
80         
81         include_once($path_to_root . "/reporting/includes/pdf_report.inc");
82
83         $rep = new FrontReport(_('Tax Report'), "TaxReport.pdf", user_pagesize());
84
85         $from = $_POST['PARAM_0'];
86         $to = $_POST['PARAM_1'];
87         $summaryOnly = $_POST['PARAM_2'];
88         $comments = $_POST['PARAM_3'];
89         $dec = user_price_dec();
90
91         if ($summaryOnly == 1)
92                 $summary = _('Summary Only');
93         else
94                 $summary = _('Detailed Report');
95
96         $res = getTaxTypes();
97
98         $taxes = array();
99         while ($tax=db_fetch($res))
100                 $taxes[$tax['id']] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);
101
102         if (!$summaryOnly)
103         {
104                 $cols = array(0, 80, 130, 180, 290, 370, 455, 505, 555);
105
106                 $headers = array(_('Trans Type'), _('Ref'), _('Date'), _('Name'), _('Branch Name'),
107                         _('Net'), _('Rate'), _('Tax'));
108                 $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
109
110                 $params =   array(      0 => $comments,
111                                                         1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
112                                                         2 => array('text' => _('Type'), 'from' => $summary, 'to' => ''));
113
114                 $rep->Font();
115                 $rep->Info($params, $cols, $headers, $aligns);
116                 $rep->Header();
117         }
118         
119         $totalnet = 0.0;
120         $totaltax = 0.0;
121         $transactions = getTaxTransactions($from, $to);
122
123         while ($trans=db_fetch($transactions))
124         {
125                 if (in_array($trans['trans_type'], array(11,20,1))) {
126                         $trans['net_amount'] *= -1;
127                         $trans['amount'] *= -1;
128                 }
129                 
130                 if (!$summaryOnly)
131                 {
132                         $rep->TextCol(0, 1,     $trans['type_name']);
133                         $rep->TextCol(1, 2,     $trans['memo']);
134                         $rep->TextCol(2, 3,     sql2date($trans['tran_date']));
135                         $rep->TextCol(3, 4,     $trans['name']);
136                         $rep->TextCol(4, 5,     $trans['br_name']);
137
138                         $rep->TextCol(5, 6,     number_format2($trans['net_amount'], $dec));
139                         $rep->TextCol(6, 7,     number_format2($trans['rate'], $dec));
140                         $rep->TextCol(7, 8,     number_format2($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->Header();
148                         }
149                 }
150                 if ($trans['net_amount'] > 0) {
151                         $taxes[$trans['tax_type_id']]['taxout'] += $trans['amount'];
152                         $taxes[$trans['tax_type_id']]['out'] += $trans['net_amount'];
153                 } else {
154                         $taxes[$trans['tax_type_id']]['taxin'] -= $trans['amount'];
155                         $taxes[$trans['tax_type_id']]['in'] -= $trans['net_amount'];
156                 }
157                 
158                 $totalnet += $trans['net_amount'];
159                 $totaltax += $trans['amount'];
160         }
161         
162         // Summary
163         $cols2 = array(0, 100, 180,     260, 340, 420, 500);
164
165         $headers2 = array(_('Tax Rate'), _('Outputs'), _('Output Tax'), _('Inputs'), _('Input Tax'), _('Net Tax'));
166
167         $aligns2 = array('left', 'right', 'right', 'right',     'right', 'right', 'right');
168
169         for ($i = 0; $i < count($cols2); $i++)
170                 $rep->cols[$i] = $rep->leftMargin + $cols2[$i];
171
172         $rep->headers = $headers2;
173         $rep->aligns = $aligns2;
174         $rep->Header();
175
176         $taxtotal = 0;
177         foreach( $taxes as $id=>$sum)
178         {
179                 $tx = getTaxInfo($id);
180                 
181                 $rep->TextCol(0, 1, $tx['name'] . " " . number_format2($tx['rate'], $dec) . "%");
182                 $rep->TextCol(1, 2, number_format2($sum['out'], $dec));
183                 $rep->TextCol(2, 3,number_format2($sum['taxout'], $dec));
184                 $rep->TextCol(3, 4, number_format2($sum['in'], $dec));
185                 $rep->TextCol(4, 5,number_format2($sum['taxin'], $dec)); 
186                 $rep->TextCol(5, 6, number_format2($sum['taxout']-$sum['taxin'], $dec));
187                 $taxtotal += $sum['taxout']-$sum['taxin'];
188                 $rep->NewLine();
189         }
190
191         $rep->Font('bold');
192         $rep->NewLine();
193         $rep->Line($rep->row + $rep->lineHeight);
194         $rep->TextCol(3, 5,     _("Total payable or refund"));
195         $rep->TextCol(5, 6,     number_format2($taxtotal, $dec));
196         $rep->Line($rep->row - 5);
197         $rep->Font();
198         $rep->NewLine();
199
200         $locale = $path_to_root . "/lang/" . $_SESSION['language']->code . "/locale.inc";
201         if (file_exists($locale))
202         {
203                 $taxinclude = true;
204                 include($locale);
205                 
206 //              if (function_exists("TaxFunction"))
207 //                      TaxFunction();
208                 
209         }
210
211         $rep->End();
212 }
213
214 ?>