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


$page_security = 'SA_ITEM_SERIAL_NO_ADJUSTMENTS';
$path_to_root = "..";
include($path_to_root . "/includes/session.inc");
include_once ($path_to_root . "/trace.php");
$js = "";
if ($SysPrefs->use_popup_windows)
	$js .= get_js_open_window(900, 500);
if (user_use_date_picker())
	$js .= get_js_date_picker();
	
page(_($help_context = "Serial Numbers Adjustments"), @$_REQUEST['popup'], false, "", $js);

include_once($path_to_root . "/includes/date_functions.inc");
include_once($path_to_root . "/includes/ui.inc");
include_once($path_to_root . "/includes/data_checks.inc");
include_once($path_to_root . "/inventory/includes/serial_no_validation.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_db.inc");
include_once($path_to_root . "/inventory/includes/db/serial_no_adjustments_db.inc");


$user_comp = user_company();
$new_item = get_post('serial_no_id')=='' || get_post('cancel') || get_post('clone'); 


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

if (isset($_GET['NewAdjustment=1']))
{
	clear_data();
}
if (isset($_GET['serial_no_id']))
{
	$_POST['serial_no_id'] = $_GET['serial_no_id'];
}

$serial_no_id = get_post('serial_no_id');

if (list_updated('serial_no_id')) {
	$_POST['NewPromotionCode'] = $serial_no_id = get_post('serial_no_id');
    clear_data();
	$Ajax->activate('details');
	$Ajax->activate('controls');
}

if (get_post('cancel')) {
	$_POST['NewPromotionCode'] = $serial_no_id = $_POST['serial_no_id'] = '';
    clear_data();
	set_focus('serial_no_id');
	$Ajax->activate('_page_body');
}

function clear_data()
{
	unset($_POST['tran_reference']);
//        unset($_POST['qty']);
        unset($_POST['comment']);
}

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

if (isset($_POST['addupdate'])) 
{ 
    $tran_no = get_next_trans_no(ST_SERIAL_NO_ADJUSTMENT);
    $stock_item = get_item($_POST['stock_id']);
    $sn_row = get_serial_no_details($_POST['serial_no_id']); 

    $info['single_item'] = 1;
    $info['tran_type'] = ST_SERIAL_NO_ADJUSTMENT;
    $info['tran_no'] = $tran_no;
    $info['stock_id'] = $_POST['stock_id'];
    $info['stock_control_method'] = $stock_item['stock_control_method'];
    $info['serial_no_id'] = $sn_row['id'];
    $info['serial_no'] = $sn_row['serial_no'];
    $info['serial_no_qty'] = $sn_row['qty'];
    $info['row_state'] = $sn_row['row_state'];
    $info['tran_reference'] = $_POST['tran_reference'];
    $info['location'] = $_POST['location'];
    $info['tran_date'] = $_POST['tran_date'];
    $info['type'] = $_POST['type'];
    $info['serial_no_tran_comment'] = $_POST['type'];
    
    $cpsn_msg = can_process_serial_no_adjustment($info);
      
	if ($cpsn_msg['success'] == true)
	{   
        $ssnt_msg = save_serial_no_adjustment($info);
        if ($ssnt_msg['success'] == true){
            clear_data();
            display_notification("Serial no adjustment ".$tran_no." has been saved.");
        }
        else{
            display_error($ssnt_msg['message']);
        }
            
        set_focus('serial_no_id');
        $Ajax->activate('serial_no_id'); // in case of status change
        //display_notification(_("Transaction has been created."));

        $Ajax->activate('_page_body');
    }
}

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


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


function item_settings(&$serial_no_id) 
{
	global $SysPrefs, $path_to_root, $new_item, $pic_height, $Refs;

        br();
        
        start_outer_table(TABLESTYLE2);
        table_section_title(_("Enter Lot Adjustment"));

        $_POST['tran_reference'] = $Refs->get_next(ST_LOT_ADJUSTMENT);
      

        ref_row('Reference:', 'tran_reference', null);
        date_row('Date :', 'tran_date');
        three_value_list_row(_("Type:"), 'type', null, ' ', '', _("Positive Adjustment"), '1', 
                 _("Negative Adjustment"), '2');
//        $item_info = get_item_edit_info($_POST['stock_id']);
//		$dec = $item_info['decimals'];
//        amount_row('Quantity:', 'qty', null, null, null, $dec);
        textarea_row('Comment', 'comment', '', 25, 2);
        
	end_outer_table();
        
        br();

	div_start('controls');

        submit_center_first('addupdate', _("Process Transaction"), '', 
                @$_REQUEST['popup'] ? true : 'default');

        submit_return('select', get_post('serial_no_id'), 
                _("Select this Promotion and return to document entry."), 'default');


	div_end();
}

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

start_form(true);

start_table(TABLESTYLE_NOBORDER);
start_row();
    //stock_costable_items_list_cells(null, 'stock_id', null, false, true);
    serial_no_cotrolled_items_row (null, 'stock_id', null, false, true);
    if (list_updated('stock_id')) {
        $Ajax->activate('serial_no_id');
    }
end_row();  
end_table();

start_table(TABLESTYLE_NOBORDER);
start_row();        
    $item_has_lot = get_serial_nos_for_stock_id($_POST['stock_id']);
    if($item_has_lot == true){
        serial_no_list_cells(_("Serial No:"), 'serial_no_id', null,  "Select a serial no",false, $_POST['stock_id']);
    }
    else{
        serial_no_list_cells(_("Serial No:"), 'serial_no_id', null,  "",false,"aaaaaaaaaaaaaaaaaaaaa");
    }
    locations_list_cells(_("Location:"), 'location', null, "Slecet a location");
    $new_item = get_post('serial_no_id')=='';
end_row();
end_table();

if (get_post('_show_inactive_update')) {
    $Ajax->activate('serial_no_id');
    set_focus('serial_no_id');
}


div_start('details');

$serial_no_id = get_post('serial_no_id');

item_settings($serial_no_id);

br();
tabbed_content_end();

div_end();


hidden('popup', @$_REQUEST['popup']);

end_form();

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

end_page(@$_REQUEST['popup']);
?>
