b02993a2274c31449ad08404d442badaec0bd232
[order_line_extra.git] / includes / order_lines.inc
1 <?php
2 include_once($path_to_root."/includes/ui/ui_input.inc");
3
4 function update_extra_order_details() {
5         if(!isset($_POST['Update']) || $_POST['Update'] != 'Update')  return;
6
7 begin_transaction();
8         // update in database all field
9         foreach($_POST['detail'] as $detail_id => $values) {
10                 $set = array();
11                 if($comment = $values['comment'])  array_push($set, " comment='$comment' ");
12                 if(isset($values['required_date'])) {
13                         $required_date = trim($values['required_date']);
14                         if($required_date == "")  {
15                                 array_push($set," required_date = NULL");
16                         } else {
17                                 $date = date2sql($required_date);
18                                 array_push($set," required_date='$date' ");
19                         }
20                 }
21
22                 if(!empty($set))  {
23                         $sql = "UPDATE ".TB_PREF."sales_order_details  SET ".implode($set,', ')."  WHERE id = $detail_id ";
24                         
25                         if(!db_query($sql)) {
26                                 display_error('Problem while updating order details. Try again');
27                                 cancel_transaction();
28                                 return;
29                         }
30                 }
31         
32         }
33
34         display_notification('Order details updated');
35
36 }
37
38 function compute_input_name($row, $field) {
39         $row_id = $row['id'];
40         return "detail[$row_id][$field]";
41 }
42
43 function view_link($dummy, $order_no)
44 {
45         return  get_customer_trans_view_str(ST_SALESORDER, $order_no);
46 }
47
48
49 function order_link($row)
50 {
51   return pager_link( _("Sales Order"),
52         "/sales/sales_order_entry.php?NewQuoteToSalesOrder=" .$row['order_no'], ICON_DOC);
53 }
54
55 function customer_link($row) {
56         return pager_link(_($row['debtor_ref']), "/modules/order_line_extra/order_lines_view.php?customer_id=${row['debtor_no']}");
57         
58 }
59
60 function aggregate_comment($row) {
61         $comment =  $row['order_comment'].$row['detail_comment'];
62         if($comment) {
63                 $comments = array_map('trim', explode(';', $comment));
64                 $comments = array_filter($comments);
65                 $first = array_shift($comments);
66                 if(count($comments) == 0)
67                         return $first;
68                 else {
69                         $tooltip = implode('<br>', $comments);
70                         return "$first <span class='before-tooltip'>more</span><span class='tooltip'>&nbsp$tooltip</span>";
71                 }
72         }
73         return '';
74 }
75
76 function input_date_details($row, $date) {
77 $row_id = $row['id'];
78         $name = compute_input_name($row, 'required_date');
79
80 $_POST[$name] = sql2date($date);
81
82         start_extract_cell();
83                 date_cells (null, $name, 'title', null, 0,0,1001);
84         return end_extract_cell();
85
86
87         return "
88 <input type='text' value='$date' name='$name'>
89 ";
90 }
91
92 function input_comment_details($row, $comment) {
93 $row_id = $row['id'];
94         return "
95 <input type='text' value='$comment' name='detail[$row_id][comment]'>
96 ";
97 }
98
99 function get_order_details_extra($customer_id) {
100         $sql = "SELECT sod.id
101         , so.order_no
102         , stk_code
103         , quantity - qty_sent
104         , required_date
105         ,comment
106         FROM ".TB_PREF."sales_order_details sod
107         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
108                 AND so.trans_type = sod.trans_type
109                 AND so.trans_type = ".ST_SALESORDER."
110                 AND so.debtor_no = $customer_id
111         )
112         WHERE quantity > qty_sent
113         ";
114
115         return $sql;
116 }
117
118 function get_order_summary() {
119         $sql = "SELECT debtor_no, debtor_ref, branch_ref
120         , min(delivery_date)
121         , sum(quantity - qty_sent) as quantity
122         , sum((quantity - qty_sent)*unit_price*(1-discount_percent/100)) as amount
123         , min(required_date)
124         , group_concat(distinct comments separator ';') as order_comment
125         , group_concat(distinct comment separator ';') as detail_comment
126         FROM ".TB_PREF."sales_order_details sod
127         JOIN ".TB_PREF."sales_orders so ON (so.order_no = sod.order_no
128                 AND so.trans_type = sod.trans_type
129                 AND so.trans_type = ".ST_SALESORDER."
130         )
131         NATURAL JOIN ".TB_PREF."debtors_master
132         NATURAL JOIN ".TB_PREF."cust_branch
133         WHERE quantity > qty_sent AND ".ST_SALESORDER."
134         GROUP BY debtor_no, branch_code
135         ";
136
137         return $sql;
138 }
139 ?>