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

function add_asset_booking($items, $reference, $issuing_location, $project_id, $task_id, $date_,
        $memo_, $ab_complete)
{
	global $loc_notification, $path_to_root, $Refs;
        
        $created_by = $_SESSION['wa_current_user']->user;

	begin_transaction();
	$args = func_get_args();
	$args = (object)array_combine(array('items', 'location', 'date_',
		'reference', 'memo_', 'project_id', 'task_id', 'ab_complete'), $args);
	$args->trans_no = 0;
	hook_db_prewrite($args, ST_MATERIAL_REQ);

        if($reference == '' || $reference == null )
	{
            $mtr_auto_ref = get_next_trans_no(ST_MATERIAL_REQ);
            $reference = $mtr_auto_ref;
        }
        
        $ab_no = add_ab_header($date_, $reference, $issuing_location, $project_id, $task_id,
                                            $ab_complete, $memo_, $created_by);
         
        if($ab_no != 0)
        {
            foreach ($items as $line_item)
            {
                    add_ab_item($ab_no, $reference, $line_item->stock_id, $line_item->required_from_date, 
                            $line_item->required_to_date, $line_item->line_comment, $created_by);
            }

            $Refs->save(ST_MATERIAL_REQ, $ab_no, $reference);
            add_audit_trail(ST_MATERIAL_REQ, $ab_no, $date_);

            $args->trans_no = $ab_no;
        }

	hook_db_postwrite($args, ST_MATERIAL_REQ);
	commit_transaction();
        
	return $ab_no;
}

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

function update_asset_booking($ab_no, $ab_cart, $issuing_location, $project_id, 
                                            $task_id,$ab_date, $memo_, $ab_complete, $ab_reference)
{
	global $loc_notification, $path_to_root, $Refs;
        
        $items = $ab_cart->line_items;
        
        $updated_by = $_SESSION['wa_current_user']->user;

	begin_transaction();
	$args = func_get_args();
	$args = (object)array_combine(array('ab_no', 'ab_cart', 'ab_date', 'memo_', 'ab_complete', 'location','project_id', 'task_id','reference'), $args);
	$args->trans_no = 0;
	hook_db_prewrite($args, ST_MATERIAL_REQ);

//        $new_total_issued = get_total_qty_issued($mtr_no);
//        if($mtr_cart->total_qty_issued != $new_total_issued["total_qty_issued"])
//        {
//            display_error( _("This material requisition has been modified by another user. "
//                            . "Not possible to continue with updating "));
//            return false;
//        }
        
        update_ab_header($ab_no, $ab_date, $ab_complete, $memo_, $updated_by, $issuing_location, $project_id, $task_id, $ab_reference);
        
        delete_ab_items($ab_no);

        foreach ($items as $line_item)
        {
                recreate_ab_item($ab_no, $ab_cart->reference, $line_item, $updated_by);
        }

        add_audit_trail(ST_MATERIAL_REQ, $ab_no, $ab_date);

        $args->trans_no = $ab_no;


	hook_db_postwrite($args, ST_MATERIAL_REQ);
	commit_transaction();
        
	return $ab_no;
}
//
////-------------------------------------------------------------------------------------------------------------
//
//function void_material_requisition($type_no)
//{
//	hook_db_prevoid(ST_MATERIAL_REQ, $type_no);
//	void_gl_trans(ST_MATERIAL_REQ, $type_no);
//	void_stock_move(ST_MATERIAL_REQ, $type_no);
//}
//
////-------------------------------------------------------------------------------------------------------------
//
//function get_material_requisition_items($trans_no)
//{
//	$result = get_stock_moves(ST_MATERIAL_REQ, $trans_no);
//
//	if (db_num_rows($result) == 0)
//	{
//		return null;
//	}
//
//	return $result;
//}
//
////--------------------------------------------------------------------------------------------------

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

	$sql = "INSERT INTO ".TB_PREF."asset_booking( ab_date, reference,"
                        . " issuing_location,project_id,"
                        . " task_id,"
                        . " ab_complete, comments,"
                        . " created_by, created_on) "
                . "VALUES (".db_escape($ab_date).", ".db_escape($reference).", "
                        .db_escape($issuing_location).", ".db_escape($project_id).", "
                        .db_escape($task_id).", "
                        .db_escape($ab_complete).", ".db_escape($memo_).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

	if ($error_msg == "")
		$error_msg = "The asset booking request cannot be inserted";

	db_query($sql, $error_msg);

	return db_insert_id();
}
//
//
////--------------------------------------------------------------------------------------------------
//
function update_ab_header($ab_no, $ab_date, $ab_complete, $memo_, $updated_by, $issuing_location, $project_id, $task_id, $ab_reference, $error_msg = "")
{
	$ab_date = date2sql($ab_date);
        $updated_on = date('Y-m-d H:i:s');

	$sql = "UPDATE ".TB_PREF."asset_booking 
                SET  
                        ab_date = ".db_escape($ab_date).", 
                        comments = ".db_escape($memo_).", 
                        ab_complete = ".db_escape($ab_complete).",  
                        updated_by = ".db_escape($updated_by).", 
                        updated_on = ".db_escape($updated_on).",
                            issuing_location = ".db_escape($issuing_location).",  
                            project_id = ".db_escape($project_id).", 
                            task_id = ".db_escape($task_id).",
                                reference = ".db_escape($ab_reference)."
                            
                WHERE ab_no = ".$ab_no;

	if ($error_msg == "")
		$error_msg = "Could not update asset booking request";

	db_query($sql, $error_msg);

	return;
}

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

