<?php

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

$json = getJSONobj();
$pll = array();

// Quick fix;
$_POST['sym_dok'] = $_POST['sym_dok_symbol'];
$_POST['sym_par'] = $_POST['sym_par_symbol'];

$exp = explode("||||", $_POST['items_list']);

foreach($exp as $ep){
  	if($ep){
		$pll[] = $json->decode(htmlspecialchars_decode($ep));
	}
}

$_POST['position_list'] = $pll;

// dump($_POST['position_list']);
// exit;

require_once("modules/EcmFkVatRegisters/EcmFkVatRegister.php");
require_once('include/formbase.php');

$focus = new EcmFkVatRegister();

if(isset($_POST['record']) && $_POST['record'] != '') {
    $focus->retrieve($_POST['record']);
}

if(!$focus->ACLAccess('Save')){
  	ACLController::displayNoAccess(true);
  	sugar_cleanup(true);
}

$check_notify = FALSE;

foreach($focus->column_fields as $field) {
  	if(isset($_POST[$field])) {
  		$value = $_POST[$field];
  		$focus->$field = $value;
  	}
}

foreach($focus->additional_column_fields as $field) {
  	if(isset($_POST[$field])){
  		$value = $_POST[$field];
  		$focus->$field = $value;
  	}
}

$json = getJSONobj();

$pl = $_POST['position_list'];

// dump($pl);
// dump($pll);
// dump($focus->type);
// dump($focus->column_fields);
// dump($_POST);
// dump($focus->toArray());
// exit;

$focus->position_list = $pl;
$focus->save($check_notify);
$return_id = $focus->id;

handleRedirect($return_id,'EcmFkVatRegisters');