<?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/EcmPurchaseOrders/EcmPurchaseOrder.php');
require_once ('include/time.php');

$db = $GLOBALS['db'];

//add jquery
echo '<link rel="stylesheet" type="text/css" href="include/jQuery/jquery-table/jquery.appendGrid-1.3.1.css"/>';
echo '<link rel="stylesheet" type="text/css" href="include/jQuery/jquery-ui/themes/base/jquery-ui.css"/>';
echo '<script type="text/javascript"
src="include/jQuery/jquery-2.1.0.min.js"></script>';
echo '<script type="text/javascript"
src="include/jQuery/jquery-table/jquery.appendGrid-1.3.1.js"></script>';
echo '<script type="text/javascript"
src="include/jQuery/jquery-ui/ui/jquery-ui.js"></script>';
echo '<script type="text/javascript"
src="modules/EcmPurchaseOrders/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 EcmPurchaseOrder ();

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->register_date = date("d.m.Y");
	$OPT ['check_parent_id'] = false;
}
if(count($_REQUEST['mass'])>0 && count($_REQUEST['compid'])>0){
	$mass=json_encode($_REQUEST['mass']);
	$compid=json_encode($_REQUEST['compid']);
} else {
	$mass=json_encode([]);
	$compid=json_encode([]);
}
//get stocks
$res = $db->query("SELECT id, name FROM ecmstocks WHERE deleted = '0' ORDER BY name");
$stock_pw = '<select id="stock_pw" name="stock_pw"><option value=""></option>';
$stock_rw = '<select id="stock_rw" name="stock_rw"><option value=""></option>';
$stock_body = '';
while ($row = $db->fetchByAssoc($res)) 
	$stock_body.='<option value="'.$row['id'].'">'.$row['name'].'</option>';
$stock_pw.=$stock_body.'</select><input id="pw_stock_id" name="pw_stock_id" type="hidden"/>';
$stock_rw.=$stock_body.'</select><input id="rw_stock_id" name="rw_stock_id" type="hidden"/>';

// get employers
$earr=array(

        'Darek' => 'Darek',
        'Helena' => 'Helena',
        'Emila' => 'Emila',
        'Bogusia Serymoch' => 'Bogusia Serymoch',
        'Marcin Popławski' => 'Marcin Popławski',
);
foreach($earr as $ea){
    $eas.='<option value="'.$ea.'"';
    if(in_array($ea,explode("|",$rdoc['employers'])))$eas.=' selected';
    $eas.='>'.$ea.'</option>';
}

$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($focus->stock_id!='' && $row['id']==$focus->stock_id){$s='selected';} else { $s='';}
	$stock_body.='<option value="'.$row['id'].'" '.$s.'>'.$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/EcmPurchaseOrders/views/EditView/view.edit.ecmpurchaseorders.php');
$edit = new ViewEditEcmPurchaseOrders ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmPurchaseOrders';
$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_PW", $stock_pw);
$edit->ss->assign("STOCK_RW", $stock_rw);
$edit->ss->assign("STOCK", $stock);
$edit->ss->assign("EMPLOYERS", $eas);
$edit->ss->assign("mass", $mass);
$edit->ss->assign("compid", $compid);

echo $edit->display ();
//loading view
echo '<link rel="stylesheet" type="text/css" href="modules/EcmPurchaseOrders/javascript/helper.css" media="screen" /><div class="loading_panel"></div>';