/**
  * 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']))));
 }
 public function postGeneralLedgerGridData()
 {
     return $this->JournalManagerService->getGeneralLedgerGridData($this->Input->all());
 }
 public function postTrialBalanceGridData()
 {
     return $this->JournalManagerService->getTrialBalanceGridData($this->Input->all());
 }
 public function postGridData()
 {
     return $this->JournalManagerService->getTaxControlGridData($this->Input->all());
 }
 public function postDeleteJournalEntry()
 {
     return $this->JournalManagerService->deleteJournalEntry($this->Input->json()->all());
 }
 public function getIndex()
 {
     return $this->View->make('decima-accounting::close-fiscal-year')->with('accounts', $this->AccountManagerService->getNonGroupAccounts())->with('costCenters', $this->JournalManagerService->getCostCenters())->with('voucherTypes', $this->JournalManagerService->getVoucherTypes());
 }
 public function postProfitAndLossGridData()
 {
     return $this->JournalManagerService->getProfitAndLossGridData($this->Input->all());
 }