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