<?php
if (! defined ( 'sugarEntry' ) || ! sugarEntry)
	die ( 'NotAValidEntryPoint' );

global $sugar_version, $sugar_config, $current_user, $app_strings, $mod_strings, $current_user, $app_list_strings;
$db=$GLOBALS['db'];
require_once ('modules/EcmPrepaymentInvoices/EcmPrepaymentInvoice.php');
require_once ('include/time.php');

echo '<script type="text/javascript"
src="modules/EcmPrepaymentInvoices/javascript/searchcolumndefs.js"></script>';
echo '<script type="text/javascript"
src="include/ECM/tablenavigator.js"></script>';
echo '<script type="text/javascript"
src="include/ECM/SearchProductTable.js"></script>';


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

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

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['ecmsale_id']) {
	$new_number=true;
	$ecmsale_id	=$_REQUEST['ecmsale_id'];
	$ecmsale	=true;
	$q= new EcmSale();
	//get data
	$q->retrieve($_REQUEST['ecmsale_id']);
	$focus->parent_id=$q->parent_id;
	$focus->parent_name=$q->parent_name;
	$focus->register_date=date("d.m.Y");
	$focus->position_list= $q->getPositionList(true);	
	$focus->vat_id=$focus->position_list[0]['price_ecmvat_id'];
	$q1=$GLOBALS['db']->query("select sum(prepaid_amount) as total from ecmprepaymentinvoices where ecmsale_id='".$q->id."'");
	$res=$GLOBALS['db']->fetchByAssoc($q1);
	$focus->prepaid_amount=number_format($q->total_brutto-$res['total'],'2',',','.');

	unset($q);
}
else {
	$new_number=true;
	$focus->register_date = date("d.m.Y");
	$focus->payment_date = date("d.m.Y");
	//for subpanels??
	if (isset ( $_REQUEST ['contact_id'] ) && $_REQUEST ['contact_id'] != '' && isset ( $_REQUEST ['contact_name'] ) && $_REQUEST ['contact_name'] != '') {
		$_REQUEST ['parent_type'] = 'Contacts';
		$_REQUEST ['parent_name'] = $_REQUEST ['contact_name'];
		$_REQUEST ['parent_id'] = $_REQUEST ['contact_id'];
		$OPT ['check_parent_id'] = false;
	}
	
	if (isset ( $_REQUEST ['account_id'] ) && $_REQUEST ['account_id'] != '' && isset ( $_REQUEST ['account_name'] ) && $_REQUEST ['account_name'] != '') {
		$_REQUEST ['parent_type'] = 'Accounts';
		$_REQUEST ['parent_name'] = $_REQUEST ['account_name'];
		$_REQUEST ['parent_id'] = $_REQUEST ['account_id'];
		$OPT ['check_parent_id'] = false;
	}	
}

//get stocks
if($focus->vat_id=='')$focus->vat_id='28079566-b825-e38f-9993-4ccc7b781de5';
$res = $db->query("SELECT id, name,value FROM ecmvats WHERE deleted = '0' ORDER BY name");
$stock = '<select id="vat" name="vat" onchange="calculateVat();"><option value=""></option>';
$stock_body = '';
while ($row = $db->fetchByAssoc($res)){
	if($focus->vat_id!='' && $row['id']==$focus->vat_id){$s='selected';} else { $s='';}
    $stock_body.='<option value="'.$row['id'].':'.$row['value'].'" '.$s.'>'.$row['name'].'</option>';
}
$stock.=$stock_body.'</select><input id="vat_id" name="vat_id" type="hidden" value="'.$focus->vat_id.'"/>';
//includ OO VAT
$v = new EcmVat();
$v->retrieve('a8c52d5e-15df-2a13-c934-5578307bebd2');
$oo = array ('id' => 'a8c52d5e-15df-2a13-c934-5578307bebd2', 'name' => $v->name, 'value' => $v->value);
echo '<script> var OO=\''.json_encode($oo).'\'</script>';
require_once ('include/MVC/View/SugarView.php');
require_once ('modules/EcmPrepaymentInvoices/views/EditView/view.edit.ecmprepaymentinvoices.php');
$edit = new ViewEditEcmPrepaymentInvoices ();
$edit->ss = new Sugar_Smarty ();
$edit->module = 'EcmPrepaymentInvoices';
$edit->bean = $focus;
$edit->tplFile = 'include/ECM/EcmViews/EditView/Tabs/EditView.tpl';
$edit->preDisplay ();
$edit->ss->assign ( "NEW_NUMBER", $new_number );
$edit->ss->assign ( "ECMSALE_ID", $ecmsale_id );
$edit->ss->assign ( "ECMSALE", $ecmsale );
$edit->ss->assign("VAT_LIST", $stock);
$edit->ss->assign("DUPLICATE", $duplicate);
echo $edit->display ();
//loading view
echo '<link rel="stylesheet" type="text/css" href="modules/EcmPrepaymentInvoices/javascript/helper.css" media="screen" />';