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

//-------------------------------------------------------------------------------------------------------------
include_once($path_to_root . "/projects/includes/db/material_requisitions_db.inc");

function add_issue_assets($items, $reference, $issuing_location, $project_id, $task_id,
            $memo_, $source_mtr_no, $mr_complete = false)
{
	global $loc_notification, $path_to_root, $Refs;
        
        $type = 104;
        $date = sql2date(date('Y/m/d'));
//        $issuing_date = sql2date($issuing_date);
        $created_by = $_SESSION['wa_current_user']->user;
        
        $total_cost = 0;
        $issue_total = 0;

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

        if($reference == '' || $reference == null )
	{
            $reference = get_next_trans_no($type);
        }
        
        //$mrt_no = get_next_trans_no($type);
        
        $mrt_no = add_issue_assets_header($reference, $issuing_location, $project_id, $task_id,
                                $memo_, $source_mtr_no, $created_by, $error_msg="");
         
        if(!$mrt_no > 0)
        {
            cancel_transaction();
            return array("passed" => false, "message" => "Could not add material requisition transfer");
        }
        
        if ($loc_notification == 1)
	{
		include_once($path_to_root . "/inventory/includes/inventory_db.inc");
		$st_ids = array();
		$st_names = array();
		$st_num = array();
		$st_reorder = array();
	}
     
        
	foreach ($items as $line_item)
	{
		if ($loc_notification == 1)
			$loc = calculate_reorder_level($location_from, $line_item, $st_ids, $st_names, $st_num, $st_reorder); 
		
//                if($line_item->quantity > 0){
                    	
//                    add_stock_move(104, $line_item->stock_id, $mrt_no, $issuing_location,
//                            $date, $reference, -$line_item->quantity, 0);

//                    add_stock_move(ST_REQTRANSFER, $line_item->stock_id, $mrt_no, $receiving_location,
//                            $date, $reference, $line_item->quantity, 0);
                    if($line_item->check == 1)
                    {
                        $abd_no = add_ab_details($_SESSION['ia_items']->order_id, $line_item);
                    }
                    if(isset($abd_no) && !$abd_no > 0)
                    {
                        cancel_transaction();
                        return array("passed" => false, "message" => "Could not add issue assets detail");
                    }
                    if($line_item->check == 1)
                    {
                        update_ab_item($line_item->row_id);
                    }
                    
//                }
	}
        
//        add_comments(ST_REQTRANSFER, $mrt_no, $date, $memo_);
//
//	$Refs->save(ST_REQTRANSFER, $mrt_no, $reference);
//	add_audit_trail(ST_REQTRANSFER, $mrt_no, $date);
//	
//	if ($loc_notification == 1 && count($st_ids) > 0)
//		send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);  
        
        
	$args->trans_no = $mrt_no;
	hook_db_postwrite($args, $type);

//        if($mr_complete)
//            close_ab($source_mtr_no);
            
	commit_transaction();
        
	return $mrt_no;
}

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

function add_issue_assets_header($reference, $issuing_location, $project_id, $task_id,
	$memo_, $source_mtr_no, $created_by, $error_msg = "")
{
	//$mr_date = date2sql($date_);
        $created_on = date('Y-m-d H:i:s');

	$sql = "INSERT INTO ".TB_PREF."asset_issues(reference,"
                        . " issuing_location,"
                        . " project_id,"
                        . " task_id,"
                        . " source_ab_no, "
                        . " comment, "
                        . " created_by, created_on) "
                . "VALUES (".db_escape($reference).", "
                        .db_escape($issuing_location).", "
                        .db_escape($project_id).", "
                        .db_escape($task_id).", "
                        .db_escape($source_mtr_no).", "
                        .db_escape($memo_).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

	if ($error_msg == "")
		$error_msg = "The material requisition transfer could not be inserted";

	db_query($sql, $error_msg);

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

function get_issue_assets()
{
    $sql = "SELECT * FROM ".TB_PREF."asset_issues ";
    
    $result = db_query($sql);
    return db_fetch($result);
    
}

function add_ab_details($order_no, $line_item, $error_msg = "")
{
    
    $created_by = $_SESSION['wa_current_user']->user;
    $created_on = date('Y-m-d H:i:s');
    
    $sql = "INSERT INTO ".TB_PREF."asset_issue_details(ai_no,"
                        . " asset_id,"
                        . " issuing_date,"
                        . " source_ab_detail_no,"
                        . " created_by, created_on) "
                . "VALUES (".db_escape($order_no).", "
                        .db_escape($line_item->asset_id).", "
                        .db_escape(date2sql($line_item->issuing_date)).", "
                        .db_escape($line_item->row_id).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

	if ($error_msg == "")
		$error_msg = "The material requisition transfer could not be inserted";

	db_query($sql, $error_msg);

	return db_insert_id();
}
?>