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

function add_manufactured_items($items, $receiving_location, $date_, $type, $increase, $reference, $memo_)
{
	global $loc_notification, $path_to_root, $Refs;
        
        $type = ST_PROD_ADD;
        $date = $date_; //date('m/d/Y');
        $created_by = $_SESSION['wa_current_user']->user;
        $total_receipt_value = 0;

	begin_transaction();
	$args = func_get_args();
	$args = (object)array_combine(array('items', 'location', 'date_', 'type', 'increase',
		'reference', 'memo_'), $args);
	$args->trans_no = 0;
	hook_db_prewrite($args, $type);

        if($reference == '' || $reference == null )
	{
            $reference = get_next_trans_no($type);   
        }
        
        $prh_no = add_production_receipt_header($reference, $receiving_location, $date_,
                                                $memo_, $created_by);

	foreach ($items as $line_item)
	{
            $standard_cost = get_standard_cost($line_item->stock_id);

            add_stock_move($type, $line_item->stock_id, $prh_no,
                        $receiving_location, $date, $reference, $line_item->quantity, $standard_cost, null, null, null, 
                        null, null, $line_item->tran_unit, $line_item->tran_qty, $line_item->convertion_factor);
            
            $line_value = $standard_cost * $line_item->quantity;
            $item_gl_accounts = get_stock_gl_code($line_item->stock_id);
            $stockitem = get_item($line_item->stock_id);
            
            if (!is_service($item_gl_accounts["mb_flag"]))
                    $ivaccount = $item_gl_accounts["inventory_account"];
            else
                    $ivaccount = $item_gl_accounts["cogs_account"];		

            if($line_value != 0){
                $total_receipt_value += add_gl_trans_std_cost($type, $prh_no, $date, $ivaccount, 0, 0,
                    $date.": "._("Receipt of")." ".$stockitem["description"], $line_value); 
            }
	}
        
        if ($total_receipt_value != 0){
            $assembly_account = get_company_pref('default_assembly_act');
            add_gl_trans_std_cost($type, $prh_no, $date, $assembly_account,0, 0, 
                                    $date.": "._("Production receipt:")." ".$reference,
                                    -$total_receipt_value);	
        }

	if ($memo_)
            add_comments($type, $prh_no, $date_, $memo_);

	$Refs->save($type, $prh_no, $reference);
	add_audit_trail($type, $prh_no, $date_);

	$args->trans_no = $prh_no;
	hook_db_postwrite($args, $type);
	commit_transaction();

	return $prh_no;
}

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

function get_added_manufactured_items($trans_no)
{
	$result = get_stock_moves(ST_PROD_ADD, $trans_no);

	if (db_num_rows($result) == 0)
	{
		return null;
	}

	return $result;
}

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

function add_production_receipt_header($reference, $receiving_location,
                            $tran_date, $comments, $created_by)
{
	//$mr_date = date2sql($date_);
        $created_on = date('Y-m-d H:i:s');

	$sql = "INSERT INTO ".TB_PREF."production_receipts( reference,"
                        . " receiving_location, "
                        . "tran_date, "
                        . " comments, "
                        . " created_by, created_on) "
                . "VALUES (".db_escape($reference).", "
                        .db_escape($receiving_location).", "
                        .db_escape(date2sql($tran_date)).", " 
                        .db_escape($comments).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";


        $error_msg = "The production receipt cannot be added";

	db_query($sql, $error_msg);

	return db_insert_id();
}

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

function get_production_receipt_header($pr_no)
{
    $sql = "SELECT pr.*, 
                    rl.location_name receiving_location_name,
                    u.real_name created_user_name 
            FROM ".TB_PREF."production_receipts pr,
                    ".TB_PREF."locations rl,
                    ".TB_PREF."users u 
            WHERE pr.receiving_location = rl.loc_code AND 
                    pr.created_by = u.id AND  
                    production_receipt_no = '".$pr_no."'";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}

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

function get_production_receipt_details($pr_no)
{
    $sql = "SELECT  smoves.stock_id ,
                    smoves.qty ,
                    smaster.description  
            FROM    ".TB_PREF."stock_moves smoves,
                    ".TB_PREF."stock_master smaster  
            WHERE   smoves.stock_id = smaster.stock_id AND 
                    smoves.type = '".ST_PROD_ADD."' AND 
                    smoves.trans_no = '".$pr_no."'";
    
    $result = db_query($sql);
    
    return $result;
    
}
//-------------------------------------------------------------------------------------------------------------

?>