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.
10 function start_extract_cell() {
15 function end_extract_cell() {
16 $content = ob_get_contents();
18 return extract_cell($content);
21 function extract_cell($td) {
22 if(preg_match('/\s*<td>(.*)<\/td>\s*/s', $td, $matches)) {
30 /* remove unchecked data so each
31 * funcion doesn't to check what's been checked or not.
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;
39 $data['detail'] = $new_details;
42 function post_to_detail_ids() {
43 if(!isset($_POST['detail'])) return;
44 $detail_ids = array();
45 foreach($_POST['detail'] as $detail_id => $values) {
46 array_push($detail_ids, $detail_id);
52 function update_extra_order_details() {
53 if(!isset($_POST['Update']) || $_POST['Update'] != 'Update') return;
55 $cart = post_to_detail_ids();
56 if($cart === null) return;
58 hook_db_prewrite($cart, 'order_xtra');
59 // update in database all field
60 $date_fields = array('hold_until_date', 'required_date', 'expiry_date', 'priority');
61 foreach($_POST['detail'] as $detail_id => $values) {
63 $comment=$values['comment'];
64 array_push($set, " comment='$comment' "); // erase comment if needed.
65 foreach($date_fields as $date_field) {
66 if(isset($values[$date_field])) {
67 $input_date = trim($values[$date_field]);
68 if($input_date == "") {
69 // Only the priority field can be set to null.
70 if($date_field == 'priority') array_push($set," $date_field = NULL");
72 $date = date2sql($input_date);
73 array_push($set," $date_field='$date' ");
79 $sql = "UPDATE ".TB_PREF."sales_order_details SET ".implode($set,', ')." WHERE id = $detail_id ";
82 display_error('Problem while updating order details. Try again');
88 hook_db_postwrite($cart, 'order_xtra');
92 display_notification('Order details updated');
96 function split_order_details() {
97 if(!isset($_POST['Split']) || $_POST['Split'] != 'Split') return;
98 $splitter = new Splitter($_POST);
100 $cart = post_to_detail_ids();
101 if($cart === null) return;
102 hook_db_prewrite($cart, 'order_xtra');
103 if($splitter->splitAll()) display_warning("Items have been split.");
104 hook_db_postwrite($cart, 'order_xtra');
105 commit_transaction();
108 function bulk_update_order_details() {
109 if(!isset($_POST['Bulk']) || $_POST['Bulk'] != 'Bulk') return;
110 $bulk_updater = new BulkUpdater($_POST);
112 $cart = post_to_detail_ids();
113 if($cart === null) return;
114 hook_db_prewrite($cart, 'order_xtra');
115 $bulk_updater->update();
116 hook_db_postwrite($cart, 'order_xtra');
117 commit_transaction();
120 function compute_input_name($row, $field) {
121 $row_id = $row['id'];
122 return "detail[$row_id][$field]";
125 function view_link($dummy, $order_no)
127 return get_customer_trans_view_str(ST_SALESORDER, $order_no);
130 function item_link($dummy, $stock_id)
132 return pager_link( _($stock_id),
133 "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
136 function order_link($row)
138 return pager_link( _("Sales Order"),
139 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
142 function customer_link($row) {
143 return customer_link2($row['debtor_ref'], $row['debtor_no']);
146 function customer_link2($name, $id) {
147 return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
151 function aggregate_comment($row) {
152 $comment = $row['order_comment'].";".$row['detail_comment'];
154 $comments = array_map('trim', explode(';', $comment));
155 $comments = array_filter($comments);
156 $first = array_shift($comments);
157 if(count($comments) == 0)
160 $tooltip = implode('<br> - ', $comments);
161 return "$first <span class='before-tooltip'>more<span class='tooltip'> $tooltip</span><span>";
167 function available_quantity($row, $available) {
168 $quantity = $row['quantity'];
170 if($available== 0) $class = 'limited';
171 else if($quantity > $available) $class = 'partial';
173 return "<span class='$class'>$available<span>";
176 function input_date_details($row, $field_name, $date) {
177 $row_id = $row['id'];
178 $name = compute_input_name($row, $field_name);
180 $_POST[$name] = sql2date($date);
182 start_extract_cell();
183 date_cells (null, $name, 'title', null, 0,0,1001);
184 return end_extract_cell();
188 <input type='text' value='$date' name='$name'>
192 function input_priority_date_details($row, $date) {
193 return input_date_details($row, 'priority', $date);
196 function input_hold_until_date_details($row, $date) {
197 return input_date_details($row, 'hold_until_date', $date);
200 function input_required_date_details($row, $date) {
201 return input_date_details($row, 'required_date', $date);
204 function input_expiry_date_details($row, $date) {
205 return input_date_details($row, 'expiry_date', $date);
208 function input_comment_details($row, $comment) {
209 $row_id = $row['id'];
211 <input type='text' value='$comment' name='detail[$row_id][comment]'>
215 function input_check_details($row, $dummy) {
216 $row_id = $row['id'];
218 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
221 function get_order_details_extra($customer_id, $location, $item_like) {
222 $sql = "SELECT sod.id
225 , sod.quantity - qty_sent quantity
226 , GREATEST(0, LEAST(qoh.quantity - quantity_before, sod.quantity - qty_sent))
233 FROM ".TB_PREF."sales_order_details sod
234 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
235 AND so.trans_type = sod.trans_type
236 AND so.trans_type = ".ST_SALESORDER."
237 AND so.debtor_no = $customer_id
239 JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
240 LEFT JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
241 WHERE sod.quantity > qty_sent
245 if($item_like[0]=='/') {
246 $regexp = substr($item_like, 1);
247 $sql .= " AND stk_code RLIKE '$regexp'";
250 $sql .= " AND stk_code LIKE '$item_like'";
256 function get_order_summary($location) {
258 $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
260 , sum(sod.quantity - qty_sent) as quantity
261 , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
263 , group_concat(distinct comments separator ';') as order_comment
264 , group_concat(distinct comment separator ';') as detail_comment
265 , max(quantity_before) as quantity_before
266 FROM ".TB_PREF."sales_order_details sod
267 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
268 AND so.trans_type = sod.trans_type
269 AND so.trans_type = ".ST_SALESORDER."
271 NATURAL JOIN ".TB_PREF."debtors_master
272 NATURAL JOIN ".TB_PREF."cust_branch
273 JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
274 WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
275 GROUP BY debtor_no, branch_code, stk_code
279 $sub = TB_PREF."order_summary_view";
281 $sql = "SELECT debtor_no, debtor_ref, branch_ref
282 , `min(delivery_date)`
283 , sum(sub.quantity) as quantity
284 , sum(sub.amount) as amount
285 , sum(greatest(least(sub.quantity, qoh.quantity - quantity_before), 0))
286 , sum(sub.amount*greatest(least(sub.quantity, qoh.quantity - quantity_before), 0)/sub.quantity)
287 , min(`min(required_date)`)
288 , group_concat(distinct order_comment separator ';') as order_comment
289 , group_concat(distinct detail_comment separator ';') as detail_comment
291 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
292 GROUP BY debtor_no, debtor_ref