5 include_once($path_to_root . "/includes/session.inc");
7 include_once($path_to_root . "/sales/includes/sales_ui.inc");
8 include_once($path_to_root . "/sales/includes/sales_db.inc");
9 include_once($path_to_root . "/reporting/includes/reporting.inc");
12 if ($use_popup_windows)
13 $js .= get_js_open_window(900, 500);
15 $js .= get_js_date_picker();
16 page(_("Customer Transactions"), false, false, "", $js);
19 if (isset($_GET['customer_id']))
21 $_POST['customer_id'] = $_GET['customer_id'];
24 //------------------------------------------------------------------------------------------------
28 if (!isset($_POST['customer_id']))
29 $_POST['customer_id'] = get_global_customer();
31 start_table("class='tablestyle_noborder'");
34 customer_list_cells(_("Select a customer: "), 'customer_id', null, true);
36 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
37 date_cells(_("To:"), 'TransToDate', '', null, 1);
39 if (!isset($_POST['filterType']))
40 $_POST['filterType'] = 0;
42 cust_allocations_list_cells(null, 'filterType', $_POST['filterType'], true);
44 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), true);
50 set_global_customer($_POST['customer_id']);
52 //------------------------------------------------------------------------------------------------
54 function display_customer_summary($customer_record)
58 $past1 = get_company_pref('past_due_days');
60 if ($customer_record["dissallow_invoices"] != 0)
62 echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
65 $nowdue = "1-" . $past1 . " " . _('Days');
66 $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
67 $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
69 start_table("width=80% $table_style");
70 $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
71 $pastdue1, $pastdue2, _("Total Balance"));
75 label_cell($customer_record["curr_code"]);
76 label_cell($customer_record["terms"]);
77 amount_cell($customer_record["Balance"] - $customer_record["Due"]);
78 amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
79 amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
80 amount_cell($customer_record["Overdue2"]);
81 amount_cell($customer_record["Balance"]);
87 //------------------------------------------------------------------------------------------------
89 function get_transactions()
91 $date_after = date2sql($_POST['TransAfterDate']);
92 $date_to = date2sql($_POST['TransToDate']);
94 $sql = "SELECT ".TB_PREF."debtor_trans.*,
95 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
96 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
97 .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)
98 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
99 ((".TB_PREF."debtor_trans.type = 10)
100 AND ".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "') AS OverDue
101 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
102 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
103 AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
104 AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
106 if ($_POST['customer_id'] != reserved_words::get_all())
107 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
109 if ($_POST['filterType'] != reserved_words::get_all())
111 if ($_POST['filterType'] == '1')
113 $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
115 elseif ($_POST['filterType'] == '2')
117 $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
119 elseif ($_POST['filterType'] == '3')
121 $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
123 elseif ($_POST['filterType'] == '4')
125 $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
127 elseif ($_POST['filterType'] == '5')
129 $sql .= " AND ".TB_PREF."debtor_trans.type = 13 ";
132 if ($_POST['filterType'] == '2')
134 $today = date2sql(Today());
135 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
136 AND (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
137 .TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_freight + "
138 .TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc > 0) ";
142 $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date DESC,
143 ".TB_PREF."debtor_trans.type,".TB_PREF."debtor_trans.trans_no ";
145 return db_query($sql,"No transactions were returned");
148 //------------------------------------------------------------------------------------------------
150 div_start('totals_tbl');
151 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != reserved_words::get_all())
153 $customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
154 display_customer_summary($customer_record);
158 //------------------------------------------------------------------------------------------------
160 $result = get_transactions();
162 //------------------------------------------------------------------------------------------------
163 if(get_post('RefreshInquiry'))
165 $Ajax->activate('trans_tbl');
166 $Ajax->activate('totals_tbl');
168 //------------------------------------------------------------------------------------------------
169 print_hidden_script(10);
171 div_start('trans_tbl');
172 if (db_num_rows($result) == 0)
174 display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
177 start_table("$table_style width='80%'");
179 $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
180 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "","","");
182 if ($_POST['customer_id'] != reserved_words::get_all()) {
183 unset($th[6], $th[8]);
190 $k = 0; //row colour counter
192 while ($myrow = db_fetch($result))
195 if ($myrow['OverDue'] == 1)
197 start_row("class='overduebg'");
201 alt_table_row_color($k);
207 switch($myrow['type']) {
209 $due_date_str = sql2date($myrow["due_date"]);
210 /*Show a link to allow an invoice to be credited */
211 // only allow crediting if it's not been totally allocated
212 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
213 $credit_me_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
214 $edit_page= $path_to_root.'/sales/customer_invoice.php?ModifyInvoice='
215 . $myrow['trans_no'];
219 if ($myrow['order_']==0) // free-hand credit note
220 $edit_page= $path_to_root.'/sales/credit_note_entry.php?ModifyCredit='
221 . $myrow['trans_no'];
222 else // credit invoice
223 $edit_page= $path_to_root.'/sales/customer_credit_invoice.php?ModifyCredit='
224 . $myrow['trans_no'];
228 $edit_page= $path_to_root.'/sales/customer_delivery.php?ModifyDelivery='
229 . $myrow['trans_no']; break;
232 $date = sql2date($myrow["tran_date"]);
234 if ($myrow["order_"] > 0)
235 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
237 $preview_order_str = "";
239 $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
242 if ($myrow["branch_code"] > 0)
244 $branch_name = get_branch_name($myrow["branch_code"]);
247 $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
249 label_cell(systypes::name($myrow["type"]));
251 label_cell($preview_trans_str);
252 label_cell($preview_order_str);
253 label_cell($myrow["reference"]);
254 label_cell($date, "nowrap");
255 label_cell($due_date_str, "nowrap");
256 if ($_POST['customer_id'] == reserved_words::get_all())
257 label_cell($myrow["CustName"]);
258 label_cell($branch_name);
259 if ($_POST['customer_id'] == reserved_words::get_all())
260 label_cell($myrow["CustCurrCode"]);
261 display_debit_or_credit_cells(
262 $myrow['type']==11 || $myrow['type']==12 || $myrow['type']==2 ?
263 -$myrow["TotalAmount"] : $myrow["TotalAmount"]);
267 label_cell($edit_page=='' ? '' : "<a href='$edit_page'>" . _('Edit') . '</a>');
268 if ($myrow['type'] == 10 || $myrow['type'] == 11)
269 label_cell(print_document_link($myrow['trans_no'], _("Print"), true, $myrow['type']));
273 if ($credit_me_str != "")
274 label_cell($credit_me_str, "nowrap");
284 } //end of page full new headings if
285 } //end of transaction while loop
290 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");