BulkUpdater work.
[order_line_extra.git] / includes / order_lines.inc
1 <?php
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.
9  */
10 function start_extract_cell() {
11         // We need 
12         ob_start();
13 }
14
15 function end_extract_cell() {
16         $content = ob_get_contents();
17         ob_end_clean();
18         return extract_cell($content);
19 }
20
21 function extract_cell($td) {
22         if(preg_match('/\s*<td>(.*)<\/td>\s*/s', $td, $matches)) {
23                 return $matches[1];
24         }
25         else {
26                 return $td;
27         }
28 }
29
30 /* remove unchecked data so each
31  * funcion doesn't to check what's been checked or not. 
32  */
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;
38         }
39         $data['detail'] = $new_details;
40 }
41
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);
47         }
48
49         return $detail_ids;
50 }
51
52 function update_extra_order_details() {
53         if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
54
55         $cart = post_to_detail_ids();
56         if($cart === null) return;
57 begin_transaction();
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) {
62                 $set = array();
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");
71                                 } else {
72                                         $date = date2sql($input_date);
73                                         array_push($set," $date_field='$date' ");
74                                 }
75                         }
76                 }
77
78                 if(!empty($set))  {
79                         $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
80
81                         if(!db_query($sql)) {
82                                 display_error('Problem while updating order details. Try again');
83                                 cancel_transaction();
84                                 return;
85                         }
86                 }
87         }
88         hook_db_postwrite($cart, 'order_xtra');
89         commit_transaction();
90
91
92         display_notification('Order details updated');
93
94 }
95
96 function split_order_details() {
97         if(!isset($_POST['Split']) || $_POST['Split'] != 'Split')  return;
98         $splitter = new Splitter($_POST);
99         begin_transaction();
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();
106 }
107
108 function bulk_update_order_details() {
109         if(!isset($_POST['Bulk']) || $_POST['Bulk'] != 'Bulk')  return;
110         $bulk_updater = new BulkUpdater($_POST);
111         begin_transaction();
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();
118 }
119
120 function compute_input_name($row, $field) {
121         $row_id = $row['id'];
122         return "detail[$row_id][$field]";
123 }
124
125 function view_link($dummy, $order_no)
126 {
127         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
128 }
129
130 function item_link($dummy, $stock_id)
131 {
132         return pager_link( _($stock_id),
133         "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
134 }
135
136 function order_link($row)
137 {
138         return pager_link( _("Sales Order"),
139         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
140 }
141
142 function customer_link($row) {
143         return customer_link2($row['debtor_ref'], $row['debtor_no']);
144 }
145
146 function customer_link2($name, $id) {
147         return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
148
149 }
150
151 function aggregate_comment($row) {
152         $comment =  $row['order_comment'].";".$row['detail_comment'];
153         if($comment) {
154                 $comments = array_map('trim', explode(';', $comment));
155                 $comments = array_filter($comments);
156                 $first = array_shift($comments);
157                 if(count($comments) == 0)
158                         return $first;
159                 else {
160                         $tooltip = implode('<br> - ', $comments);
161                         return "$first <span class='before-tooltip'>more<span class='tooltip'>&nbsp$tooltip</span><span>";
162                 }
163         }
164         return '';
165 }
166
167 function available_quantity($row, $available) {
168         $quantity = $row['quantity'];
169         $class = '';
170         if($available== 0) $class = 'limited';
171         else if($quantity > $available)  $class = 'partial';
172
173         return "<span class='$class'>$available<span>";
174 }
175
176 function input_date_details($row, $field_name, $date) {
177 $row_id = $row['id'];
178         $name = compute_input_name($row, $field_name);
179
180 $_POST[$name] = sql2date($date);
181
182         start_extract_cell();
183                 date_cells (null, $name, 'title', null, 0,0,1001);
184         return end_extract_cell();
185
186
187         return "
188 <input type='text' value='$date' name='$name'>
189 ";
190 }
191
192 function input_priority_date_details($row, $date) {
193         return input_date_details($row, 'priority', $date);
194 }
195
196 function input_hold_until_date_details($row, $date) {
197         return input_date_details($row, 'hold_until_date', $date);
198 }
199
200 function input_required_date_details($row, $date) {
201         return input_date_details($row, 'required_date', $date);
202 }
203
204 function input_expiry_date_details($row, $date) {
205         return input_date_details($row, 'expiry_date', $date);
206 }
207
208 function input_comment_details($row, $comment) {
209 $row_id = $row['id'];
210         return "
211 <input type='text' value='$comment' name='detail[$row_id][comment]'>
212 ";
213 }
214
215 function input_check_details($row, $dummy) {
216         $row_id = $row['id'];
217                 return "
218                 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
219 }
220
221 function get_order_details_extra($customer_id, $location, $item_like) {
222         $sql = "SELECT sod.id
223         , so.order_no
224         , stk_code
225         , sod.quantity - qty_sent quantity
226         , GREATEST(0, LEAST(qoh.quantity  - quantity_before, sod.quantity - qty_sent))
227         , quantity_before
228         , sod.`priority`
229         , hold_until_date
230         , required_date
231         , expiry_date
232         ,comment
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
238         )
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
242         ";
243
244         if($item_like) {
245                         if($item_like[0]=='/')  {
246                                 $regexp = substr($item_like, 1);
247                                 $sql .= " AND stk_code RLIKE '$regexp'";
248                         }
249                         else 
250                                 $sql .= " AND stk_code LIKE '$item_like'";
251         }
252
253         return $sql;
254 }
255
256 function get_order_summary($location) {
257 /*
258         $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
259         , min(delivery_date)
260         , sum(sod.quantity - qty_sent) as quantity
261         , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
262         , min(required_date)
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."
270         )
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
276         ";
277 */
278
279         $sub = TB_PREF."order_summary_view";
280
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
290                                 FROM $sub sub
291                                 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
292                                 GROUP BY debtor_no, debtor_ref
293         ";
294
295         return $sql;
296 }
297 ?>