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