<?php

/* * ********************************************************************
 * ********************************************************************* */

//-------------------------------------------------------------------------------------------------------------
include_once($path_to_root . "/includes/common_functions.inc");

function add_stock_reservation_release($items, $reference, 
                                        $reservation_location, $location,  
                                        $project_id, $srr_date, $memo_, 
                                        $source_sr_no, $sr_row_state) 
{
    
    global $loc_notification, $path_to_root, $Refs;
        
    $tran_type = ST_STOCK_RELEASE;
    $date = sql2date(date('Y/m/d'));

    $created_by = $_SESSION['wa_current_user']->user;

    begin_transaction();
    $args = func_get_args();
    $args = (object) array_combine(array('items', 'reference', 'reservation_location', 'location', 
                                        'project_id', '$srr_date', '$memo_', 'source_mtr_no', '$sr_row_state'), $args);
    $args->trans_no = 0;
    hook_db_prewrite($args, $tran_type);

    if ($reference == '' || $reference == null) {
        $reference = get_next_trans_no($tran_type);
    }
    
    $srr_no = add_stock_reservation_release_header($reference, $reservation_location, $location, $project_id, 
                                                    $srr_date, $memo_, $source_sr_no, 
                                                    $created_by);

    if (!$srr_no > 0) {
        cancel_transaction();
        return array("passed" => false, "message" => "Could not create stock reservation release");
    }

    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, $line_item, $st_ids, $st_names, $st_num, $st_reorder);
        }

        if ($line_item->tran_qty > 0) {

            add_stock_reservation_release_item($srr_no, $line_item->stock_id, $line_item->standard_cost, 
                        $line_item->quantity, $line_item->tran_unit, $line_item->tran_qty, 
                        $line_item->convertion_factor, $line_item->line_comment,$line_item->project_site_id,  
                        $created_by);
            
            add_stock_transfer_item($srr_no, $line_item->stock_id, $reservation_location,
                        $location, $srr_date, $reference, $line_item->quantity, $line_item->tran_unit,
                        $line_item->tran_qty, $line_item->convertion_factor, $line_item->project_site_id, $tran_type);
            
            update_stock_reservation_item($line_item->row_id, convert_to_base_unit_qty($line_item->tran_qty, $line_item->convertion_factor));
        }
    }

    if ($loc_notification == 1 && count($st_ids) > 0) {
        send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
    }

    if (check_all_sr_lines_released($source_sr_no) === true) {
        $umrs_msg = update_stock_reservation_status($source_sr_no, $sr_row_state, true);
    }
    else{
        // Update with a new row_state to make sure that no one else has updated the stock reservation.
        $umrs_msg = update_stock_reservation_status($source_sr_no, $sr_row_state);
    }
    
    if($umrs_msg['success'] !== true)
    {
        cancel_transaction();
        return array("success" => false, "tran_no" => $srr_no, "message" => $umrs_msg['message']);
    }
    
    $Refs->save($tran_type, $srr_no, $reference);
    add_audit_trail($tran_type, $srr_no, $date);
    
    $args->trans_no = $srr_no;
    hook_db_postwrite($args, $tran_type);

    commit_transaction();

    return array("success" => true, "tran_no" => $srr_no, "message" => "");
}

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

function add_stock_reservation_release_header($reference, $reservation_location, $location, $project_id, 
                                                $srr_date, $memo_, $source_sr_no, 
                                                $created_by)
{
    //$mr_date = date2sql($date_);
    $created_on = date('Y-m-d H:i:s');

    $sql = "INSERT INTO ".TB_PREF."stock_reservation_releases(
                        source_sr_no, reservation_location, location, 
                        project_id, reference, 
                        srr_date, comments, 
                        created_by, created_on, 
                        row_state) 
            VALUES (" . db_escape($source_sr_no) . ", ". db_escape($reservation_location) . ", ". db_escape($location) . ", "
            . db_escape($project_id) . ", " . db_escape($reference) . ", "
            . db_escape(date2sql($srr_date)) . ", " . db_escape($memo_) . ", "
            . db_escape($created_by) . ", " . db_escape($created_on) .", "
            . db_escape(0) . ")";


    $error_msg = "Could not add_stock_reservation_release_header";

    db_query($sql, $error_msg);

    return db_insert_id();
}

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


function add_stock_reservation_release_item($srr_no, $stock_id, $standard_cost, 
                        $quantity, $tran_unit, $tran_qty, 
                        $convertion_factor, $line_comment,$project_site_id,  
                        $created_by)
{


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

	$sql = "INSERT INTO ".TB_PREF."stock_reservation_release_details(
                        srr_no, stock_id, 
                        price, released_qty, 
                        released_unit, released_unit_qty, 
                        released_unit_conv_factor, 
                        line_comment, project_site_id, 
                        created_by, created_on) 
            VALUES (".db_escape($srr_no).", ".db_escape($stock_id).", " 
                        .db_escape($standard_cost).", ".db_escape($quantity).", "
                        .db_escape($tran_unit).", ".db_escape($tran_qty).", "
                        .db_escape($convertion_factor).", "
                        .db_escape($line_comment).", ".db_escape($project_site_id).", "
                        .db_escape($created_by).", ".db_escape($created_on).")";

    $error_msg = "Could not add_stock_reservation_release_item";

	db_query($sql, $error_msg);

}



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

function get_material_issue_details($mrt_no, $type) {
    $sql = "SELECT  smoves.stock_id ,
                    smoves.qty ,
                    smaster.description , 
                    smoves.tran_unit , 
                    smoves.standard_cost 
            FROM    " . TB_PREF . "stock_moves smoves,
                    " . TB_PREF . "stock_master smaster  
            WHERE   smoves.stock_id = smaster.stock_id AND 
                    smoves.trans_no = '" . $mrt_no . "' AND type = '" . $type . "'";

    $result = db_query($sql);

    return $result;
}



//-------------------------------------------------------------------------------------------------------------
?>