Preparing for Graphic Links final.Optimized.
[fa-stable.git] / sales / inquiry / customer_inquiry.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 1;
13 $path_to_root="../..";
14 include($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 ($use_date_picker)
25         $js .= get_js_date_picker();
26 page(_("Customer Transactions"), false, false, "", $js);
27
28
29 if (isset($_GET['customer_id']))
30 {
31         $_POST['customer_id'] = $_GET['customer_id'];
32 }
33
34 //------------------------------------------------------------------------------------------------
35
36 start_form();
37
38 if (!isset($_POST['customer_id']))
39         $_POST['customer_id'] = get_global_customer();
40
41 start_table("class='tablestyle_noborder'");
42 start_row();
43
44 customer_list_cells(_("Select a customer: "), 'customer_id', null, true);
45
46 date_cells(_("From:"), 'TransAfterDate', '', null, -30);
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'), true);
55 end_row();
56 end_table();
57 end_form();
58 set_global_customer($_POST['customer_id']);
59
60 //------------------------------------------------------------------------------------------------
61
62 function display_customer_summary($customer_record)
63 {
64         global $table_style;
65
66         $past1 = get_company_pref('past_due_days');
67         $past2 = 2 * $past1;
68     if ($customer_record["dissallow_invoices"] != 0)
69     {
70         echo "<center><font color=red size=4><b>" . _("CUSTOMER ACCOUNT IS ON HOLD") . "</font></b></center>";
71     }
72
73         $nowdue = "1-" . $past1 . " " . _('Days');
74         $pastdue1 = $past1 + 1 . "-" . $past2 . " " . _('Days');
75         $pastdue2 = _('Over') . " " . $past2 . " " . _('Days');
76
77     start_table("width=80% $table_style");
78     $th = array(_("Currency"), _("Terms"), _("Current"), $nowdue,
79         $pastdue1, $pastdue2, _("Total Balance"));
80     table_header($th);
81
82         start_row();
83     label_cell($customer_record["curr_code"]);
84     label_cell($customer_record["terms"]);
85         amount_cell($customer_record["Balance"] - $customer_record["Due"]);
86         amount_cell($customer_record["Due"] - $customer_record["Overdue1"]);
87         amount_cell($customer_record["Overdue1"] - $customer_record["Overdue2"]);
88         amount_cell($customer_record["Overdue2"]);
89         amount_cell($customer_record["Balance"]);
90         end_row();
91
92         end_table();
93 }
94 //------------------------------------------------------------------------------------------------
95
96 div_start('totals_tbl');
97 if ($_POST['customer_id'] != "" && $_POST['customer_id'] != reserved_words::get_all())
98 {
99         $customer_record = get_customer_details($_POST['customer_id'], $_POST['TransToDate']);
100     display_customer_summary($customer_record);
101     echo "<br>";
102 }
103 div_end();
104
105 if(get_post('RefreshInquiry'))
106 {
107         $Ajax->activate('totals_tbl');
108 }
109 //------------------------------------------------------------------------------------------------
110
111 function systype_name($dummy, $type)
112 {
113         return systypes::name($type);
114 }
115
116 function order_view($row)
117 {
118         return $row['order_']>0 ?
119                 get_customer_trans_view_str(systypes::sales_order(), $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"]==20 || $row["type"]==21)
131                 ? $row["due_date"] : '';
132 }
133
134 function gl_view($row)
135 {
136         return get_gl_view_str($row["type"], $row["trans_no"]);
137 }
138
139 function fmt_debit($row)
140 {
141         $value =
142             $row['type']==11 || $row['type']==12 || $row['type']==2 ?
143                 -$row["TotalAmount"] : $row["TotalAmount"];
144         return $value>=0 ? price_format($value) : '';
145
146 }
147
148 function fmt_credit($row)
149 {
150         $value =
151             !($row['type']==11 || $row['type']==12 || $row['type']==2) ?
152                 -$row["TotalAmount"] : $row["TotalAmount"];
153         return $value>0 ? price_format($value) : '';
154 }
155
156 function credit_link($row)
157 {
158         return $row['type'] == 10 ?
159                 pager_link(_("Credit This"),
160                         "/sales/customer_credit_invoice.php?InvoiceNumber=".
161                         $row['trans_no'], ICON_CREDIT)
162                         : '';
163 }
164
165 function edit_link($row)
166 {
167         $str = '';
168
169         switch($row['type']) {
170         case 10:
171                 $str = "/sales/customer_invoice.php?ModifyInvoice=".$row['trans_no'];
172                 break;
173         case 11:
174                 if ($row['order_']==0) // free-hand credit note
175                     $str = "/sales/credit_note_entry.php?ModifyCredit=".$row['trans_no'];
176                 else    // credit invoice
177                     $str = "/sales/customer_credit_invoice.php?ModifyCredit=".$row['trans_no'];
178                 break;
179          case 13:
180                 $str = "/sales/customer_delivery.php?ModifyDelivery=".$row['trans_no'];
181                 break;
182         }
183         return pager_link(_('Edit'), $str, ICON_EDIT);
184 }
185
186 function prt_link($row)
187 {
188         if ($row['type'] != 12) // customer payment printout not defined yet.
189                 return print_document_link($row['trans_no'], _("Print"), true, $row['type'], ICON_PRINT);
190 }
191
192 function check_overdue($row)
193 {
194         return $row['OverDue'] == 1
195                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0);
196 }
197 //------------------------------------------------------------------------------------------------
198     $date_after = date2sql($_POST['TransAfterDate']);
199     $date_to = date2sql($_POST['TransToDate']);
200
201   $sql = "SELECT 
202                 trans.type, 
203                 trans.trans_no, 
204                 trans.order_, 
205                 trans.reference,
206                 trans.tran_date, 
207                 trans.due_date, 
208                 debtor.name, 
209                 branch.br_name,
210                 debtor.curr_code,
211                 (trans.ov_amount + trans.ov_gst + trans.ov_freight 
212                         + trans.ov_freight_tax + trans.ov_discount)     AS TotalAmount, 
213                 trans.alloc AS Allocated,
214                 ((trans.type = 10)
215                         AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
216                 FROM "
217                         .TB_PREF."debtor_trans as trans, "
218                         .TB_PREF."debtors_master as debtor, "
219                         .TB_PREF."cust_branch as branch
220                 WHERE debtor.debtor_no = trans.debtor_no
221                         AND trans.tran_date >= '$date_after'
222                         AND trans.tran_date <= '$date_to'
223                         AND trans.branch_code = branch.branch_code";
224
225         if ($_POST['customer_id'] != reserved_words::get_all())
226                 $sql .= " AND trans.debtor_no = '" . $_POST['customer_id'] . "'";
227
228         if ($_POST['filterType'] != reserved_words::get_all())
229         {
230                 if ($_POST['filterType'] == '1')
231                 {
232                         $sql .= " AND (trans.type = 10 OR trans.type = 1) ";
233                 }
234                 elseif ($_POST['filterType'] == '2')
235                 {
236                         $sql .= " AND (trans.type = 10) ";
237                 }
238                 elseif ($_POST['filterType'] == '3')
239                 {
240                         $sql .= " AND (trans.type = " . systypes::cust_payment() 
241                                         ." OR trans.type = 2) ";
242                 }
243                 elseif ($_POST['filterType'] == '4')
244                 {
245                         $sql .= " AND trans.type = 11 ";
246                 }
247                 elseif ($_POST['filterType'] == '5')
248                 {
249                         $sql .= " AND trans.type = 13 ";
250                 }
251
252         if ($_POST['filterType'] == '2')
253         {
254                 $today =  date2sql(Today());
255                 $sql .= " AND trans.due_date < '$today'
256                                 AND (trans.ov_amount + trans.ov_gst + trans.ov_freight_tax + 
257                                 trans.ov_freight + trans.ov_discount - trans.alloc > 0) ";
258         }
259         }
260
261 //------------------------------------------------------------------------------------------------
262
263 $cols = array(
264         _("Type") => array('fun'=>'systype_name', 'ord'=>''),
265         _("#") => array('fun'=>'trans_view', 'ord'=>''),
266         _("Order") => array('fun'=>'order_view'), 
267         _("Reference"), 
268         _("Date") => array('type'=>'date', 'ord'=>'desc'),
269         _("Due Date") => array('type=>date', 'fun'=>'due_date'),
270         _("Customer") => array('ord'=>''), 
271         _("Branch") => array('ord'=>''), 
272         _("Currency") => array('align'=>'center'),
273         _("Debit") => array('align'=>'right', 'fun'=>'fmt_debit'), 
274         _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'), 
275                 array('insert'=>true, 'fun'=>'gl_view'),
276                 array('insert'=>true, 'fun'=>'credit_link'),
277                 array('insert'=>true, 'fun'=>'edit_link'),
278                 array('insert'=>true, 'fun'=>'prt_link')
279         );
280
281 if ($_POST['customer_id'] != reserved_words::get_all()) {
282         $cols[_("Customer")] = 'skip';
283         $cols[_("Currency")] = 'skip';
284 }
285
286
287 $table =& new_db_pager('trans_tbl', $sql, $cols);
288 $table->set_marker('check_overdue', _("Marked items are overdue."));
289
290 if (get_post('RefreshInquiry')) {
291         $table->set_sql($sql);
292         $table->set_columns($cols);
293 }
294 start_form();
295 display_db_pager($table);
296
297 end_form();
298 end_page();
299
300 ?>