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)) {
31 function post_to_detail_ids() {
32 $detail_ids = array();
33 foreach($_POST['detail'] as $detail_id => $values) {
34 array_push($detail_ids, $detail_id);
40 function update_extra_order_details() {
41 if(!isset($_POST['Update']) || $_POST['Update'] != 'Update') return;
43 $cart = post_to_detail_ids();
45 hook_db_prewrite($cart, 'order_xtra');
46 // update in database all field
47 $date_fields = array('hold_until_date', 'required_date', 'expiry_date', 'priority');
48 foreach($_POST['detail'] as $detail_id => $values) {
50 if($comment = $values['comment']) array_push($set, " comment='$comment' ");
51 foreach($date_fields as $date_field) {
52 if(isset($values[$date_field])) {
53 $input_date = trim($values[$date_field]);
54 if($input_date == "") {
55 // Only the priority field can be set to null.
56 if($date_field == 'priority') array_push($set," $date_field = NULL");
58 $date = date2sql($input_date);
59 array_push($set," $date_field='$date' ");
65 $sql = "UPDATE ".TB_PREF."sales_order_details SET ".implode($set,', ')." WHERE id = $detail_id ";
68 display_error('Problem while updating order details. Try again');
74 hook_db_postwrite($cart, 'order_xtra');
78 display_notification('Order details updated');
82 function split_order_details() {
83 if(!isset($_POST['Split']) || $_POST['Split'] != 'Split') return;
84 $splitter = new Splitter($_POST);
86 $cart = post_to_detail_ids();
87 hook_db_prewrite($cart, 'order_xtra');
88 if($splitter->splitAll()) display_warning("Items have been split.");
89 hook_db_postwrite($cart, 'order_xtra');
93 function compute_input_name($row, $field) {
95 return "detail[$row_id][$field]";
98 function view_link($dummy, $order_no)
100 return get_customer_trans_view_str(ST_SALESORDER, $order_no);
103 function item_link($dummy, $stock_id)
105 return pager_link( _($stock_id),
106 "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
109 function order_link($row)
111 return pager_link( _("Sales Order"),
112 "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
115 function customer_link($row) {
116 return customer_link2($row['debtor_ref'], $row['debtor_no']);
119 function customer_link2($name, $id) {
120 return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
124 function aggregate_comment($row) {
125 $comment = $row['order_comment'].";".$row['detail_comment'];
127 $comments = array_map('trim', explode(';', $comment));
128 $comments = array_filter($comments);
129 $first = array_shift($comments);
130 if(count($comments) == 0)
133 $tooltip = implode('<br> - ', $comments);
134 return "$first <span class='before-tooltip'>more<span class='tooltip'> $tooltip</span><span>";
140 function available_quantity($row, $available) {
141 $quantity = $row['quantity'];
143 if($available== 0) $class = 'limited';
144 else if($quantity > $available) $class = 'partial';
146 return "<span class='$class'>$available<span>";
149 function input_date_details($row, $field_name, $date) {
150 $row_id = $row['id'];
151 $name = compute_input_name($row, $field_name);
153 $_POST[$name] = sql2date($date);
155 start_extract_cell();
156 date_cells (null, $name, 'title', null, 0,0,1001);
157 return end_extract_cell();
161 <input type='text' value='$date' name='$name'>
165 function input_priority_date_details($row, $date) {
166 return input_date_details($row, 'priority', $date);
169 function input_hold_until_date_details($row, $date) {
170 return input_date_details($row, 'hold_until_date', $date);
173 function input_required_date_details($row, $date) {
174 return input_date_details($row, 'required_date', $date);
177 function input_expiry_date_details($row, $date) {
178 return input_date_details($row, 'expiry_date', $date);
181 function input_comment_details($row, $comment) {
182 $row_id = $row['id'];
184 <input type='text' value='$comment' name='detail[$row_id][comment]'>
188 function get_order_details_extra($customer_id, $location) {
189 $sql = "SELECT sod.id
192 , sod.quantity - qty_sent quantity
193 , GREATEST(0, LEAST(qoh.quantity - quantity_before, sod.quantity - qty_sent))
200 FROM ".TB_PREF."sales_order_details sod
201 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
202 AND so.trans_type = sod.trans_type
203 AND so.trans_type = ".ST_SALESORDER."
204 AND so.debtor_no = $customer_id
206 JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
207 LEFT JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
208 WHERE sod.quantity > qty_sent
214 function get_order_summary($location) {
216 $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
218 , sum(sod.quantity - qty_sent) as quantity
219 , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
221 , group_concat(distinct comments separator ';') as order_comment
222 , group_concat(distinct comment separator ';') as detail_comment
223 , max(quantity_before) as quantity_before
224 FROM ".TB_PREF."sales_order_details sod
225 JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
226 AND so.trans_type = sod.trans_type
227 AND so.trans_type = ".ST_SALESORDER."
229 NATURAL JOIN ".TB_PREF."debtors_master
230 NATURAL JOIN ".TB_PREF."cust_branch
231 JOIN ".TB_PREF."denorm_order_details_queue d ON (d.id = sod.id)
232 WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
233 GROUP BY debtor_no, branch_code, stk_code
237 $sub = TB_PREF."order_summary_view";
239 $sql = "SELECT debtor_no, debtor_ref, branch_ref
240 , `min(delivery_date)`
241 , sum(sub.quantity) as quantity
242 , sum(sub.amount) as amount
243 , sum(greatest(least(sub.quantity, qoh.quantity - quantity_before), 0))
244 , sum(sub.amount*greatest(least(sub.quantity, qoh.quantity - quantity_before), 0)/sub.quantity)
245 , min(`min(required_date)`)
246 , group_concat(distinct order_comment separator ';') as order_comment
247 , group_concat(distinct detail_comment separator ';') as detail_comment
249 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
250 GROUP BY debtor_no, debtor_ref