Display available quantity in order line view (for customer)
[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         foreach($_POST['detail'] as $detail_id => $values) {
38                 $set = array();
39                 if($comment = $values['comment'])  array_push($set, " comment='$comment' ");
40                 if(isset($values['required_date'])) {
41                         $required_date = trim($values['required_date']);
42                         if($required_date == "")  {
43                                 array_push($set," required_date = NULL");
44                         } else {
45                                 $date = date2sql($required_date);
46                                 array_push($set," required_date='$date' ");
47                         }
48                 }
49
50                 if(!empty($set))  {
51                         $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
52                         
53                         if(!db_query($sql)) {
54                                 display_error('Problem while updating order details. Try again');
55                                 cancel_transaction();
56                                 return;
57                         }
58                 }
59         
60         }
61
62         display_notification('Order details updated');
63
64 }
65
66 function compute_input_name($row, $field) {
67         $row_id = $row['id'];
68         return "detail[$row_id][$field]";
69 }
70
71 function view_link($dummy, $order_no)
72 {
73         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
74 }
75
76 function item_link($dummy, $stock_id)
77 {
78   return pager_link( _($stock_id),
79         "/modules/order_line_extra/item_schedule.php?stock_id=" .$stock_id);
80 }
81
82 function order_link($row)
83 {
84   return pager_link( _("Sales Order"),
85         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
86 }
87
88 function customer_link($row) {
89         return customer_link2($row['debtor_ref'], $row['debtor_no']);
90 }
91
92 function customer_link2($name, $id) {
93         return pager_link(_($name), "/modules/order_line_extra/order_lines_view.php?customer_id=${id}");
94         
95 }
96
97 function aggregate_comment($row) {
98         $comment =  $row['order_comment'].";".$row['detail_comment'];
99         if($comment) {
100                 $comments = array_map('trim', explode(';', $comment));
101                 $comments = array_filter($comments);
102                 $first = array_shift($comments);
103                 if(count($comments) == 0)
104                         return $first;
105                 else {
106                         $tooltip = implode('<br> - ', $comments);
107                         return "$first <span class='before-tooltip'>more<span class='tooltip'>&nbsp$tooltip</span><span>";
108                 }
109         }
110         return '';
111 }
112
113 function input_date_details($row, $date) {
114 $row_id = $row['id'];
115         $name = compute_input_name($row, 'required_date');
116
117 $_POST[$name] = sql2date($date);
118
119         start_extract_cell();
120                 date_cells (null, $name, 'title', null, 0,0,1001);
121         return end_extract_cell();
122
123
124         return "
125 <input type='text' value='$date' name='$name'>
126 ";
127 }
128
129 function input_comment_details($row, $comment) {
130 $row_id = $row['id'];
131         return "
132 <input type='text' value='$comment' name='detail[$row_id][comment]'>
133 ";
134 }
135
136 function get_order_details_extra($customer_id, $location) {
137         $sql = "SELECT sod.id
138         , so.order_no
139         , stk_code
140         , sod.quantity - qty_sent
141         , GREATEST(0, LEAST(qoh.quantity  - quantity_before, sod.quantity - qty_sent))
142         , quantity_before
143         , required_date
144         ,comment
145         FROM ".TB_PREF."sales_order_details sod
146         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
147                 AND so.trans_type = sod.trans_type
148                 AND so.trans_type = ".ST_SALESORDER."
149                 AND so.debtor_no = $customer_id
150         )
151         JOIN ".TB_PREF."denorm_qoh qoh ON (stock_id = stk_code AND loc_code = '$location')
152         JOIN ".TB_PREF."denorm_order_details_queue  d ON (d.id = sod.id)
153         WHERE sod.quantity > qty_sent
154         ";
155
156         return $sql;
157 }
158
159 function get_order_summary() {
160         $sql = "SELECT debtor_no, debtor_ref, branch_ref
161         , min(delivery_date)
162         , sum(quantity - qty_sent) as quantity
163         , sum((quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
164         , min(required_date)
165         , group_concat(distinct comments separator ';') as order_comment
166         , group_concat(distinct comment separator ';') as detail_comment
167         FROM ".TB_PREF."sales_order_details sod
168         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
169                 AND so.trans_type = sod.trans_type
170                 AND so.trans_type = ".ST_SALESORDER."
171         )
172         NATURAL JOIN ".TB_PREF."debtors_master
173         NATURAL JOIN ".TB_PREF."cust_branch
174         WHERE quantity > qty_sent AND ".ST_SALESORDER."
175         GROUP BY debtor_no, branch_code
176         ";
177
178         return $sql;
179 }
180 ?>