2 /**********************************************************************
3 Copyright (C) FrontAccounting Team.
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';
14 // ----------------------------------------------------------------
16 // Creator: @boxygen, Joe Hunt
18 // Title: Supplier Trial Balances
19 // ----------------------------------------------------------------
22 include_once($path_to_root . "/includes/session.inc");
23 include_once($path_to_root . "/includes/date_functions.inc");
24 include_once($path_to_root . "/includes/data_checks.inc");
25 include_once($path_to_root . "/gl/includes/gl_db.inc");
27 //----------------------------------------------------------------------------------------------------
29 print_supplier_balances();
31 function get_open_balance($supplier_id, $to)
36 $sql = "SELECT SUM(IF(t.type = ".ST_SUPPINVOICE." OR (t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.") AND t.ov_amount>0),
37 -abs(t.ov_amount + t.ov_gst + t.ov_discount), 0)) AS charges,";
39 $sql .= "SUM(IF(t.type != ".ST_SUPPINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.") AND t.ov_amount>0),
40 abs(t.ov_amount + t.ov_gst + t.ov_discount) * -1, 0)) AS credits,";
42 $sql .= "SUM(IF(t.type != ".ST_SUPPINVOICE." AND NOT(t.type IN (".ST_JOURNAL." , ".ST_BANKDEPOSIT.")),
43 t.alloc * -1, t.alloc)) AS Allocated,";
45 $sql .= "SUM(IF(t.type = ".ST_SUPPINVOICE.", 1, -1) *
46 (abs(t.ov_amount + t.ov_gst + t.ov_discount) - abs(t.alloc))) AS OutStanding
47 FROM ".TB_PREF."supp_trans t
48 WHERE t.supplier_id = ".db_escape($supplier_id);
50 $sql .= " AND t.tran_date < '$to'";
51 $sql .= " GROUP BY supplier_id";
53 $result = db_query($sql,"No transactions were returned");
54 return db_fetch($result);
57 function getTransactions($supplier_id, $from, $to)
59 $from = date2sql($from);
61 //memo added by faisal
62 $sql = "SELECT ".TB_PREF."supp_trans.*, comments.memo_,
63 (".TB_PREF."supp_trans.ov_amount + ".TB_PREF."supp_trans.ov_gst + ".TB_PREF."supp_trans.ov_discount)
64 AS TotalAmount, ".TB_PREF."supp_trans.alloc AS Allocated,
65 ((".TB_PREF."supp_trans.type = ".ST_SUPPINVOICE.")
66 AND ".TB_PREF."supp_trans.due_date < '$to') AS OverDue
67 FROM ".TB_PREF."supp_trans
68 LEFT JOIN ".TB_PREF."comments comments ON ".TB_PREF."supp_trans.type=comments.type AND ".TB_PREF."supp_trans.trans_no=comments.id
69 WHERE ".TB_PREF."supp_trans.tran_date >= '$from' AND ".TB_PREF."supp_trans.tran_date <= '$to'
70 AND ".TB_PREF."supp_trans.supplier_id = '$supplier_id' AND ".TB_PREF."supp_trans.ov_amount!=0
71 ORDER BY ".TB_PREF."supp_trans.tran_date";
73 return db_query($sql,"No transactions were returned");
76 //----------------------------------------------------------------------------------------------------
78 function print_supplier_balances()
80 global $path_to_root, $systypes_array;
82 $from = $_POST['PARAM_0'];
83 $to = $_POST['PARAM_1'];
84 $fromsupp = $_POST['PARAM_2'];
85 $currency = $_POST['PARAM_3'];
86 $no_zeros = $_POST['PARAM_4'];
87 $comments = $_POST['PARAM_5'];
88 $orientation = $_POST['PARAM_6'];
89 $destination = $_POST['PARAM_7'];
91 include_once($path_to_root . "/reporting/includes/excel_report.inc");
93 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
95 $orientation = ($orientation ? 'L' : 'P');
96 if ($fromsupp == ALL_TEXT)
99 $supp = get_supplier_name($fromsupp);
100 $dec = user_price_dec();
102 if ($currency == ALL_TEXT)
105 $currency = _('Balances in Home currency');
110 if ($no_zeros) $nozeros = _('Yes');
111 else $nozeros = _('No');
113 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
115 $headers = array(_('Name'), '', '', _('Open Balance'), _('Debit'),
116 _('Credit'), '', _('Balance'));
118 $aligns = array('left', 'left', 'left', 'right', 'right', 'right', 'right', 'right');
120 $params = array( 0 => $comments,
121 1 => array('text' => _('Period'), 'from' => $from, 'to' => $to),
122 2 => array('text' => _('Supplier'), 'from' => $supp, 'to' => ''),
123 3 => array( 'text' => _('Currency'),'from' => $currency, 'to' => ''),
124 4 => array('text' => _('Suppress Zeros'), 'from' => $nozeros, 'to' => ''));
126 $rep = new FrontReport(_('Supplier Trial Balance'), "SupplierTB", user_pagesize(), 9, $orientation);
127 if ($orientation == 'L')
128 recalculate_cols($cols);
131 $rep->Info($params, $cols, $headers, $aligns);
135 $grandtotal = array(0,0,0,0);
137 $sql = "SELECT supplier_id, supp_name AS name, curr_code FROM ".TB_PREF."suppliers";
138 if ($fromsupp != ALL_TEXT)
139 $sql .= " WHERE supplier_id=".db_escape($fromsupp);
140 $sql .= " ORDER BY supp_name";
141 $result = db_query($sql, "The customers could not be retrieved");
143 $tot_cur_cr = $tot_cur_db = 0;
144 while ($myrow=db_fetch($result))
146 if (!$convert && $currency != $myrow['curr_code'])
149 $rate = $convert ? get_exchange_rate_from_home_currency($myrow['curr_code'], Today()) : 1;
150 $bal = get_open_balance($myrow['supplier_id'], $from);
151 $init[0] = $init[1] = 0.0;
152 $init[0] = round2(abs($bal['charges']*$rate), $dec);
153 $init[1] = round2(Abs($bal['credits']*$rate), $dec);
154 $init[2] = round2($bal['Allocated']*$rate, $dec);
156 $init[3] = $init[1] - $init[0];
157 $accumulate += $init[3];
159 $res = getTransactions($myrow['supplier_id'], $from, $to);
161 $total = array(0,0,0,0);
162 for ($i = 0; $i < 4; $i++)
164 $total[$i] += $init[$i];
165 $grandtotal[$i] += $init[$i];
168 if (db_num_rows($res) == 0 && !$no_zeros)
170 $rep->TextCol(0, 2, $myrow['name']);
171 $rep->AmountCol(3, 4, $init[3], $dec);
172 $rep->AmountCol(7, 8, $init[3], $dec);
173 //$rep->Line($rep->row - 2);
178 $curr_db = $curr_cr = 0;
179 while ($trans=db_fetch($res))
181 //if ($no_zeros && floatcmp(abs($trans['TotalAmount']), $trans['Allocated']) == 0) continue;
182 $item[0] = $item[1] = 0.0;
183 if ($trans['TotalAmount'] > 0.0)
185 $item[0] = round2(abs($trans['TotalAmount']) * $rate, $dec);
186 $curr_cr += $item[0];
187 $tot_cur_cr += $item[0];
189 $accumulate -= $item[0];
193 $item[1] = round2(abs($trans['TotalAmount']) * $rate, $dec);
194 $curr_db += $item[1];
195 $tot_cur_db += $item[1];
196 $accumulate += $item[1];
198 $item[2] = round2($trans['Allocated'] * $rate, $dec);
199 if ($trans['TotalAmount'] > 0.0)
200 $item[3] = $item[2] - $item[0];
202 $item[3] = ($item[2] - $item[1]) * -1;
204 for ($i = 0; $i < 4; $i++)
206 $total[$i] += $item[$i];
207 $grandtotal[$i] += $item[$i];
209 $total[3] = $total[1] - $total[0];
211 if ($no_zeros && $total[3] == 0.0 && $curr_db == 0.0 && $curr_cr == 0.0) continue;
212 $rep->TextCol(0, 2, $myrow['name']);
213 $rep->AmountCol(3, 4, $total[3] + $curr_cr - $curr_db, $dec);
214 $rep->AmountCol(4, 5, $curr_db, $dec);
215 $rep->AmountCol(5, 6, $curr_cr, $dec);
216 $rep->AmountCol(7, 8, $total[3], $dec);
217 for ($i = 2; $i < 4; $i++)
221 //$rep->Line($rep->row - 2);
224 $rep->Line($rep->row + 4); // added line by Joe
227 $rep->TextCol(0, 3, _('Grand Total'));
230 $grandtotal[3] = $grandtotal[1] - $grandtotal[0];
232 $rep->AmountCol(3, 4,$grandtotal[3] - $tot_cur_db + $tot_cur_cr, $dec);
234 $rep->AmountCol(4, 5,$tot_cur_db, $dec);
235 $rep->AmountCol(5, 6,$tot_cur_cr, $dec);
237 $rep->AmountCol(7, 8,$grandtotal[3], $dec);
238 $rep->Line($rep->row - 6, 1);