Add extract cell functions.
[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
33
34
35 function update_extra_order_details() {
36         if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
37
38 begin_transaction();
39         // update in database all field
40         foreach($_POST['detail'] as $detail_id => $values) {
41                 $set = array();
42                 if($comment = $values['comment'])  array_push($set, " comment='$comment' ");
43                 if(isset($values['required_date'])) {
44                         $required_date = trim($values['required_date']);
45                         if($required_date == "")  {
46                                 array_push($set," required_date = NULL");
47                         } else {
48                                 $date = date2sql($required_date);
49                                 array_push($set," required_date='$date' ");
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         
63         }
64
65         display_notification('Order details updated');
66
67 }
68
69 function compute_input_name($row, $field) {
70         $row_id = $row['id'];
71         return "detail[$row_id][$field]";
72 }
73
74 function view_link($dummy, $order_no)
75 {
76         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
77 }
78
79
80 function order_link($row)
81 {
82   return pager_link( _("Sales Order"),
83         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
84 }
85
86 function customer_link($row) {
87         return pager_link(_($row['debtor_ref']), "/modules/order_line_extra/order_lines_view.php?customer_id=${row['debtor_no']}");
88         
89 }
90
91 function aggregate_comment($row) {
92         $comment =  $row['order_comment'].$row['detail_comment'];
93         if($comment) {
94                 $comments = array_map('trim', explode(';', $comment));
95                 $comments = array_filter($comments);
96                 $first = array_shift($comments);
97                 if(count($comments) == 0)
98                         return $first;
99                 else {
100                         $tooltip = implode('<br>', $comments);
101                         return "$first <span class='before-tooltip'>more</span><span class='tooltip'>&nbsp$tooltip</span>";
102                 }
103         }
104         return '';
105 }
106
107 function input_date_details($row, $date) {
108 $row_id = $row['id'];
109         $name = compute_input_name($row, 'required_date');
110
111 $_POST[$name] = sql2date($date);
112
113         start_extract_cell();
114                 date_cells (null, $name, 'title', null, 0,0,1001);
115         return end_extract_cell();
116
117
118         return "
119 <input type='text' value='$date' name='$name'>
120 ";
121 }
122
123 function input_comment_details($row, $comment) {
124 $row_id = $row['id'];
125         return "
126 <input type='text' value='$comment' name='detail[$row_id][comment]'>
127 ";
128 }
129
130 function get_order_details_extra($customer_id) {
131         $sql = "SELECT sod.id
132         , so.order_no
133         , stk_code
134         , quantity - qty_sent
135         , required_date
136         ,comment
137         FROM ".TB_PREF."sales_order_details sod
138         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
139                 AND so.trans_type = sod.trans_type
140                 AND so.trans_type = ".ST_SALESORDER."
141                 AND so.debtor_no = $customer_id
142         )
143         WHERE quantity > qty_sent
144         ";
145
146         return $sql;
147 }
148
149 function get_order_summary() {
150         $sql = "SELECT debtor_no, debtor_ref, branch_ref
151         , min(delivery_date)
152         , sum(quantity - qty_sent) as quantity
153         , sum((quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
154         , min(required_date)
155         , group_concat(distinct comments separator ';') as order_comment
156         , group_concat(distinct comment separator ';') as detail_comment
157         FROM ".TB_PREF."sales_order_details sod
158         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
159                 AND so.trans_type = sod.trans_type
160                 AND so.trans_type = ".ST_SALESORDER."
161         )
162         NATURAL JOIN ".TB_PREF."debtors_master
163         NATURAL JOIN ".TB_PREF."cust_branch
164         WHERE quantity > qty_sent AND ".ST_SALESORDER."
165         GROUP BY debtor_no, branch_code
166         ";
167
168         return $sql;
169 }
170 ?>