function add_ab_item($ab_no, $reference, $asset_id, $required_from_date, $required_to_date, 
                     $line_comment, $created_by, $error_msg = "")
{
	$mb_flag = get_mb_flag($asset_id);

        if (is_service($mb_flag))
        {
            display_db_error("Cannot request Service item : $asset_id", "");
        }

	$required_from_date = date2sql($required_from_date);
        $required_to_date = date2sql($required_to_date);
        $created_on = date('Y-m-d H:i:s');

	$sql = "INSERT INTO ".TB_PREF."asset_booking_details (ab_no, "
                        . " asset_id, "
                        . " required_from_date, "
                        . " required_to_date, "
                        . " line_comment, "
                        . " created_by, created_on) "
                . " VALUES (".db_escape($ab_no).", " 
                        .db_escape($asset_id).", "
                        .db_escape($required_from_date).", "
                        .db_escape($required_to_date).", "
                        .db_escape($line_comment).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

	if ($error_msg == "")
		$error_msg = "The asset booking request cannot be inserted";

	db_query($sql, $error_msg);

	return db_insert_id();
}
//
////-------------------------------------------------------------------------------------------------------------
function delete_ab_items($ab_no, $error_msg = "")
{
    $sql = " DELETE FROM ".TB_PREF."asset_booking_details 
            WHERE ab_no = ".$ab_no;
    
    	if ($error_msg == "")
		$error_msg = "The asset booking request could not be removed";

	db_query($sql, $error_msg);
}

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

function recreate_ab_item($ab_no, $reference, $li, $updated_by, $error_msg = "")
{
	$mb_flag = get_mb_flag($li->stock_id);

//        if (is_service($mb_flag))
//        {
//            display_db_error("Cannot request Service item : $li->stock_id", "");
//        }

	$required_from_date = date2sql($li->required_from_date);
        $required_to_date = date2sql($li->required_to_date);
        $updated_on = date('Y-m-d H:i:s');
        
        if($li->created_by)
            $created_by = $li->created_by;
        else {
            $created_by = $updated_by;
        }
        
        if($li->created_on)
            $created_on = $li->created_on;
        else {
            $created_on = $updated_on;
        }
        
        //$$created_on = date2sql($created_on);
        
	$sql = "INSERT INTO ".TB_PREF."asset_booking_details(
                        id, ab_no, 
                        asset_id, 
                        required_from_date,  
                        required_to_date, 
                        line_comment, 
                        created_by, created_on, 
                        updated_by, updated_on) 
                VALUES (".db_escape($li->row_id).", ".db_escape($ab_no).", "
                        .db_escape($li->stock_id).", "
                        .db_escape($required_from_date).", ".db_escape($required_to_date).", "
                        .db_escape($li->line_comment).", "
                        .db_escape($created_by).", ".db_escape($created_on).", "
                        .db_escape($updated_by).", ".db_escape($updated_on).")";
                
	if ($error_msg == "")
		$error_msg = "The asset booking request details cannot be inserted";

	db_query($sql, $error_msg);

}

