c6408bd9688cf19fa73aee2b6cd0e6e754f9622b
[fa-stable.git] / sales / includes / db / sales_order_db.inc
1 <?php
2
3 //----------------------------------------------------------------------------------------      
4
5 function add_sales_order($order)
6 {
7         begin_transaction();
8
9         $del_date = date2sql($order->delivery_date);
10
11         $sql = "INSERT INTO ".TB_PREF."sales_orders (debtor_no, branch_code, customer_ref, Comments, ord_date, 
12                 order_type, ship_via, deliver_to, delivery_address, contact_phone, 
13                 contact_email, freight_cost, from_stk_loc, delivery_date) 
14                 VALUES ('" . $order->customer_id . "', '" . $order->Branch . "', '". 
15                         $order->cust_ref ."','". db_escape($order->Comments) ."','" . 
16                         date2sql($order->orig_order_date) . "', '" . 
17                         $order->default_sales_type . "', " . 
18                         $_POST['ship_via'] .",'" . $order->deliver_to . "', '" . 
19                         $order->delivery_address . "', '" .  
20                         $order->phone . "', '" . $order->email . "', " . 
21                         $order->freight_cost .", '" . $order->Location ."', '" . 
22                         $del_date . "')";
23
24         db_query($sql, "order Cannot be Added");
25
26         $order_no = db_insert_id();
27         
28         foreach ($order->line_items as $stock_item) 
29         {
30                 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
31                 $sql .= $order_no . 
32                                 ",'$stock_item->stock_id', '$stock_item->item_description', $stock_item->price, 
33                                 $stock_item->quantity, 
34                                 $stock_item->discount_percent)";
35                 db_query($sql, "order Details Cannot be Added");
36                 
37         } /* inserted line items into sales order details */
38         
39         add_forms_for_sys_type(systypes::sales_order(), $order_no);     
40         
41         commit_transaction();
42         
43         return $order_no;               
44 }
45
46 //----------------------------------------------------------------------------------------
47
48 function delete_sales_order($order_no)
49 {
50         begin_transaction();
51                 
52         $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
53         db_query($sql, "order Header Delete");
54
55         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
56         db_query($sql, "order Detail Delete");
57         
58         delete_forms_for_systype(systypes::sales_order(), $order_no);
59         
60         commit_transaction();   
61 }
62
63 //----------------------------------------------------------------------------------------
64
65 function update_sales_order($order_no, $order)
66 {
67         $del_date = date2sql($order->delivery_date);
68         $ord_date = date2sql($order->orig_order_date);
69
70         begin_transaction();
71
72         $sql = "UPDATE ".TB_PREF."sales_orders SET debtor_no = '" . $order->customer_id . "', 
73                 branch_code = '" . $order->Branch . "', 
74                 customer_ref = '". $order->cust_ref ."', 
75                 Comments = '". db_escape($order->Comments) ."', ord_date = '" . $ord_date . "', 
76                 order_type = '" . $order->default_sales_type . "', ship_via = " . $order->ship_via .", 
77                 deliver_to = '" . $order->deliver_to . "', delivery_address = '" . $order->delivery_address . "', 
78                 contact_phone = '" . $order->phone . "', 
79                 contact_email = '" . $order->email . "', freight_cost = " . $order->freight_cost .", 
80                 from_stk_loc = '" . $order->Location ."', delivery_date = '" . $del_date . "'
81                 WHERE order_no=" . $order_no;
82
83         db_query($sql, "order Cannot be Updated");
84
85         $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
86
87         db_query($sql, "Old order Cannot be Deleted");
88
89         foreach ($order->line_items as $stock_item) 
90         {
91
92                 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code,  description, unit_price, quantity, discount_percent, qty_invoiced) VALUES (";
93                 
94                 $sql .= $order_no . ",'" . $stock_item->stock_id . "','" . $stock_item->item_description . "', " . $stock_item->price . ", " . $stock_item->quantity . ", " . $stock_item->discount_percent . ", " . $stock_item->qty_inv . " )";
95
96                 db_query($sql, "Old order Cannot be Inserted");
97
98         } /* inserted line items into sales order details */
99
100         commit_transaction();
101 }
102
103 //----------------------------------------------------------------------------------------
104
105 function get_sales_order($order_no)
106 {
107         $sql = "SELECT ".TB_PREF."sales_orders.*, ".TB_PREF."debtors_master.name, ".TB_PREF."debtors_master.curr_code, ".TB_PREF."locations.location_name, 
108                 ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."debtors_master.discount, ".TB_PREF."sales_types.sales_type, ".TB_PREF."shippers.shipper_name,
109                 ".TB_PREF."tax_groups.name AS tax_group_name , ".TB_PREF."tax_groups.id AS tax_group_id 
110                 FROM ".TB_PREF."sales_orders, ".TB_PREF."debtors_master, ".TB_PREF."sales_types, ".TB_PREF."tax_groups, ".TB_PREF."cust_branch, ".TB_PREF."locations, ".TB_PREF."shippers  
111                         WHERE ".TB_PREF."sales_orders.order_type=".TB_PREF."sales_types.id
112                                 AND ".TB_PREF."cust_branch.branch_code = ".TB_PREF."sales_orders.branch_code 
113                                 AND ".TB_PREF."cust_branch.tax_group_id = ".TB_PREF."tax_groups.id
114                                 AND ".TB_PREF."sales_orders.debtor_no = ".TB_PREF."debtors_master.debtor_no 
115                                 AND ".TB_PREF."locations.loc_code = ".TB_PREF."sales_orders.from_stk_loc
116                                 AND ".TB_PREF."shippers.shipper_id = ".TB_PREF."sales_orders.ship_via
117                                 AND ".TB_PREF."sales_orders.order_no = " . $order_no;
118                                                                         
119         $result = db_query($sql, "order Retreival");    
120
121         $num = db_num_rows($result);    
122         if ($num > 1) 
123         {
124                 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
125         } 
126         else if ($num == 1) 
127         {
128                 return db_fetch($result);
129         }
130         else
131                 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
132                                                                                 
133 }
134
135 //----------------------------------------------------------------------------------------
136
137 function read_sales_order($order_no, &$order, $skip_completed_items=false)
138 {
139         $myrow = get_sales_order($order_no);
140
141         $order->customer_id = $myrow["debtor_no"];
142         $order->Branch = $myrow["branch_code"];
143         $order->customer_name = $myrow["name"];
144         $order->cust_ref = $myrow["customer_ref"];
145         $order->default_sales_type =$myrow["order_type"];
146         $order->sales_type_name =$myrow["sales_type"];          
147         $order->customer_currency = $myrow["curr_code"];
148         $order->default_discount = $myrow["discount"];          
149         
150         $order->Comments = $myrow["comments"];
151
152         $order->ship_via = $myrow["ship_via"];
153         $order->deliver_to = $myrow["deliver_to"];
154         $order->delivery_date = sql2date($myrow["delivery_date"]);
155         $order->freight_cost = $myrow["freight_cost"];
156         $order->delivery_address = $myrow["delivery_address"];
157         $order->phone = $myrow["contact_phone"];
158         $order->email = $myrow["contact_email"];
159         $order->Location = $myrow["from_stk_loc"];
160         $order->location_name = $myrow["location_name"];
161         $order->orig_order_date = sql2date($myrow["ord_date"]);
162         
163         $order->tax_group_name = $myrow["tax_group_name"];
164         $order->tax_group_id = $myrow["tax_group_id"];
165         
166         $sql = "SELECT stk_code, unit_price, ".TB_PREF."sales_order_details.description,
167                 ".TB_PREF."sales_order_details.quantity, discount_percent, 
168                 qty_invoiced, ".TB_PREF."stock_master.units,
169                 ".TB_PREF."stock_master.material_cost + ".TB_PREF."stock_master.labour_cost + ".TB_PREF."stock_master.overhead_cost AS standard_cost
170                 FROM ".TB_PREF."sales_order_details, ".TB_PREF."stock_master 
171                         WHERE ".TB_PREF."sales_order_details.stk_code = ".TB_PREF."stock_master.stock_id 
172                                 AND order_no =" . $order_no;
173                                                 
174         if ($skip_completed_items)
175                 $sql .= " 
176                         AND ".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_invoiced > 0 ";                                                        
177
178         $result = db_query($sql, "Retreive order Line Items");
179
180         if (db_num_rows($result) > 0) 
181         {
182
183                 while ($myrow = db_fetch($result)) 
184                 {
185                         $order->add_to_cart($myrow["stk_code"],$myrow["quantity"],
186                                 $myrow["unit_price"], $myrow["discount_percent"],
187                                 $myrow["qty_invoiced"], $myrow["standard_cost"], $myrow["description"]);
188                 } 
189         } 
190         
191         return true;
192 }
193
194 //----------------------------------------------------------------------------------------
195
196 function sales_order_has_invoices($order_no)
197 {
198         $sql = "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE order_=$order_no";
199         
200         $result = db_query($sql, "could not query for sales order usage");
201         
202         $row = db_fetch_row($result);
203         
204         return ($row[0] > 0);   
205 }
206
207 //----------------------------------------------------------------------------------------
208
209 function close_sales_order($order_no)
210 {
211         // set the quantity of each item to the already invoiced quantity. this will mark item as closed.
212         $sql = "UPDATE ".TB_PREF."sales_order_details 
213                 SET quantity = qty_invoiced 
214                         WHERE order_no = $order_no";    
215         
216         db_query($sql, "The sales order detail record could not be updated");                           
217 }
218
219 //----------------------------------------------------------------------------------------
220
221 function dispatch_sales_order_item($order_no, $stock_id, $qty_dispatched)
222 {
223         $sql = "UPDATE ".TB_PREF."sales_order_details 
224                 SET qty_invoiced = qty_invoiced + $qty_dispatched ";
225         $sql .= " WHERE order_no = $order_no 
226                 AND stk_code = '$stock_id'";
227
228         db_query($sql, "The sales order detail record could not be updated");   
229 }
230
231 //---------------------------------------------------------------------------------------------------------------
232
233 function get_invoice_duedate($debtorno, $invdate)
234 {
235         if (!is_date($invdate)) 
236         {
237                 return Today();
238         }
239     $sql = "SELECT ".TB_PREF."debtors_master.debtor_no, ".TB_PREF."debtors_master.payment_terms, ".TB_PREF."payment_terms.* FROM ".TB_PREF."debtors_master,
240                 ".TB_PREF."payment_terms WHERE ".TB_PREF."debtors_master.payment_terms = ".TB_PREF."payment_terms.terms_indicator AND
241                 ".TB_PREF."debtors_master.debtor_no = '$debtorno'";
242
243     $result = db_query($sql,"The customer details could not be retrieved");
244     $myrow = db_fetch($result);
245
246     if (db_num_rows($result) == 0)
247         return $invdate;
248     if ($myrow['day_in_following_month'] > 0)
249         $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
250         else
251         $duedate = add_days($invdate, $myrow['days_before_due']);
252     return $duedate;
253 }
254
255
256 ?>