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