Old ineffective sql_trail superseded by new improved db_trail logging only calls...
[fa-stable.git] / sales / includes / db / sales_order_db.inc
index 95f2ead3b740f030f28a842e492c709317ac33eb..e76ea3eee2c60b563bb4e288dee218fb3b2f6eae 100644 (file)
@@ -14,7 +14,7 @@ function add_sales_order(&$order)
 {
        global $SysPrefs, $path_to_root, $Refs;
 
-       begin_transaction();
+       begin_transaction(__FUNCTION__, func_get_args());
        hook_db_prewrite($order, $order->trans_type);
        $order_no = get_next_trans_no($order->trans_type);
        $del_date = date2sql($order->due_date);
@@ -72,7 +72,7 @@ function add_sales_order(&$order)
        } /* inserted line items into sales order details */
 
        add_audit_trail($order->trans_type, $order_no, $order->document_date);
-       $Refs->save($order->trans_type, $order_no, $order->reference);
+       $Refs->save($order->trans_type, $order_no, $order->reference, null, $order->fixed_asset);
 
        hook_db_postwrite($order, $order->trans_type);
        commit_transaction();
@@ -86,7 +86,7 @@ function add_sales_order(&$order)
 
 function delete_sales_order($order_no, $trans_type)
 {
-       begin_transaction();
+       begin_transaction(__FUNCTION__, func_get_args());
        hook_db_prevoid($trans_type, $order_no);
 
        $sql = "DELETE FROM ".TB_PREF."sales_orders WHERE order_no=" . db_escape($order_no) 
@@ -126,11 +126,11 @@ function update_sales_order($order)
        $version= current($order->trans_no);
        $total = $order->get_trans_total();
 
-       begin_transaction();
+       begin_transaction(__FUNCTION__, func_get_args());
        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,9 +207,9 @@ 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);
+       $Refs->save($order->trans_type, $order_no, $order->reference, null, $order->fixed_asset);
 
        hook_db_postwrite($order, $order->trans_type);
        commit_transaction();
@@ -231,7 +231,7 @@ function get_sales_order_header($order_no, $trans_type)
          stype.id AS sales_type_id,
          stype.tax_included,
          stype.factor,
-         ship.shipper_name,
+         ship.description as shipping,
          tax_group.name AS tax_group_name,
          tax_group.id AS tax_group_id,
          cust.tax_id,
@@ -245,7 +245,7 @@ function get_sales_order_header($order_no, $trans_type)
                        LEFT JOIN (SELECT order_, sum(alloc) inv_allocs FROM ".TB_PREF."debtor_trans 
                                WHERE type=".ST_SALESINVOICE." AND order_=".db_escape($order_no)."  GROUP BY order_)
                                 inv ON sorder.trans_type=".ST_SALESORDER." AND inv.order_=sorder.order_no
-                       LEFT JOIN ".TB_PREF."shippers ship ON  ship.shipper_id = sorder.ship_via,"
+                       LEFT JOIN ".TB_PREF."stock_master ship ON ship.stock_id = sorder.ship_via,"
          .TB_PREF."debtors_master cust,"
          .TB_PREF."sales_types stype, "
          .TB_PREF."tax_groups tax_group, "
@@ -285,10 +285,10 @@ 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 = line.stock_id
+                       WHERE line.stk_code = item.stock_id
                                AND order_no =".db_escape($order_no) 
                                ." AND trans_type = ".db_escape($trans_type) . " ORDER BY id";
 
@@ -330,7 +330,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 +340,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++;
                }
        }
@@ -603,3 +603,53 @@ 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, "Retreive last order detail");
+        $row = db_fetch_row($last_query);
+        return $row[0];
+}
+
+function get_sales_vat_category($trans_no, $type=ST_SALESINVOICE)
+{
+       $sql = "SELECT vat_category
+                       FROM ".TB_PREF."debtor_trans_details line
+                       LEFT JOIN ".TB_PREF."stock_master stock ON line.stock_id=stock.stock_id
+               WHERE debtor_trans_type=".db_escape($type)." AND debtor_trans_no=".db_escape($trans_no);
+       $result = db_query($sql, 'cannot check invoice category');
+  $line = db_fetch($result);
+       return $line['vat_category'];
+}
+