Remove test material from source.
[fa-stable.git] / sales / inquiry / customer_inquiry.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_SALESTRANSVIEW';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/db_pager.inc");
15 include_once($path_to_root . "/includes/session.inc");
16
17 include_once($path_to_root . "/sales/includes/sales_ui.inc");
18 include_once($path_to_root . "/sales/includes/sales_db.inc");
19 include_once($path_to_root . "/reporting/includes/reporting.inc");
20
21 $js = "";
22 if ($SysPrefs->use_popup_windows)
23         $js .= get_js_open_window(900, 500);
24 if (user_use_date_picker())
25         $js .= get_js_date_picker();
26 page(_($help_context = "Customer Transactions"), isset($_GET['customer_id']), false, "", $js);
27
28 //------------------------------------------------------------------------------------------------
29
30 function systype_name($dummy, $type)
31 {
32         global $systypes_array;
33
34         return $systypes_array[$type];
35 }
36
37 function order_view($row)
38 {
39         return $row['order_']>0 ?
40                 get_customer_trans_view_str(ST_SALESORDER, $row['order_'])
41                 : "";
42 }
43
44 function trans_view($trans)
45 {
46         return get_trans_view_str($trans["type"], $trans["trans_no"]);
47 }
48
49 function due_date($row)
50 {
51         return  $row["type"] == ST_SALESINVOICE ? $row["due_date"] : '';
52 }
53
54 function gl_view($row)
55 {
56         return get_gl_view_str($row["type"], $row["trans_no"]);
57 }
58
59 function fmt_amount($row)
60 {
61         $value =
62             $row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT ? -$row["TotalAmount"] : $row["TotalAmount"];
63     return price_format($value);
64 }
65
66 function credit_link($row)
67 {
68         global $page_nested;
69
70         if ($page_nested)
71                 return '';
72         if ($row["Outstanding"] > 0)
73         {
74                 if ($row['type'] == ST_CUSTDELIVERY)
75                         return pager_link(_('Invoice'), "/sales/customer_invoice.php?DeliveryNumber=" 
76                                 .$row['trans_no'], ICON_DOC);
77                 else if ($row['type'] == ST_SALESINVOICE)
78                         return pager_link(_("Credit This") ,
79                         "/sales/customer_credit_invoice.php?InvoiceNumber=". $row['trans_no'], ICON_CREDIT);
80         }       
81 }
82
83 function edit_link($row)
84 {
85         global $page_nested;
86
87         if ($page_nested)
88                 return '';
89
90         return $row['type'] == ST_CUSTCREDIT && $row['order_'] ? '' :   // allow  only free hand credit notes edition
91                         trans_editor_link($row['type'], $row['trans_no']);
92 }
93
94 function copy_link($row)
95 {
96     global $page_nested;
97
98     if ($page_nested)
99         return '';
100     if ($row['type'] == ST_CUSTDELIVERY)
101         return pager_link(_("Copy Delivery"), "/sales/sales_order_entry.php?NewDelivery=" 
102             .$row['order_'], ICON_DOC);
103     elseif ($row['type'] == ST_SALESINVOICE)
104         return pager_link(_("Copy Invoice"),    "/sales/sales_order_entry.php?NewInvoice="
105             . $row['order_'], ICON_DOC);
106 }
107
108 function prt_link($row)
109 {
110         if ($row['type'] == ST_CUSTPAYMENT || $row['type'] == ST_BANKDEPOSIT) 
111                 return print_document_link($row['trans_no']."-".$row['type'], _("Print Receipt"), true, ST_CUSTPAYMENT, ICON_PRINT);
112         elseif ($row['type'] == ST_BANKPAYMENT) // bank payment printout not defined yet.
113                 return '';
114         else
115                 return print_document_link($row['trans_no']."-".$row['type'], _("Print"), true, $row['type'], ICON_PRINT);
116 }
117
118 function check_overdue($row)
119 {
120         return $row['OverDue'] == 1
121                 && floatcmp(ABS($row["TotalAmount"]), $row["Allocated"]) != 0;
122 }
123 //------------------------------------------------------------------------------------------------
124
125 function display_customer_summary($customer_record)
126 {
127         $past1 = get_company_pref('past_due_days');
128         $past2 = 2 * $past1;
129     if ($customer_record && $customer_record["dissallow_invoices"] != 0)
130     {
131         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
132     }
133
134         $nowdue = "1-" . $past1 . " " . _('Days');
135         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
136         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
137
138     start_table(TABLESTYLE, "width='80%'");
139     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
140         $pastdue1, $pastdue2, _("Total Balance"));
141     table_header($th);
142     if ($customer_record != false)
143     {
144                 start_row();
145             label_cell($customer_record["curr_code"]);
146             label_cell($customer_record["terms"]);
147                 amount_cell($customer_record["Balance"] - $customer_record["Due"]);
148                 amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
149                 amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
150                 amount_cell($customer_record["Overdue2"]);
151                 amount_cell($customer_record["Balance"]);
152                 end_row();
153         }
154
155         end_table();
156 }
157
158 if (isset($_GET['customer_id']))
159 {
160         $_POST['customer_id'] = $_GET['customer_id'];
161 }
162
163 //------------------------------------------------------------------------------------------------
164
165 start_form();
166
167 if (!isset($_POST['customer_id']))
168         $_POST['customer_id'] = get_global_customer();
169
170 start_table(TABLESTYLE_NOBORDER);
171 start_row();
172
173 ref_cells(_("Reference:"), 'Ref', '', NULL, _('Enter reference fragment or leave empty'));
174
175 if (!$page_nested)
176         customer_list_cells(_("Select a customer: "), 'customer_id', null, true, true, false, true);
177
178 cust_allocations_list_cells(null, 'filterType', null, true, true);
179
180 if ($_POST['filterType'] != '2')
181 {
182         date_cells(_("From:"), 'TransAfterDate', '', null, -user_transaction_days());
183         date_cells(_("To:"), 'TransToDate', '', null);
184 }
185 check_cells(_("Zero values"), 'show_voided');
186
187 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
188 end_row();
189 end_table();
190
191 set_global_customer($_POST['customer_id']);
192
193 //------------------------------------------------------------------------------------------------
194
195 div_start('totals_tbl');
196 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != ALL_TEXT)
197 {
198         $customer_record = get_customer_details(get_post('customer_id'), get_post('TransToDate'), false);
199     display_customer_summary($customer_record);
200     echo "<br>";
201 }
202 div_end();
203
204 if (get_post('RefreshInquiry') || list_updated('filterType'))
205 {
206         $Ajax->activate('_page_body');
207 }
208 //------------------------------------------------------------------------------------------------
209 $sql = get_sql_for_customer_inquiry(get_post('TransAfterDate'), get_post('TransToDate'),
210         get_post('customer_id'), get_post('filterType'), check_value('show_voided'), get_post('Ref'));
211
212 //------------------------------------------------------------------------------------------------
213 //db_query("set @bal:=0");
214
215 $cols = array(
216         _("Type") => array('fun'=>'systype_name', 'ord'=>''),
217         _("#") => array('fun'=>'trans_view', 'ord'=>'', 'align'=>'right'),
218         _("Order") => array('fun'=>'order_view', 'align'=>'right'), 
219         _("Reference"), 
220         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'),
221         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'),
222         _("Customer") => array('ord'=>''), 
223         _("Branch") => array('ord'=>''), 
224         _("Currency") => array('align'=>'center'),
225         _("Amount") => array('align'=>'right', 'fun'=>'fmt_amount'), 
226         _("Balance") => array('align'=>'right', 'type'=>'amount'),
227                 array('insert'=>true, 'fun'=>'gl_view'),
228                 array('insert'=>true, 'fun'=>'edit_link'),
229                 array('insert'=>true, 'fun'=>'copy_link'),
230                 array('insert'=>true, 'fun'=>'credit_link'),
231                 array('insert'=>true, 'fun'=>'prt_link')
232         );
233
234
235 if ($_POST['customer_id'] != ALL_TEXT) {
236         $cols[_("Customer")] = 'skip';
237         $cols[_("Currency")] = 'skip';
238 }
239 if ($_POST['filterType'] != '2')
240         $cols[_("Balance")] = 'skip';
241
242 $table =& new_db_pager('trans_tbl', $sql, $cols);
243 $table->set_marker('check_overdue', _("Marked items are overdue."));
244
245 $table->width = "85%";
246
247 display_db_pager($table);
248
249 end_form();
250 end_page();