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