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


$page_security = 'SA_ITEM_LOT_ADJUSTMENT';
$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 = "Lot 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/lots_validation.inc");
include_once($path_to_root . "/inventory/includes/db/lots_db.inc");
include_once($path_to_root . "/inventory/includes/db/lot_adjustments_db.inc");


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


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

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

$lot_id = get_post('lot_id');

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

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

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

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

if (isset($_POST['addupdate'])) 
{ 
    
//    if ($_POST['tran_reference'] == "") 
//	{
//		$input_error = 1;
//        display_error(_("Enter a reference."));
//		set_focus('tran_reference');		
//	} 
    
    $tran_no = get_next_trans_no(ST_LOT_ADJUSTMENT);
    $stock_item = get_item($_POST['stock_id']);
    $lot_row = get_lots_header_details($_POST['lot_id']);
    
    $info['single_item'] = 1;
    $info['tran_type'] = ST_LOT_ADJUSTMENT;
    $info['tran_no'] = $tran_no;
    $info['stock_id'] = $_POST['stock_id'];
    $info['stock_control_method'] = $stock_item['stock_control_method'];
    $info['lot_id'] = $lot_row['id'];
    $info['tran_qty'] = $_POST['qty'];
    $info['row_state'] = $lot_row['row_state'];
    $info['tran_reference'] = $_POST['tran_reference'];
    $info['location'] = $_POST['location'];
    $info['tran_date'] = $_POST['tran_date'];
    $info['type'] = $_POST['type'];
    $info['lot_tran_comment'] = $_POST['type'];
    
    $cpla_msg = can_process_lot_adjustment($info);
      
	if ($cpla_msg['success'] == true)
	{         
        save_lot_adjustment($info);

            clear_data();
            display_notification("Member loyalty points updated.");


            set_focus('lot_id');
            $Ajax->activate('lot_id'); // in case of status change
            //display_notification(_("Transaction has been created."));

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

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


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


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

        br();
        
//	start_outer_table(TABLESTYLE2,"width=36%");
//
//	table_section(1,'40%');
//
//
//			$_POST['NewPromotionCode'] = $_POST['lot_id'];
//
//			$myrow1 = get_lots_header_details($_POST['lot_id']);
//                        
//                        $myrow = db_fetch($myrow1);
//                        //echo 'fff|'.$myrow["card_type_id"];
//                        
//                        $_POST['stock_id'] = $myrow['stock_id'];
//                        $_POST['description'] = $myrow['description'];
//
////                        hidden('member_type_id', $_POST['member_type_id']);
//
//	
//        label_row('Stock ID:', $_POST['stock_id']);
//      
//   
//        table_section(2,'60%');
//        //table_section_title(_("Loyalty Card Details"));
//        label_row('Item Description:', $_POST['description']);
//        
//        
//        end_outer_table();
//        
//        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('lot_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);
            lot_cotrolled_items_cells (null, 'stock_id', null, false, true);
            if (list_updated('stock_id')) {
                                $Ajax->activate('lot_id');
                    }
        end_row();  
        end_table();
        
        start_table(TABLESTYLE_NOBORDER);
        start_row();        
            $item_has_lot = get_lots_for_stock_id($_POST['stock_id']);
            if($item_has_lot == true){
                lots_list_cells(_("Lot:"), 'lot_id', null,  "Select a lot",false, $_POST['stock_id']);
            }
            else{
                lots_list_cells(_("Lot:"), 'lot_id', null,  "",false,"aaaaaaaaaaaaaaaaaaaaa");
            }
            locations_list_cells(_("Location:"), 'location', null, "Slecet a location");
            $new_item = get_post('lot_id')=='';
	end_row();
	end_table();

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


div_start('details');

$lot_id = get_post('lot_id');

item_settings($lot_id);



br();
tabbed_content_end();

div_end();


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

end_form();

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

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