Cleanup, $SysPrefs.
[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 ($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 if (isset($_GET['customer_id']))
29 {
30         $_POST['customer_id'] = $_GET['customer_id'];
31 }
32
33 //------------------------------------------------------------------------------------------------
34
35 start_form();
36
37 if (!isset($_POST['customer_id']))
38         $_POST['customer_id'] = get_global_customer();
39
40 start_table(TABLESTYLE_NOBORDER);
41 start_row();
42
43 if (!$page_nested)
44         customer_list_cells(_("Select a customer: "), 'customer_id', null, true, false, false, !@$_GET['popup']);
45
46 date_cells(_("From:"), 'TransAfterDate', '', null, -$_SESSION["wa_current_user"]->prefs->transaction_days());
47 date_cells(_("To:"), 'TransToDate', '', null, 1);
48
49 if (!isset($_POST['filterType']))
50         $_POST['filterType'] = 0;
51
52 cust_allocations_list_cells(null, 'filterType', $_POST['filterType'], true);
53
54 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
55 end_row();
56 end_table();
57
58 set_global_customer($_POST['customer_id']);
59
60 //------------------------------------------------------------------------------------------------
61
62 function display_customer_summary($customer_record)
63 {
64         $past1 = get_company_pref('past_due_days');
65         $past2 = 2 * $past1;
66     if ($customer_record["dissallow_invoices"] != 0)
67     {
68         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
69     }
70
71         $nowdue = "1-" . $past1 . " " . _('Days');
72         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
73         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
74
75     start_table(TABLESTYLE, "width=80%");
76     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
77         $pastdue1, $pastdue2, _("Total Balance"));
78     table_header($th);
79
80         start_row();
81     label_cell($customer_record["curr_code"]);
82     label_cell($customer_record["terms"]);
83         amount_cell($customer_record["Balance"] - $customer_record["Due"]);
84         amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
85         amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
86         amount_cell($customer_record["Overdue2"]);
87         amount_cell($customer_record["Balance"]);
88         end_row();
89
90         end_table();
91 }
92 //------------------------------------------------------------------------------------------------
93
94 div_start('totals_tbl');
95 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != ALL_TEXT)
96 {
97         $customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
98     display_customer_summary($customer_record);
99     echo "<br>";
100 }
101 div_end();
102
103 if(get_post('RefreshInquiry'))
104 {
105         $Ajax->activate('totals_tbl');
106 }
107 //------------------------------------------------------------------------------------------------
108
109 function systype_name($dummy, $type)
110 {
111         global $systypes_array;
112
113         return $systypes_array[$type];
114 }
115
116 function order_view($row)
117 {
118         return $row['order_']>0 ?
119                 get_customer_trans_view_str(ST_SALESORDER, $row['order_'])
120                 : "";
121 }
122
123 function trans_view($trans)
124 {
125         return get_trans_view_str($trans["type"], $trans["trans_no"]);
126 }
127
128 function due_date($row)
129 {
130         return  $row["type"] == ST_SALESINVOICE ? $row["due_date"] : '';
131 }
132
133 function gl_view($row)
134 {
135         return get_gl_view_str($row["type"], $row["trans_no"]);
136 }
137
138 function fmt_debit($row)
139 {
140         $value =
141             $row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT ?
142                 -$row["TotalAmount"] : $row["TotalAmount"];
143         return $value>=0 ? price_format($value) : '';
144
145 }
146
147 function fmt_credit($row)
148 {
149         $value =
150             !($row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT) ?
151                 -$row["TotalAmount"] : $row["TotalAmount"];
152         return $value>0 ? price_format($value) : '';
153 }
154
155 function credit_link($row)
156 {
157         global $page_nested;
158         
159         if ($page_nested)
160                 return '';
161         return $row['type'] == ST_SALESINVOICE && $row["Outstanding"] > 0 ?
162                 pager_link(_("Credit This") ,
163                         "/sales/customer_credit_invoice.php?InvoiceNumber=". $row['trans_no'], ICON_CREDIT):'';
164 }
165
166 function edit_link($row)
167 {
168         global $page_nested;
169
170         $str = '';
171         if ($page_nested)
172                 return '';
173
174         return edit_trans_link($row['type'], $row['trans_no'], $row['type']==ST_CUSTCREDIT && $row['order_']==0 ?
175                 "/sales/credit_note_entry.php?ModifyCredit=%d" : 0);
176 }
177
178 function prt_link($row)
179 {
180         if ($row['type'] == ST_CUSTPAYMENT || $row['type'] == ST_BANKDEPOSIT) 
181                 return print_document_link($row['trans_no']."-".$row['type'], _("Print Receipt"), true, ST_CUSTPAYMENT, ICON_PRINT);
182         elseif ($row['type'] == ST_BANKPAYMENT) // bank payment printout not defined yet.
183                 return '';
184         else    
185                 return print_document_link($row['trans_no']."-".$row['type'], _("Print"), true, $row['type'], ICON_PRINT);
186 }
187
188 function check_overdue($row)
189 {
190         return $row['OverDue'] == 1
191                 && floatcmp($row["TotalAmount"], $row["Allocated"]) != 0;
192 }
193 //------------------------------------------------------------------------------------------------
194 $sql = get_sql_for_customer_inquiry(get_post('TransAfterDate'), get_post('TransToDate'),
195         get_post('customer_id'), get_post('filterType'));
196
197 //------------------------------------------------------------------------------------------------
198 db_query("set @bal:=0");
199
200 $cols = array(
201         _("Type") => array('fun'=>'systype_name', 'ord'=>''),
202         _("#") => array('fun'=>'trans_view', 'ord'=>''),
203         _("Order") => array('fun'=>'order_view'), 
204         _("Reference"), 
205         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'desc'),
206         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'),
207         _("Customer") => array('ord'=>''), 
208         _("Branch") => array('ord'=>''), 
209         _("Currency") => array('align'=>'center'),
210         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
211         _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'), 
212         _("RB") => array('align'=>'right', 'type'=>'amount'),
213                 array('insert'=>true, 'fun'=>'gl_view'),
214                 array('insert'=>true, 'fun'=>'credit_link'),
215                 array('insert'=>true, 'fun'=>'edit_link'),
216                 array('insert'=>true, 'fun'=>'prt_link')
217         );
218
219
220 if ($_POST['customer_id'] != ALL_TEXT) {
221         $cols[_("Customer")] = 'skip';
222         $cols[_("Currency")] = 'skip';
223 }
224 if ($_POST['filterType'] == ALL_TEXT)
225         $cols[_("RB")] = 'skip';
226
227 $table =& new_db_pager('trans_tbl', $sql, $cols);
228 $table->set_marker('check_overdue', _("Marked items are overdue."));
229
230 $table->width = "85%";
231
232 display_db_pager($table);
233
234 end_form();
235 end_page();
236 ?>