<?php
/**********************************************************************
***********************************************************************/
//-------------------------------------------------------------------------------------------------------------
include_once($path_to_root . "/inventory/includes/db/transfers_db.inc");


function add_stock_transfer($Items, $location_from, $location_to, 
                            $date_, $movement_type, $linked_field_type, 
                            $linked_field_id, $reference, $memo_, 
                            $project_id, $sample_item)
{
	global $loc_notification, $path_to_root, $Refs;
    $current_user_id = $_SESSION['wa_current_user']->user;
	begin_transaction();
	$args = func_get_args();
	$args = (object)array_combine(array('Items', 'location_from', 'location_to', 
                                'date_', 'movement_type', 'linked_field_type', 
                                'linked_field_id', 'reference', 'memo_', 
                                'project_id', 'sample_item'), $args);
	$args->trans_no = 0;
	hook_db_prewrite($args, ST_LOCTRANSFER);

        $transfer_id = get_next_trans_no(ST_LOCTRANSFER);

	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();
	}
        
        $lt_no = add_lt_header($date_, $reference, $location_to, 
                                $location_from, $project_id, $movement_type, 
                                $linked_field_type, $linked_field_id, $memo_, 
                                $sample_item);
        if($lt_no != 0)
        {
            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); 
                }
                
                add_stock_transfer_item($lt_no, $line_item->stock_id, $location_from,
                        $location_to, $date_, $reference, $line_item->quantity, $line_item->tran_unit,
                            $line_item->tran_qty, $line_item->convertion_factor, $line_item->project_site_id, ST_LOCTRANSFER, $project_id);
                
                $ailqdm_msg1 =  add_item_line_quantity_detail_movements(ST_LOCTRANSFER, $lt_no, $location_from, 
                                                                            $date_, $line_item, false, '');
                if($ailqdm_msg1['success'] !== true)
                {
                    cancel_transaction();
                    return array("success" => $ailqdm_msg1['success'], "tran_no" => $lt_no, "message" => $ailqdm_msg1['message']);
                }
                else{
                    $ailqdm_msg2 =  add_item_line_quantity_detail_movements(ST_LOCTRANSFER, $lt_no, $location_to, 
                                                                            $date_, $line_item, true, '');
                    if($ailqdm_msg2['success'] !== true)
                    {
                        cancel_transaction();
                        return array("success" => $ailqdm_msg2['success'], "tran_no" => $lt_no, "message" => $ailqdm_msg2['message']);
                    }
                }
                
            }
        }

	$Refs->save(ST_LOCTRANSFER, $lt_no, $reference);
	add_audit_trail(ST_LOCTRANSFER, $lt_no, $date_);

	$args->trans_no = $lt_no;
	hook_db_postwrite($args, ST_LOCTRANSFER);
    
	commit_transaction();
	
	if ($loc_notification == 1 && count($st_ids) > 0){
		send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
    }

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

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

// add 2 stock_moves entries for a stock transfer
// $date_ is display date (not sql)
// std_cost is in HOME currency
// it seems the standard_cost field is not used at all

function add_stock_transfer_item($lt_no, $stock_id, $location_from, $location_to, $date_, $reference, $quantity, $tran_unit, $tran_qty, $convertion_factor, $project_site_id, 
                                 $tran_type = ST_LOCTRANSFER, $project_id=0, $reference_line_id=0, $check_insert=false) {
	$n_1 = add_stock_move($tran_type, $stock_id, $lt_no, $location_from, $date_, $reference, -$quantity, 0, 0, 1, 0, 0, '', $tran_unit, -$tran_qty, $convertion_factor, 
                              0, $project_site_id, $reference_line_id, $project_id);
            if (! ($n_1 > 0) && $check_insert === true) {
                return 0;
            }

	$n_2 = add_stock_move($tran_type, $stock_id, $lt_no, $location_to, $date_, $reference, $quantity, 0, 0, 1, 0, 0, '', $tran_unit, $tran_qty, $convertion_factor, 
                              0, $project_site_id, $reference_line_id, $project_id);
            if (! ($n_2 > 0) && $check_insert === true) {
                return 0;
            }
            
            return $n_2;
}

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

function get_stock_transfer($trans_no)
{
	$result = get_stock_transfer_items($trans_no);
	if (db_num_rows($result) < 2)
	{
		display_db_error("transfer with less than 2 items : $trans_no", "");
	}

	// this function is very bad that it assumes that 1st record and 2nd record contain the
	// from and to locations - if get_stock_moves uses a different ordering than trans_no then
	// it will bomb
	$move1 = db_fetch($result);
	$move2 = db_fetch($result);

	// return an array of (From, To)
	if ($move1['qty'] < 0)
		return array($move1, $move2);
	else
		return array($move2, $move1);
}

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

function get_stock_transfer_items($trans_no)
{
	$result = get_stock_moves(ST_LOCTRANSFER, $trans_no);

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

	return $result;
}

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

function void_stock_transfer($type_no)
{
	hook_db_prevoid(ST_LOCTRANSFER, $type_no);
	void_stock_move(ST_LOCTRANSFER, $type_no);
}

?>