Security update merged from 2.1.
[fa-stable.git] / sales / inquiry / customer_allocation_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_SALESALLOC';
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
20 $js = "";
21 if ($use_popup_windows)
22         $js .= get_js_open_window(900, 500);
23 if ($use_date_picker)
24         $js .= get_js_date_picker();
25 page(_("Customer Allocation Inquiry"), false, false, "", $js);
26
27 if (isset($_GET['customer_id']))
28 {
29         $_POST['customer_id'] = $_GET['customer_id'];
30 }
31
32 //------------------------------------------------------------------------------------------------
33
34 if (!isset($_POST['customer_id']))
35         $_POST['customer_id'] = get_global_customer();
36
37 start_form();
38
39 start_table("class='tablestyle_noborder'");
40 start_row();
41
42 customer_list_cells(_("Select a customer: "), 'customer_id', $_POST['customer_id'], true);
43
44 date_cells(_("from:"), 'TransAfterDate', '', null, -30);
45 date_cells(_("to:"), 'TransToDate', '', null, 1);
46
47 cust_allocations_list_cells(_("Type:"), 'filterType', null);
48
49 check_cells(" " . _("show settled:"), 'showSettled', null);
50
51 submit_cells('RefreshInquiry', _("Search"),'',_('Refresh Inquiry'), 'default');
52
53 set_global_customer($_POST['customer_id']);
54
55 end_row();
56 end_table();
57 end_form();
58 //------------------------------------------------------------------------------------------------
59 function check_overdue($row)
60 {
61         return ($row['OverDue'] == 1 
62                 && (abs($row["TotalAmount"]) - $row["Allocated"] != 0));
63 }
64
65 function order_link($row)
66 {
67         return $row['order_']>0 ?
68                 get_customer_trans_view_str(ST_SALESORDER, $row['order_'])
69                 : "";
70 }
71
72 function systype_name($dummy, $type)
73 {
74         global $systypes_array;
75
76         return $systypes_array[$type];
77 }
78
79 function view_link($trans)
80 {
81         return get_trans_view_str($trans["type"], $trans["trans_no"]);
82 }
83
84 function due_date($row)
85 {
86         return $row["type"] == 10 ? $row["due_date"] : '';
87 }
88
89 function fmt_balance($row)
90 {
91         return $row["TotalAmount"] - $row["Allocated"];
92 }
93
94 function alloc_link($row)
95 {
96         $link = 
97         pager_link(_("Allocation"),
98                 "/sales/allocations/customer_allocate.php?trans_no=" . $row["trans_no"] 
99                 ."&trans_type=" . $row["type"], ICON_MONEY);
100
101         if ($row["type"] == ST_CUSTCREDIT && $row['TotalAmount'] > 0)
102         {
103                 /*its a credit note which could have an allocation */
104                 return $link;
105         }
106         elseif (($row["type"] == ST_CUSTPAYMENT || $row["type"] == ST_BANKDEPOSIT) &&
107                 ($row['TotalAmount'] - $row['Allocated']) > 0)
108         {
109                 /*its a receipt  which could have an allocation*/
110                 return $link;
111         }
112         elseif ($row["type"] == ST_CUSTPAYMENT && $row['TotalAmount'] < 0)
113         {
114                 /*its a negative receipt */
115                 return '';
116         }
117 }
118
119 function fmt_debit($row)
120 {
121         $value =
122             $row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT ?
123                 -$row["TotalAmount"] : $row["TotalAmount"];
124         return $value>=0 ? price_format($value) : '';
125
126 }
127
128 function fmt_credit($row)
129 {
130         $value =
131             !($row['type']==ST_CUSTCREDIT || $row['type']==ST_CUSTPAYMENT || $row['type']==ST_BANKDEPOSIT) ?
132                 -$row["TotalAmount"] : $row["TotalAmount"];
133         return $value>0 ? price_format($value) : '';
134 }
135 //------------------------------------------------------------------------------------------------
136
137   $data_after = date2sql($_POST['TransAfterDate']);
138   $date_to = date2sql($_POST['TransToDate']);
139
140   $sql = "SELECT 
141                 trans.type,
142                 trans.order_,
143                 trans.trans_no,
144                 trans.reference,
145                 trans.tran_date,
146                 trans.due_date,
147                 debtor.name,
148                 debtor.curr_code,
149         (trans.ov_amount + trans.ov_gst + trans.ov_freight 
150                         + trans.ov_freight_tax + trans.ov_discount)     AS TotalAmount,
151                 trans.alloc AS Allocated,
152                 ((trans.type = ".ST_SALESINVOICE.")
153                         AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
154         FROM "
155                         .TB_PREF."debtor_trans as trans, "
156                         .TB_PREF."debtors_master as debtor
157         WHERE debtor.debtor_no = trans.debtor_no
158                         AND (trans.ov_amount + trans.ov_gst + trans.ov_freight 
159                                 + trans.ov_freight_tax + trans.ov_discount != 0)
160                 AND trans.tran_date >= '$data_after'
161                 AND trans.tran_date <= '$date_to'";
162
163         if ($_POST['customer_id'] != ALL_TEXT)
164                 $sql .= " AND trans.debtor_no = ".db_escape($_POST['customer_id']);
165
166         if (isset($_POST['filterType']) && $_POST['filterType'] != ALL_TEXT)
167         {
168                 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
169                 {
170                         $sql .= " AND trans.type = ".ST_SALESINVOICE." ";
171                 }
172                 elseif ($_POST['filterType'] == '3')
173                 {
174                         $sql .= " AND trans.type = " . ST_CUSTPAYMENT;
175                 }
176                 elseif ($_POST['filterType'] == '4')
177                 {
178                         $sql .= " AND trans.type = ".ST_CUSTCREDIT." ";
179                 }
180
181         if ($_POST['filterType'] == '2')
182         {
183                 $today =  date2sql(Today());
184                 $sql .= " AND trans.due_date < '$today'
185                                 AND (round(abs(trans.ov_amount + "
186                                 ."trans.ov_gst + trans.ov_freight + "
187                                 ."trans.ov_freight_tax + trans.ov_discount) - trans.alloc,6) > 0) ";
188         }
189         }else
190         {
191             $sql .= " AND trans.type <> ".ST_CUSTDELIVERY." ";
192         }
193
194
195         if (!check_value('showSettled'))
196         {
197                 $sql .= " AND (round(abs(trans.ov_amount + trans.ov_gst + "
198                 ."trans.ov_freight + trans.ov_freight_tax + "
199                 ."trans.ov_discount) - trans.alloc,6) != 0) ";
200         }
201 //------------------------------------------------------------------------------------------------
202
203 $cols = array(
204         _("Type") => array('fun'=>'systype_name'),
205         _("#") => array('fun'=>'view_link'),
206         _("Reference"), 
207         _("Order") => array('fun'=>'order_link'), 
208         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
209         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'),
210         _("Customer"), 
211         _("Currency") => array('align'=>'center'),
212         _("Debit") => array('align'=>'right','fun'=>'fmt_debit'), 
213         _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'), 
214         _("Allocated") => 'amount', 
215         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'fmt_balance'),
216         array('insert'=>true, 'fun'=>'alloc_link')
217         );
218
219 if ($_POST['customer_id'] != ALL_TEXT) {
220         $cols[_("Customer")] = 'skip';
221         $cols[_("Currency")] = 'skip';
222 }
223
224 $table =& new_db_pager('doc_tbl', $sql, $cols);
225 $table->set_marker('check_overdue', _("Marked items are overdue."));
226
227 $table->width = "80%";
228 start_form();
229
230 display_db_pager($table);
231
232 end_form();
233 end_page();
234 ?>