<?php

if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( 'NotAValidEntryPoint' );

global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings, $current_user, $app_list_strings;
require_once ('modules/EcmStockDocInsideIns/EcmStockDocInsideIn.php');
require_once ('include/time.php');

$db = $GLOBALS['db'];

//add jquery
echo '<script type="text/javascript" src="modules/EcmStockDocInsideIns/javascript/searchcolumndefs.js"></script>';
echo '<script type="text/javascript" src="include/ECM/SearchProductTable.js"></script>';
echo '<script type="text/javascript" src="include/ECM/tablenavigator.js"></script>';
// $cq=$current_user->getPreference('confirm_quotes');

$new_number = false;
$duplicate = false;
$focus = new EcmStockDocInsideIn ();

if (isset ( $_REQUEST ['record'] ) && $_REQUEST['isDuplicate']=="false" ) {
	$focus->retrieve ( $_REQUEST ['record'] );	
	if (isset ( $focus->id ) && $focus->id != '') {
		$focus->format_all_fields ();		
	}
} 
else if ($_REQUEST['isDuplicate']=="true") {
	$new_number=true;
	$duplicate=true;
	//get data
	$focus->retrieve($_REQUEST['return_id']);
}
else {
	$new_number=true;
	//dates
	$focus->stock_id=$current_user->stock_id;
	$focus->register_date = date("d.m.Y");
	$OPT ['check_parent_id'] = false;
}

//get stocks
$res = $db->query("SELECT id, name FROM ecmstocks WHERE deleted = '0' ORDER BY name");
$stock = '<select id="stock" name="stock"><option value=""></option>';
$stock_body = '';
while ($row = $db->fetchByAssoc($res)) {
	if($row['id']==$focus->stock_id){$selected='selected';} else {
		$selected='';}
		$stock_body.='<option value="'.$row['id'].'" '.$selected.'>'.$row['name'].'</option>';
}
$stock.=$stock_body.'</select><input id="stock_id" name="stock_id" type="hidden" value="'.$focus->stock_id.'"/>';

require_once ('include/MVC/View/SugarView.php');
require_once ('modules/EcmStockDocInsideIns/views/EditView/view.edit.ecmstockdocinsideins.php');
$edit = new ViewEditEcmStockDocInsideIns ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmStockDocInsideIns';
$edit->bean = $focus;
$edit->tplFile = 'include/ECM/EcmViews/EditView/Tabs/EditView.tpl';
$edit->preDisplay ();
$edit->ss->assign ( "NEW_NUMBER", $new_number );
$edit->ss->assign("DUPLICATE", $duplicate);
$edit->ss->assign("STOCK", $stock);
echo $edit->display ();
//loading view
echo '<link rel="stylesheet" type="text/css" href="modules/EcmStockDocInsideIns/javascript/helper.css" media="screen" /><div class="loading_panel"></div>';