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);
14 $js .= get_js_date_picker();
15 page(_("Customer Inquiry"), false, false, "", $js);
18 if (isset($_GET['customer_id']))
20 $_POST['customer_id'] = $_GET['customer_id'];
23 //------------------------------------------------------------------------------------------------
27 if (!isset($_POST['customer_id']))
28 $_POST['customer_id'] = get_global_customer();
30 start_table("class='tablestyle_noborder'");
32 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
34 date_cells(_("From:"), 'TransAfterDate', null, -30);
35 date_cells(_("To:"), 'TransToDate', null, 1);
37 if (!isset($_POST['filterType']))
38 $_POST['filterType'] = 0;
40 cust_allocations_list_cells(null, 'filterType', $_POST['filterType']);
42 submit_cells('Refresh Inquiry', _("Search"));
48 set_global_customer($_POST['customer_id']);
50 //------------------------------------------------------------------------------------------------
52 function display_customer_summary($customer_record)
56 $past1 = get_company_pref('past_due_days');
58 if ($customer_record["dissallow_invoices"] != 0)
60 echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
63 $nowdue = "1-" . $past1 . " " . _('Days');
64 $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
65 $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
67 start_table("width=80% $table_style");
68 $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
69 $pastdue1, $pastdue2, _("Total Balance"));
73 label_cell($customer_record["curr_code"]);
74 label_cell($customer_record["terms"]);
75 amount_cell($customer_record["Balance"] - $customer_record["Due"]);
76 amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
77 amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
78 amount_cell($customer_record["Overdue2"]);
79 amount_cell($customer_record["Balance"]);
85 //------------------------------------------------------------------------------------------------
87 function get_transactions()
89 $date_after = date2sql($_POST['TransAfterDate']);
90 $date_to = date2sql($_POST['TransToDate']);
92 $sql = "SELECT ".TB_PREF."debtor_trans.*,
93 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
94 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount)
95 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
96 ((".TB_PREF."debtor_trans.type = 10)
97 AND (".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "')
98 AND ((".TB_PREF."debtor_trans.ov_amount + "
99 .TB_PREF."debtor_trans.ov_gst + "
100 .TB_PREF."debtor_trans.ov_freight )>".TB_PREF."debtor_trans.alloc
102 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
103 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
104 AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
105 AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
107 if ($_POST['customer_id'] != reserved_words::get_all())
108 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
110 if ($_POST['filterType'] != reserved_words::get_all())
112 if ($_POST['filterType'] == '1')
114 $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
116 elseif ($_POST['filterType'] == '2')
118 $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
120 elseif ($_POST['filterType'] == '3')
122 $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
124 elseif ($_POST['filterType'] == '4')
126 $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
129 if ($_POST['filterType'] == '2')
131 $today = date2sql(Today());
132 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
133 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) ";
137 $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
139 return db_query($sql,"No transactions were returned");
142 //------------------------------------------------------------------------------------------------
144 if (($_POST['customer_id'] != "") && ($_POST['customer_id'] != reserved_words::get_all()))
146 $customer_record = get_customer_details($_POST['customer_id']);
147 display_customer_summary($customer_record);
151 //------------------------------------------------------------------------------------------------
153 $result = get_transactions();
155 if (db_num_rows($result) == 0)
157 display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
162 //------------------------------------------------------------------------------------------------
164 start_table("$table_style width='80%'");
166 if ($_POST['customer_id'] == reserved_words::get_all())
167 $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
168 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "");
170 $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
171 _("Branch"), _("Debit"), _("Credit"), "", "");
176 $k = 0; //row colour counter
178 while ($myrow = db_fetch($result))
181 if ($myrow['OverDue'] == 1)
183 start_row("class='overduebg'");
187 alt_table_row_color($k);
189 $date = sql2date($myrow["tran_date"]);
191 if ($myrow["order_"] > 0)
192 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
194 $preview_order_str = "";
196 $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
200 $credit_invoice_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
204 if ($myrow["type"] == 10)
205 $due_date_str = sql2date($myrow["due_date"]);
207 if ($myrow["type"] == 10)
209 /*Show a link to allow an invoice to be credited */
210 // only allow crediting if it's not been totally allocated
211 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
212 $credit_me_str = $credit_invoice_str;
216 if ($myrow["branch_code"] > 0)
218 $branch_name = get_branch_name($myrow["branch_code"]);
221 $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
223 label_cell(systypes::name($myrow["type"]));
225 label_cell($preview_trans_str);
226 label_cell($preview_order_str);
227 label_cell($myrow["reference"]);
228 label_cell($date, "nowrap");
229 label_cell($due_date_str, "nowrap");
230 if ($_POST['customer_id'] == reserved_words::get_all())
231 label_cell($myrow["CustName"]);
232 label_cell($branch_name);
233 if ($_POST['customer_id'] == reserved_words::get_all())
234 label_cell($myrow["CustCurrCode"]);
235 display_debit_or_credit_cells($myrow["TotalAmount"]);
237 if ($credit_me_str != "")
238 label_cell($credit_me_str, "nowrap");
249 //end of page full new headings if
256 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");