////-------------------------------------------------------------------------------------------------------------
//
//
function close_ab($mtr_no, $error_msg = "")
{
	$updated_by = $_SESSION['wa_current_user']->user;
        $updated_on = date('Y-m-d H:i:s');

	$sql = "UPDATE ".TB_PREF."asset_booking 
                SET ab_complete = ".db_escape(1).", 
                    updated_by = ".$updated_by.", 
                    updated_on = ".db_escape($updated_on)." 
                WHERE ab_no = ".$mtr_no ;

	if ($error_msg == "")
		$error_msg = "Could not update Material Transfer Requisition ";

	db_query($sql, $error_msg);

	return ;
}
//
////-------------------------------------------------------------------------------------------------------------
//
function update_ab_item($row_id, $error_msg = "")
{

	$sql = "UPDATE ".TB_PREF."asset_booking_details 
                SET  issued = 1
                WHERE
                        id = ".$row_id ;

	if ($error_msg == "")
		$error_msg = "Could not update Material Transfer Requisition Detail record ";

	db_query($sql, $error_msg);

	return ;
}
////-------------------------------------------------------------------------------------------------------------
//
//
function get_sql_for_ab_view(
//        $required_by_from = null, $required_by_to = null, 
            $created_on_from = null, $created_on_to = null,  
            $issuing_location = "",
            $ab_no = 0,  $ab_reference = "", $ab_complete = null, 
            $asset_id = null)
{
        $filter = "";
        
//        if($required_by_from != null)
//        {
//            $filter = " AND ab.ab_date > ".db_escape(date2sql($required_by_from));
//        }
//        
//        if($required_by_to != null)
//        {
//                $filter .= " AND ab.ab_date < ".db_escape(date2sql($required_by_to));
//        }
        
        if($created_on_from != null)
        {
                $filter .= " AND ab.created_on > ".db_escape(date2sql($created_on_from));
        }
        
        if($created_on_to != null)
        {
                $filter .= " AND ab.created_on < ".db_escape(date2sql($created_on_to));
        }
        
        if($issuing_location != "")
        {
                $filter .= " AND ab.issuing_location = ".db_escape($issuing_location);            
        }
        
        if ($ab_no != 0)
        {
                $filter .= " AND ab.ab_no = ".$ab_no;
        }
        
        if ($ab_reference != "")
        {
                $filter .= " AND ab.reference = ".db_escape($ab_reference); 
        }
        

        $filter .= " AND ab.ab_complete = ".db_escape($ab_complete);         

        
        //$filter = "";
        
        if ($asset_id == null)
        {
            $sql = "SELECT 
                        ab.ab_no,
                        ab.reference, 
                        ab.issuing_location,
                        ab.ab_date,
                        ab.comments, 
                        ab.ab_complete,
                        u.real_name requestor_name,
                        ab.created_on,
                        ab.created_by
                    FROM 
                        ".TB_PREF."asset_booking ab,
                        ".TB_PREF."users u 
                    WHERE  
                        ab.created_by = u.id "
                        .$filter." 
                    ORDER BY
                        ab.ab_no";
        }
//        else
//        {
//            $filter .= " AND sm.stock_id = ".db_escape($stock_item);         
//        
//            $sql = "SELECT DISTINCT
//                        mr.mr_no,
//                        mr.reference, 
//                        mr.issuing_location,
//                        mr.mr_date mr_required_date,
//                        mr.comments, 
//                        mr.mr_complete,
//                        u.real_name requestor_name,
//                        mr.created_on
//                    FROM 
//                        ".TB_PREF."material_requisitions mr,
//                        ".TB_PREF."users u,
//                        ".TB_PREF."material_requisition_details mrd,
//                        ".TB_PREF."stock_master sm
//                    WHERE 
//                        mr.created_by = u.id AND 
//                        mr.mr_no = mrd.mr_no AND 
//                        mrd.stock_id = sm.stock_id "
//                        .$filter."  
//                    ORDER BY
//                        mr.mr_no";
//        }

	return $sql;
}
//
//
////-------------------------------------------------------------------------------------------------------------
//
function get_asset_booking($ab_no)
{
    $sql = "SELECT 
                ab.ab_no,
                ab.reference, 
                ab.issuing_location,
                ab.ab_date,
                ab.comments, 
                ab.ab_complete,
                ab.project_id,
                pm.description project_description,
                ab.task_id, 
                pt.description task_description,
                u.real_name requestor_name,
                ab.created_on,
                l.location_name
            FROM 
                ".TB_PREF."asset_booking ab,
                ".TB_PREF."projects_master pm,
                ".TB_PREF."project_tasks pt,
                ".TB_PREF."asset_locations l,
                ".TB_PREF."users u 
            WHERE  
                ab.created_by = u.id AND 
                ab.project_id = pm.project_id AND 
                ab.task_id = pt.task_id AND 
                ab.issuing_location = l.loc_code AND 
                ab.ab_no = ".  db_escape($ab_no);
    
    $result = db_query($sql,"Asset Booking Request could not be retreived");

    return db_fetch($result);

}
//
////-------------------------------------------------------------------------------------------------------------
//
function get_ab_details($ab_no)
{
    $sql = "SELECT 
                abd.id,
                abd.ab_no,
                abd.asset_id,
                am.asset_name,
                abd.required_from_date,
                abd.required_to_date,
                abd.line_comment,
                abd.created_by, 
                abd.created_on
            FROM 
                ".TB_PREF."asset_booking_details abd,
                    ".TB_PREF."asset_master am
            WHERE 
                abd.ab_no = ".$ab_no." AND
                abd.asset_id = am.asset_id
            ORDER BY
                abd.id";
    
    $result = db_query($sql,"Asset booking request details could not be retreived");

    return $result;

}

