Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[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()) . "') AS OverDue
98                 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
99                 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
100                         AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
101                         AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
102
103         if ($_POST['customer_id'] != reserved_words::get_all())
104                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
105
106         if ($_POST['filterType'] != reserved_words::get_all())
107         {
108                 if ($_POST['filterType'] == '1') 
109                 {
110                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
111                 } 
112                 elseif ($_POST['filterType'] == '2') 
113                 {
114                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
115                 } 
116                 elseif ($_POST['filterType'] == '3') 
117                 {
118                         $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
119                 } 
120                 elseif ($_POST['filterType'] == '4') 
121                 {
122                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
123                 }
124
125         if ($_POST['filterType'] == '2') 
126         {
127                 $today =  date2sql(Today());
128                 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
129                                 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) ";
130         }
131         }
132
133     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
134
135     return db_query($sql,"No transactions were returned");
136 }
137
138 //------------------------------------------------------------------------------------------------
139
140 if (($_POST['customer_id'] != "") && ($_POST['customer_id'] != reserved_words::get_all()))
141 {
142         $customer_record = get_customer_details($_POST['customer_id']);
143     display_customer_summary($customer_record);
144     echo "<br>";
145 }
146
147 //------------------------------------------------------------------------------------------------
148
149 $result = get_transactions();
150
151 if (db_num_rows($result) == 0)
152 {
153         display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
154         end_page();
155         exit;
156 }
157
158 //------------------------------------------------------------------------------------------------
159
160 start_table("$table_style width='80%'");
161
162 if ($_POST['customer_id'] == reserved_words::get_all())
163         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
164                 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "");
165 else            
166         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
167                 _("Branch"), _("Debit"), _("Credit"), "", "");
168 table_header($th);
169
170
171 $j = 1;
172 $k = 0; //row colour counter
173 $over_due = false;
174 while ($myrow = db_fetch($result)) 
175 {
176
177         if ($myrow['OverDue'] == 1)
178         {
179                 start_row("class='overduebg'");
180                 $over_due = true;
181         } 
182         else
183                 alt_table_row_color($k);
184
185         $date = sql2date($myrow["tran_date"]);
186
187         if ($myrow["order_"] > 0)
188                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
189         else
190                 $preview_order_str = "";
191
192         $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
193
194         $credit_me_str = "";
195
196         $credit_invoice_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
197
198         $due_date_str = "";
199
200         if ($myrow["type"] == 10)
201                 $due_date_str = sql2date($myrow["due_date"]);
202
203         if ($myrow["type"] == 10)
204         {
205                 /*Show a link to allow an invoice to be credited */
206                 // only allow crediting if it's not been totally allocated
207                 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
208                         $credit_me_str = $credit_invoice_str;
209         }
210
211         $branch_name = "";
212         if ($myrow["branch_code"] > 0) 
213         {
214                 $branch_name = get_branch_name($myrow["branch_code"]);
215         }
216
217         $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
218
219         label_cell(systypes::name($myrow["type"]));
220
221         label_cell($preview_trans_str);
222         label_cell($preview_order_str);
223         label_cell($myrow["reference"]);
224         label_cell($date, "nowrap");
225         label_cell($due_date_str, "nowrap");
226         if ($_POST['customer_id'] == reserved_words::get_all())
227                 label_cell($myrow["CustName"]);
228         label_cell($branch_name);
229         if ($_POST['customer_id'] == reserved_words::get_all())
230                 label_cell($myrow["CustCurrCode"]);
231         display_debit_or_credit_cells($myrow["TotalAmount"]);
232         echo $gl_trans_str;
233         if ($credit_me_str != "")
234                 label_cell($credit_me_str, "nowrap");
235
236
237         end_row();
238
239         $j++;
240         If ($j == 12)
241         {
242                 $j = 1;
243                 table_header($th);
244         }
245 //end of page full new headings if
246 }
247 //end of while loop
248
249 end_table(1);
250
251 if ($over_due)
252         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
253         
254 end_page();
255
256 ?>