/**
  * 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));
     }
 }
 /**
  * Update an existing cost center
  *
  * @param array $input
  * 	An array as follows: array('id' => $id, 'key'=>$key, 'name'=>$name, 'balance_type'=>$balanceType, 'is_group'=>$isGroup,
  *                             'parent_cc_id'=>$parentCostCenterId, 'parent_cc' => $parentCostCenter
  *                            );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessUpdateMessage}
  */
 public function update(array $input)
 {
     $newParentCostCenter = $input['parent_cc'];
     $groupsCostCenters = $info = false;
     unset($input['_token'], $input['parent_cc']);
     $input = eloquent_array_filter_for_update($input);
     if ($this->getCostCenterChildrenCount($input['id']) > 0 && $input['is_group'] == 0) {
         return json_encode(array('info' => $this->Lang->get('decima-accounting::cost-center-management.isGroupException')));
         // return json_encode(array('validationFailed' => true , 'fieldValidationMessages' => array('is-group' => $this->Lang->get('decima-accounting::cost-center-management.isGroupException'))));
     }
     $this->DB->transaction(function () use(&$input, $newParentCostCenter, &$groupsCostCenters, &$info) {
         $CostCenter = $this->CostCenter->byId($input['id']);
         $unchangedCostCenterValues = $CostCenter->toArray();
         if (rtrim($input['key']) != rtrim($CostCenter->key) && $this->CostCenter->byOrganizationAndByKey($this->AuthenticationManager->getCurrentUserOrganizationId(), $input['key'])->count() > 0) {
             $info = $this->Lang->get('decima-accounting::cost-center-management.keyValidationMessage', array('key' => $input['key']));
             return;
         }
         $this->CostCenter->update($input, $CostCenter);
         if (!empty($CostCenter->is_group)) {
             $groupsCostCenters = $this->getGroupsCostCenters();
         }
         $diff = 0;
         foreach ($input as $key => $value) {
             // var_dump($unchangedCostCenterValues);die();
             if ($unchangedCostCenterValues[$key] != $value) {
                 $diff++;
                 if ($diff == 1) {
                     $Journal = $this->Journal->create(array('journalized_id' => $CostCenter->id, 'journalized_type' => $this->CostCenter->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId()));
                 }
                 if ($key == 'parent_cc_id') {
                     if (!empty($unchangedCostCenterValues[$key])) {
                         $OldParentcostCenter = $this->CostCenter->byId($unchangedCostCenterValues[$key]);
                         $oldParentcostCenter = $OldParentcostCenter->key . ' ' . $OldParentcostCenter->name;
                     } else {
                         $oldParentcostCenter = '';
                     }
                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::cost-center-management.parentCc'), 'field_lang_key' => 'decima-accounting::cost-center-management.parentCc', 'old_value' => $oldParentcostCenter, 'new_value' => $newParentCostCenter), $Journal);
                 } else {
                     if ($key == 'is_group') {
                         $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::cost-center-management.isGroupLong'), 'field_lang_key' => 'decima-accounting::cost-center-management.isGroupLong', 'old_value' => $this->Lang->get('decima-accounting::account-management.' . $unchangedCostCenterValues[$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' => $unchangedCostCenterValues[$key], 'new_value' => $value), $Journal);
                     }
                 }
             }
         }
     });
     if (!$info) {
         return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'), 'groupsCostCenters' => $groupsCostCenters));
     } else {
         return json_encode(array('info' => $info));
     }
 }
 /**
  * Update an existing ...
  *
  * @param array $input
  * 	An array as follows: array('id' => $id, 'field0'=>$field0, 'field1'=>$field1
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessUpdateMessage}
  */
 public function update(array $input)
 {
     unset($input['_token']);
     $input = eloquent_array_filter_for_update($input);
     // $input['date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['date'])->format('Y-m-d');
     $this->DB->transaction(function () use(&$input) {
         $VoucherType = $this->VoucherType->byId($input['id']);
         $unchangedVoucherTypeValues = $VoucherType->toArray();
         $this->VoucherType->update($input, $VoucherType);
         $diff = 0;
         foreach ($input as $key => $value) {
             if ($unchangedVoucherTypeValues[$key] != $value) {
                 $diff++;
                 if ($diff == 1) {
                     $Journal = $this->Journal->create(array('journalized_id' => $VoucherType->id, 'journalized_type' => $this->VoucherType->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId()));
                 }
                 if ($key == 'name') {
                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::voucher-type-management.name'), 'field_lang_key' => 'decima-accounting::voucher-type-management.name', 'old_value' => $this->Lang->get($unchangedVoucherTypeValues[$key]), 'new_value' => $this->Lang->get($value)), $Journal);
                 } else {
                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::voucher-type-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::voucher-type-management.' . camel_case($key), 'old_value' => $unchangedVoucherTypeValues[$key], 'new_value' => $value), $Journal);
                 }
             }
         }
     });
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage')));
 }
 /**
  * Update an existing journal entry
  *
  * @param array $input
  * 	An array as follows: array('journal_entry_id' => $journalEntryId, 'debit'=>$debit, 'credit'=>$credit, 'system_reference_type'=>$systemReferenceType, 'system_reference_field'=>$systemReferenceField,
  *                             'journal_voucher_id'=> journalVoucherId, 'cost_center_id'=>$costCenterId, 'account_id' => $accountId
  *                            );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessUpdateMessage, "statusLabel": $statusLabel, "status": $status}
  */
 public function updateJournalEntry(array $input, $Journal = null)
 {
     $journalNumber = $input['number'];
     $unchangedInputValues = $input;
     $input['id'] = $input['journal_entry_id'];
     unset($input['_token'], $input['cost_center'], $input['account'], $input['number'], $input['journal_entry_id']);
     $input = eloquent_array_filter_for_update($input);
     $input['debit'] = remove_thousands_separator($input['debit']);
     $input['credit'] = remove_thousands_separator($input['credit']);
     $JournalVoucher = $this->JournalVoucher->byId($input['journal_voucher_id']);
     $Period = $this->Period->byId($JournalVoucher->period_id);
     if ($Period->is_closed) {
         return json_encode(array('success' => false, 'info' => $this->Lang->get('decima-accounting::journal-management.closedPeriodValidationMessage2', array('period' => $this->Lang->get('decima-accounting::period-management.' . $Period->month)))));
     }
     $this->DB->transaction(function () use(&$input, $journalNumber, $unchangedInputValues, &$status, $Journal) {
         $JournalEntry = $this->JournalEntry->byId($input['id']);
         $unchangedJournalEntryValues = $JournalEntry->toArray();
         $this->JournalEntry->update($input, $JournalEntry);
         $diff = 0;
         foreach ($input as $key => $value) {
             if ($unchangedJournalEntryValues[$key] != $value) {
                 $diff++;
                 if ($diff == 1) {
                     if (empty($Journal)) {
                         $Journal = $this->Journal->create(array('journalized_id' => $JournalEntry->journal_voucher_id, 'journalized_type' => $this->JournalVoucher->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId()));
                     }
                 }
                 if ($key == 'cost_center_id') {
                     $CostCenter = $this->CostCenter->byId($value);
                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.costCenter'), 'field_lang_key' => 'decima-accounting::journal-management.costCenter', 'old_value' => $unchangedInputValues['cost_center'], 'new_value' => $CostCenter->key . ' ' . $CostCenter->name), $Journal);
                 } else {
                     if ($key == 'account_id') {
                         $Account = $this->Account->byId($value);
                         $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.account'), 'field_lang_key' => 'decima-accounting::journal-management.account', 'old_value' => $unchangedInputValues['account'], 'new_value' => $Account->key . ' ' . $Account->name), $Journal);
                     } else {
                         $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $unchangedJournalEntryValues[$key], 'new_value' => $value), $Journal);
                     }
                 }
             }
         }
         if (empty($Journal)) {
             $status = $this->updateJournalVoucherStatus($JournalEntry->journal_voucher_id);
         } else {
             $status = $this->updateJournalVoucherStatus($JournalEntry->journal_voucher_id, $Journal);
         }
     });
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'), 'status' => $status, 'statusLabel' => $this->Lang->get('decima-accounting::journal-management.' . $status)));
 }