Fixed bug selecting allocated transactions as overdue.
[fa-stable.git] / sales / inquiry / customer_inquiry.php
1 <?php
2
3 $page_security = 1;
4 $path_to_root="../..";
5 include_once($path_to_root . "/includes/session.inc");
6
7 include_once($path_to_root . "/sales/includes/sales_ui.inc");
8 include_once($path_to_root . "/sales/includes/sales_db.inc");
9
10 $js = "";
11 if ($use_popup_windows)
12         $js .= get_js_open_window(900, 500);
13 if ($use_date_picker)
14         $js .= get_js_date_picker();
15 page(_("Customer Inquiry"), false, false, "", $js);
16
17
18 if (isset($_GET['customer_id']))
19 {
20         $_POST['customer_id'] = $_GET['customer_id'];
21 }
22
23 //------------------------------------------------------------------------------------------------
24
25 start_form();
26
27 if (!isset($_POST['customer_id']))
28         $_POST['customer_id'] = get_global_customer();
29
30 start_table("class='tablestyle_noborder'");
31
32 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
33
34 date_cells(_("From:"), 'TransAfterDate', null, -30);
35 date_cells(_("To:"), 'TransToDate', null, 1);
36
37 if (!isset($_POST['filterType']))
38         $_POST['filterType'] = 0;
39
40 cust_allocations_list_cells(null, 'filterType', $_POST['filterType']);
41
42 submit_cells('Refresh Inquiry', _("Search"));
43
44 end_table();
45
46 end_form();
47
48 set_global_customer($_POST['customer_id']);
49
50 //------------------------------------------------------------------------------------------------
51
52 function display_customer_summary($customer_record)
53 {
54         global $table_style;
55
56         $past1 = get_company_pref('past_due_days');
57         $past2 = 2 * $past1;
58     if ($customer_record["dissallow_invoices"] != 0)
59     {
60         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
61     }
62
63         $nowdue = "1-" . $past1 . " " . _('Days');
64         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
65         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
66         
67     start_table("width=80% $table_style");
68     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
69         $pastdue1, $pastdue2, _("Total Balance"));
70     table_header($th);  
71
72         start_row();
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"]);
80         end_row();
81
82         end_table();;
83 }
84
85 //------------------------------------------------------------------------------------------------
86
87 function get_transactions()
88 {
89     $date_after = date2sql($_POST['TransAfterDate']);
90     $date_to = date2sql($_POST['TransToDate']);
91
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
101                 )) AS OverDue
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'";
106
107         if ($_POST['customer_id'] != reserved_words::get_all())
108                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
109
110         if ($_POST['filterType'] != reserved_words::get_all())
111         {
112                 if ($_POST['filterType'] == '1') 
113                 {
114                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
115                 } 
116                 elseif ($_POST['filterType'] == '2') 
117                 {
118                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
119                 } 
120                 elseif ($_POST['filterType'] == '3') 
121                 {
122                         $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
123                 } 
124                 elseif ($_POST['filterType'] == '4') 
125                 {
126                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
127                 }
128
129         if ($_POST['filterType'] == '2') 
130         {
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) ";
134         }
135         }
136
137     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
138
139     return db_query($sql,"No transactions were returned");
140 }
141
142 //------------------------------------------------------------------------------------------------
143
144 if (($_POST['customer_id'] != "") && ($_POST['customer_id'] != reserved_words::get_all()))
145 {
146         $customer_record = get_customer_details($_POST['customer_id']);
147     display_customer_summary($customer_record);
148     echo "<br>";
149 }
150
151 //------------------------------------------------------------------------------------------------
152
153 $result = get_transactions();
154
155 if (db_num_rows($result) == 0)
156 {
157         display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
158         end_page();
159         exit;
160 }
161
162 //------------------------------------------------------------------------------------------------
163
164 start_table("$table_style width='80%'");
165
166 if ($_POST['customer_id'] == reserved_words::get_all())
167         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
168                 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "");
169 else            
170         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
171                 _("Branch"), _("Debit"), _("Credit"), "", "");
172 table_header($th);
173
174
175 $j = 1;
176 $k = 0; //row colour counter
177 $over_due = false;
178 while ($myrow = db_fetch($result)) 
179 {
180
181         if ($myrow['OverDue'] == 1)
182         {
183                 start_row("class='overduebg'");
184                 $over_due = true;
185         } 
186         else
187                 alt_table_row_color($k);
188
189         $date = sql2date($myrow["tran_date"]);
190
191         if ($myrow["order_"] > 0)
192                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
193         else
194                 $preview_order_str = "";
195
196         $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
197
198         $credit_me_str = "";
199
200         $credit_invoice_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
201
202         $due_date_str = "";
203
204         if ($myrow["type"] == 10)
205                 $due_date_str = sql2date($myrow["due_date"]);
206
207         if ($myrow["type"] == 10)
208         {
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;
213         }
214
215         $branch_name = "";
216         if ($myrow["branch_code"] > 0) 
217         {
218                 $branch_name = get_branch_name($myrow["branch_code"]);
219         }
220
221         $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
222
223         label_cell(systypes::name($myrow["type"]));
224
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"]);
236         echo $gl_trans_str;
237         if ($credit_me_str != "")
238                 label_cell($credit_me_str, "nowrap");
239
240
241         end_row();
242
243         $j++;
244         If ($j == 12)
245         {
246                 $j = 1;
247                 table_header($th);
248         }
249 //end of page full new headings if
250 }
251 //end of while loop
252
253 end_table(1);
254
255 if ($over_due)
256         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
257         
258 end_page();
259
260 ?>