3 //----------------------------------------------------------------------------------------
5 function add_sales_order($order)
9 $del_date = date2sql($order->delivery_date);
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 ."', '" .
24 db_query($sql, "order Cannot be Added");
26 $order_no = db_insert_id();
28 foreach ($order->line_items as $stock_item)
30 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent) VALUES (";
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");
37 } /* inserted line items into sales order details */
39 add_forms_for_sys_type(systypes::sales_order(), $order_no);
46 //----------------------------------------------------------------------------------------
48 function delete_sales_order($order_no)
52 $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . $order_no;
53 db_query($sql, "order Header Delete");
55 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
56 db_query($sql, "order Detail Delete");
58 delete_forms_for_systype(systypes::sales_order(), $order_no);
63 //----------------------------------------------------------------------------------------
65 function update_sales_order($order_no, $order)
67 $del_date = date2sql($order->delivery_date);
68 $ord_date = date2sql($order->orig_order_date);
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;
83 db_query($sql, "order Cannot be Updated");
85 $sql = "DELETE FROM ".TB_PREF."sales_order_details WHERE order_no =" . $order_no;
87 db_query($sql, "Old order Cannot be Deleted");
89 foreach ($order->line_items as $stock_item)
92 $sql = "INSERT INTO ".TB_PREF."sales_order_details (order_no, stk_code, description, unit_price, quantity, discount_percent, qty_invoiced) VALUES (";
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 . " )";
96 db_query($sql, "Old order Cannot be Inserted");
98 } /* inserted line items into sales order details */
100 commit_transaction();
103 //----------------------------------------------------------------------------------------
105 function get_sales_order($order_no)
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;
119 $result = db_query($sql, "order Retreival");
121 $num = db_num_rows($result);
124 display_db_error("FATAL : sales order query returned a duplicate - " . db_num_rows($result), $sql, true);
128 return db_fetch($result);
131 display_db_error("FATAL : sales order return nothing - " . db_num_rows($result), $sql, true);
135 //----------------------------------------------------------------------------------------
137 function read_sales_order($order_no, &$order, $skip_completed_items=false)
139 $myrow = get_sales_order($order_no);
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"];
150 $order->Comments = $myrow["comments"];
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"]);
163 $order->tax_group_name = $myrow["tax_group_name"];
164 $order->tax_group_id = $myrow["tax_group_id"];
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;
174 if ($skip_completed_items)
176 AND ".TB_PREF."sales_order_details.quantity - ".TB_PREF."sales_order_details.qty_invoiced > 0 ";
178 $result = db_query($sql, "Retreive order Line Items");
180 if (db_num_rows($result) > 0)
183 while ($myrow = db_fetch($result))
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"]);
194 //----------------------------------------------------------------------------------------
196 function sales_order_has_invoices($order_no)
198 $sql = "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE order_=$order_no";
200 $result = db_query($sql, "could not query for sales order usage");
202 $row = db_fetch_row($result);
204 return ($row[0] > 0);
207 //----------------------------------------------------------------------------------------
209 function close_sales_order($order_no)
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";
216 db_query($sql, "The sales order detail record could not be updated");
219 //----------------------------------------------------------------------------------------
221 function dispatch_sales_order_item($order_no, $stock_id, $qty_dispatched)
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'";
228 db_query($sql, "The sales order detail record could not be updated");
231 //---------------------------------------------------------------------------------------------------------------
233 function get_invoice_duedate($debtorno, $invdate)
235 if (!is_date($invdate))
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'";
243 $result = db_query($sql,"The customer details could not be retrieved");
244 $myrow = db_fetch($result);
246 if (db_num_rows($result) == 0)
248 if ($myrow['day_in_following_month'] > 0)
249 $duedate = add_days(end_month($invdate), $myrow['day_in_following_month']);
251 $duedate = add_days($invdate, $myrow['days_before_due']);