Bug 5522, 5523, 5524: Journal Entry Affecting in both sales and purchase and more...
[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 tt.name as taxname, taxrec.*, taxrec.amount*ex_rate AS amount,
37                     taxrec.net_amount*ex_rate AS net_amount,
38                 IF(ISNULL(supp.supp_name),
39                     IF(ISNULL(debt.name),
40                         IF(gl.person_type_id<>".PT_MISC.", gl.memo_, gl.person_id), debt.name),
41                         supp.supp_name) as name,
42                                 branch.br_name
43                 FROM ".TB_PREF."trans_tax_details taxrec
44                 LEFT JOIN ".TB_PREF."tax_types tt
45                         ON taxrec.tax_type_id=tt.id
46                 LEFT JOIN ".TB_PREF."gl_trans gl 
47                         ON taxrec.trans_type=gl.type AND taxrec.trans_no=gl.type_no AND gl.amount<>0 AND
48                         gl.amount=taxrec.amount AND
49                         (tt.purchasing_gl_code=gl.account OR tt.sales_gl_code=gl.account)
50                 LEFT JOIN ".TB_PREF."supp_trans strans
51                         ON taxrec.trans_no=strans.trans_no AND taxrec.trans_type=strans.type
52                 LEFT JOIN ".TB_PREF."suppliers as supp ON strans.supplier_id=supp.supplier_id
53                 LEFT JOIN ".TB_PREF."debtor_trans dtrans
54                         ON taxrec.trans_no=dtrans.trans_no AND taxrec.trans_type=dtrans.type
55                 LEFT JOIN ".TB_PREF."debtors_master as debt ON dtrans.debtor_no=debt.debtor_no
56                 LEFT JOIN ".TB_PREF."cust_branch as branch ON dtrans.branch_code=branch.branch_code
57                 WHERE (taxrec.amount <> 0 OR taxrec.net_amount <> 0)
58                         AND taxrec.tran_date >= '$fromdate'
59                         AND taxrec.tran_date <= '$todate'
60                         AND taxrec.trans_type <> ".ST_CUSTDELIVERY."
61         GROUP BY taxrec.id                      
62                 ORDER BY taxrec.trans_type, taxrec.tran_date, taxrec.trans_no, taxrec.ex_rate";
63
64     return db_query($sql,"No transactions were returned");
65 }
66
67 function getTaxTypes()
68 {
69         $sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
70     return db_query($sql,"No transactions were returned");
71 }
72
73 function getTaxInfo($id)
74 {
75         $sql = "SELECT * FROM ".TB_PREF."tax_types WHERE id=$id";
76     $result = db_query($sql,"No transactions were returned");
77     return db_fetch($result);
78 }
79
80 //----------------------------------------------------------------------------------------------------
81
82 function print_tax_report()
83 {
84         global $path_to_root, $systypes_array;
85
86         $from = $_POST['PARAM_0'];
87         $to = $_POST['PARAM_1'];
88         $summaryOnly = $_POST['PARAM_2'];
89         $comments = $_POST['PARAM_3'];
90         $orientation = $_POST['PARAM_4'];
91         $destination = $_POST['PARAM_5'];
92
93         if ($destination)
94                 include_once($path_to_root . "/reporting/includes/excel_report.inc");
95         else
96                 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
97
98         $orientation = ($orientation ? 'L' : 'P');
99         $dec = user_price_dec();
100
101         $rep = new FrontReport(_('Tax Report'), "TaxReport", user_pagesize(), 9, $orientation);
102         if ($summaryOnly == 1)
103                 $summary = _('Summary Only');
104         else
105                 $summary = _('Detailed Report');
106
107         $res = getTaxTypes();
108
109         $taxes[0] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);
110         while ($tax=db_fetch($res))
111                 $taxes[$tax['id']] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);
112
113         $params =   array(      0 => $comments,
114                                                 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
115                                                 2 => array('text' => _('Type'), 'from' => $summary, 'to' => ''));
116
117         $cols = array(0, 80, 130, 180, 270, 350, 400, 430, 480, 485, 520);
118
119         $headers = array(_('Trans Type'), _('Ref'), _('Date'), _('Name'), _('Branch Name'),
120                 _('Net'), _('Rate'), _('Tax'), '', _('Name'));
121         $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right', 'right','left');
122     if ($orientation == 'L')
123         recalculate_cols($cols);
124
125         $rep->Font();
126         $rep->Info($params, $cols, $headers, $aligns);
127         if (!$summaryOnly)
128         {
129                 $rep->NewPage();
130         }
131         
132         $totalnet = 0.0;
133         $totaltax = 0.0;
134         $transactions = getTaxTransactions($from, $to);
135
136         while ($trans=db_fetch($transactions))
137         {
138                 if (in_array($trans['trans_type'], array(ST_CUSTCREDIT,ST_SUPPINVOICE, ST_JOURNAL))) {
139                         if ($trans['reg_type'] == TR_INPUT) {
140                                 $trans['net_amount'] *= -1;
141                                 $trans['amount'] *= -1;
142                         }
143                 }
144
145                 if (!$summaryOnly)
146                 {
147                         $rep->TextCol(0, 1, $systypes_array[$trans['trans_type']]);
148                         if ($trans['memo'] == '')
149                                 $trans['memo'] = get_reference($trans['trans_type'], $trans['trans_no']);
150                         $rep->TextCol(1, 2,     $trans['memo']);
151                         $rep->DateCol(2, 3,     $trans['tran_date'], true);
152                         $rep->TextCol(3, 4,     $trans['name']);
153                         $rep->TextCol(4, 5,     $trans['br_name']);
154
155                         $rep->AmountCol(5, 6, $trans['net_amount'], $dec);
156                         $rep->AmountCol(6, 7, $trans['rate'], $dec);
157                         $rep->AmountCol(7, 8, $trans['amount'], $dec);
158
159                         $rep->TextCol(9, 10, $trans['taxname']);
160
161                         $rep->NewLine();
162
163                         if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
164                         {
165                                 $rep->Line($rep->row - 2);
166                                 $rep->NewPage();
167                         }
168                 }
169                 $tax_type = $trans['tax_type_id'];
170                 if ($trans['trans_type']==ST_JOURNAL && $trans['reg_type']==TR_INPUT) {
171                         $taxes[$tax_type]['taxin'] += $trans['amount'];
172                         $taxes[$tax_type]['in'] += $trans['net_amount'];
173                 }
174                 elseif ($trans['trans_type']==ST_JOURNAL && $trans['reg_type']==TR_OUTPUT) {
175                         $taxes[$tax_type]['taxout'] += $trans['amount'];
176                         $taxes[$tax_type]['out'] += $trans['net_amount'];
177                 }
178                 elseif (in_array($trans['trans_type'], array(ST_BANKDEPOSIT,ST_SALESINVOICE,ST_CUSTCREDIT))) {
179                         $taxes[$tax_type]['taxout'] += $trans['amount'];
180                         $taxes[$tax_type]['out'] += $trans['net_amount'];
181                 } elseif ($trans['reg_type'] !== NULL || in_array($trans['trans_type'], array(ST_SUPPINVOICE, ST_SUPPCREDIT, ST_BANKPAYMENT))) {
182                         $taxes[$tax_type]['taxin'] += $trans['amount'];
183                         $taxes[$tax_type]['in'] += $trans['net_amount'];
184                 }
185                 $totalnet += $trans['net_amount'];
186                 $totaltax += $trans['amount'];
187         }
188         
189         // Summary
190         $cols2 = array(0, 100, 180,     260, 340, 420, 500);
191     if ($orientation == 'L')
192         recalculate_cols($cols2);
193
194         $headers2 = array(_('Tax Rate'), _('Outputs'), _('Output Tax'), _('Inputs'), _('Input Tax'), _('Net Tax'));
195
196         $aligns2 = array('left', 'right', 'right', 'right',     'right', 'right', 'right');
197
198         $rep->Info($params, $cols2, $headers2, $aligns2);
199
200         $rep->headers = $headers2;
201         $rep->aligns = $aligns2;
202         $rep->NewPage();
203
204         $taxtotal = 0;
205         foreach( $taxes as $id=>$sum)
206         {
207                 if ($id)
208                 {
209                         $tx = getTaxInfo($id);
210                         $rep->TextCol(0, 1, $tx['name'] . " " . number_format2($tx['rate'], $dec) . "%");
211                 } else {
212                         $rep->TextCol(0, 1, _('Exempt'));
213                 }
214                 $rep->AmountCol(1, 2, $sum['out'], $dec);
215                 $rep->AmountCol(2, 3, $sum['taxout'], $dec);
216                 $rep->AmountCol(3, 4, $sum['in'], $dec);
217                 $rep->AmountCol(4, 5, $sum['taxin'], $dec); 
218                 $rep->AmountCol(5, 6, $sum['taxout']+$sum['taxin'], $dec);
219                 $taxtotal += $sum['taxout']+$sum['taxin'];
220                 $rep->NewLine();
221         }
222
223         $rep->Font('bold');
224         $rep->NewLine();
225         $rep->Line($rep->row + $rep->lineHeight);
226         $rep->TextCol(3, 5,     _("Total payable or refund"));
227         $rep->AmountCol(5, 6, $taxtotal, $dec);
228         $rep->Line($rep->row - 5);
229         $rep->Font();
230         $rep->NewLine();
231
232         hook_tax_report_done();
233
234         $rep->End();
235 }
236