Bug [0000112] It shows invoices as overdue still (in red), even though its been payed...
[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 include_once($path_to_root . "/reporting/includes/reporting.inc");
10
11 $js = "";
12 if ($use_popup_windows)
13         $js .= get_js_open_window(900, 500);
14 if ($use_date_picker)
15         $js .= get_js_date_picker();
16 page(_("Customer Transactions"), false, false, "", $js);
17
18
19 if (isset($_GET['customer_id']))
20 {
21         $_POST['customer_id'] = $_GET['customer_id'];
22 }
23
24 //------------------------------------------------------------------------------------------------
25
26 start_form();
27
28 if (!isset($_POST['customer_id']))
29         $_POST['customer_id'] = get_global_customer();
30
31 start_table("class='tablestyle_noborder'");
32 start_row();
33
34 customer_list_cells(_("Select a customer: "), 'customer_id', null, true);
35
36 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
37 date_cells(_("To:"), 'TransToDate', '', null, 1);
38
39 if (!isset($_POST['filterType']))
40         $_POST['filterType'] = 0;
41
42 cust_allocations_list_cells(null, 'filterType', $_POST['filterType'], true);
43
44 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), true);
45 end_row();
46 end_table();
47
48 end_form();
49
50 set_global_customer($_POST['customer_id']);
51
52 //------------------------------------------------------------------------------------------------
53
54 function display_customer_summary($customer_record)
55 {
56         global $table_style;
57
58         $past1 = get_company_pref('past_due_days');
59         $past2 = 2 * $past1;
60     if ($customer_record["dissallow_invoices"] != 0)
61     {
62         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
63     }
64
65         $nowdue = "1-" . $past1 . " " . _('Days');
66         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
67         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
68
69     start_table("width=80% $table_style");
70     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
71         $pastdue1, $pastdue2, _("Total Balance"));
72     table_header($th);
73
74         start_row();
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"]);
82         end_row();
83
84         end_table();;
85 }
86
87 //------------------------------------------------------------------------------------------------
88
89 function get_transactions()
90 {
91     $date_after = date2sql($_POST['TransAfterDate']);
92     $date_to = date2sql($_POST['TransToDate']);
93         $dec = user_price_dec();
94         
95     $sql = "SELECT ".TB_PREF."debtor_trans.*,
96                 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
97                 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
98                 .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_discount)
99                 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
100                 ((".TB_PREF."debtor_trans.type = 10)
101                         AND (".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "')
102                         AND ((".TB_PREF."debtor_trans.ov_amount + 
103                                 ROUND(".TB_PREF."debtor_trans.ov_gst,$dec) + "
104                           .TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_freight_tax)>".TB_PREF."debtor_trans.alloc
105                 )) AS OverDue
106                 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
107                 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
108                         AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
109                         AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
110
111         if ($_POST['customer_id'] != reserved_words::get_all())
112                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
113
114         if ($_POST['filterType'] != reserved_words::get_all())
115         {
116                 if ($_POST['filterType'] == '1')
117                 {
118                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
119                 }
120                 elseif ($_POST['filterType'] == '2')
121                 {
122                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
123                 }
124                 elseif ($_POST['filterType'] == '3')
125                 {
126                         $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
127                 }
128                 elseif ($_POST['filterType'] == '4')
129                 {
130                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
131                 }
132                 elseif ($_POST['filterType'] == '5')
133                 {
134                         $sql .= " AND ".TB_PREF."debtor_trans.type = 13 ";
135                 }
136
137         if ($_POST['filterType'] == '2')
138         {
139                 $today =  date2sql(Today());
140                 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
141                                 AND (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + "
142                                 .TB_PREF."debtor_trans.ov_freight_tax + ".TB_PREF."debtor_trans.ov_freight + "
143                                 .TB_PREF."debtor_trans.ov_discount - ".TB_PREF."debtor_trans.alloc > 0) ";
144         }
145         }
146
147     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date DESC,
148           ".TB_PREF."debtor_trans.type,".TB_PREF."debtor_trans.trans_no ";
149
150     return db_query($sql,"No transactions were returned");
151 }
152
153 //------------------------------------------------------------------------------------------------
154
155 div_start('totals_tbl');
156 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != reserved_words::get_all())
157 {
158         $customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
159     display_customer_summary($customer_record);
160     echo "<br>";
161 }
162 div_end();
163 //------------------------------------------------------------------------------------------------
164
165 $result = get_transactions();
166
167 //------------------------------------------------------------------------------------------------
168 if(get_post('RefreshInquiry'))
169 {
170         $Ajax->activate('trans_tbl');
171         $Ajax->activate('totals_tbl');
172 }
173 //------------------------------------------------------------------------------------------------
174 print_hidden_script(10);
175
176 div_start('trans_tbl');
177 if (db_num_rows($result) == 0)
178 {
179         display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
180 } else {
181
182         start_table("$table_style width='80%'");
183
184         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
185                 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "","","");
186
187         if ($_POST['customer_id'] != reserved_words::get_all()) {
188           unset($th[6], $th[8]);
189         }
190
191         table_header($th);
192
193
194         $j = 1;
195         $k = 0; //row colour counter
196         $over_due = false;
197         while ($myrow = db_fetch($result))
198         {
199
200         if ($myrow['OverDue'] == 1)
201         {
202                 start_row("class='overduebg'");
203                 $over_due = true;
204         }
205         else
206                 alt_table_row_color($k);
207
208         $edit_page='';
209         $due_date_str = '';
210         $credit_me_str = '';
211
212         switch($myrow['type']) {
213          case 10:
214                 $due_date_str = sql2date($myrow["due_date"]);
215                         /*Show a link to allow an invoice to be credited */
216                     // only allow crediting if it's not been totally allocated
217                 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
218                         $credit_me_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
219                 if (get_voided_entry(10, $myrow["trans_no"]) === false && $myrow['Allocated'] == 0) // 2008-11-19 Joe Hunt
220                         $edit_page= $path_to_root.'/sales/customer_invoice.php?ModifyInvoice='
221                                         . $myrow['trans_no'];
222                 break;
223
224          case 11:
225                 if (get_voided_entry(11, $myrow["trans_no"]) === false && $myrow['Allocated'] == 0) // 2008-11-19 Joe Hunt
226                 {        
227                         if ($myrow['order_']==0) // free-hand credit note
228                             $edit_page= $path_to_root.'/sales/credit_note_entry.php?ModifyCredit='
229                                         . $myrow['trans_no'];
230                         else    // credit invoice
231                             $edit_page= $path_to_root.'/sales/customer_credit_invoice.php?ModifyCredit='
232                                         . $myrow['trans_no'];
233                 }                       
234                 break;
235
236          case 13:
237                 if (get_voided_entry(13, $myrow["trans_no"]) === false)
238                         $edit_page= $path_to_root.'/sales/customer_delivery.php?ModifyDelivery='
239                                         . $myrow['trans_no']; 
240                 break;
241         }
242
243         $date = sql2date($myrow["tran_date"]);
244
245         if ($myrow["order_"] > 0)
246                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
247         else
248                 $preview_order_str = "";
249
250         $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
251
252         $branch_name = "";
253         if ($myrow["branch_code"] > 0)
254         {
255                 $branch_name = get_branch_name($myrow["branch_code"]);
256         }
257
258         $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
259
260         label_cell(systypes::name($myrow["type"]));
261
262         label_cell($preview_trans_str);
263         label_cell($preview_order_str);
264         label_cell($myrow["reference"]);
265         label_cell($date, "nowrap");
266         label_cell($due_date_str, "nowrap");
267         if ($_POST['customer_id'] == reserved_words::get_all())
268                 label_cell($myrow["CustName"]);
269         label_cell($branch_name);
270         if ($_POST['customer_id'] == reserved_words::get_all())
271                 label_cell($myrow["CustCurrCode"]);
272         display_debit_or_credit_cells(
273             $myrow['type']==11 || $myrow['type']==12 || $myrow['type']==2 ?
274                 -$myrow["TotalAmount"] : $myrow["TotalAmount"]);
275
276         echo $gl_trans_str;
277
278         label_cell($edit_page=='' ? '' :        "<a href='$edit_page'>" . _('Edit') . '</a>');
279         if ($myrow['type'] == 10 || $myrow['type'] == 11)
280                 label_cell(print_document_link($myrow['trans_no'], _("Print"), true, $myrow['type']));
281         else
282                 label_cell("");
283
284         if ($credit_me_str != "")
285                 label_cell($credit_me_str, "nowrap");
286         else
287                 label_cell('');
288         end_row();
289
290                 $j++;
291                 if ($j == 12)
292                 {
293                         $j = 1;
294                         table_header($th);
295                 } //end of page full new headings if
296         } //end of transaction while loop
297
298         end_table(1);
299
300         if ($over_due)
301                 display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
302 }
303 div_end();
304 end_page();
305
306 ?>