/**
  * 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']))));
 }
 /**
  * Delete an existing account (soft delete)
  *
  * @param array $input
  * 	An array as follows: array(id => $id);
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessDeleteMessage}
  */
 public function delete(array $input)
 {
     $info = false;
     $this->DB->transaction(function () use($input, &$info) {
         $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
         $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id');
         $ids = $this->getAccountChildrenIds($input['id']);
         array_push($ids, $input['id']);
         if ($this->JournalEntry->byAccountIds($ids)->count() > 0) {
             $info = $this->Lang->get('decima-accounting::account-management.accountValidationMessage');
             return;
         }
         $this->Account->byIds($ids)->each(function ($Account) use($loggedUserId, $organizationId) {
             $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::account-management.accountDeletedAccount', array('account' => $Account->key . ' ' . $Account->name)), $Journal));
         });
         $this->Account->delete($ids);
     });
     if (!$info) {
         return json_encode(array('success' => $this->Lang->get('form.defaultSuccessDeleteMessage')));
     } else {
         return json_encode(array('info' => $info));
     }
 }
 /**
  * Delete an existing journal entry (soft delete)
  *
  * @param array $input
  * 	An array as follows: array($id0, $id1,…);
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessDeleteMessage}
  */
 public function deleteJournalEntry(array $input)
 {
     $count = 0;
     $periodIsClosed = false;
     $this->DB->transaction(function () use($input, &$count, &$status, &$periodIsClosed, &$Period) {
         $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
         $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id');
         foreach ($input['id'] as $key => $id) {
             $count++;
             $JournalEntry = $this->JournalEntry->byId($id);
             if ($count == 1) {
                 $JournalVoucher = $this->JournalVoucher->byId($JournalEntry->journal_voucher_id);
                 $Period = $this->Period->byId($JournalVoucher->period_id);
                 if ($Period->is_closed) {
                     $periodIsClosed = true;
                     return;
                 }
                 $JournalVoucher = $this->JournalVoucher->byId($JournalEntry->journal_voucher_id);
                 $Journal = $this->Journal->create(array('journalized_id' => $JournalEntry->journal_voucher_id, 'journalized_type' => $this->JournalVoucher->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
             }
             $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::journal-management.journalEntryDeletedJournal', array('number' => $JournalVoucher->number)), $Journal));
             $this->JournalEntry->delete($input['id']);
         }
         $status = $this->updateJournalVoucherStatus($JournalVoucher->id, $Journal, $JournalVoucher);
     });
     if ($periodIsClosed) {
         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)))));
     }
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessDeleteMessage'), 'status' => $status, 'statusLabel' => $this->Lang->get('decima-accounting::journal-management.' . $status)));
 }