<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');

global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings, $current_user, $app_list_strings, $db;
	  
$app_list_strings['ecmproducts_parent_dom'] = array (
  'Accounts' => $app_list_strings['moduleList']['Accounts'],
  'Leads' => $app_list_strings['moduleList']['Leads'],
);
require_once('modules/EcmProductB2Bs/EcmProductB2B.php');
require_once('modules/EcmProductB2Bs/Forms.php');
require_once('include/time.php');
require_once('include/json_config.php');

//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>';

$json_config = new json_config();

$focus = new EcmProductB2B();   
  
if(isset($_REQUEST['record'])){
	$focus->retrieve($_REQUEST['record']);
    //if(isset($focus->id) && $focus->id != '')$focus->format_all_fields();
}

$json = getJSONobj();

require_once('include/MVC/View/SugarView.php');  
require_once('modules/EcmProductB2Bs/views/EditView/view.edit.ecmproducts.php');
$edit = new ViewEditEcmProductB2Bs();
$edit->ss = new Sugar_Smarty();
$edit->bean = $focus;

$edit->preDisplay(); 

   $uunit='';
   $result = $db->query("select id,name from ecmproductusageunits order by name asc");
   while(($row=$db->fetchByAssoc($result))!=null)
   {
		$uunit.='<option value="'.$row['id'].'"';
		if($focus->usage_unit_id==$row['id'])$uunit.=' selected';
		$uunit.='>'.$row['name'].'</option>';
   }
   $edit->ss->assign("USAGE_UNIT_ID",$uunit);  

   $tax='';
   $result = $db->query("select id,name from ecmvats where deleted='0' order by name asc");
   while(($row=$db->fetchByAssoc($result))!=null)
   {
		$tax.='<option value="'.$row['id'].'"';
		if($focus->vat_id==$row['id'])$tax.=' selected';
		$tax.='>'.$row['name'].'</option>';
   }
   $edit->ss->assign("VAT_ID",$tax);  

   $fbas='';
   $result = $db->query("select id,name from ecmproductbasis order by name asc");
   while(($row=$db->fetchByAssoc($result))!=null)
   {
		$fbas.='<option value="'.$row['id'].'"';
		if($focus->fob_basis_id==$row['id'])$fbas.=' selected';
		$fbas.='>'.$row['name'].'</option>';
   }
   $edit->ss->assign("FOB_BASIS_ID",$fbas);  

   $pack='';
   $result = $db->query("select id,name from ecmproductpackingtypes order by name asc");
   while(($row=$db->fetchByAssoc($result))!=null)
   {
		$pack.='<option value="'.$row['id'].'"';
		if($focus->packing_type_id==$row['id'])$pack.=' selected';
		$pack.='>'.$row['name'].'</option>';
   }
   $edit->ss->assign("PACKING_TYPE_ID",$pack);  
   
	//load currencies
    require_once('modules/Currencies/Currency.php');
    $currency = new Currency();
    $currency_list = $currency->get_full_list('name');
    $currency->retrieve('-99');
    if(is_array($currency_list))
	{
    	$currency_list = array_merge(Array($currency), $currency_list);
    }
	else
	{
   		$currency_list = Array($currency); 
    } 
    $arr = array();
    foreach($currency_list as $key=>$value)
	{
    	$arr[$value->id] = $value->name;
    } 
    $edit->ss->assign("EXCHANGE_RATE_ID", get_select_options_with_id($arr, $focus->exchange_rate_id)); 

$arr=unserialize(base64_decode($focus->models));
$result = $GLOBALS['db']->query("select * from ecmproductmodels where deleted='0' and parent_id='".$focus->product_subcategory_id."'");
$sm='<div id="models_div"><select name="models[]" multiple id="models" sieze="6">';
while($row=$GLOBALS['db']->fetchByAssoc($result)){
	$sm.='<option value="'.$row['id'].'"';
	if(@in_array($row['id'],$arr))$sm.=' selected';
	$sm.='>'.$row['name'].'</option>';
}
$sm.='</select></div>';
$edit->ss->assign("SM", $sm);

