/**
  * Create a new account
  *
  * @param array $input
  * 	An array as follows: array('key'=>$key, 'name'=>$name, 'balance_type'=>$balanceType, 'is_group'=>$isGroup,
  *                              'account_type_id'=>$accountTypeId, 'parent_account_id'=>$parentAccountId
  *                            );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessSaveMessage}
  */
 public function create(array $input)
 {
     unset($input['_token'], $input['parent_account'], $input['balance_type_name'], $input['account_type']);
     $groupsAccounts = $info = false;
     $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
     $organizationId = $this->AuthenticationManager->getCurrentUserOrganizationId();
     $input = eloquent_array_filter_for_insert($input);
     $input = array_add($input, 'organization_id', $organizationId);
     $this->DB->transaction(function () use($input, $loggedUserId, $organizationId, &$groupsAccounts, &$info) {
         if ($this->Account->byOrganizationAndByKey($organizationId, $input['key'])->count() > 0) {
             $info = $this->Lang->get('decima-accounting::account-management.keyValidationMessage', array('key' => $input['key']));
             return;
         }
         $Account = $this->Account->create($input);
         $Journal = $this->Journal->create(array('journalized_id' => $Account->id, 'journalized_type' => $this->Account->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.accountAddedJournal', array('account' => $Account->key . ' ' . $Account->name)), $Journal));
         if (!empty($Account->is_group)) {
             $groupsAccounts = $this->getGroupsAccounts();
         }
     });
     if (!$info) {
         return json_encode(array('success' => $this->Lang->get('form.defaultSuccessSaveMessage'), 'groupsAccounts' => $groupsAccounts));
     } else {
         return json_encode(array('info' => $info));
     }
 }
 /**
  * Create closing balance voucher
  *
  * @param array $input
  * 	An array as follows: array('fiscal_year_id'=>$id, 'date'=> $date, 'period_id' => periodId, 'voucher_type_id' => $voucherTypeId, 'remark' => $remark, 'cost_center_id' => $costCenterId, 'account_id' => $accountId );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessSaveMessage}
  */
 public function createClosingBalanceVoucher(array $input)
 {
     $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
     $organizationId = $this->AuthenticationManager->getCurrentUserOrganizationId();
     $input = eloquent_array_filter_for_insert($input);
     $input = array_add($input, 'organization_id', $organizationId);
     $this->DB->transaction(function () use($input, $loggedUserId, $organizationId, &$resultClosing, &$resultOpening) {
         $resultClosing = json_decode($this->JournalManagerService->saveJournalVoucher(array('notransaction' => '', 'status' => 'B', 'date' => $input['date_closing'], 'remark' => $input['remark_closing'], 'period_id' => $input['period_id_closing'], 'voucher_type_id' => $input['voucher_type_id_closing'])), true);
         $resultOpening = json_decode($this->JournalManagerService->saveJournalVoucher(array('notransaction' => '', 'status' => 'B', 'date' => $input['date_opening'], 'remark' => $input['remark_opening'], 'period_id' => $input['period_id_opening'], 'voucher_type_id' => $input['voucher_type_id_opening'])), true);
         $entries = $this->JournalEntry->getJournalEntriesGroupedByPlBsCategoryByOrganizationAndByFiscalYear(array('D', 'E'), $organizationId, $input['fiscal_year_id']);
         $voucherClosingEntries = array();
         $voucherOpeningEntries = array();
         foreach ($entries as $Entry) {
             //$Entry->balance_type
             //Deudor,Receivable:D and Acreedor,Payable: A
             if ($Entry->balance_type == 'D') {
                 $credit = round($Entry->debit - $Entry->credit, 2);
                 $debit = 0;
             } else {
                 $debit = round($Entry->credit - $Entry->debit, 2);
                 $credit = 0;
             }
             if ($debit != 0 || $credit != 0) {
                 array_push($voucherClosingEntries, array('debit' => $debit, 'credit' => $credit, 'cost_center_id' => $Entry->cost_center_id, 'account_id' => $Entry->account_id, 'journal_voucher_id' => $resultClosing['id']));
                 array_push($voucherOpeningEntries, array('debit' => $credit, 'credit' => $debit, 'cost_center_id' => $Entry->cost_center_id, 'account_id' => $Entry->account_id, 'journal_voucher_id' => $resultOpening['id']));
             }
         }
         $this->JournalEntry->massCreate($voucherClosingEntries);
         $this->JournalEntry->massCreate($voucherOpeningEntries);
         $Journal = $this->Journal->create(array('journalized_id' => $input['fiscal_year_id'], 'journalized_type' => $this->FiscalYear->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::close-fiscal-year.closingBalanceAddedJournal', array('number' => $resultClosing['number'], 'period' => $input['period_label_closing'])), $Journal));
         $this->JournalManagerService->updateJournalVoucherStatus($resultClosing['id']);
         $Journal = $this->Journal->create(array('journalized_id' => $input['fiscal_year_id_opening'], 'journalized_type' => $this->FiscalYear->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::close-fiscal-year.openingBalanceAddedJournal', array('number' => $resultOpening['number'], 'period' => $input['period_label_opening'])), $Journal));
         $this->JournalManagerService->updateJournalVoucherStatus($resultOpening['id']);
     });
     return json_encode(array('success' => $this->Lang->get('decima-accounting::close-fiscal-year.closingBalanceAddedJournal', array('number' => $resultClosing['number'], 'period' => $input['period_label_closing'])) . '<br>' . $this->Lang->get('decima-accounting::close-fiscal-year.closingBalanceAddedJournal', array('number' => $resultOpening['number'], 'period' => $input['period_label_opening']))));
 }
 /**
  * Create a new ...
  *
  * @param array $input
  * 	An array as follows: array('field0'=>$field0, 'field1'=>$field1
  *                            );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessSaveMessage}
  */
 public function create(array $input)
 {
     unset($input['_token']);
     $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
     $organizationId = $this->AuthenticationManager->getCurrentUserOrganizationId();
     $input = eloquent_array_filter_for_insert($input);
     $input = array_add($input, 'organization_id', $organizationId);
     // $input['date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['date'])->format('Y-m-d');
     $this->DB->transaction(function () use($input, $loggedUserId, $organizationId) {
         $VoucherType = $this->VoucherType->create($input);
         $Journal = $this->Journal->create(array('journalized_id' => $VoucherType->id, 'journalized_type' => $this->VoucherType->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::voucher-type-management.addedJournal', array('VoucherType' => $VoucherType->name)), $Journal));
     });
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessSaveMessage')));
 }
 /**
  * Create a new journal entry
  *
  * @param array $input
  * 	An array as follows: array('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.defaultSuccessSaveMessage, "statusLabel": $statusLabel, "status": $status}
  */
 public function saveJournalEntry(array $input)
 {
     $journalNumber = $input['number'];
     $unchangedInputValues = $input;
     unset($input['_token'], $input['cost_center'], $input['account'], $input['number']);
     $input = eloquent_array_filter_for_insert($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) {
         $JournalEntry = $this->JournalEntry->create($input);
         $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()));
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::journal-management.journalEntryAddedJournal', array('number' => $journalNumber)), $Journal));
         // $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.account'), 'field_lang_key' => 'decima-accounting::journal-management.account', 'new_value' => $unchangedInputValues['account']), $Journal);
         $status = $this->updateJournalVoucherStatus($JournalEntry->journal_voucher_id, $Journal);
     });
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessSaveMessage'), 'status' => $status, 'statusLabel' => $this->Lang->get('decima-accounting::journal-management.' . $status)));
 }