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 $path_to_root="../..";
14 include($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
17 include_once($path_to_root . "/sales/includes/sales_ui.inc");
18 include_once($path_to_root . "/sales/includes/sales_db.inc");
19 include_once($path_to_root . "/reporting/includes/reporting.inc");
22 if ($use_popup_windows)
23 $js .= get_js_open_window(900, 500);
25 $js .= get_js_date_picker();
26 page(_("Customer Transactions"), false, false, "", $js);
29 if (isset($_GET['customer_id']))
31 $_POST['customer_id'] = $_GET['customer_id'];
34 //------------------------------------------------------------------------------------------------
38 if (!isset($_POST['customer_id']))
39 $_POST['customer_id'] = get_global_customer();
41 start_table("class='tablestyle_noborder'");
44 customer_list_cells(_("Select a customer: "), 'customer_id', null, true);
46 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
47 date_cells(_("To:"), 'TransToDate', '', null, 1);
49 if (!isset($_POST['filterType']))
50 $_POST['filterType'] = 0;
52 cust_allocations_list_cells(null, 'filterType', $_POST['filterType'], true);
54 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), true);
58 set_global_customer($_POST['customer_id']);
60 //------------------------------------------------------------------------------------------------
62 function display_customer_summary($customer_record)
66 $past1 = get_company_pref('past_due_days');
68 if ($customer_record["dissallow_invoices"] != 0)
70 echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
73 $nowdue = "1-" . $past1 . " " . _('Days');
74 $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
75 $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
77 start_table("width=80% $table_style");
78 $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
79 $pastdue1, $pastdue2, _("Total Balance"));
83 label_cell($customer_record["curr_code"]);
84 label_cell($customer_record["terms"]);
85 amount_cell($customer_record["Balance"] - $customer_record["Due"]);
86 amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
87 amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
88 amount_cell($customer_record["Overdue2"]);
89 amount_cell($customer_record["Balance"]);
94 //------------------------------------------------------------------------------------------------
96 div_start('totals_tbl');
97 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != reserved_words::get_all())
99 $customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
100 display_customer_summary($customer_record);
105 if(get_post('RefreshInquiry'))
107 $Ajax->activate('totals_tbl');
109 //------------------------------------------------------------------------------------------------
111 function systype_name($dummy, $type)
113 return systypes::name($type);
116 function order_view($row)
118 return $row['order_']>0 ?
119 get_customer_trans_view_str(systypes::sales_order(), $row['order_'])
123 function trans_view($trans)
125 return get_trans_view_str($trans["type"], $trans["trans_no"]);
128 function due_date($row)
130 return $row["type"] == 10 ? $row["due_date"] : '';
133 function gl_view($row)
135 return get_gl_view_str($row["type"], $row["trans_no"]);
138 function fmt_debit($row)
141 $row['type']==11 || $row['type']==12 || $row['type']==2 ?
142 -$row["TotalAmount"] : $row["TotalAmount"];
143 return $value>=0 ? price_format($value) : '';
147 function fmt_credit($row)
150 !($row['type']==11 || $row['type']==12 || $row['type']==2) ?
151 -$row["TotalAmount"] : $row["TotalAmount"];
152 return $value>0 ? price_format($value) : '';
155 function credit_link($row)
157 return $row['type'] == 10 && $row["TotalAmount"] - $row["Allocated"] > 0 ?
158 pager_link(_("Credit This"),
159 "/sales/customer_credit_invoice.php?InvoiceNumber=".
160 $row['trans_no'], ICON_CREDIT)
164 function edit_link($row)
168 switch($row['type']) {
170 if (get_voided_entry(10, $row["trans_no"]) === false && $row['Allocated'] == 0)
171 $str = "/sales/customer_invoice.php?ModifyInvoice=".$row['trans_no'];
174 if (get_voided_entry(11, $row["trans_no"]) === false && $row['Allocated'] == 0) // 2008-11-19 Joe Hunt
176 if ($row['order_']==0) // free-hand credit note
177 $str = "/sales/credit_note_entry.php?ModifyCredit=".$row['trans_no'];
178 else // credit invoice
179 $str = "/sales/customer_credit_invoice.php?ModifyCredit=".$row['trans_no'];
183 if (get_voided_entry(13, $row["trans_no"]) === false)
184 $str = "/sales/customer_delivery.php?ModifyDelivery=".$row['trans_no'];
188 return pager_link(_('Edit'), $str, ICON_EDIT);
192 function prt_link($row)
194 if ($row['type'] != 12 && $row['type'] != 2) // customer payment or bank deposit printout not defined yet.
195 return print_document_link($row['trans_no'], _("Print"), true, $row['type'], ICON_PRINT);
198 function check_overdue($row)
200 return $row['OverDue'] == 1
201 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
203 //------------------------------------------------------------------------------------------------
204 $date_after = date2sql($_POST['TransAfterDate']);
205 $date_to = date2sql($_POST['TransToDate']);
217 @bal := @bal+trans.ov_amount,
218 (trans.ov_amount + trans.ov_gst + trans.ov_freight
219 + trans.ov_freight_tax + trans.ov_discount) AS TotalAmount,
220 trans.alloc AS Allocated,
222 AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
224 .TB_PREF."debtor_trans as trans, "
225 .TB_PREF."debtors_master as debtor, "
226 .TB_PREF."cust_branch as branch
227 WHERE debtor.debtor_no = trans.debtor_no
228 AND trans.tran_date >= '$date_after'
229 AND trans.tran_date <= '$date_to'
230 AND trans.branch_code = branch.branch_code";
232 if ($_POST['customer_id'] != reserved_words::get_all())
233 $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
235 if ($_POST['filterType'] != reserved_words::get_all())
237 if ($_POST['filterType'] == '1')
239 $sql .= " AND (trans.type = 10 OR trans.type = 1) ";
241 elseif ($_POST['filterType'] == '2')
243 $sql .= " AND (trans.type = 10) ";
245 elseif ($_POST['filterType'] == '3')
247 $sql .= " AND (trans.type = " . systypes::cust_payment()
248 ." OR trans.type = 2) ";
250 elseif ($_POST['filterType'] == '4')
252 $sql .= " AND trans.type = 11 ";
254 elseif ($_POST['filterType'] == '5')
256 $sql .= " AND trans.type = 13 ";
259 if ($_POST['filterType'] == '2')
261 $today = date2sql(Today());
262 $sql .= " AND trans.due_date < '$today'
263 AND (trans.ov_amount + trans.ov_gst + trans.ov_freight_tax +
264 trans.ov_freight + trans.ov_discount - trans.alloc > 0) ";
268 //------------------------------------------------------------------------------------------------
269 db_query("set @bal:=0");
272 _("Type") => array('fun'=>'systype_name', 'ord'=>''),
273 _("#") => array('fun'=>'trans_view', 'ord'=>''),
274 _("Order") => array('fun'=>'order_view'),
276 _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'),
277 _("Due Date") => array('type'=>'date', 'fun'=>'due_date'),
278 _("Customer") => array('ord'=>''),
279 _("Branch") => array('ord'=>''),
280 _("Currency") => array('align'=>'center'),
282 _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'),
283 _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'),
284 array('insert'=>true, 'fun'=>'gl_view'),
285 array('insert'=>true, 'fun'=>'credit_link'),
286 array('insert'=>true, 'fun'=>'edit_link'),
287 array('insert'=>true, 'fun'=>'prt_link')
290 if ($_POST['customer_id'] != reserved_words::get_all()) {
291 $cols[_("Customer")] = 'skip';
292 $cols[_("Currency")] = 'skip';
296 $table =& new_db_pager('trans_tbl', $sql, $cols);
297 $table->set_marker('check_overdue', _("Marked items are overdue."));
299 if (get_post('RefreshInquiry')) {
300 $table->set_sql($sql);
301 $table->set_columns($cols);
303 $table->width = "85%";
306 display_db_pager($table);