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 Allocation Inquiry"), false, false, "", $js);
17 if (isset($_GET['customer_id']))
19 $_POST['customer_id'] = $_GET['customer_id'];
22 //------------------------------------------------------------------------------------------------
24 if (!isset($_POST['customer_id']))
25 $_POST['customer_id'] = get_global_customer();
27 start_form(false, true);
29 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 cust_allocations_list_cells(_("Type:"), 'filterType', null);
39 check_cells(" " . _("show settled:"), 'showSettled', null);
41 submit_cells('Refresh Inquiry', _("Search"));
43 set_global_customer($_POST['customer_id']);
49 //------------------------------------------------------------------------------------------------
51 function get_transactions()
53 $data_after = date2sql($_POST['TransAfterDate']);
54 $date_to = date2sql($_POST['TransToDate']);
56 $sql = "SELECT ".TB_PREF."debtor_trans.*,
57 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
58 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount)
60 ".TB_PREF."debtor_trans.alloc AS Allocated,
61 ((".TB_PREF."debtor_trans.type = 10)
62 AND ".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "') AS OverDue
63 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
64 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
65 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 != 0)
66 AND ".TB_PREF."debtor_trans.tran_date >= '$data_after'
67 AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
69 if ($_POST['customer_id'] != reserved_words::get_all())
70 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
72 if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
74 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
76 $sql .= " AND ".TB_PREF."debtor_trans.type = 10 ";
78 elseif ($_POST['filterType'] == '3')
80 $sql .= " AND ".TB_PREF."debtor_trans.type = " . systypes::cust_payment();
82 elseif ($_POST['filterType'] == '4')
84 $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
87 if ($_POST['filterType'] == '2')
89 $today = date2sql(Today());
90 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
91 AND (round(abs(".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,6) > 0) ";
95 $sql .= " AND ".TB_PREF."debtor_trans.type != 13 ";
99 if (!check_value('showSettled'))
101 $sql .= " AND (round(abs(".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,6) != 0) ";
104 $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
106 return db_query($sql,"No transactions were returned");
109 //------------------------------------------------------------------------------------------------
111 $result = get_transactions();
113 if (db_num_rows($result) == 0)
115 display_note(_("The selected customer has no transactions for the given dates."), 1, 1);
120 //------------------------------------------------------------------------------------------------
122 start_table("$table_style width='80%'");
124 if ($_POST['customer_id'] == reserved_words::get_all())
125 $th = array(_("Type"), _("Number"), _("Reference"), _("Order"), _("Date"), _("Due Date"),
126 _("Customer"), _("Currency"), _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
128 $th = array(_("Type"), _("Number"), _("Reference"), _("Order"), _("Date"), _("Due Date"),
129 _("Debit"), _("Credit"), _("Allocated"), _("Balance"), "");
135 $k = 0; //row colour counter
137 while ($myrow = db_fetch($result))
140 if ($myrow['OverDue'] == 1 && (abs($myrow["TotalAmount"]) - $myrow["Allocated"] != 0))
142 start_row("class='overduebg'");
146 alt_table_row_color($k);
148 $date = sql2date($myrow["tran_date"]);
150 if ($myrow["order_"] > 0)
151 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
153 $preview_order_str = "";
155 $allocations_str = "";
157 $allocations = "<a href='$path_to_root/sales/allocations/customer_allocate.php?trans_no=" . $myrow["trans_no"] ."&trans_type=" . $myrow["type"] ."'>" . _("Allocation") . "</a>";
161 if ($myrow["type"] == 10)
162 $due_date_str = sql2date($myrow["due_date"]);
163 elseif ($myrow["type"] == 11 && $myrow['TotalAmount'] < 0)
165 /*its a credit note which could have an allocation */
166 $allocations_str = $allocations;
169 elseif ($myrow["type"] == systypes::cust_payment() &&
170 ($myrow['TotalAmount'] + $myrow['Allocated']) < 0)
172 /*its a receipt which could have an allocation*/
173 $allocations_str = $allocations;
176 elseif ($myrow["type"] == systypes::cust_payment() && $myrow['TotalAmount'] > 0)
178 /*its a negative receipt */
181 label_cell(systypes::name($myrow["type"]));
183 label_cell(get_customer_trans_view_str($myrow["type"], $myrow["trans_no"]));
184 label_cell($myrow["reference"]);
185 label_cell($preview_order_str);
186 label_cell(sql2date($myrow["tran_date"]), "nowrap");
187 label_cell($due_date_str, "nowrap");
188 if ($_POST['customer_id'] == reserved_words::get_all())
190 label_cell($myrow["CustName"]);
191 label_cell($myrow["CustCurrCode"]);
193 display_debit_or_credit_cells($myrow["TotalAmount"]);
194 amount_cell(abs($myrow["Allocated"]));
195 amount_cell(abs($myrow["TotalAmount"]) - $myrow["Allocated"]);
196 label_cell($allocations_str);
207 //end of page full new headings if
213 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");