325d848ffd82b5a76cb8f5d3afe8dda68186ee85
[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 = 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
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(false, true);
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'), true);
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(systypes::sales_order(), $row['order_'])
69                 : "";
70 }
71
72 function systype_name($dummy, $type)
73 {
74         return systypes::name($type);
75 }
76
77 function view_link($trans)
78 {
79         return get_trans_view_str($trans["type"], $trans["trans_no"]);
80 }
81
82 function due_date($row)
83 {
84         return $row["type"] == 10 ? $row["due_date"] : '';
85 }
86
87 function fmt_balance($row)
88 {
89         return $row["TotalAmount"] - $row["Allocated"];
90 }
91
92 function alloc_link($row)
93 {
94         $link = 
95         pager_link(_("Allocation"),
96                 "/sales/allocations/customer_allocate.php?trans_no=" . $row["trans_no"] 
97                 ."&trans_type=" . $row["type"], ICON_MONEY);
98
99         if ($row["type"] == 11 && $row['TotalAmount'] > 0)
100         {
101                 /*its a credit note which could have an allocation */
102                 return $link;
103         }
104         elseif (($row["type"] == systypes::cust_payment() || $row["type"] == systypes::bank_deposit()) &&
105                 ($row['TotalAmount'] - $row['Allocated']) > 0)
106         {
107                 /*its a receipt  which could have an allocation*/
108                 return $link;
109         }
110         elseif ($row["type"] == systypes::cust_payment() && $row['TotalAmount'] < 0)
111         {
112                 /*its a negative receipt */
113                 return '';
114         }
115 }
116
117 function fmt_debit($row)
118 {
119         $value =
120             $row['type']==11 || $row['type']==12 || $row['type']==2 ?
121                 -$row["TotalAmount"] : $row["TotalAmount"];
122         return $value>=0 ? price_format($value) : '';
123
124 }
125
126 function fmt_credit($row)
127 {
128         $value =
129             !($row['type']==11 || $row['type']==12 || $row['type']==2) ?
130                 -$row["TotalAmount"] : $row["TotalAmount"];
131         return $value>0 ? price_format($value) : '';
132 }
133 //------------------------------------------------------------------------------------------------
134
135   $data_after = date2sql($_POST['TransAfterDate']);
136   $date_to = date2sql($_POST['TransToDate']);
137
138   $sql = "SELECT 
139                 trans.type,
140                 trans.order_,
141                 trans.trans_no,
142                 trans.reference,
143                 trans.tran_date,
144                 trans.due_date,
145                 debtor.name,
146                 debtor.curr_code,
147         (trans.ov_amount + trans.ov_gst + trans.ov_freight 
148                         + trans.ov_freight_tax + trans.ov_discount)     AS TotalAmount,
149                 trans.alloc AS Allocated,
150                 ((trans.type = 10)
151                         AND trans.due_date < '" . date2sql(Today()) . "') AS OverDue
152         FROM "
153                         .TB_PREF."debtor_trans as trans, "
154                         .TB_PREF."debtors_master as debtor
155         WHERE debtor.debtor_no = trans.debtor_no
156                         AND (trans.ov_amount + trans.ov_gst + trans.ov_freight 
157                                 + trans.ov_freight_tax + trans.ov_discount != 0)
158                 AND trans.tran_date >= '$data_after'
159                 AND trans.tran_date <= '$date_to'";
160
161         if ($_POST['customer_id'] != reserved_words::get_all())
162                 $sql .= " AND trans.debtor_no = '" . $_POST['customer_id'] . "'";
163
164         if (isset($_POST['filterType']) && $_POST['filterType'] != reserved_words::get_all())
165         {
166                 if ($_POST['filterType'] == '1' || $_POST['filterType'] == '2')
167                 {
168                         $sql .= " AND trans.type = 10 ";
169                 }
170                 elseif ($_POST['filterType'] == '3')
171                 {
172                         $sql .= " AND trans.type = " . systypes::cust_payment();
173                 }
174                 elseif ($_POST['filterType'] == '4')
175                 {
176                         $sql .= " AND trans.type = 11 ";
177                 }
178
179         if ($_POST['filterType'] == '2')
180         {
181                 $today =  date2sql(Today());
182                 $sql .= " AND trans.due_date < '$today'
183                                 AND (round(abs(trans.ov_amount + "
184                                 ."trans.ov_gst + trans.ov_freight + "
185                                 ."trans.ov_freight_tax + trans.ov_discount) - trans.alloc,6) > 0) ";
186         }
187         }else
188         {
189             $sql .= " AND trans.type != 13 ";
190         }
191
192
193         if (!check_value('showSettled'))
194         {
195                 $sql .= " AND (round(abs(trans.ov_amount + trans.ov_gst + "
196                 ."trans.ov_freight + trans.ov_freight_tax + "
197                 ."trans.ov_discount) - trans.alloc,6) != 0) ";
198         }
199 //------------------------------------------------------------------------------------------------
200
201 $cols = array(
202         _("Type") => array('fun'=>'systype_name'),
203         _("#") => array('fun'=>'view_link'),
204         _("Reference"), 
205         _("Order") => array('fun'=>'order_link'), 
206         _("Date") => array('name'=>'tran_date', 'type'=>'date', 'ord'=>'asc'),
207         _("Due Date") => array('type'=>'date', 'fun'=>'due_date'),
208         _("Customer"), 
209         _("Currency") => array('align'=>'center'),
210         _("Debit") => array('align'=>'right','fun'=>'fmt_debit'), 
211         _("Credit") => array('align'=>'right','insert'=>true, 'fun'=>'fmt_credit'), 
212         _("Allocated") => 'amount', 
213         _("Balance") => array('type'=>'amount', 'insert'=>true, 'fun'=>'fmt_balance'),
214         array('insert'=>true, 'fun'=>'alloc_link')
215         );
216
217 if ($_POST['customer_id'] != reserved_words::get_all()) {
218         $cols[_("Customer")] = 'skip';
219         $cols[_("Currency")] = 'skip';
220 }
221
222 $table =& new_db_pager('doc_tbl', $sql, $cols);
223 $table->set_marker('check_overdue', _("Marked items are overdue."));
224
225 if (get_post('RefreshInquiry')) {
226         $table->set_sql($sql);
227         $table->set_columns($cols);
228 }
229 $table->width = "80%";
230 start_form();
231
232 display_db_pager($table);
233
234 end_form();
235 end_page();
236 ?>