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

$db = $GLOBALS['db'];

//add jquery


echo '<link href="modules/EcmQuotes/MyTable.css" type="text/css" rel="stylesheet">';
echo '<script type="text/javascript"
src="include/jQuery/jquery-2.1.0.min.js"></script>';
echo '<script type="text/javascript"
src="include/SubPanel/SubPanelTiles.js"></script>';
echo '<link rel="stylesheet" type="text/css" href="include/jQuery/jquery.appendGrid-master/jquery.appendGrid-1.5.0.css"/>';
echo '<script type="text/javascript"
src="include/jQuery/jquery.appendGrid-master/jquery.appendGrid-1.5.0.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 EcmNewKpkw ();

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 EcmInvoiceOut();

	$s->retrieve($_REQUEST['parent_doc_id']);
	$focus->parent_name=$s->parent_name;
	$focus->amount=format_number($s->total_brutto);
	$current_user->cash_id='4fda89f8-3879-57f1-00fb-53a9755e80fa';
	$focus->parent_id=$s->parent_id;
	$focus->parent_address_name=$s->parent_address_name;
	$focus->parent_address_street=$s->parent_address_street;
	$focus->parent_address_postalcode=$s->parent_address_postalcode;
	$focus->parent_address_city=$s->parent_address_city;
	$focus->parent_address_country=$s->parent_address_country;
	$focus->order_no=$s->parent_document_no;
	$focus->currency_id=$s->currency_id;
	$focus->register_date = date("d.m.Y");
	$focus->so_id=$_REQUEST['parent_doc_id'];
	//get data
	$r=$focus->db->query("select id from ecmtransactions where deleted=0 and record_id= '$s->id'");
	$dane= $focus->db->fetchByAssoc($r);
	$trans_id=$dane['id'];
}
else {
	$new_number=true;
	//dates

	$focus->type=$_REQUEST ['type'];
	$focus->stock_id=$current_user->stock_id;
	$focus->register_date = date("d.m.Y");
	$OPT ['check_parent_id'] = false;
	if($current_user->cash_id!=''){
	    $n=new EcmCash();
	    $n->retrieve($current_user->cash_id);
	    $focus->currency_id=$n->currency_id;
	  
	    $focus->number = EcmNewKpkw::generateNumber($current_user->cash_id,$focus->type,$focus->register_date);
	    $focus->document_no= EcmNewKpkw::generateDocNumber($current_user->cash_id,$focus->type,$focus->register_date);
	}
	
}

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

// get types
$type= '<select id="type" name="type"><option value=""></option>';
$type_body = '';

foreach ($app_list_strings['ecmkpkw_type_dir'] as $k=>$v){
    if($focus->type==$k){$selected='selected'; } else {$selected=''; }
    $type_body.='<option value="'.$k.'" '.$selected.'>'.$v.'</option>';
}
$type.=$type_body.'</select><input id="type_id" name="type_id" type="hidden" value="'.$_REQUEST ['type'].'"/>';

//get currencies
$cur= '<select id="currency" name="currency"><option value=""></option>';
$cur_body = '';

foreach ($app_list_strings['currency_dom'] as $k=>$v){
    if($focus->currency_id==$k){$selected='selected'; } else {$selected=''; }
    $cur_body.='<option value="'.$k.'" '.$selected.'>'.$v.'</option>';
}
$cur.=$cur_body.'</select><input id="currency_id" name="currency_id" type="hidden" value="'.$focus->currency_id.'"/>';

require_once ('include/MVC/View/SugarView.php');
require_once ('modules/EcmNewKpkws/views/EditView/view.edit.ecmnewkpkws.php');
$edit = new ViewEditEcmNewKpkws ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmNewKpkws';
$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("CASH", $stock);
$edit->ss->assign("trans_id",$trans_id);
$edit->ss->assign("TYPE", $type);
$edit->ss->assign("CURRENCY", $cur);
echo $edit->display ();
//loading view
echo '<link rel="stylesheet" type="text/css" href="modules/EcmNewKpkws/javascript/helper.css" media="screen" /><div class="loading_panel"></div>';