Hook only modify what's needed. MUCH faster.
[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 print_r('cacou');
27                 return $td;
28         }
29 }
30
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);
35         }
36
37         return $detail_ids;
38 }
39
40 function update_extra_order_details() {
41         if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
42
43         $cart = post_to_detail_ids();
44 begin_transaction();
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) {
49                 $set = array();
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");
57                                 } else {
58                                         $date = date2sql($input_date);
59                                         array_push($set," $date_field='$date' ");
60                                 }
61                         }
62                 }
63
64                 if(!empty($set))  {
65                         $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
66
67                         if(!db_query($sql)) {
68                                 display_error('Problem while updating order details. Try again');
69                                 cancel_transaction();
70                                 return;
71                         }
72                 }
73         }
74         hook_db_postwrite($cart, 'order_xtra');
75         commit_transaction();
76
77
78         display_notification('Order details updated');
79
80 }
81
82 function split_order_details() {
83         if(!isset($_POST['Split']) || $_POST['Split'] != 'Split')  return;
84         $splitter = new Splitter($_POST);
85         begin_transaction();
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');
90         commit_transaction();
91 }
92
93 function compute_input_name($row, $field) {
94         $row_id = $row['id'];
95         return "detail[$row_id][$field]";
96 }
97
98 function view_link($dummy, $order_no)
99 {
100         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
101 }
102
103 function item_link($dummy, $stock_id)
104 {
105         return pager_link( _($stock_id),
106         "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
107 }
108
109 function order_link($row)
110 {
111         return pager_link( _("Sales Order"),
112         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
113 }
114
115 function customer_link($row) {
116         return customer_link2($row['debtor_ref'], $row['debtor_no']);
117 }
118
119 function customer_link2($name, $id) {
120         return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
121
122 }
123
124 function aggregate_comment($row) {
125         $comment =  $row['order_comment'].";".$row['detail_comment'];
126         if($comment) {
127                 $comments = array_map('trim', explode(';', $comment));
128                 $comments = array_filter($comments);
129                 $first = array_shift($comments);
130                 if(count($comments) == 0)
131                         return $first;
132                 else {
133                         $tooltip = implode('<br> - ', $comments);
134                         return "$first <span class='before-tooltip'>more<span class='tooltip'>&nbsp$tooltip</span><span>";
135                 }
136         }
137         return '';
138 }
139
140 function available_quantity($row, $available) {
141         $quantity = $row['quantity'];
142         $class = '';
143         if($available== 0) $class = 'limited';
144         else if($quantity > $available)  $class = 'partial';
145
146         return "<span class='$class'>$available<span>";
147 }
148
149 function input_date_details($row, $field_name, $date) {
150 $row_id = $row['id'];
151         $name = compute_input_name($row, $field_name);
152
153 $_POST[$name] = sql2date($date);
154
155         start_extract_cell();
156                 date_cells (null, $name, 'title', null, 0,0,1001);
157         return end_extract_cell();
158
159
160         return "
161 <input type='text' value='$date' name='$name'>
162 ";
163 }
164
165 function input_priority_date_details($row, $date) {
166         return input_date_details($row, 'priority', $date);
167 }
168
169 function input_hold_until_date_details($row, $date) {
170         return input_date_details($row, 'hold_until_date', $date);
171 }
172
173 function input_required_date_details($row, $date) {
174         return input_date_details($row, 'required_date', $date);
175 }
176
177 function input_expiry_date_details($row, $date) {
178         return input_date_details($row, 'expiry_date', $date);
179 }
180
181 function input_comment_details($row, $comment) {
182 $row_id = $row['id'];
183         return "
184 <input type='text' value='$comment' name='detail[$row_id][comment]'>
185 ";
186 }
187
188 function get_order_details_extra($customer_id, $location) {
189         $sql = "SELECT sod.id
190         , so.order_no
191         , stk_code
192         , sod.quantity - qty_sent quantity
193         , GREATEST(0, LEAST(qoh.quantity  - quantity_before, sod.quantity - qty_sent))
194         , quantity_before
195         , sod.`priority`
196         , hold_until_date
197         , required_date
198         , expiry_date
199         ,comment
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
205         )
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
209         ";
210
211         return $sql;
212 }
213
214 function get_order_summary($location) {
215 /*
216         $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
217         , min(delivery_date)
218         , sum(sod.quantity - qty_sent) as quantity
219         , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
220         , min(required_date)
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."
228         )
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
234         ";
235 */
236
237         $sub = TB_PREF."order_summary_view";
238
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
248                                 FROM $sub sub
249                                 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
250                                 GROUP BY debtor_no, debtor_ref
251         ";
252
253         return $sql;
254 }
255 ?>