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 compute_input_name($row, $field) {
109 $row_id = $row['id'];
110 return "detail[$row_id][$field]";
113 function view_link($dummy, $order_no)
115 return get_customer_trans_view_str(ST_SALESORDER, $order_no);
118 function item_link($dummy, $stock_id)
120 return pager_link( _($stock_id),
121 "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
124 function order_link($row)
126 return pager_link( _("Sales Order"),
127 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
130 function customer_link($row) {
131 return customer_link2($row['debtor_ref'], $row['debtor_no']);
134 function customer_link2($name, $id) {
135 return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
139 function aggregate_comment($row) {
140 $comment = $row['order_comment'].";".$row['detail_comment'];
142 $comments = array_map('trim', explode(';', $comment));
143 $comments = array_filter($comments);
144 $first = array_shift($comments);
145 if(count($comments) == 0)
148 $tooltip = implode('<br> - ', $comments);
149 return "$first <span class='before-tooltip'>more<span class='tooltip'> $tooltip</span><span>";
155 function available_quantity($row, $available) {
156 $quantity = $row['quantity'];
158 if($available== 0) $class = 'limited';
159 else if($quantity > $available) $class = 'partial';
161 return "<span class='$class'>$available<span>";
164 function input_date_details($row, $field_name, $date) {
165 $row_id = $row['id'];
166 $name = compute_input_name($row, $field_name);
168 $_POST[$name] = sql2date($date);
170 start_extract_cell();
171 date_cells (null, $name, 'title', null, 0,0,1001);
172 return end_extract_cell();
176 <input type='text' value='$date' name='$name'>
180 function input_priority_date_details($row, $date) {
181 return input_date_details($row, 'priority', $date);
184 function input_hold_until_date_details($row, $date) {
185 return input_date_details($row, 'hold_until_date', $date);
188 function input_required_date_details($row, $date) {
189 return input_date_details($row, 'required_date', $date);
192 function input_expiry_date_details($row, $date) {
193 return input_date_details($row, 'expiry_date', $date);
196 function input_comment_details($row, $comment) {
197 $row_id = $row['id'];
199 <input type='text' value='$comment' name='detail[$row_id][comment]'>
203 function input_check_details($row, $dummy) {
204 $row_id = $row['id'];
206 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
209 function get_order_details_extra($customer_id, $location, $item_like) {
210 $sql = "SELECT sod.id
213 , sod.quantity - qty_sent quantity
214 , GREATEST(0, LEAST(qoh.quantity - quantity_before, sod.quantity - qty_sent))
221 FROM ".TB_PREF."sales_order_details sod
222 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
223 AND so.trans_type = sod.trans_type
224 AND so.trans_type = ".ST_SALESORDER."
225 AND so.debtor_no = $customer_id
227 JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
228 LEFT JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
229 WHERE sod.quantity > qty_sent
233 if($item_like[0]=='/') {
234 $regexp = substr($item_like, 1);
235 $sql .= " AND stk_code RLIKE '$regexp'";
238 $sql .= " AND stk_code LIKE '$item_like'";
244 function get_order_summary($location) {
246 $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
248 , sum(sod.quantity - qty_sent) as quantity
249 , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
251 , group_concat(distinct comments separator ';') as order_comment
252 , group_concat(distinct comment separator ';') as detail_comment
253 , max(quantity_before) as quantity_before
254 FROM ".TB_PREF."sales_order_details sod
255 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
256 AND so.trans_type = sod.trans_type
257 AND so.trans_type = ".ST_SALESORDER."
259 NATURAL JOIN ".TB_PREF."debtors_master
260 NATURAL JOIN ".TB_PREF."cust_branch
261 JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
262 WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
263 GROUP BY debtor_no, branch_code, stk_code
267 $sub = TB_PREF."order_summary_view";
269 $sql = "SELECT debtor_no, debtor_ref, branch_ref
270 , `min(delivery_date)`
271 , sum(sub.quantity) as quantity
272 , sum(sub.amount) as amount
273 , sum(greatest(least(sub.quantity, qoh.quantity - quantity_before), 0))
274 , sum(sub.amount*greatest(least(sub.quantity, qoh.quantity - quantity_before), 0)/sub.quantity)
275 , min(`min(required_date)`)
276 , group_concat(distinct order_comment separator ';') as order_comment
277 , group_concat(distinct detail_comment separator ';') as detail_comment
279 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
280 GROUP BY debtor_no, debtor_ref