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

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

function add_material_issue_out($items, $reference, $location, $date, $memo_)
{
	global $loc_notification, $path_to_root, $Refs;
        
        $type = ST_RAW_MATERIAL_ISSUE;
        
        $created_by = $_SESSION['wa_current_user']->user;
        
        $total_cost = 0;
        $issue_total = 0;
        
        //return;

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

        if($reference == '' || $reference == null )
	{
            $mio_auto_ref = get_next_trans_no($type);
            $reference = $mio_auto_ref;
        }
        
        $mio_no = add_mio_header($reference, $location, $date, $created_by);
         
        if(!$mio_no > 0)
        {
            cancel_transaction();
            return array("passed" => false, "message" => "Could not add material issue");
        }
        
        foreach ($items as $line_item)
        {
            if($line_item->quantity > 0)
            {
                //Chaitanya: Stamp the standard_cost
                $standard_cost = get_standard_cost($line_item->stock_id);
                // insert a -ve stock move for each item
                add_stock_move($type, $line_item->stock_id, $mio_no,
                        $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);

                $issue_cost = $standard_cost * $line_item->quantity;
                $issue = get_stock_gl_code($line_item->stock_id);
                $stockitem = get_item($line_item->stock_id);

                //Chaitanya : Compatibility for Service Items
                if (!is_service($issue["mb_flag"]))
                        $ivaccount = $issue["inventory_account"];
                else
                        $ivaccount = $issue["cogs_account"];		

                if($issue_cost != 0){
                    $total_cost += add_gl_trans_std_cost($type, $mio_no, $date, $ivaccount, 0, 0,
                        $date.": "._("Issue of")." ".$stockitem["description"], -$issue_cost);	
                
                    $issue_total += $issue_cost;
                }
            }
        }
            
            
        if ($issue_total != 0){
            $assembly_account = get_company_pref('default_assembly_act');
            add_gl_trans_std_cost($type, $mio_no, $date, $assembly_account,
            0, 0, $date.": "._("Production raw material issue:")." ".$reference, -$total_cost);	
        }
        
	if ($memo_)
		add_comments($type, $mio_no, $date, $memo_);

	$Refs->save($type, $mio_no, $reference);
	add_audit_trail($type, $mio_no, $date);

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

	commit_transaction();
        
	return $mio_no;
}

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

function add_mio_header($reference, $location, $date, $created_by)
{
	$rmi_date = date2sql($date);
        $created_on = date('Y-m-d H:i:s');

	$sql = "INSERT INTO ".TB_PREF."raw_material_issues( reference,"
                        . " location, tran_date,"
                        . " created_by, created_on) "
                . "VALUES (".db_escape($reference).", "
                        .db_escape($location).", ".db_escape($rmi_date).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

	$error_msg = "The raw material issue could not be inserted";

	db_query($sql, $error_msg);

	return db_insert_id();
}
//--------------------------------------------------------------------------------------------------

function get_raw_material_issue_header($mio_no)
{
    $sql = "SELECT rmi.*, 
                    l.location_name,
                    u.real_name created_user_name 
            FROM ".TB_PREF."raw_material_issues rmi,
                    ".TB_PREF."locations l,
                    ".TB_PREF."users u 
            WHERE rmi.location = l.loc_code AND 
                    rmi.created_by = u.id AND  
                    rmi.rmi_no = '".$mio_no."'";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}

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

function get_material_issue_details($mio_no, $type)
{
    $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.trans_no = '".$mio_no."' AND type = '".$type."'";
    
    $result = db_query($sql);
    
    return $result;
    
}

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

?>