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 $detail_ids = array();
44 foreach($_POST['detail'] as $detail_id => $values) {
45 array_push($detail_ids, $detail_id);
51 function update_extra_order_details() {
52 if(!isset($_POST['Update']) || $_POST['Update'] != 'Update') return;
54 $cart = post_to_detail_ids();
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) {
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");
70 $date = date2sql($input_date);
71 array_push($set," $date_field='$date' ");
77 $sql = "UPDATE ".TB_PREF."sales_order_details SET ".implode($set,', ')." WHERE id = $detail_id ";
80 display_error('Problem while updating order details. Try again');
86 hook_db_postwrite($cart, 'order_xtra');
90 display_notification('Order details updated');
94 function split_order_details() {
95 if(!isset($_POST['Split']) || $_POST['Split'] != 'Split') return;
96 $splitter = new Splitter($_POST);
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();
105 function compute_input_name($row, $field) {
106 $row_id = $row['id'];
107 return "detail[$row_id][$field]";
110 function view_link($dummy, $order_no)
112 return get_customer_trans_view_str(ST_SALESORDER, $order_no);
115 function item_link($dummy, $stock_id)
117 return pager_link( _($stock_id),
118 "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
121 function order_link($row)
123 return pager_link( _("Sales Order"),
124 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
127 function customer_link($row) {
128 return customer_link2($row['debtor_ref'], $row['debtor_no']);
131 function customer_link2($name, $id) {
132 return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
136 function aggregate_comment($row) {
137 $comment = $row['order_comment'].";".$row['detail_comment'];
139 $comments = array_map('trim', explode(';', $comment));
140 $comments = array_filter($comments);
141 $first = array_shift($comments);
142 if(count($comments) == 0)
145 $tooltip = implode('<br> - ', $comments);
146 return "$first <span class='before-tooltip'>more<span class='tooltip'> $tooltip</span><span>";
152 function available_quantity($row, $available) {
153 $quantity = $row['quantity'];
155 if($available== 0) $class = 'limited';
156 else if($quantity > $available) $class = 'partial';
158 return "<span class='$class'>$available<span>";
161 function input_date_details($row, $field_name, $date) {
162 $row_id = $row['id'];
163 $name = compute_input_name($row, $field_name);
165 $_POST[$name] = sql2date($date);
167 start_extract_cell();
168 date_cells (null, $name, 'title', null, 0,0,1001);
169 return end_extract_cell();
173 <input type='text' value='$date' name='$name'>
177 function input_priority_date_details($row, $date) {
178 return input_date_details($row, 'priority', $date);
181 function input_hold_until_date_details($row, $date) {
182 return input_date_details($row, 'hold_until_date', $date);
185 function input_required_date_details($row, $date) {
186 return input_date_details($row, 'required_date', $date);
189 function input_expiry_date_details($row, $date) {
190 return input_date_details($row, 'expiry_date', $date);
193 function input_comment_details($row, $comment) {
194 $row_id = $row['id'];
196 <input type='text' value='$comment' name='detail[$row_id][comment]'>
200 function input_check_details($row, $dummy) {
201 $row_id = $row['id'];
203 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
206 function get_order_details_extra($customer_id, $location, $item_like) {
207 $sql = "SELECT sod.id
210 , sod.quantity - qty_sent quantity
211 , GREATEST(0, LEAST(qoh.quantity - quantity_before, sod.quantity - qty_sent))
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
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
230 if($item_like[0]=='/') {
231 $regexp = substr($item_like, 1);
232 $sql .= " AND stk_code RLIKE '$regexp'";
235 $sql .= " AND stk_code LIKE '$item_like'";
241 function get_order_summary($location) {
243 $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
245 , sum(sod.quantity - qty_sent) as quantity
246 , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
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."
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
264 $sub = TB_PREF."order_summary_view";
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
276 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
277 GROUP BY debtor_no, debtor_ref