<?php

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

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

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

// dump($_POST);
// exit;

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

$_POST['position_list'] = $pll;

require_once("modules/EcmFkVatTables/EcmFkVatTable.php");
require_once('include/formbase.php');

$focus = new EcmFkVatTable();

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->id);
// exit;

if($focus->account_acc_ma == '1') {
    $focus->acc_ma = '';
    $focus->acc_ma_code = '';
}

if($focus->account_acc_wn == '1') {
    $focus->acc_wn = '';
    $focus->acc_wn_code = '';
}

// dump($focus->toArray());
// exit;

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

handleRedirect($return_id,'EcmFkVatTables');