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 // ----------------------------------------------------------------
17 // Title: Payment Report
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 // trial_inquiry_controls();
29 print_payment_report();
31 function getTransactions($supplier, $date)
33 $date = date2sql($date);
35 $sql = "SELECT ".TB_PREF."sys_types.type_name,
36 ".TB_PREF."supp_trans.supp_reference,
37 ".TB_PREF."supp_trans.tran_date,
38 ".TB_PREF."supp_trans.due_date,
39 ".TB_PREF."supp_trans.trans_no,
40 ".TB_PREF."supp_trans.type,
41 ".TB_PREF."supp_trans.rate,
42 (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc) AS Balance,
43 (ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) ) AS TranTotal
44 FROM ".TB_PREF."supp_trans,
46 WHERE ".TB_PREF."sys_types.type_id = ".TB_PREF."supp_trans.type
47 AND ".TB_PREF."supp_trans.supplier_id = '" . $supplier . "'
48 AND ABS(".TB_PREF."supp_trans.ov_amount) + ABS(".TB_PREF."supp_trans.ov_gst) - ".TB_PREF."supp_trans.alloc != 0
49 AND ".TB_PREF."supp_trans.tran_date <='" . $date . "'
50 ORDER BY ".TB_PREF."supp_trans.type,
51 ".TB_PREF."supp_trans.trans_no";
53 return db_query($sql, "No transactions were returned");
56 //----------------------------------------------------------------------------------------------------
58 function print_payment_report()
62 include_once($path_to_root . "/reporting/includes/pdf_report.inc");
64 $to = $_POST['PARAM_0'];
65 $fromsupp = $_POST['PARAM_1'];
66 $currency = $_POST['PARAM_2'];
67 $comments = $_POST['PARAM_3'];
69 if ($fromsupp == reserved_words::get_all_numeric())
72 $from = get_supplier_name($fromsupp);
74 $dec = user_price_dec();
76 if ($currency == reserved_words::get_all())
79 $currency = _('Balances in Home Currency');
84 $cols = array(0, 100, 130, 190, 250, 320, 385, 450, 515);
86 $headers = array(_('Trans Type'), _('#'), _('Due Date'), '', '',
87 '', _('Total'), _('Balance'));
89 $aligns = array('left', 'left', 'left', 'left', 'right', 'right', 'right', 'right');
91 $params = array( 0 => $comments,
92 1 => array('text' => _('End Date'), 'from' => $to, 'to' => ''),
93 2 => array('text' => _('Supplier'), 'from' => $from, 'to' => ''),
94 3 => array( 'text' => _('Currency'),'from' => $currency, 'to' => ''));
96 $rep = new FrontReport(_('Payment Report'), "PaymentReport.pdf", user_pagesize());
99 $rep->Info($params, $cols, $headers, $aligns);
103 $grandtotal = array(0,0);
105 $sql = "SELECT supplier_id, supp_name AS name, curr_code, ".TB_PREF."payment_terms.terms FROM ".TB_PREF."suppliers, ".TB_PREF."payment_terms
107 if ($fromsupp != reserved_words::get_all_numeric())
108 $sql .= "supplier_id=$fromsupp AND ";
109 $sql .= "".TB_PREF."suppliers.payment_terms = ".TB_PREF."payment_terms.terms_indicator
111 $result = db_query($sql, "The customers could not be retrieved");
113 while ($myrow=db_fetch($result))
115 if (!$convert && $currency != $myrow['curr_code'])
118 $rep->TextCol(0, 6, $myrow['name'] . " - " . $myrow['terms']);
120 $rep->TextCol(6, 7, $myrow['curr_code']);
123 $res = getTransactions($myrow['supplier_id'], $to);
124 if (db_num_rows($res)==0)
126 $rep->Line($rep->row + 4);
127 $total[0] = $total[1] = 0.0;
128 while ($trans=db_fetch($res))
131 $rate = $trans['rate'];
135 $rep->TextCol(0, 1, $trans['type_name']);
136 $rep->TextCol(1, 2, $trans['supp_reference']);
137 if ($trans['type'] == 20)
138 $rep->TextCol(2, 3, sql2date($trans['due_date']));
140 $rep->TextCol(2, 3, sql2date($trans['tran_date']));
141 if ($trans['type'] != 20)
143 $trans['TranTotal'] = -$trans['TranTotal'];
144 $trans['Balance'] = -$trans['Balance'];
146 $item[0] = $trans['TranTotal'] * $rate;
147 $rep->TextCol(6, 7, number_format2($item[0], $dec));
148 $item[1] = $trans['Balance'] * $rate;
149 $rep->TextCol(7, 8, number_format2($item[1], $dec));
150 for ($i = 0; $i < 2; $i++)
152 $total[$i] += $item[$i];
153 $grandtotal[$i] += $item[$i];
156 $rep->Line($rep->row - 8);
158 $rep->TextCol(0, 3, _('Total'));
159 for ($i = 0; $i < 2; $i++)
161 $rep->TextCol($i + 6, $i + 7, number_format2($total[$i], $dec));
164 $rep->Line($rep->row - 4);
168 $rep->TextCol(0, 3, _('Grand Total'));
170 for ($i = 0; $i < 2; $i++)
171 $rep->TextCol($i + 6, $i + 7,number_format2($grandtotal[$i], $dec));
172 $rep->Line($rep->row - 4);