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