function get_iab_details($id)
{
    $sql = "SELECT 
                abd.id,
                abd.ab_no,
                abd.asset_id,
                am.asset_name,
                abd.required_from_date,
                abd.required_to_date,
                abd.line_comment,
                abd.created_by, 
                abd.created_on
            FROM 
                ".TB_PREF."asset_booking_details abd,
                    ".TB_PREF."asset_master am
            WHERE 
                abd.id = ".$id." AND
                abd.asset_id = am.asset_id
            ORDER BY
                abd.id";
    
    $result = db_query($sql,"Asset booking request details could not be retreived");

    return $result;

}

function get_abd($ab_no)
{
    $sql = "SELECT 
                abd.id,
                abd.ab_no,
                abd.asset_id,
                am.asset_name,
                abd.required_from_date,
                abd.required_to_date,
                abd.line_comment,
                abd.created_by,
                abd.issued,
                abd.created_on
            FROM 
                ".TB_PREF."asset_booking_details abd,
                    ".TB_PREF."asset_master am
            WHERE 
                abd.ab_no = ".$ab_no." AND
                abd.asset_id = am.asset_id AND
                abd.issued = 0
            ORDER BY
                abd.id";
    
    $result = db_query($sql,"Asset booking request details could not be retreived");

    return $result;

}
//
////-------------------------------------------------------------------------------------------------------------
//
//function get_mr_item($row_id)
//{
//        
//    $sql = "SELECT issued_qty 
//            FROM ".TB_PREF."material_requisition_details 
//            WHERE id = ".$row_id ;
//    
//    $result = db_query($sql,"Material transfer_requisition item detail could not be retreived");
//
//    return db_fetch($result);
//
//}
////-------------------------------------------------------------------------------------------------------------
//
//
//function get_total_qty_issued($mr_no)
//{
//        
//    $sql = " SELECT SUM(issued_qty) total_qty_issued FROM ".TB_PREF."material_requisition_details 
//                WHERE mr_no = ".$mr_no ;
//    
//    $result = db_query($sql,"Material requisition issued quantity total could not be retreived");
//
//    return db_fetch($result);
//
//}
//
////-------------------------------------------------------------------------------------------------------------
//
//function get_material_transfers_for_requisition($mtr_no)
//{
//    $sql = "SELECT 
//                mrt.*,
//                u.real_name issuer_name   
//            FROM 
//                ".TB_PREF."material_requisition_transfers mrt,
//                ".TB_PREF."users u 
//            WHERE  
//                mrt.created_by = u.id AND 
//                mrt.source_mtr_no = ".  db_escape($mtr_no);
//    
//    return db_query($sql,"Material requisition transfers could not be retreived");
//
//
//}
////-------------------------------------------------------------------------------------------------------------
//
?>