/**
  * Get accounts children
  *
  * @param array $input
  * 	An array as follows: array(id => $id);
  *
  * @return JSON encoded string
  *  A string as follows: [{"text" : $accountKey . " " . $accountName, "state" : {"opened" : true }, "icon" : $icon, "children" : [{"text" : $childAccountKey0 . " " . $childAccountName0, "icon" : $childIcon0}, …]}]
  */
 public function getAccountChildren($input)
 {
     $Account = $this->Account->byId($input['id']);
     $AccountType = $this->AccountType->byId($Account->account_type_id);
     $accountTree = array('text' => $Account->key . ' ' . $Account->name, 'state' => array('opened' => true), 'icon' => 'fa fa-sitemap', 'children' => array());
     $this->Account->byParent($input['id'])->each(function ($Account) use(&$accountTree) {
         if ($Account->is_group) {
             array_push($accountTree['children'], array('text' => $Account->key . ' ' . $Account->name, 'icon' => 'fa fa-sitemap'));
         } else {
             array_push($accountTree['children'], array('text' => $Account->key . ' ' . $Account->name, 'icon' => 'fa fa-leaf'));
         }
     });
     // return array(array('label' => $this->Lang->get('decima-accounting::account-management.D'), 'value'=> 'D'), array('label' => $this->Lang->get('decima-accounting::account-management.A'), 'value'=> 'A'));
     // $this->AccountType->byOrganization($this->AuthenticationManager->getCurrentUserOrganizationId())->each(function($AccountType) use (&$accountTypes)
     // {
     // array_push($accountTypes, array('label'=> $AccountType->name, 'value'=>$AccountType->id));
     // });
     return json_encode(array('accountTree' => $accountTree, 'balanceTypeName' => $this->Lang->get('decima-accounting::account-management.' . $Account->balance_type), 'balanceTypeValue' => $Account->balance_type, 'accountTypeName' => $AccountType->name, 'accountTypeValue' => $Account->account_type_id));
 }
 /**
  * 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)));
 }