/**
  * Close an existing period
  *
  * @param array $input
  * 	An array as follows: array(id => $id);
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessOperationMessage}
  */
 public function closePeriod(array $input)
 {
     $canBeClosed = true;
     $this->DB->transaction(function () use($input, &$canBeClosed) {
         $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
         $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id');
         if ($this->JournalVoucher->getByOrganizationByPeriodAndByStatus($organizationId, array($input['id']), 'A')->count() > 0) {
             $canBeClosed = false;
             return;
         }
         $Period = $this->Period->byId($input['id']);
         $Journal = $this->Journal->create(array('journalized_id' => $input['id'], 'journalized_type' => $this->Period->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
         // $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.status'), 'field_lang_key' => 'decima-accounting::journal-management.status', 'old_value' => $this->Lang->get('decima-accounting::period-management.opened'), 'new_value' => $this->Lang->get('decima-accounting::period-management.closed')), $Journal);
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::period-management.periodClosedJournal', array('period' => $this->Lang->get('decima-accounting::period-management.' . $Period->month))), $Journal));
         $this->Period->update(array('is_closed' => true), $Period);
     });
     if (!$canBeClosed) {
         return json_encode(array('success' => false, 'info' => $this->Lang->get('decima-accounting::period-management.voucherException')));
     }
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessOperationMessage')));
 }
 /**
  * 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)));
 }