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_SUPPLIERANALYTIC';
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 get_open_balance($supplier_id, $to, $convert)
34 $sql = "SELECT SUM(IF(".TB_PREF."supp_trans.type = ".ST_SUPPINVOICE.", (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst +
35 ".TB_PREF."supp_trans.ov_discount)";
38 $sql .= ", 0)) AS charges,
39 SUM(IF(".TB_PREF."supp_trans.type <> ".ST_SUPPINVOICE.", (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst +
40 ".TB_PREF."supp_trans.ov_discount)";
43 $sql .= ", 0)) AS credits,
44 SUM(".TB_PREF."supp_trans.alloc";
47 $sql .= ") AS Allocated,
48 SUM((".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst +
49 ".TB_PREF."supp_trans.ov_discount - ".TB_PREF."supp_trans.alloc)";
52 $sql .= ") AS OutStanding
53 FROM ".TB_PREF."supp_trans
54 WHERE ".TB_PREF."supp_trans.tran_date < '$to'
55 AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id' GROUP BY supplier_id";
57 $result = db_query($sql,"No transactions were returned");
58 return db_fetch($result);
61 function getTransactions($supplier_id, $from, $to)
63 $from = date2sql($from);
66 $sql = "SELECT ".TB_PREF."supp_trans.*,
67 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
68 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
69 ((".TB_PREF."supp_trans.type = ".ST_SUPPINVOICE.")
70 AND ".TB_PREF."supp_trans.due_date < '$to') AS OverDue
71 FROM ".TB_PREF."supp_trans
72 WHERE ".TB_PREF."supp_trans.tran_date >= '$from' AND ".TB_PREF."supp_trans.tran_date <= '$to'
73 AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id' AND ".TB_PREF."supp_trans.ov_amount!=0
74 ORDER BY ".TB_PREF."supp_trans.tran_date";
76 $TransResult = db_query($sql,"No transactions were returned");
81 //----------------------------------------------------------------------------------------------------
83 function print_supplier_balances()
85 global $path_to_root, $systypes_array;
87 $from = $_POST['PARAM_0'];
88 $to = $_POST['PARAM_1'];
89 $fromsupp = $_POST['PARAM_2'];
90 $currency = $_POST['PARAM_3'];
91 $no_zeros = $_POST['PARAM_4'];
92 $comments = $_POST['PARAM_5'];
93 $orientation = $_POST['PARAM_6'];
94 $destination = $_POST['PARAM_7'];
96 include_once($path_to_root . "/reporting/includes/excel_report.inc");
98 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
100 $orientation = ($orientation ? 'L' : 'P');
101 if ($fromsupp == ALL_TEXT)
104 $supp = get_supplier_name($fromsupp);
105 $dec = user_price_dec();
107 if ($currency == ALL_TEXT)
110 $currency = _('Balances in Home currency');
115 if ($no_zeros) $nozeros = _('Yes');
116 else $nozeros = _('No');
118 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
120 $headers = array(_('Trans Type'), _('#'), _('Date'), _('Due Date'), _('Charges'),
121 _('Credits'), _('Allocated'), _('Outstanding'));
123 $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
125 $params = array( 0 => $comments,
126 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
127 2 => array('text' => _('Supplier'), 'from' => $supp, 'to' => ''),
128 3 => array( 'text' => _('Currency'),'from' => $currency, 'to' => ''),
129 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
131 $rep = new FrontReport(_('Supplier Balances'), "SupplierBalances", user_pagesize(), 9, $orientation);
132 if ($orientation == 'L')
133 recalculate_cols($cols);
136 $rep->Info($params, $cols, $headers, $aligns);
140 $grandtotal = array(0,0,0,0);
142 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers";
143 if ($fromsupp != ALL_TEXT)
144 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
145 $sql .= " ORDER BY supp_name";
146 $result = db_query($sql, "The customers could not be retrieved");
148 while ($myrow=db_fetch($result))
150 if (!$convert && $currency != $myrow['curr_code'])
152 $bal = get_open_balance($myrow['supplier_id'], $from, $convert);
153 $init[0] = $init[1] = 0.0;
154 $init[0] = round2(abs($bal['charges']), $dec);
155 $init[1] = round2(Abs($bal['credits']), $dec);
156 $init[2] = round2($bal['Allocated'], $dec);
157 $init[3] = round2($bal['OutStanding'], $dec);;
158 $total = array(0,0,0,0);
159 for ($i = 0; $i < 4; $i++)
161 $total[$i] += $init[$i];
162 $grandtotal[$i] += $init[$i];
164 $res = getTransactions($myrow['supplier_id'], $from, $to);
165 if ($no_zeros && db_num_rows($res) == 0) continue;
168 $rep->TextCol(0, 2, $myrow['name']);
169 if ($convert) $rep->TextCol(2, 3, $myrow['curr_code']);
171 $rep->TextCol(3, 4, _("Open Balance"));
172 $rep->AmountCol(4, 5, $init[0], $dec);
173 $rep->AmountCol(5, 6, $init[1], $dec);
174 $rep->AmountCol(6, 7, $init[2], $dec);
175 $rep->AmountCol(7, 8, $init[3], $dec);
177 if (db_num_rows($res)==0) continue;
179 $rep->Line($rep->row + 4);
180 while ($trans=db_fetch($res))
182 if ($no_zeros && floatcmp(abs($trans['TotalAmount']), $trans['Allocated']) == 0) continue;
184 $rep->TextCol(0, 1, $systypes_array[$trans['type']]);
185 $rep->TextCol(1, 2, $trans['reference']);
186 $rep->DateCol(2, 3, $trans['tran_date'], true);
187 if ($trans['type'] == ST_SUPPINVOICE)
188 $rep->DateCol(3, 4, $trans['due_date'], true);
189 $item[0] = $item[1] = 0.0;
191 $rate = $trans['rate'];
194 if ($trans['TotalAmount'] > 0.0)
196 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
197 $rep->AmountCol(4, 5, $item[0], $dec);
201 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
202 $rep->AmountCol(5, 6, $item[1], $dec);
204 $item[2] = round2($trans['Allocated'] * $rate, $dec);
205 $rep->AmountCol(6, 7, $item[2], $dec);
207 if ($trans['type'] == 20)
208 $item[3] = ($trans['TotalAmount'] - $trans['Allocated']) * $rate;
210 $item[3] = ($trans['TotalAmount'] + $trans['Allocated']) * $rate;
212 if ($trans['type'] == ST_SUPPINVOICE || $trans['type'] == ST_BANKDEPOSIT)
213 $item[3] = $item[0] + $item[1] - $item[2];
215 $item[3] = $item[0] - $item[1] + $item[2];
216 $rep->AmountCol(7, 8, $item[3], $dec);
217 for ($i = 0; $i < 4; $i++)
219 $total[$i] += $item[$i];
220 $grandtotal[$i] += $item[$i];
223 $rep->Line($rep->row - 8);
225 $rep->TextCol(0, 3, _('Total'));
226 for ($i = 0; $i < 4; $i++)
228 $rep->AmountCol($i + 4, $i + 5, $total[$i], $dec);
231 $rep->Line($rep->row - 4);
235 $rep->TextCol(0, 3, _('Grand Total'));
237 for ($i = 0; $i < 4; $i++)
238 $rep->AmountCol($i + 4, $i + 5,$grandtotal[$i], $dec);
239 $rep->Line($rep->row - 4);