2 include_once('order_xtra_config.inc');
3 include_once($path_to_root."/includes/ui/ui_input.inc");
4 /* This utility function remove beginning <td> and </td> from a string
5 * generated by the above *_cell function.
6 * This is usefull to use the result of the function within a pager
7 * which already include the <td>...</td>
8 * However, for function which doesn't return a string
9 * but echo, we need to capture the output of the echo first.
11 function start_extract_cell() {
16 function end_extract_cell() {
17 $content = ob_get_contents();
19 return extract_cell($content);
22 function extract_cell($td) {
23 if(preg_match('/\s*<td>(.*)<\/td>\s*/s', $td, $matches)) {
31 /* remove unchecked data so each
32 * funcion doesn't to check what's been checked or not.
34 function filter_data(&$data) {
35 if(!isset($data['detail'])) return;
36 $new_details = array();
37 foreach(@$data['detail'] as $detail_id => $values) {
38 if(@$values['check'] === 'on') $new_details[$detail_id]=$values;
40 $data['detail'] = $new_details;
43 function post_to_detail_ids() {
44 if(!isset($_POST['detail'])) return;
45 $detail_ids = array();
46 foreach($_POST['detail'] as $detail_id => $values) {
47 array_push($detail_ids, $detail_id);
53 function update_extra_order_details() {
54 if(!isset($_POST['Update']) || $_POST['Update'] != 'Update') return;
56 $cart = post_to_detail_ids();
57 if($cart === null) return;
59 hook_db_prewrite($cart, 'order_xtra');
60 // update in database all field
61 $date_fields = array('hold_until_date', 'required_date', 'expiry_date', 'priority');
62 foreach($_POST['detail'] as $detail_id => $values) {
64 $comment=$values['comment'];
65 array_push($set, " comment='$comment' "); // erase comment if needed.
66 foreach($date_fields as $date_field) {
67 if(isset($values[$date_field])) {
68 $input_date = trim($values[$date_field]);
69 if($input_date == "") {
70 // Only the priority field can be set to null.
71 if($date_field == 'priority') array_push($set," $date_field = NULL");
73 $date = date2sql($input_date);
74 array_push($set," $date_field='$date' ");
80 $sql = "UPDATE ".TB_PREF."sales_order_details SET ".implode($set,', ')." WHERE id = $detail_id ";
83 display_error('Problem while updating order details. Try again');
89 hook_db_postwrite($cart, 'order_xtra');
93 display_notification('Order details updated');
97 function split_order_details() {
98 if(!isset($_POST['Split']) || $_POST['Split'] != 'Split') return;
99 $splitter = new Splitter($_POST);
101 $cart = post_to_detail_ids();
102 if($cart === null) return;
103 hook_db_prewrite($cart, 'order_xtra');
104 if($splitter->splitAll()) display_warning("Items have been split.");
105 hook_db_postwrite($cart, 'order_xtra');
106 commit_transaction();
109 function bulk_update_order_details() {
110 if(!isset($_POST['Bulk']) || $_POST['Bulk'] != 'Bulk') return;
111 $bulk_updater = new BulkUpdater($_POST);
113 $cart = post_to_detail_ids();
114 if($cart === null) return;
115 hook_db_prewrite($cart, 'order_xtra');
116 $bulk_updater->update();
117 hook_db_postwrite($cart, 'order_xtra');
118 commit_transaction();
123 function compute_input_name($row, $field) {
124 $row_id = $row['id'];
125 return "detail[$row_id][$field]";
128 function view_link($dummy, $order_no)
130 return get_customer_trans_view_str(ST_SALESORDER, $order_no);
133 function item_link($dummy, $stock_id)
135 return pager_link( _($stock_id),
136 "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
139 function order_link($row)
141 return pager_link( _("Sales Order"),
142 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
145 function customer_link($row) {
146 return customer_link2($row['debtor_ref'], $row['debtor_no']);
149 function customer_link2($name, $id) {
150 return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
154 function aggregate_comment($row) {
155 $comment = $row['order_comment'].";".$row['detail_comment'];
157 $comments = array_map('trim', explode(';', $comment));
158 $comments = array_filter($comments);
159 $first = array_shift($comments);
160 if(count($comments) == 0)
163 $tooltip = implode('<br> - ', $comments);
164 return "$first <span class='before-tooltip'>more<span class='tooltip'> $tooltip</span><span>";
170 function available_quantity($row, $available) {
171 $quantity = $row['quantity'];
173 if($available== 0) $class = 'limited';
174 else if($quantity > $available) $class = 'partial';
175 else if(OrderXtraConfig::early(sql2date($row['hold_until_date']))) $class = 'early';
177 return "<span class='$class'>$available<span>";
180 function input_date_details($row, $field_name, $date) {
181 $row_id = $row['id'];
182 $name = compute_input_name($row, $field_name);
184 $_POST[$name] = sql2date($date);
186 start_extract_cell();
187 date_cells (null, $name, 'title', null, 0,0,1001);
188 return end_extract_cell();
192 <input type='text' value='$date' name='$name'>
196 function input_priority_date_details($row, $date) {
197 return input_date_details($row, 'priority', $date);
200 function input_hold_until_date_details($row, $date) {
201 return input_date_details($row, 'hold_until_date', $date);
204 function input_required_date_details($row, $date) {
205 return input_date_details($row, 'required_date', $date);
208 function input_expiry_date_details($row, $date) {
209 return input_date_details($row, 'expiry_date', $date);
212 function input_comment_details($row, $comment) {
213 $row_id = $row['id'];
215 <input type='text' value='$comment' name='detail[$row_id][comment]'>
219 function input_check_details($row, $dummy) {
220 $row_id = $row['id'];
222 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
225 function pick_checkbox($row)
227 $id = $row['debtor_no']."_".$row['branch_code'];
228 $name = "chgpck" .$id;
229 $value = $row['quantity_to_pick'] > 0 ? 1:0;
231 // save also in hidden field for testing during 'Update'
232 return checkbox(null, $name, $value, true,
233 _('Pick all possible item for this branch.'))
234 . hidden('pick['.$id.']', $value, false);
237 function get_order_details_extra($customer_id, $location, $item_like) {
238 $sql = "SELECT sod.id
241 , sod.quantity - qty_sent quantity
242 , GREATEST(0, LEAST(qoh.quantity - quantity_before, sod.quantity - qty_sent))
249 FROM ".TB_PREF."sales_order_details sod
250 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
251 AND so.trans_type = sod.trans_type
252 AND so.trans_type = ".ST_SALESORDER."
253 AND so.debtor_no = $customer_id
255 JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
256 LEFT JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
257 WHERE sod.quantity > qty_sent
261 if($item_like[0]=='/') {
262 $regexp = substr($item_like, 1);
263 $sql .= " AND stk_code RLIKE '$regexp'";
266 $sql .= " AND stk_code LIKE '$item_like'";
272 function get_order_summary($location) {
273 $held_condition = OrderXtraConfig::sql_held_condition();
275 $pick = "SELECT detail_id, -sum(quantity) as quantity
276 FROM ".TB_PREF."topick
277 WHERE type IN ('order', 'booked')
278 GROUP BY detail_id ";
280 $sub = "SELECT debtor_no, debtor_ref, branch_code, branch_ref, stk_code
282 , sum(d.quantity) as quantity
283 , sum((d.quantity)*unit_price*(1-discount_percent/100)) as amount
285 , group_concat(distinct comments separator ';') as order_comment
286 , group_concat(distinct comment separator ';') as detail_comment
287 , max(quantity_before) as quantity_before
288 , sum(if($held_condition, greatest(least(d.quantity, qoh.quantity - quantity_before), 0), 0)) as quantity_held
289 , sum(if($held_condition, 0, greatest(least(d.quantity, qoh.quantity - quantity_before), 0))) as available_quantity
290 , sum(if($held_condition, 0, greatest(least(d.quantity, qoh.quantity - quantity_before), 0)*unit_price*(1-discount_percent/100))) as available_amount
291 , sum(p.quantity) as quantity_to_pick
292 , sum(p.quantity*unit_price) as amount_to_pick
293 FROM ".TB_PREF."sales_order_details sod
294 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
295 AND so.trans_type = sod.trans_type
296 AND so.trans_type = ".ST_SALESORDER."
298 NATURAL JOIN ".TB_PREF."debtors_master
299 NATURAL JOIN ".TB_PREF."cust_branch
300 LEFT JOIN ($pick) p ON (detail_id = sod.id)
301 JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
302 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (qoh.stock_id = stk_code AND loc_code = '$location')
303 WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
304 GROUP BY debtor_no, branch_code, stk_code
307 //$sub = TB_PREF."order_summary_view";
310 $sql = array( "select" => "debtor_no, branch_code, debtor_ref, branch_ref
311 , `min(delivery_date)` as delivery_date
312 , sum(sub.quantity) as quantity
313 , sum(sub.amount) as amount
314 , sum(sub.available_quantity)
315 , sum(sub.available_amount)
316 , sum(quantity_held) as quantity_help
317 , min(`min(required_date)`) as required_date
318 , group_concat(distinct order_comment separator ';') as order_comment
319 , group_concat(distinct detail_comment separator ';') as detail_comment
320 , sum(quantity_to_pick) as quantity_to_pick
321 , sum(amount_to_pick) as amount_to_pick
322 ", "from" => " ($sub) sub
323 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
325 ", "group" => " debtor_no, branch_code