/**
  * Create a new journal voucher
  *
  * @param array $input
  * 	An array as follows: array('date'=>$date, 'manual_reference'=>$manualReference, 'remark'=>$remark,
  *                              'system_reference_type'=>$systemTeferenceType, 'system_reference_field'=>$systemReferenceField, 'system_reference_id'=>$systemReferenceId
  *                              'is_editable'=>$isEditable, 'status'=>$status, 'voucher_type_id'=>$voucherTypeId
  *                            );
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessSaveMessage, "id": $id, "number": $number}
  *	In case of success and user has two organization: {"success" : form.defaultSuccessSaveMessage, "organizationMenuTooltip" : 1, "currentUserOrganization" => $currentUserOrganization, "userOrganizations" : [{id: $organizationId0, name:$organizationName0}, {id: $organizationId1, name:$organizationName1}], organizationMenuLang: Lang::get('top-bar.userOrganizations')}
  */
 public function saveJournalVoucher(array $input)
 {
     //$input = eloquent_array_filter_for_insert($input);
     //var_dump($input);die();
     unset($input['_token'], $input['voucher_type'], $input['status_label'], $input['period_label'], $input['document_type_label'], $input['supplier_label'], $input['client_label'], $input['employee_label']);
     $input = eloquent_array_filter_for_insert($input);
     $input = array_add($input, 'created_by', $this->AuthenticationManager->getLoggedUserId());
     $input = array_add($input, 'organization_id', $this->AuthenticationManager->getCurrentUserOrganizationId());
     $input['date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['date'])->format('Y-m-d');
     if (isset($input['document_date']) && !empty($input['document_date'])) {
         $input['document_date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['document_date'])->format('Y-m-d');
     }
     if (!isset($input['is_editable'])) {
         $input = array_add($input, 'is_editable', true);
     }
     $Period = $this->Period->byId($input['period_id']);
     if ($Period->is_closed) {
         return json_encode(array('success' => false, 'fieldValidationMessages' => array('date' => $this->Lang->get('decima-accounting::journal-management.closedPeriodValidationMessage', array('period' => $this->Lang->get('decima-accounting::period-management.' . $Period->month))))));
     }
     $this->DB->transaction(function () use(&$input, &$JournalVoucher) {
         $currentSettingConfiguration = $this->SettingManager->getCurrentSettingConfiguration();
         if ($currentSettingConfiguration['voucher_numeration_type'] == 'P') {
             $input = array_add($input, 'number', $this->JournalVoucher->getMaxJournalVoucherNumberByPeriod($input['organization_id'], $input['period_id']) + 1);
         } else {
             $input = array_add($input, 'number', $this->JournalVoucher->getMaxJournalVoucherNumberByPeriodAndAccountType($input['organization_id'], $input['period_id'], $input['voucher_type_id']) + 1);
         }
         $input = array_add($input, 'number', $this->JournalVoucher->getMaxJournalVoucherNumber($input['organization_id']) + 1);
         $JournalVoucher = $this->JournalVoucher->create($input);
         $input['id'] = $JournalVoucher->id;
         $Journal = $this->Journal->create(array('journalized_id' => $JournalVoucher->id, 'journalized_type' => $this->JournalVoucher->getTable(), 'user_id' => $JournalVoucher->created_by, 'organization_id' => $JournalVoucher->organization_id));
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::journal-management.journalVoucherAddedJournal', array('number' => $JournalVoucher->number)), $Journal));
     });
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessSaveMessage'), 'id' => $input['id'], 'number' => $input['number'], 'numberLabel' => $this->Lang->get('decima-accounting::journal-management.voucherNumber', array('number' => $input['number']))));
 }
 public function getIndex()
 {
     return $this->View->make('decima-accounting::initial-accounting-setup')->with('years', $this->SettingManagerService->getYears())->with('journals', $this->SettingManagerService->getSettingJournals())->with('accountsChartsTypes', $this->SettingManagerService->getCountryAccountsChartsTypes())->with('currentSettingConfiguration', $this->SettingManagerService->getCurrentSettingConfiguration());
 }