Early payment discount option moved from branch to payment terms.
[fa-stable.git] / manufacturing / includes / db / work_order_costing_db.inc
index 7a0bd413790ee03521969bc01d5b2472564d8894..f610356b92d601836259a44ae47849131364cb0b 100644 (file)
@@ -1,4 +1,5 @@
 <?php
+
 /**********************************************************************
     Copyright (C) FrontAccounting, LLC.
        Released under the terms of the GNU General Public License, GPL, 
 
 function add_wo_costing($workorder_id, $cost_type, $trans_type, $trans_no, $factor=1)
 {
+       begin_transaction(__FUNCTION__, func_get_args());
        $sql = "INSERT INTO ".TB_PREF."wo_costing (workorder_id, cost_type, trans_type, trans_no, factor)
                VALUES (".db_escape($workorder_id).","
                .db_escape($cost_type).",".db_escape($trans_type).","
                .db_escape($trans_no).",".db_escape($factor).")";
 
        db_query($sql, "could not add work order costing");
-
+       commit_transaction();
 }
 
 function get_wo_costing($workorder_id)
@@ -35,10 +37,12 @@ function get_wo_costing($workorder_id)
 
 function delete_wo_costing($trans_type, $trans_no)
 {
+       begin_transaction(__FUNCTION__, func_get_args());
        $sql="DELETE FROM ".TB_PREF."wo_costing WHERE trans_type=".db_escape($trans_type)
                ." AND trans_no=".db_escape($trans_no);
 
        db_query($sql, "could not delete work order costing");
+       commit_transaction();
 }
 
 /*
@@ -48,8 +52,8 @@ function update_material_cost($stock_id, $qty, $unit_cost, $date)
 {
        global $Refs;
 
-       $product = get_item($stock_id);
-       $avg_cost =  $product['material_cost'];
+       $item = get_item($stock_id);
+       $avg_cost =  $item['material_cost'];
 
        $qoh = get_qoh_on_date($stock_id, null, $date);
 
@@ -103,12 +107,8 @@ function update_material_cost($stock_id, $qty, $unit_cost, $date)
                write_journal_entries($cart);
        }
 
-       if ($qty > 0 && ($qoh != -$qty)) {
-               if ($qoh == 0 && $avg_cost != 0)
-                       $avg_cost = ($avg_cost + ($unit_cost*$qty_new)/$qty)/2;
-               else
-                       $avg_cost = ($avg_cost*($qoh+$qty_delayed)+$unit_cost*$qty_new)/($qoh+$qty);
-       }
+       if ($qty > 0 && ($qoh != -$qty))
+            $avg_cost = ($avg_cost*($qoh+$qty_delayed)+$unit_cost*$qty_new)/($qoh+$qty);
 
        $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($avg_cost)."
                    WHERE stock_id=".db_escape($stock_id);
@@ -124,7 +124,7 @@ function add_wo_costs_journal($wo_id, $amount, $cost_type, $cr_acc, $date, $dim1
 //
        global $Refs, $wo_cost_types;
 
-       begin_transaction();
+       begin_transaction(__FUNCTION__, func_get_args());
 
     $journal_id = get_next_trans_no(ST_JOURNAL);
 
@@ -139,8 +139,7 @@ function add_wo_costs_journal($wo_id, $amount, $cost_type, $cr_acc, $date, $dim1
        if ($is_bank_to)
        {
                add_bank_trans(ST_JOURNAL, $journal_id, $is_bank_to, "",
-                       $date, -$amount, PT_WORKORDER, $wo_id, get_company_currency(),
-                       "Cannot insert a destination bank transaction");
+                       $date, -$amount, PT_WORKORDER, $wo_id, get_company_currency());
        }
        add_journal(ST_JOURNAL, $journal_id, $amount, $date, get_company_currency(), $ref, '', 1, $date, $date);
 
@@ -229,6 +228,7 @@ function void_wo_costing($wo_id)
 {
        global $Refs;
 
+       begin_transaction(__FUNCTION__, func_get_args());
        $res = get_wo_costing($wo_id);
 
        while($row = db_fetch($res))
@@ -246,4 +246,5 @@ function void_wo_costing($wo_id)
 
        $sql = "DELETE FROM ".TB_PREF."wo_costing WHERE workorder_id=".db_escape($wo_id);
        db_query($sql, "could not delete work order costing");
+       commit_transaction();
 }