Huge sales module changes toward delivery and invoicing separation. Includes some...
[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 Transactions"), 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'], true);
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                 elseif ($_POST['filterType'] == '5') 
125                 {
126                         $sql .= " AND ".TB_PREF."debtor_trans.type = 13 ";
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 DESC, 
138           debtor_trans.type,debtor_trans.trans_no ";
139
140     return db_query($sql,"No transactions were returned");
141 }
142
143 //------------------------------------------------------------------------------------------------
144
145 if (($_POST['customer_id'] != "") && ($_POST['customer_id'] != reserved_words::get_all()))
146 {
147         $customer_record = get_customer_details($_POST['customer_id']);
148     display_customer_summary($customer_record);
149     echo "<br>";
150 }
151
152 //------------------------------------------------------------------------------------------------
153
154 $result = get_transactions();
155
156 if (db_num_rows($result) == 0)
157 {
158         display_note(_("The selected customer has no transactions for the given dates."), 0, 2);
159         end_page();
160         exit;
161 }
162
163 //------------------------------------------------------------------------------------------------
164
165 start_table("$table_style width='80%'");
166
167 if ($_POST['customer_id'] == reserved_words::get_all())
168         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
169                 _("Customer"), _("Branch"), _("Currency"), _("Debit"), _("Credit"), "", "","");
170 else            
171         $th = array(_("Type"), _("#"), _("Order"), _("Reference"), _("Date"), _("Due Date"),
172                 _("Branch"), _("Debit"), _("Credit"), "", "","");
173 table_header($th);
174
175
176 $j = 1;
177 $k = 0; //row colour counter
178 $over_due = false;
179 while ($myrow = db_fetch($result)) 
180 {
181
182         if ($myrow['OverDue'] == 1)
183         {
184                 start_row("class='overduebg'");
185                 $over_due = true;
186         } 
187         else
188                 alt_table_row_color($k);
189
190         $edit_page='';
191         $due_date_str = '';
192         $credit_me_str = '';
193
194         switch($myrow['type']) {
195          case 10:
196                         $due_date_str = sql2date($myrow["due_date"]);
197                         /*Show a link to allow an invoice to be credited */
198                         // only allow crediting if it's not been totally allocated
199                         if ($myrow["TotalAmount"] - $myrow["Allocated"] > 0)
200                                 $credit_me_str = "<a href='$path_to_root/sales/customer_credit_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Credit This") . "</a>";
201                   $edit_page= $path_to_root.'/sales/customer_invoice.php?ModifyInvoice='
202                                         . $myrow['trans_no']; 
203                         break;
204          case 13:
205                 $edit_page= $path_to_root.'/sales/customer_delivery.php?ModifyDelivery='
206                                         . $myrow['trans_no']; break;
207         }
208
209         $date = sql2date($myrow["tran_date"]);
210
211         if ($myrow["order_"] > 0)
212                 $preview_order_str = get_customer_trans_view_str(systypes::sales_order(), $myrow["order_"]);
213         else
214                 $preview_order_str = "";
215
216         $gl_trans_str = get_gl_view_str_cell($myrow["type"], $myrow["trans_no"]);
217
218 //      $print_str = "<a href='$path_to_root/reporting/print_invoice.php?InvoiceNumber=" . $myrow["trans_no"] . "'>" . _("Print") . "</a>";
219
220
221         $branch_name = "";
222         if ($myrow["branch_code"] > 0) 
223         {
224                 $branch_name = get_branch_name($myrow["branch_code"]);
225         }
226
227         $preview_trans_str = get_trans_view_str($myrow["type"], $myrow["trans_no"]);
228
229         label_cell(systypes::name($myrow["type"]));
230
231         label_cell($preview_trans_str);
232         label_cell($preview_order_str);
233         label_cell($myrow["reference"]);
234         label_cell($date, "nowrap");
235         label_cell($due_date_str, "nowrap");
236         if ($_POST['customer_id'] == reserved_words::get_all())
237                 label_cell($myrow["CustName"]);
238         label_cell($branch_name);
239         if ($_POST['customer_id'] == reserved_words::get_all())
240                 label_cell($myrow["CustCurrCode"]);
241         display_debit_or_credit_cells($myrow["TotalAmount"]);
242         echo $gl_trans_str;
243
244   label_cell($edit_page=='' ? '' :      "<a href='$edit_page'>" . _('Edit') . '</a>');
245         
246         if ($credit_me_str != "")
247                 label_cell($credit_me_str, "nowrap");
248         else
249                 label_cell('');
250         
251 //      if ($myrow["type"] == 10)
252 //              label_cell($print_str, 'nowrap');
253 //      else
254 //              label_cell('');
255         
256         end_row();
257
258         $j++;
259         if ($j == 12)
260         {
261                 $j = 1;
262                 table_header($th);
263         } //end of page full new headings if
264 } //end of transaction while loop
265
266 end_table(1);
267
268 if ($over_due)
269         display_note(_("Marked items are overdue."), 0, 1, "class='overduefg'");
270         
271 end_page();
272
273 ?>