Call hooks on split.
[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 update_extra_order_details() {
32         if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
33
34 begin_transaction();
35         // update in database all field
36         $date_fields = array('hold_until_date', 'required_date', 'expiry_date', 'priority');
37         foreach($_POST['detail'] as $detail_id => $values) {
38                 $set = array();
39                 if($comment = $values['comment'])  array_push($set, " comment='$comment' ");
40                 foreach($date_fields as $date_field) {
41                         if(isset($values[$date_field])) {
42                                 $input_date = trim($values[$date_field]);
43                                 if($input_date == "")  {
44                                         // Only the priority field can be set to null.
45                                         if($date_field == 'priority') array_push($set," $date_field = NULL");
46                                 } else {
47                                         $date = date2sql($input_date);
48                                         array_push($set," $date_field='$date' ");
49                                 }
50                         }
51                 }
52
53                 if(!empty($set))  {
54                         $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
55
56                         if(!db_query($sql)) {
57                                 display_error('Problem while updating order details. Try again');
58                                 cancel_transaction();
59                                 return;
60                         }
61                 }
62 commit_transaction();
63
64         }
65
66         display_notification('Order details updated');
67
68 }
69
70 function split_order_details() {
71         if(!isset($_POST['Split']) || $_POST['Split'] != 'Split')  return;
72         $splitter = new Splitter($_POST);
73         begin_transaction();
74         $cart  = null;
75         hook_db_prewrite($cart, 'order_xtra');
76                 if($splitter->splitAll()) display_warning("Items have been split.");
77         hook_db_postwrite($cart, 'order_xtra');
78         commit_transaction();
79 }
80
81 function compute_input_name($row, $field) {
82         $row_id = $row['id'];
83         return "detail[$row_id][$field]";
84 }
85
86 function view_link($dummy, $order_no)
87 {
88         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
89 }
90
91 function item_link($dummy, $stock_id)
92 {
93         return pager_link( _($stock_id),
94         "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
95 }
96
97 function order_link($row)
98 {
99         return pager_link( _("Sales Order"),
100         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
101 }
102
103 function customer_link($row) {
104         return customer_link2($row['debtor_ref'], $row['debtor_no']);
105 }
106
107 function customer_link2($name, $id) {
108         return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
109
110 }
111
112 function aggregate_comment($row) {
113         $comment =  $row['order_comment'].";".$row['detail_comment'];
114         if($comment) {
115                 $comments = array_map('trim', explode(';', $comment));
116                 $comments = array_filter($comments);
117                 $first = array_shift($comments);
118                 if(count($comments) == 0)
119                         return $first;
120                 else {
121                         $tooltip = implode('<br> - ', $comments);
122                         return "$first <span class='before-tooltip'>more<span class='tooltip'>&nbsp$tooltip</span><span>";
123                 }
124         }
125         return '';
126 }
127
128 function available_quantity($row, $available) {
129         $quantity = $row['quantity'];
130         $class = '';
131         if($available== 0) $class = 'limited';
132         else if($quantity > $available)  $class = 'partial';
133
134         return "<span class='$class'>$available<span>";
135 }
136
137 function input_date_details($row, $field_name, $date) {
138 $row_id = $row['id'];
139         $name = compute_input_name($row, $field_name);
140
141 $_POST[$name] = sql2date($date);
142
143         start_extract_cell();
144                 date_cells (null, $name, 'title', null, 0,0,1001);
145         return end_extract_cell();
146
147
148         return "
149 <input type='text' value='$date' name='$name'>
150 ";
151 }
152
153 function input_priority_date_details($row, $date) {
154         return input_date_details($row, 'priority', $date);
155 }
156
157 function input_hold_until_date_details($row, $date) {
158         return input_date_details($row, 'hold_until_date', $date);
159 }
160
161 function input_required_date_details($row, $date) {
162         return input_date_details($row, 'required_date', $date);
163 }
164
165 function input_expiry_date_details($row, $date) {
166         return input_date_details($row, 'expiry_date', $date);
167 }
168
169 function input_comment_details($row, $comment) {
170 $row_id = $row['id'];
171         return "
172 <input type='text' value='$comment' name='detail[$row_id][comment]'>
173 ";
174 }
175
176 function get_order_details_extra($customer_id, $location) {
177         $sql = "SELECT sod.id
178         , so.order_no
179         , stk_code
180         , sod.quantity - qty_sent quantity
181         , GREATEST(0, LEAST(qoh.quantity  - quantity_before, sod.quantity - qty_sent))
182         , quantity_before
183         , sod.`priority`
184         , hold_until_date
185         , required_date
186         , expiry_date
187         ,comment
188         FROM ".TB_PREF."sales_order_details sod
189         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
190                 AND so.trans_type = sod.trans_type
191                 AND so.trans_type = ".ST_SALESORDER."
192                 AND so.debtor_no = $customer_id
193         )
194         JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
195         LEFT JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
196         WHERE sod.quantity > qty_sent
197         ";
198
199         return $sql;
200 }
201
202 function get_order_summary($location) {
203 /*
204         $sub = "SELECT debtor_no, debtor_ref, branch_ref, stk_code
205         , min(delivery_date)
206         , sum(sod.quantity - qty_sent) as quantity
207         , sum((sod.quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
208         , min(required_date)
209         , group_concat(distinct comments separator ';') as order_comment
210         , group_concat(distinct comment separator ';') as detail_comment
211         , max(quantity_before) as quantity_before
212         FROM ".TB_PREF."sales_order_details sod
213         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
214                 AND so.trans_type = sod.trans_type
215                 AND so.trans_type = ".ST_SALESORDER."
216         )
217         NATURAL JOIN ".TB_PREF."debtors_master
218         NATURAL JOIN ".TB_PREF."cust_branch
219         JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
220         WHERE sod.quantity > qty_sent AND ".ST_SALESORDER."
221         GROUP BY debtor_no, branch_code, stk_code
222         ";
223 */
224
225         $sub = TB_PREF."order_summary_view";
226
227         $sql = "SELECT debtor_no, debtor_ref, branch_ref
228         , `min(delivery_date)`
229         , sum(sub.quantity) as quantity
230         , sum(sub.amount) as amount
231         , sum(greatest(least(sub.quantity, qoh.quantity - quantity_before), 0))
232         , sum(sub.amount*greatest(least(sub.quantity, qoh.quantity - quantity_before), 0)/sub.quantity)
233         ,  min(`min(required_date)`)
234         , group_concat(distinct order_comment separator ';') as order_comment
235         , group_concat(distinct detail_comment separator ';') as detail_comment
236                                 FROM $sub sub
237                                 LEFT JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
238                                 GROUP BY debtor_no, debtor_ref
239         ";
240
241         return $sql;
242 }
243 ?>