$result = $GLOBALS['db']->query("select * from ecmproduct_language where ecmproduct_id='".$_REQUEST['record']."'");
while($row=$GLOBALS['db']->fetchByAssoc($result))
{
   	$edit->ss->assign("EAN_".$row['language'],$row['ean']);
   	$edit->ss->assign("REMARKS_".$row['language'],$row['remarks']);
   	$edit->ss->assign("SHORT_DESCRIPTION_".$row['language'],$row['short_description']);
   	$edit->ss->assign("LONG_DESCRIPTION_".$row['language'],$row['long_description']);
   	$edit->ss->assign("PRICE_".$row['language'],number_format($row['price'],2,",","."));
}
$re=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select ean, ean2 from ecmproducts where id='".$_REQUEST['record']."'"));
$edit->ss->assign("EAN",$re['ean']);
$edit->ss->assign("EAN2",$re['ean2']);

$edit->ss->assign("CARTON_DIMENSIONS_1",number_format($focus->carton_dimensions_1,3,",","."));
$edit->ss->assign("CARTON_DIMENSIONS_2",number_format($focus->carton_dimensions_2,2,",","."));
$edit->ss->assign("CARTON_DIMENSIONS_3",number_format($focus->carton_dimensions_3,2,",","."));
$edit->ss->assign("PACKING_DIMENSIONS_1",number_format($focus->packing_dimensions_1,2,",","."));
$edit->ss->assign("PACKING_DIMENSIONS_2",number_format($focus->packing_dimensions_2,2,",","."));
$edit->ss->assign("PACKING_DIMENSIONS_3",number_format($focus->packing_dimensions_3,2,",","."));

$pl3 = $this->bean->getPositionList3();
$edit->ss->assign('POSITION_LIST3', $pl3);

if($focus->product_picture)$edit->ss->assign("PRODUCT_PICTURE_UPLOAD",$mod_strings['LBL_UPLOADED']);
if($focus->packing_front_picture)$edit->ss->assign("PACKING_FRONT_PICTURE_UPLOAD",$mod_strings['LBL_UPLOADED']);
if($focus->driver_1)$edit->ss->assign("DRIVER_1_UPLOAD",$mod_strings['LBL_UPLOADED']);
if($focus->driver_2)$edit->ss->assign("DRIVER_2_UPLOAD",$mod_strings['LBL_UPLOADED']);

//Added for Graduated Prices - Begin
/*	require_once('modules/EcmProductB2Bs/EcmProductB2BGraduatedPrices.php');
	$epgp = new EcmProductB2BGraduatedPrices($focus->graduated_prices);
	$edit->ss->assign("PRODUCT_GRADUATED_PRICES_HTML", $epgp->getHtmlEdit());*/
//Added for Graduated Prices - End

//Added for Components
$pl4 = $this->bean->getPricesList();
$edit->ss->assign('POSITION_LIST4', $pl4);

	$edit->ss->assign("POSITION_LIST", $focus->getPositionList());

	$file = 'modules/EcmGroupSales/EcmGroupSale.php';
	if(file_exists($file)) {
		$cc = array();
		require_once($file);
		$cc = EcmGroupSale::loadSettings();	
	}
	$OPT = array();
	$OPT['row_item_height'] = $cc['row_item_height'];
	$OPT['row_item_height_selected'] = $cc['row_item_height_selected'];
	$OPT['rows_on_item_list'] = $cc['rows_on_item_list'];
	$OPT['position_table_height'] = $OPT['row_item_height']*$OPT['rows_on_item_list']+40+$OPT['rows_on_item_list']*4;
	$OPT['quick_product_item_adding'] = $cc['quick_product_item_adding'];
	
	global $current_user;
	$tmp = $current_user->getPreference('num_grp_sep');
	if(!isset($tmp) || $tmp == '' || $tmp == NULL) $tmp = $sugar_config['default_number_grouping_seperator'];
	$OPT['sep_1000'] = $tmp;
	$tmp = $current_user->getPreference('dec_sep');
	if(!isset($tmp) || $tmp == '' || $tmp == NULL) $tmp = $sugar_config['default_decimal_seperator'];
	$OPT['dec_sep'] = $tmp;
	$tmp = $current_user->getPreference('default_currency_significant_digits');
	if(!isset($tmp) || $tmp == '' || $tmp == NULL) $tmp = $sugar_config['default_currency_significant_digits'];
	$OPT['dec_len'] = $tmp;
	$OPT['default_unit'] = "1";
	$OPT['default_vat'] = "19.00";
	$OPT['default_category'] = "";
	$OPT['default_currency'] = "-99";
	$OPT['type'] = $focus->type;
	$OPT['to_is_vat_free'] = $focus->to_is_vat_free;
	$OPT['ecmproduct_usage_units_options'] = $uunit;
	
	$scriptOpt = '
		<script language="javascript">
			var VAT = '.str_replace('&quot;','\"',$json->encode($VAT)).';
			var OPT = '.str_replace('&quot;','\"',$json->encode($OPT)).';
			var MOD = '.str_replace('&quot;','\"',$json->encode($mod_strings)).';
			var N;
		</script>';
	echo $scriptOpt;
	$edit->ss->assign("OPT", $OPT);
