/**
  * Update an existing account
  *
  * @param array $input
  * 	An array as follows: array('id' => $id, 'key'=>$key, 'name'=>$name, 'balance_type'=>$balanceType, 'is_group'=>$isGroup,
  *                             'account_type_id'=>$accountTypeId, 'parent_account_id'=>$parentAccountId, 'parent_account' => $parentAccount
  *                             'account_type' => $accountType, 'parent_account' => $parentAccount
  *                            );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessUpdateMessage}
  */
 public function update(array $input)
 {
     $newParentAccount = $input['parent_account'];
     $newAccountType = $input['account_type'];
     $groupsAccounts = $info = false;
     unset($input['_token'], $input['parent_account'], $input['balance_type_name'], $input['account_type']);
     $input = eloquent_array_filter_for_update($input);
     // $input['date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['date'])->format('Y-m-d');
     if ($this->getAccountChildrenCount($input['id']) > 0 && $input['is_group'] == 0) {
         return json_encode(array('info' => $this->Lang->get('decima-accounting::account-management.isGroupException')));
     }
     $this->DB->transaction(function () use(&$input, $newParentAccount, $newAccountType, &$groupsAccounts, &$info) {
         $Account = $this->Account->byId($input['id']);
         $unchangedAccountValues = $Account->toArray();
         if (rtrim($input['key']) != rtrim($Account->key) && $this->Account->byOrganizationAndByKey($this->AuthenticationManager->getCurrentUserOrganizationId(), $input['key'])->count() > 0) {
             $info = $this->Lang->get('decima-accounting::account-management.keyValidationMessage', array('key' => $input['key']));
             return;
         }
         $this->Account->update($input, $Account);
         if (!empty($input['is_group'])) {
             $groupsAccounts = $this->getGroupsAccounts();
         }
         $diff = 0;
         foreach ($input as $key => $value) {
             if ($unchangedAccountValues[$key] != $value) {
                 $diff++;
                 if ($diff == 1) {
                     $Journal = $this->Journal->create(array('journalized_id' => $Account->id, 'journalized_type' => $this->Account->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId()));
                 }
                 if ($key == 'balance_type') {
                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.balanceType'), 'field_lang_key' => 'decima-accounting::account-management.balanceType', 'old_value' => $this->Lang->get('decima-accounting::account-management.' . $unchangedAccountValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::account-management.' . $value)), $Journal);
                 } else {
                     if ($key == 'account_type_id') {
                         $oldAccountType = $this->AccountType->byId($unchangedAccountValues[$key]);
                         $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.accountType'), 'field_lang_key' => 'decima-accounting::account-management.accountType', 'old_value' => $oldAccountType->name, 'new_value' => $newAccountType), $Journal);
                     } else {
                         if ($key == 'parent_account_id') {
                             if (!empty($unchangedAccountValues[$key])) {
                                 $OldParentAccount = $this->Account->byId($unchangedAccountValues[$key]);
                                 $oldParentAccount = $OldParentAccount->key . ' ' . $OldParentAccount->name;
                             } else {
                                 $oldParentAccount = '';
                             }
                             $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.parentAccount'), 'field_lang_key' => 'decima-accounting::account-management.parentAccount', 'old_value' => $oldParentAccount, 'new_value' => $newParentAccount), $Journal);
                         } else {
                             if ($key == 'is_group') {
                                 $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.isGroupLong'), 'field_lang_key' => 'decima-accounting::account-management.isGroupLong', 'old_value' => $this->Lang->get('decima-accounting::account-management.' . $unchangedAccountValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::account-management.' . $value)), $Journal);
                             } else {
                                 $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::account-management.' . camel_case($key), 'old_value' => $unchangedAccountValues[$key], 'new_value' => $value), $Journal);
                             }
                         }
                     }
                 }
             }
         }
     });
     if (!$info) {
         return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'), 'groupsAccounts' => $groupsAccounts));
     } else {
         return json_encode(array('info' => $info));
     }
 }