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