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