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");
11 if ($use_popup_windows)
12 $js .= get_js_open_window(900, 500);
13 page(_("Customer Inquiry"), false, false, "", $js);
16 if (isset($_GET['customer_id']))
18 $_POST['customer_id'] = $_GET['customer_id'];
21 //------------------------------------------------------------------------------------------------
25 if (!isset($_POST['customer_id']))
26 $_POST['customer_id'] = get_global_customer();
28 start_table("class='tablestyle_noborder'");
30 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
32 date_cells(_("From:"), 'TransAfterDate', null, -30);
33 date_cells(_("To:"), 'TransToDate', null, 1);
35 if (!isset($_POST['filterType']))
36 $_POST['filterType'] = 0;
38 cust_allocations_list_cells(null, 'filterType', $_POST['filterType']);
40 submit_cells('Refresh Inquiry', _("Search"));
46 set_global_customer($_POST['customer_id']);
48 //------------------------------------------------------------------------------------------------
50 function display_customer_summary($customer_record)
54 $past1 = get_company_pref('past_due_days');
56 if ($customer_record["dissallow_invoices"] != 0)
58 echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
61 $nowdue = "1-" . $past1 . " " . _('Days');
62 $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
63 $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
65 start_table("width=80% $table_style");
66 $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
67 $pastdue1, $pastdue2, _("Total Balance"));
71 label_cell($customer_record["curr_code"]);
72 label_cell($customer_record["terms"]);
73 amount_cell($customer_record["Balance"] - $customer_record["Due"]);
74 amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
75 amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
76 amount_cell($customer_record["Overdue2"]);
77 amount_cell($customer_record["Balance"]);
83 //------------------------------------------------------------------------------------------------
85 function get_transactions()
87 $date_after = date2sql($_POST['TransAfterDate']);
88 $date_to = date2sql($_POST['TransToDate']);
90 $sql = "SELECT ".TB_PREF."debtor_trans.*,
91 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
92 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount)
93 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
94 ((".TB_PREF."debtor_trans.type = 10)
95 AND ".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "') AS OverDue
96 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
97 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
98 AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
99 AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
101 if ($_POST['customer_id'] != reserved_words::get_all())
102 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
104 if ($_POST['filterType'] != reserved_words::get_all())
106 if ($_POST['filterType'] == '1')
108 $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
110 elseif ($_POST['filterType'] == '2')
112 $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
114 elseif ($_POST['filterType'] == '3')
116 $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
118 elseif ($_POST['filterType'] == '4')
120 $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
123 if ($_POST['filterType'] == '2')
125 $today = date2sql(Today());
126 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
127 AND (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc > 0) ";
131 $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
133 return db_query($sql,"No transactions were returned");
136 //------------------------------------------------------------------------------------------------
138 if (($_POST['customer_id'] != "") && ($_POST['customer_id'] != reserved_words::get_all()))
140 $customer_record = get_customer_details($_POST['customer_id']);
141 display_customer_summary($customer_record);
145 //------------------------------------------------------------------------------------------------
147 $result = get_transactions();
149 if (db_num_rows($result) == 0)
151 display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
156 //------------------------------------------------------------------------------------------------
158 start_table("$table_style width='80%'");
160 if ($_POST['customer_id'] == reserved_words::get_all())
161 $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
162 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "");
164 $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
165 _("Branch"), _("Debit"), _("Credit"), "", "");
170 $k = 0; //row colour counter
172 while ($myrow = db_fetch($result))
175 if ($myrow['OverDue'] == 1)
177 start_row("class='overduebg'");
181 alt_table_row_color($k);
183 $date = sql2date($myrow["tran_date"]);
185 if ($myrow["order_"] > 0)
186 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
188 $preview_order_str = "";
190 $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
194 $credit_invoice_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
198 if ($myrow["type"] == 10)
199 $due_date_str = sql2date($myrow["due_date"]);
201 if ($myrow["type"] == 10)
203 /*Show a link to allow an invoice to be credited */
204 // only allow crediting if it's not been totally allocated
205 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
206 $credit_me_str = $credit_invoice_str;
210 if ($myrow["branch_code"] > 0)
212 $branch_name = get_branch_name($myrow["branch_code"]);
215 $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
217 label_cell(systypes::name($myrow["type"]));
219 label_cell($preview_trans_str);
220 label_cell($preview_order_str);
221 label_cell($myrow["reference"]);
222 label_cell($date, "nowrap");
223 label_cell($due_date_str, "nowrap");
224 if ($_POST['customer_id'] == reserved_words::get_all())
225 label_cell($myrow["CustName"]);
226 label_cell($branch_name);
227 if ($_POST['customer_id'] == reserved_words::get_all())
228 label_cell($myrow["CustCurrCode"]);
229 display_debit_or_credit_cells($myrow["TotalAmount"]);
231 if ($credit_me_str != "")
232 label_cell($credit_me_str, "nowrap");
243 //end of page full new headings if
250 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");