*** empty log message ***
[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 page(_("Customer Inquiry"), false, false, "", $js);
14
15
16 if (isset($_GET['customer_id']))
17 {
18         $_POST['customer_id'] = $_GET['customer_id'];
19 }
20
21 //------------------------------------------------------------------------------------------------
22
23 start_form();
24
25 if (!isset($_POST['customer_id']))
26         $_POST['customer_id'] = get_global_customer();
27
28 start_table("class='tablestyle_noborder'");
29
30 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
31
32 date_cells(_("From:"), 'TransAfterDate', null, -30);
33 date_cells(_("To:"), 'TransToDate', null, 1);
34
35 if (!isset($_POST['filterType']))
36         $_POST['filterType'] = 0;
37
38 cust_allocations_list_cells(null, 'filterType', $_POST['filterType']);
39
40 submit_cells('Refresh Inquiry', _("Search"));
41
42 end_table();
43
44 end_form();
45
46 set_global_customer($_POST['customer_id']);
47
48 //------------------------------------------------------------------------------------------------
49
50 function display_customer_summary($customer_record)
51 {
52         global $table_style;
53
54         $past1 = get_company_pref('past_due_days');
55         $past2 = 2 * $past1;
56     if ($customer_record["dissallow_invoices"] != 0)
57     {
58         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
59     }
60
61         $nowdue = "1-" . $past1 . " " . _('Days');
62         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
63         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
64         
65     start_table("width=80% $table_style");
66     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
67         $pastdue1, $pastdue2, _("Total Balance"));
68     table_header($th);  
69
70         start_row();
71     label_cell($customer_record["curr_code"]);
72     label_cell($customer_record["terms"]);
73         amount_cell($customer_record["Balance"] - $customer_record["Due"]);
74         amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
75         amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
76         amount_cell($customer_record["Overdue2"]);
77         amount_cell($customer_record["Balance"]);
78         end_row();
79
80         end_table();;
81 }
82
83 //------------------------------------------------------------------------------------------------
84
85 function get_transactions()
86 {
87     $date_after = date2sql($_POST['TransAfterDate']);
88     $date_to = date2sql($_POST['TransToDate']);
89
90     $sql = "SELECT ".TB_PREF."debtor_trans.*,
91                 ".TB_PREF."debtors_master.name AS CustName, ".TB_PREF."debtors_master.curr_code AS CustCurrCode,
92                 (".TB_PREF."debtor_trans.ov_amount + ".TB_PREF."debtor_trans.ov_gst + ".TB_PREF."debtor_trans.ov_freight + ".TB_PREF."debtor_trans.ov_discount)
93                 AS TotalAmount, ".TB_PREF."debtor_trans.alloc AS Allocated,
94                 ((".TB_PREF."debtor_trans.type = 10)
95                         AND ".TB_PREF."debtor_trans.due_date < '" . date2sql(Today()) . "') AS OverDue
96                 FROM ".TB_PREF."debtor_trans, ".TB_PREF."debtors_master
97                 WHERE ".TB_PREF."debtors_master.debtor_no = ".TB_PREF."debtor_trans.debtor_no
98                         AND ".TB_PREF."debtor_trans.tran_date >= '$date_after'
99                         AND ".TB_PREF."debtor_trans.tran_date <= '$date_to'";
100
101         if ($_POST['customer_id'] != reserved_words::get_all())
102                 $sql .= " AND ".TB_PREF."debtor_trans.debtor_no = '" . $_POST['customer_id'] . "'";
103
104         if ($_POST['filterType'] != reserved_words::get_all())
105         {
106                 if ($_POST['filterType'] == '1') 
107                 {
108                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10 OR ".TB_PREF."debtor_trans.type = 1) ";
109                 } 
110                 elseif ($_POST['filterType'] == '2') 
111                 {
112                         $sql .= " AND (".TB_PREF."debtor_trans.type = 10) ";
113                 } 
114                 elseif ($_POST['filterType'] == '3') 
115                 {
116                         $sql .= " AND (".TB_PREF."debtor_trans.type = " . systypes::cust_payment() . " OR ".TB_PREF."debtor_trans.type = 2) ";
117                 } 
118                 elseif ($_POST['filterType'] == '4') 
119                 {
120                         $sql .= " AND ".TB_PREF."debtor_trans.type = 11 ";
121                 }
122
123         if ($_POST['filterType'] == '2') 
124         {
125                 $today =  date2sql(Today());
126                 $sql .= " AND ".TB_PREF."debtor_trans.due_date < '$today'
127                                 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) ";
128         }
129         }
130
131     $sql .= " ORDER BY ".TB_PREF."debtor_trans.tran_date";
132
133     return db_query($sql,"No transactions were returned");
134 }
135
136 //------------------------------------------------------------------------------------------------
137
138 if (($_POST['customer_id'] != "") && ($_POST['customer_id'] != reserved_words::get_all()))
139 {
140         $customer_record = get_customer_details($_POST['customer_id']);
141     display_customer_summary($customer_record);
142     echo "<br>";
143 }
144
145 //------------------------------------------------------------------------------------------------
146
147 $result = get_transactions();
148
149 if (db_num_rows($result) == 0)
150 {
151         display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
152         end_page();
153         exit;
154 }
155
156 //------------------------------------------------------------------------------------------------
157
158 start_table("$table_style width='80%'");
159
160 if ($_POST['customer_id'] == reserved_words::get_all())
161         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
162                 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "");
163 else            
164         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
165                 _("Branch"), _("Debit"), _("Credit"), "", "");
166 table_header($th);
167
168
169 $j = 1;
170 $k = 0; //row colour counter
171 $over_due = false;
172 while ($myrow = db_fetch($result)) 
173 {
174
175         if ($myrow['OverDue'] == 1)
176         {
177                 start_row("class='overduebg'");
178                 $over_due = true;
179         } 
180         else
181                 alt_table_row_color($k);
182
183         $date = sql2date($myrow["tran_date"]);
184
185         if ($myrow["order_"] > 0)
186                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
187         else
188                 $preview_order_str = "";
189
190         $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
191
192         $credit_me_str = "";
193
194         $credit_invoice_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
195
196         $due_date_str = "";
197
198         if ($myrow["type"] == 10)
199                 $due_date_str = sql2date($myrow["due_date"]);
200
201         if ($myrow["type"] == 10)
202         {
203                 /*Show a link to allow an invoice to be credited */
204                 // only allow crediting if it's not been totally allocated
205                 if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
206                         $credit_me_str = $credit_invoice_str;
207         }
208
209         $branch_name = "";
210         if ($myrow["branch_code"] > 0) 
211         {
212                 $branch_name = get_branch_name($myrow["branch_code"]);
213         }
214
215         $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
216
217         label_cell(systypes::name($myrow["type"]));
218
219         label_cell($preview_trans_str);
220         label_cell($preview_order_str);
221         label_cell($myrow["reference"]);
222         label_cell($date, "nowrap");
223         label_cell($due_date_str, "nowrap");
224         if ($_POST['customer_id'] == reserved_words::get_all())
225                 label_cell($myrow["CustName"]);
226         label_cell($branch_name);
227         if ($_POST['customer_id'] == reserved_words::get_all())
228                 label_cell($myrow["CustCurrCode"]);
229         display_debit_or_credit_cells($myrow["TotalAmount"]);
230         echo $gl_trans_str;
231         if ($credit_me_str != "")
232                 label_cell($credit_me_str, "nowrap");
233
234
235         end_row();
236
237         $j++;
238         If ($j == 12)
239         {
240                 $j = 1;
241                 table_header($th);
242         }
243 //end of page full new headings if
244 }
245 //end of while loop
246
247 end_table(1);
248
249 if ($over_due)
250         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
251         
252 end_page();
253
254 ?>