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 ***********************************************************************/
13 // ----------------------------------------------------------------
17 // Title: Supplier Balances
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 //----------------------------------------------------------------------------------------------------
28 print_supplier_balances();
30 function getTransactions($supplier_id, $date)
32 $date = date2sql($date);
34 $sql = "SELECT ".TB_PREF."supp_trans.*, ".TB_PREF."sys_types.type_name,
35 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
36 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
37 ((".TB_PREF."supp_trans.type = 20)
38 AND ".TB_PREF."supp_trans.due_date < '$date') AS OverDue
39 FROM ".TB_PREF."supp_trans, ".TB_PREF."sys_types
40 WHERE ".TB_PREF."supp_trans.tran_date <= '$date' AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id'
41 AND ".TB_PREF."supp_trans.type = ".TB_PREF."sys_types.type_id
42 ORDER BY ".TB_PREF."supp_trans.tran_date";
44 $TransResult = db_query($sql,"No transactions were returned");
49 //----------------------------------------------------------------------------------------------------
51 function print_supplier_balances()
55 $to = $_POST['PARAM_0'];
56 $fromsupp = $_POST['PARAM_1'];
57 $currency = $_POST['PARAM_2'];
58 $comments = $_POST['PARAM_3'];
59 $destination = $_POST['PARAM_4'];
61 include_once($path_to_root . "/reporting/includes/excel_report.inc");
63 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
65 if ($fromsupp == reserved_words::get_all_numeric())
68 $from = get_supplier_name($fromsupp);
69 $dec = user_price_dec();
71 if ($currency == reserved_words::get_all())
74 $currency = _('Balances in Home currency');
79 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
81 $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
82 _('Credits'), _('Allocated'), _('Outstanding'));
84 $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
86 $params = array( 0 => $comments,
87 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
88 2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
89 3 => array( 'text' => _('Currency'),'from' => $currency, 'to' => ''));
91 $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances", user_pagesize());
94 $rep->Info($params, $cols, $headers, $aligns);
98 $grandtotal = array(0,0,0,0);
100 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers ";
101 if ($fromsupp != reserved_words::get_all_numeric())
102 $sql .= "WHERE supplier_id=$fromsupp ";
103 $sql .= "ORDER BY supp_name";
104 $result = db_query($sql, "The customers could not be retrieved");
106 while ($myrow=db_fetch($result))
108 if (!$convert && $currency != $myrow['curr_code'])
111 $rep->TextCol(0, 3, $myrow['name']);
113 $rep->TextCol(3, 4, $myrow['curr_code']);
116 $res = getTransactions($myrow['supplier_id'], $to);
117 if (db_num_rows($res)==0)
119 $rep->Line($rep->row + 4);
120 $total[0] = $total[1] = $total[2] = $total[3] = 0.0;
121 while ($trans=db_fetch($res))
124 $rep->TextCol(0, 1, $trans['type_name']);
125 $rep->TextCol(1, 2, $trans['reference']);
126 $rep->DateCol(2, 3, $trans['tran_date'], true);
127 if ($trans['type'] == 20)
128 $rep->DateCol(3, 4, $trans['due_date'], true);
129 $item[0] = $item[1] = 0.0;
131 $rate = $trans['rate'];
134 if ($trans['TotalAmount'] > 0.0)
136 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
137 $rep->AmountCol(4, 5, $item[0], $dec);
141 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
142 $rep->AmountCol(5, 6, $item[1], $dec);
144 $item[2] = round2($trans['Allocated'] * $rate, $dec);
145 $rep->AmountCol(6, 7, $item[2], $dec);
147 if ($trans['type'] == 20)
148 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
150 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
152 if ($trans['type'] == 20)
153 $item[3] = $item[0] + $item[1] - $item[2];
155 $item[3] = $item[0] - $item[1] + $item[2];
156 $rep->AmountCol(7, 8, $item[3], $dec);
157 for ($i = 0; $i < 4; $i++)
159 $total[$i] += $item[$i];
160 $grandtotal[$i] += $item[$i];
163 $rep->Line($rep->row - 8);
165 $rep->TextCol(0, 3, _('Total'));
166 for ($i = 0; $i < 4; $i++)
168 $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
171 $rep->Line($rep->row - 4);
175 $rep->TextCol(0, 3, _('Grand Total'));
177 for ($i = 0; $i < 4; $i++)
178 $rep->AmountCol($i + 4, $i + 5,$grandtotal[$i], $dec);
179 $rep->Line($rep->row - 4);