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