lock(distable) all fields.
[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 compute_input_name($row, $field) {
109         $row_id = $row['id'];
110         return "detail[$row_id][$field]";
111 }
112
113 function view_link($dummy, $order_no)
114 {
115         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
116 }
117
118 function item_link($dummy, $stock_id)
119 {
120         return pager_link( _($stock_id),
121         "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
122 }
123
124 function order_link($row)
125 {
126         return pager_link( _("Sales Order"),
127         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
128 }
129
130 function customer_link($row) {
131         return customer_link2($row['debtor_ref'], $row['debtor_no']);
132 }
133
134 function customer_link2($name, $id) {
135         return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
136
137 }
138
139 function aggregate_comment($row) {
140         $comment =  $row['order_comment'].";".$row['detail_comment'];
141         if($comment) {
142                 $comments = array_map('trim', explode(';', $comment));
143                 $comments = array_filter($comments);
144                 $first = array_shift($comments);
145                 if(count($comments) == 0)
146                         return $first;
147                 else {
148                         $tooltip = implode('<br> - ', $comments);
149                         return "$first <span class='before-tooltip'>more<span class='tooltip'>&nbsp$tooltip</span><span>";
150                 }
151         }
152         return '';
153 }
154
155 function available_quantity($row, $available) {
156         $quantity = $row['quantity'];
157         $class = '';
158         if($available== 0) $class = 'limited';
159         else if($quantity > $available)  $class = 'partial';
160
161         return "<span class='$class'>$available<span>";
162 }
163
164 function input_date_details($row, $field_name, $date) {
165 $row_id = $row['id'];
166         $name = compute_input_name($row, $field_name);
167
168 $_POST[$name] = sql2date($date);
169
170         start_extract_cell();
171                 date_cells (null, $name, 'title', null, 0,0,1001);
172         return end_extract_cell();
173
174
175         return "
176 <input type='text' value='$date' name='$name'>
177 ";
178 }
179
180 function input_priority_date_details($row, $date) {
181         return input_date_details($row, 'priority', $date);
182 }
183
184 function input_hold_until_date_details($row, $date) {
185         return input_date_details($row, 'hold_until_date', $date);
186 }
187
188 function input_required_date_details($row, $date) {
189         return input_date_details($row, 'required_date', $date);
190 }
191
192 function input_expiry_date_details($row, $date) {
193         return input_date_details($row, 'expiry_date', $date);
194 }
195
196 function input_comment_details($row, $comment) {
197 $row_id = $row['id'];
198         return "
199 <input type='text' value='$comment' name='detail[$row_id][comment]'>
200 ";
201 }
202
203 function input_check_details($row, $dummy) {
204         $row_id = $row['id'];
205                 return "
206                 <input type='checkbox' class='check_detail' name='detail[$row_id][check]'>";
207 }
208
209 function get_order_details_extra($customer_id, $location, $item_like) {
210         $sql = "SELECT sod.id
211         , so.order_no
212         , stk_code
213         , sod.quantity - qty_sent quantity
214         , GREATEST(0, LEAST(qoh.quantity  - quantity_before, sod.quantity - qty_sent))
215         , quantity_before
216         , sod.`priority`
217         , hold_until_date
218         , required_date
219         , expiry_date
220         ,comment
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
226         )
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
230         ";
231
232         if($item_like) {
233                         if($item_like[0]=='/')  {
234                                 $regexp = substr($item_like, 1);
235                                 $sql .= " AND stk_code RLIKE '$regexp'";
236                         }
237                         else 
238                                 $sql .= " AND stk_code LIKE '$item_like'";
239         }
240
241         return $sql;
242 }
243
244 function get_order_summary($location) {
245 /*
246         $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
247         , min(delivery_date)
248         , sum(sod.quantity - qty_sent) as quantity
249         , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
250         , min(required_date)
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."
258         )
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
264         ";
265 */
266
267         $sub = TB_PREF."order_summary_view";
268
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
278                                 FROM $sub sub
279                                 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
280                                 GROUP BY debtor_no, debtor_ref
281         ";
282
283         return $sql;
284 }
285 ?>