<?php
/**********************************************************************
***********************************************************************/
//-------------------------------------------------------------------------------------------------------------

function stock_cost_update($stock_id, $material_cost, $labour_cost, $overhead_cost,
	$last_cost)
{
       
    $trans_type = ST_ITEM_COSTUPDATE;
    
    $item = get_stock_item_edit_info($stock_id);
    $mb_flag = $item['mb_flag'];
    $previous_material_cost = $item['mb_flag'];
    $previous_labour_cost = $item['mb_flag'];
    $previous_overhead_cost = $item['mb_flag'];
    $trans_date_ = Today();
    $comment = '';
    
    $update_no = get_next_trans_no($trans_type);
    
//    $update_no = -1;

//    if (is_service($mb_flag))
//    {
    	//display_db_error("Cannot do cost update for Service item : $stock_id", "");	
		
        begin_transaction();
            //Chaitanya
        if($mb_flag == 'D'){
            $sql = "UPDATE ".TB_PREF."stock_master SET 
                        material_cost=".db_escape($material_cost)."
                    WHERE stock_id=".db_escape($stock_id);
        }
        else{            
            $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost).", 
		labour_cost=".db_escape($labour_cost).", 
		overhead_cost=".db_escape($overhead_cost).", 
		last_cost=".db_escape($last_cost)." 
		WHERE stock_id=".db_escape($stock_id);
        }

            db_query($sql,"The cost details for the inventory item could not be updated");

            $qoh = get_qoh_on_date($stock_id);
            if(abs($qoh) > 0){
                add_stock_item_cost_amendment($trans_type, $update_no, 
                                0, '',
                                $stock_id, $qoh, 
                                $previous_material_cost, $previous_labour_cost, $previous_overhead_cost, 
                                $material_cost, $labour_cost, $overhead_cost,
                                $trans_date_, $comment);
            }
            
            add_audit_trail($trans_type, $update_no, $trans_date_);

        commit_transaction();

        return $update_no;
		
        /*
    }
    else{
	
	begin_transaction();
	
	$sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost).", 
		labour_cost=".db_escape($labour_cost).", 
		overhead_cost=".db_escape($overhead_cost).", 
		last_cost=".db_escape($last_cost)." 
		WHERE stock_id=".db_escape($stock_id);
	db_query($sql,"The cost details for the inventory item could not be updated");

	$qoh = get_qoh_on_date($stock_id);
	
   	$date_ = Today();
	if (!is_date_in_fiscalyear($date_)){
		$date_ = end_fiscalyear();
        }

	if ($qoh > 0)
	{
		$new_cost = $material_cost + $labour_cost + $overhead_cost;

		$value_of_change = round2($qoh * ($new_cost - $last_cost), user_price_dec());

		if ($value_of_change != 0)
		{
			$stock_gl_code = get_stock_gl_code($stock_id);
	   		$update_no = get_next_trans_no(ST_COSTUPDATE);
			$memo_ = sprintf(_("Cost was %s changed to %s x quantity on hand for item '%s'"),
				number_format2($last_cost, 2), number_format2($new_cost, 2), $stock_id);
//			add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $date_, $stock_gl_code["adjustment_account"], 
//				$stock_gl_code["dimension_id"], $stock_gl_code["dimension2_id"], $memo_, (-$value_of_change));	   
//
//			add_gl_trans_std_cost(ST_COSTUPDATE, $update_no, $date_, $stock_gl_code["inventory_account"], 0, 0, $memo_, 
//				$value_of_change);
		}		
   	}

	if ($update_no != -1)
		add_audit_trail(ST_COSTUPDATE, $update_no, $date_);
	commit_transaction();

	return $update_no;
    }
         * */
    
}

//-------------------------------------------------------------------------------------------------------------


function add_stock_item_cost_amendment($trans_type, $trans_no, 
                                    $trans_line_id, $reference,
                                    $stock_id, $total_quantity, 
                                    $previous_material_cost, $previous_labour_cost, $previous_overhead_cost, 
                                    $new_material_cost, $new_labour_cost, $new_overhead_cost,
                                    $trans_date_, $comment)
{

    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');

    $sql = "INSERT INTO ".TB_PREF."stock_item_cost_amendments (
                                    trans_type, trans_no, 
                                    trans_line_id, reference,
                                    stock_id, total_quantity, 
                                    previous_material_cost, previous_labour_cost, previous_overhead_cost, 
                                    new_material_cost, new_labour_cost, new_overhead_cost,
                                    trans_date, comment, 
                                    created_by, created_on)                                     
                                 
            VALUES (".db_escape($trans_type).",  ".db_escape($trans_no).", "
                    .db_escape($trans_line_id).", ".db_escape($reference).", "
                    .db_escape($stock_id).",  ".db_escape($total_quantity).", "
                    .db_escape($previous_material_cost).", ".db_escape($previous_labour_cost).", ".db_escape($previous_overhead_cost).", "
                    .db_escape($new_material_cost).", ".db_escape($new_labour_cost).", ".db_escape($new_overhead_cost).", "
                    .db_escape(date2sql($trans_date_)).", ".db_escape($comment).", "
                    .db_escape($created_by).", ".db_escape($created_on).")";

    db_query($sql,"add_stock_item_cost_amendment");        
            
}

?>