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 ***********************************************************************/
13 // ----------------------------------------------------------------
18 // ----------------------------------------------------------------
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");
26 //------------------------------------------------------------------
31 function getTaxTransactions($from, $to)
33 $fromdate = date2sql($from);
34 $todate = date2sql($to);
36 $sql = "SELECT taxrec.*, taxrec.amount*ex_rate AS amount,
37 taxrec.net_amount*ex_rate AS net_amount,
39 IF(ISNULL(supp.supp_name), debt.name, supp.supp_name) as name,
41 FROM ".TB_PREF."trans_tax_details taxrec
42 LEFT JOIN ".TB_PREF."supp_trans strans
43 ON taxrec.trans_no=strans.trans_no AND taxrec.trans_type=strans.type
44 LEFT JOIN ".TB_PREF."suppliers as supp ON strans.supplier_id=supp.supplier_id
45 LEFT JOIN ".TB_PREF."debtor_trans dtrans
46 ON taxrec.trans_no=dtrans.trans_no AND taxrec.trans_type=dtrans.type
47 LEFT JOIN ".TB_PREF."debtors_master as debt ON dtrans.debtor_no=debt.debtor_no
48 LEFT JOIN ".TB_PREF."cust_branch as branch ON dtrans.branch_code=branch.branch_code,
49 ".TB_PREF."sys_types stype
50 WHERE taxrec.trans_type=stype.type_id
51 AND (taxrec.amount != 0 OR taxrec.net_amount != 0)
52 AND taxrec.trans_type != 13
53 AND taxrec.tran_date >= '$fromdate'
54 AND taxrec.tran_date <= '$todate'
55 ORDER BY taxrec.tran_date";
56 //display_error($sql);
57 return db_query($sql,"No transactions were returned");
60 function getTaxTypes()
62 $sql = "SELECT * FROM ".TB_PREF."tax_types ORDER BY id";
63 return db_query($sql,"No transactions were returned");
66 function getTaxInfo($id)
68 $sql = "SELECT * FROM ".TB_PREF."tax_types WHERE id=$id";
69 $result = db_query($sql,"No transactions were returned");
70 return db_fetch($result);
73 //----------------------------------------------------------------------------------------------------
75 function print_tax_report()
77 global $path_to_root, $trans_dir;
80 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
82 $rep = new FrontReport(_('Tax Report'), "TaxReport.pdf", user_pagesize());
84 $from = $_POST['PARAM_0'];
85 $to = $_POST['PARAM_1'];
86 $summaryOnly = $_POST['PARAM_2'];
87 $comments = $_POST['PARAM_3'];
88 $dec = user_price_dec();
90 if ($summaryOnly == 1)
91 $summary = _('Summary Only');
93 $summary = _('Detailed Report');
98 while ($tax=db_fetch($res))
99 $taxes[$tax['id']] = array('in'=>0, 'out'=>0, 'taxin'=>0, 'taxout'=>0);
101 $params = array( 0 => $comments,
102 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
103 2 => array('text' => _('Type'), 'from' => $summary, 'to' => ''));
105 $cols = array(0, 80, 130, 180, 290, 370, 455, 505, 555);
107 $headers = array(_('Trans Type'), _('Ref'), _('Date'), _('Name'), _('Branch Name'),
108 _('Net'), _('Rate'), _('Tax'));
109 $aligns = array('left', 'left', 'left', 'left', 'left', 'right', 'right', 'right');
111 $rep->Info($params, $cols, $headers, $aligns);
119 $transactions = getTaxTransactions($from, $to);
121 while ($trans=db_fetch($transactions))
123 if (in_array($trans['trans_type'], array(11,20,1))) {
124 $trans['net_amount'] *= -1;
125 $trans['amount'] *= -1;
130 $rep->TextCol(0, 1, $trans['type_name']);
131 $rep->TextCol(1, 2, $trans['memo']);
132 $rep->TextCol(2, 3, sql2date($trans['tran_date']));
133 $rep->TextCol(3, 4, $trans['name']);
134 $rep->TextCol(4, 5, $trans['br_name']);
136 $rep->TextCol(5, 6, number_format2($trans['net_amount'], $dec));
137 $rep->TextCol(6, 7, number_format2($trans['rate'], $dec));
138 $rep->TextCol(7, 8, number_format2($trans['amount'], $dec));
142 if ($rep->row < $rep->bottomMargin + $rep->lineHeight)
144 $rep->Line($rep->row - 2);
148 if (in_array($trans['trans_type'], array(0,2,10,11))) {
149 $taxes[$trans['tax_type_id']]['taxout'] += $trans['amount'];
150 $taxes[$trans['tax_type_id']]['out'] += $trans['net_amount'];
152 $taxes[$trans['tax_type_id']]['taxin'] -= $trans['amount'];
153 $taxes[$trans['tax_type_id']]['in'] -= $trans['net_amount'];
156 $totalnet += $trans['net_amount'];
157 $totaltax += $trans['amount'];
161 $cols2 = array(0, 100, 180, 260, 340, 420, 500);
163 $headers2 = array(_('Tax Rate'), _('Outputs'), _('Output Tax'), _('Inputs'), _('Input Tax'), _('Net Tax'));
165 $aligns2 = array('left', 'right', 'right', 'right', 'right', 'right', 'right');
167 for ($i = 0; $i < count($cols2); $i++)
168 $rep->cols[$i] = $rep->leftMargin + $cols2[$i];
170 $rep->headers = $headers2;
171 $rep->aligns = $aligns2;
175 foreach( $taxes as $id=>$sum)
177 $tx = getTaxInfo($id);
179 $rep->TextCol(0, 1, $tx['name'] . " " . number_format2($tx['rate'], $dec) . "%");
180 $rep->TextCol(1, 2, number_format2($sum['out'], $dec));
181 $rep->TextCol(2, 3,number_format2($sum['taxout'], $dec));
182 $rep->TextCol(3, 4, number_format2($sum['in'], $dec));
183 $rep->TextCol(4, 5,number_format2($sum['taxin'], $dec));
184 $rep->TextCol(5, 6, number_format2($sum['taxout']-$sum['taxin'], $dec));
185 $taxtotal += $sum['taxout']-$sum['taxin'];
191 $rep->Line($rep->row + $rep->lineHeight);
192 $rep->TextCol(3, 5, _("Total payable or refund"));
193 $rep->TextCol(5, 6, number_format2($taxtotal, $dec));
194 $rep->Line($rep->row - 5);
198 $locale = $path_to_root . "/lang/" . $_SESSION['language']->code . "/locale.inc";
199 if (file_exists($locale))
204 // if (function_exists("TaxFunction"))