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