<?php
ini_set('display_errors',1); 
error_reporting(LC_ALL);
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/EcmStockDocOuts/EcmStockDocOut.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>';

// $cq=$current_user->getPreference('confirm_quotes');

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

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 if ($_REQUEST['parent_doc_type']!="true" && $_REQUEST['parent_doc_id']!="") {
	$new_number=true;
	$ecmsale=true;
	$s= New EcmSale();
	$s->retrieve($_REQUEST['parent_doc_id']);

	$focus->parent_name=$s->parent_name;
	$focus->parent_id=$s->parent_id;
	$focus->ecmsale_document_no=$s->document_no;
	$focus->delivery_date=$s->delivery_date;
	$focus->shipping_address_name=$s->shipping_address_name;
	$focus->shipping_address_street=$s->shipping_address_street;
	$focus->shipping_address_postalcode=$s->shipping_address_postalcode;
	$focus->shipping_address_city=$s->shipping_address_city;
	$focus->shipping_address_country=$s->shipping_address_country;
	$focus->shipping_iln=$s->shipping_iln;
	$focus->shipping_nip=$s->shipping_nip;
	$focus->parent_order_no=$s->parent_document_no;
	$focus->register_date = date("d.m.Y");
	$focus->ecmsale_id=$_REQUEST['parent_doc_id'];
	//get data
	
}
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)) 
	$stock_body.='<option value="'.$row['id'].'" '.($row['id']=='c7afd71a-4c3a-bde4-138d-4acaee1644e4'?' selected ':'').'>'.$row['name'].'</option>';
$stock.=$stock_body.'</select><input id="stock_id" name="stock_id" value="c7afd71a-4c3a-bde4-138d-4acaee1644e4" type="hidden"/>';

require_once ('include/MVC/View/SugarView.php');
require_once ('modules/EcmStockDocOuts/views/EditView/view.edit.ecmstockdocouts.php');
$edit = new ViewEditEcmStockDocOuts ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmStockDocOuts';
$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("ECMSALE", $ecmsale);
$edit->ss->assign("ECMSALE_ID",$_REQUEST['parent_doc_id']);
$edit->ss->assign("ECMSALE_DOCUMENT_NO",$focus->ecmsale_document_no);
$edit->ss->assign("STOCK", $stock);
echo $edit->display ();
//loading view
echo '<link rel="stylesheet" type="text/css" href="modules/EcmStockDocOuts/javascript/helper.css" media="screen" /><div class="loading_panel"></div>';