//Added for Components

echo $edit->display();
echo '<script language="javascript" src="modules/EcmProductB2Bs/helper.js"></script>';
echo '<script language="javascript" src="modules/EcmProductB2Bs/mintajax.js"></script>';
echo '<script language="javascript">
var sb=document.getElementById("product_subcategory_name");
sb.onclick=function(){
	setTimeout(function(){mintajaxget("index.php?to_pdf=1&module=EcmProductB2Bs&action=getModelsBySubCategory&id="+document.getElementById("product_subcategory_id").value,"models_div");},1000);
}
function doRequest(where,post,doFunction,error) {
  this.Display = function(result) { doFunction(result.responseText); }
  this.Fail = function(result){ if(error) alert(error);}
  YAHOO.util.Connect.asyncRequest(\'POST\',where,{success:this.Display,failure:this.Fail},post);       
}

function generateEAN(nr) {
	if (document.getElementById(\'ean\').value != \'\') {
		if (confirm("'.$mod_strings['LBL_EAN_EXISTS'].'")) {							
			doRequest(\'index.php\',\'module=EcmProductB2Bs&action=generateEAN&to_pdf=1&ean=\'+document.getElementById(\'ean\').value+\'&ean2=\'+document.getElementById(\'ean2\').value+\'&fromEdit=1\', function(result) {	
				document.getElementById(\'ean\').value = result;
			});
			document.getElementById(\'newEan\').value=\'1\' 
			}
	} else {
		doRequest(\'index.php\',\'module=EcmProductB2Bs&action=generateEAN&to_pdf=1&ean=\'+document.getElementById(\'ean\').value+\'&ean2=\'+document.getElementById(\'ean2\').value+\'&fromEdit=1\', function(result) {	
				document.getElementById(\'ean\').value = result;
			});
		document.getElementById(\'newEan\').value=\'1\'
	}
}
function generateEAN2(nr) {
	if (document.getElementById(\'ean2\').value != \'\') {
		if (confirm("'.$mod_strings['LBL_EAN_EXISTS'].'")) {
			doRequest(\'index.php\',\'module=EcmProductB2Bs&action=generateEAN&to_pdf=1&ean=\'+document.getElementById(\'ean\').value+\'&ean2=\'+document.getElementById(\'ean2\').value+\'&fromEdit=1\', function(result) {	
				document.getElementById(\'ean2\').value = result;
			});
			document.getElementById(\'newEan2\').value=\'1\' }
	} else {
		doRequest(\'index.php\',\'module=EcmProductB2Bs&action=generateEAN&to_pdf=1&ean=\'+document.getElementById(\'ean\').value+\'&ean2=\'+document.getElementById(\'ean2\').value+\'&fromEdit=1\', function(result) {	
				document.getElementById(\'ean2\').value = result;
			});
		document.getElementById(\'newEan2\').value=\'1\'
		}
}
</script>';

?>