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