Filter uncheked item.
[order_line_extra.git] / includes / order_lines.inc
1 <?php
2 include_once($path_to_root."/includes/ui/ui_input.inc");
3 /* This utility function remove beginning <td> and </td> from a string
4  * generated by the above *_cell function.
5  * This is usefull to use the result of the function within a pager
6  * which already include the <td>...</td>
7  * However, for function which doesn't return a string
8  * but echo, we need to capture the output of the echo first.
9  */
10 function start_extract_cell() {
11         // We need 
12         ob_start();
13 }
14
15 function end_extract_cell() {
16         $content = ob_get_contents();
17         ob_end_clean();
18         return extract_cell($content);
19 }
20
21 function extract_cell($td) {
22         if(preg_match('/\s*<td>(.*)<\/td>\s*/s', $td, $matches)) {
23                 return $matches[1];
24         }
25         else {
26                 return $td;
27         }
28 }
29
30 /* remove unchecked data so each
31  * funcion doesn't to check what's been checked or not. 
32  */
33 function filter_data(&$data) {
34         if(!isset($data['detail'])) return;
35         $new_details = array();
36         foreach(@$data['detail'] as $detail_id => $values) {
37                 if(@$values['check'] === 'on') $new_details[$detail_id]=$values;
38         }
39         $data['detail'] = $new_details;
40 }
41
42 function post_to_detail_ids() {
43         $detail_ids = array();
44         foreach($_POST['detail'] as $detail_id => $values) {
45                 array_push($detail_ids, $detail_id);
46         }
47
48         return $detail_ids;
49 }
50
51 function update_extra_order_details() {
52         if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
53
54         $cart = post_to_detail_ids();
55 begin_transaction();
56         hook_db_prewrite($cart, 'order_xtra');
57         // update in database all field
58         $date_fields = array('hold_until_date', 'required_date', 'expiry_date', 'priority');
59         foreach($_POST['detail'] as $detail_id => $values) {
60                 $set = array();
61                 $comment=$values['comment'];
62           array_push($set, " comment='$comment' "); // erase comment if needed.
63                 foreach($date_fields as $date_field) {
64                         if(isset($values[$date_field])) {
65                                 $input_date = trim($values[$date_field]);
66                                 if($input_date == "")  {
67                                         // Only the priority field can be set to null.
68                                         if($date_field == 'priority') array_push($set," $date_field = NULL");
69                                 } else {
70                                         $date = date2sql($input_date);
71                                         array_push($set," $date_field='$date' ");
72                                 }
73                         }
74                 }
75
76                 if(!empty($set))  {
77                         $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
78
79                         if(!db_query($sql)) {
80                                 display_error('Problem while updating order details. Try again');
81                                 cancel_transaction();
82                                 return;
83                         }
84                 }
85         }
86         hook_db_postwrite($cart, 'order_xtra');
87         commit_transaction();
88
89
90         display_notification('Order details updated');
91
92 }
93
94 function split_order_details() {
95         if(!isset($_POST['Split']) || $_POST['Split'] != 'Split')  return;
96         $splitter = new Splitter($_POST);
97         begin_transaction();
98         $cart = post_to_detail_ids();
99         hook_db_prewrite($cart, 'order_xtra');
100                 if($splitter->splitAll()) display_warning("Items have been split.");
101         hook_db_postwrite($cart, 'order_xtra');
102         commit_transaction();
103 }
104
105 function compute_input_name($row, $field) {
106         $row_id = $row['id'];
107         return "detail[$row_id][$field]";
108 }
109
110 function view_link($dummy, $order_no)
111 {
112         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
113 }
114
115 function item_link($dummy, $stock_id)
116 {
117         return pager_link( _($stock_id),
118         "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
119 }
120
121 function order_link($row)
122 {
123         return pager_link( _("Sales Order"),
124         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
125 }
126
127 function customer_link($row) {
128         return customer_link2($row['debtor_ref'], $row['debtor_no']);
129 }
130
131 function customer_link2($name, $id) {
132         return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
133
134 }
135
136 function aggregate_comment($row) {
137         $comment =  $row['order_comment'].";".$row['detail_comment'];
138         if($comment) {
139                 $comments = array_map('trim', explode(';', $comment));
140                 $comments = array_filter($comments);
141                 $first = array_shift($comments);
142                 if(count($comments) == 0)
143                         return $first;
144                 else {
145                         $tooltip = implode('<br> - ', $comments);
146                         return "$first <span class='before-tooltip'>more<span class='tooltip'>&nbsp$tooltip</span><span>";
147                 }
148         }
149         return '';
150 }
151
152 function available_quantity($row, $available) {
153         $quantity = $row['quantity'];
154         $class = '';
155         if($available== 0) $class = 'limited';
156         else if($quantity > $available)  $class = 'partial';
157
158         return "<span class='$class'>$available<span>";
159 }
160
161 function input_date_details($row, $field_name, $date) {
162 $row_id = $row['id'];
163         $name = compute_input_name($row, $field_name);
164
165 $_POST[$name] = sql2date($date);
166
167         start_extract_cell();
168                 date_cells (null, $name, 'title', null, 0,0,1001);
169         return end_extract_cell();
170
171
172         return "
173 <input type='text' value='$date' name='$name'>
174 ";
175 }
176
177 function input_priority_date_details($row, $date) {
178         return input_date_details($row, 'priority', $date);
179 }
180
181 function input_hold_until_date_details($row, $date) {
182         return input_date_details($row, 'hold_until_date', $date);
183 }
184
185 function input_required_date_details($row, $date) {
186         return input_date_details($row, 'required_date', $date);
187 }
188
189 function input_expiry_date_details($row, $date) {
190         return input_date_details($row, 'expiry_date', $date);
191 }
192
193 function input_comment_details($row, $comment) {
194 $row_id = $row['id'];
195         return "
196 <input type='text' value='$comment' name='detail[$row_id][comment]'>
197 ";
198 }
199
200 function input_check_details($row, $dummy) {
201         $row_id = $row['id'];
202                 return "
203                 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
204 }
205
206 function get_order_details_extra($customer_id, $location, $item_like) {
207         $sql = "SELECT sod.id
208         , so.order_no
209         , stk_code
210         , sod.quantity - qty_sent quantity
211         , GREATEST(0, LEAST(qoh.quantity  - quantity_before, sod.quantity - qty_sent))
212         , quantity_before
213         , sod.`priority`
214         , hold_until_date
215         , required_date
216         , expiry_date
217         ,comment
218         FROM ".TB_PREF."sales_order_details sod
219         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
220                 AND so.trans_type = sod.trans_type
221                 AND so.trans_type = ".ST_SALESORDER."
222                 AND so.debtor_no = $customer_id
223         )
224         JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
225         LEFT JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
226         WHERE sod.quantity > qty_sent
227         ";
228
229         if($item_like) {
230                         if($item_like[0]=='/')  {
231                                 $regexp = substr($item_like, 1);
232                                 $sql .= " AND stk_code RLIKE '$regexp'";
233                         }
234                         else 
235                                 $sql .= " AND stk_code LIKE '$item_like'";
236         }
237
238         return $sql;
239 }
240
241 function get_order_summary($location) {
242 /*
243         $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
244         , min(delivery_date)
245         , sum(sod.quantity - qty_sent) as quantity
246         , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
247         , min(required_date)
248         , group_concat(distinct comments separator ';') as order_comment
249         , group_concat(distinct comment separator ';') as detail_comment
250         , max(quantity_before) as quantity_before
251         FROM ".TB_PREF."sales_order_details sod
252         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
253                 AND so.trans_type = sod.trans_type
254                 AND so.trans_type = ".ST_SALESORDER."
255         )
256         NATURAL JOIN ".TB_PREF."debtors_master
257         NATURAL JOIN ".TB_PREF."cust_branch
258         JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
259         WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
260         GROUP BY debtor_no, branch_code, stk_code
261         ";
262 */
263
264         $sub = TB_PREF."order_summary_view";
265
266         $sql = "SELECT debtor_no, debtor_ref, branch_ref
267         , `min(delivery_date)`
268         , sum(sub.quantity) as quantity
269         , sum(sub.amount) as amount
270         , sum(greatest(least(sub.quantity, qoh.quantity - quantity_before), 0))
271         , sum(sub.amount*greatest(least(sub.quantity, qoh.quantity - quantity_before), 0)/sub.quantity)
272         ,  min(`min(required_date)`)
273         , group_concat(distinct order_comment separator ';') as order_comment
274         , group_concat(distinct detail_comment separator ';') as detail_comment
275                                 FROM $sub sub
276                                 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
277                                 GROUP BY debtor_no, debtor_ref
278         ";
279
280         return $sql;
281 }
282 ?>