X-Git-Url: https://delta.frontaccounting.com/gitweb/?a=blobdiff_plain;f=sales%2Fincludes%2Fdb%2Fsales_order_db.inc;h=9d43fb3af94b8427c5cc9cb3d2da0f8e9ba81beb;hb=d907208297556310cc7c51fcb9086869c880ab2d;hp=7f3257afad9c8db2b9184eb3adcfdf02b4e82ea2;hpb=fd069bba86276f15311ff6ab10b22d4a06a55513;p=fa-stable.git diff --git a/sales/includes/db/sales_order_db.inc b/sales/includes/db/sales_order_db.inc index 7f3257af..9d43fb3a 100644 --- a/sales/includes/db/sales_order_db.inc +++ b/sales/includes/db/sales_order_db.inc @@ -89,6 +89,8 @@ function delete_sales_order($order_no, $trans_type) begin_transaction(); hook_db_prevoid($trans_type, $order_no); + $order = get_sales_order_header($order_no, $trans_type); + $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . db_escape($order_no) . " AND trans_type=".db_escape($trans_type); @@ -98,7 +100,7 @@ function delete_sales_order($order_no, $trans_type) .db_escape($order_no) . " AND trans_type=".db_escape($trans_type); db_query($sql, "order Detail Delete"); - add_audit_trail($trans_type, $order_no, Today(), _("Deleted.")); + add_audit_trail($trans_type, $order_no, sql2date($order['ord_date']), _("Deleted.")); commit_transaction(); } @@ -130,7 +132,7 @@ function update_sales_order($order) hook_db_prewrite($order, $order->trans_type); if ($order->trans_type == ST_SALESORDER) - $allocs = get_payments_for($order_no, ST_SALESORDER); + $allocs = get_payments_for($order_no, $order->trans_type, $order->customer_id); $sql = "UPDATE ".TB_PREF."sales_orders SET type =".db_escape($order->so_type)." , debtor_no = " . db_escape($order->customer_id) . ", @@ -207,7 +209,7 @@ function update_sales_order($order) } /* inserted line items into sales order details */ if ($order->trans_type == ST_SALESORDER) - reallocate_payments($order_no, ST_SALESORDER, $ord_date, $total, $allocs); + reallocate_payments($order_no, ST_SALESORDER, $ord_date, $total, $allocs, $order->customer_id); add_audit_trail($order->trans_type, $order_no, $order->document_date, _("Updated.")); $Refs->save($order->trans_type, $order_no, $order->reference, null, $order->fixed_asset); @@ -285,7 +287,7 @@ function get_sales_order_details($order_no, $trans_type) { qty_sent as qty_done, item.units, item.mb_flag, - item.material_cost + item.labour_cost + item.overhead_cost AS standard_cost + item.material_cost FROM ".TB_PREF."sales_order_details line," .TB_PREF."stock_master item WHERE line.stk_code = item.stock_id @@ -330,7 +332,7 @@ function read_sales_order($order_no, &$order, $trans_type) $order->alloc = $myrow['alloc']; $order->sum_paid = $myrow["sum_paid"]; // sum of all prepayments to so (also invoiced) $order->prep_amount = $myrow["prep_amount"]; - $order->prepayments = get_payments_for($order_no, $myrow['trans_type']); + $order->prepayments = get_payments_for($order_no, $myrow['trans_type'], $myrow['debtor_no']); $result = get_sales_order_details($order_no, $order->trans_type); if (db_num_rows($result) > 0) @@ -340,7 +342,7 @@ function read_sales_order($order_no, &$order, $trans_type) { $order->add_to_cart($line_no,$myrow["stk_code"],$myrow["quantity"], $myrow["unit_price"], $myrow["discount_percent"], - $myrow["qty_done"], $myrow["standard_cost"], $myrow["description"], $myrow["id"] ); + $myrow["qty_done"], $myrow["material_cost"], $myrow["description"], $myrow["id"] ); $line_no++; } } @@ -464,6 +466,10 @@ function get_branch_to_order($customer_id, $branch_id) { function get_sql_for_sales_orders_view($trans_type, $trans_no, $filter, $stock_item='', $from='', $to='', $ref='', $location=ALL_TEXT, $customer_id=ALL_TEXT) { + if ($filter=='OutstandingOnly') + $order_value = 'Sum(line.unit_price*(line.quantity-line.qty_sent)*(1-line.discount_percent))+freight_cost'; + else + $order_value = 'Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost'; $sql = "SELECT sorder.order_no, @@ -476,7 +482,7 @@ function get_sql_for_sales_orders_view($trans_type, $trans_no, $filter, ."sorder.ord_date, sorder.delivery_date, sorder.deliver_to, - Sum(line.unit_price*line.quantity*(1-line.discount_percent))+freight_cost AS OrderValue, + $order_value AS OrderValue, sorder.type, debtor.curr_code, Sum(line.qty_sent) AS TotDelivered, @@ -557,7 +563,8 @@ function get_sql_for_sales_orders_view($trans_type, $trans_no, $filter, sorder.branch_code, sorder.customer_ref, sorder.ord_date, - sorder.deliver_to"; + sorder.deliver_to + ORDER BY sorder.order_no DESC"; return $sql; } @@ -603,3 +610,42 @@ function sales_order_set_template($id, $status) $sql = "UPDATE ".TB_PREF."sales_orders SET type = ".db_escape($status)." WHERE order_no=".db_escape($id); db_query($sql, "Can't change sales order type"); } + +/* + Check whether sales order is issued in prepaid mode and already opened +*/ + +function is_prepaid_order_open($order_no) +{ + $sql = "SELECT count(*) + FROM ".TB_PREF."sales_orders o, + ((SELECT trans_no_to FROM ".TB_PREF."cust_allocations + WHERE trans_type_to=".ST_SALESORDER." AND trans_no_to=".db_escape($order_no).") + UNION + (SELECT order_ FROM ".TB_PREF."debtor_trans + WHERE type=".ST_SALESINVOICE." AND order_=".db_escape($order_no).")) related + WHERE + o.prep_amount>0 + AND o.trans_type = ".ST_SALESORDER." + AND o.order_no = " . db_escape($order_no); + + $result = db_fetch(db_query($sql, "cannot check prepaid order open")); + + return $result[0]; +} + +function last_sales_order_detail($order, $field) +{ + $sql = "SELECT $field + FROM ".TB_PREF."sales_order_details d + WHERE order_no = + (SELECT order_no FROM ".TB_PREF."sales_orders o + WHERE debtor_no=" . db_escape($order->customer_id) . " + ORDER BY order_no DESC LIMIT 1) + ORDER BY d.id DESC LIMIT 1"; + + $last_query=db_query($sql, "Could not retrieve last order detail"); + $row = db_fetch_row($last_query); + return $row == false ? false : $row[0]; +} +