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