/**
  * Get last period of fiscal year and first period of the next fiscal year
  *
  * @param array $input
  * 	An array as follows: array(id => $id);
  *
  * @return array
  *  An array as follows: array( 'id' = $id, 'month' => $month, 'end_date' => $endDate)
  */
 public function getBalanceAccountsClosingPeriods(array $input)
 {
     $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id');
     $fiscalYearId = $this->FiscalYear->lastFiscalYearByOrganization($organizationId);
     if ($fiscalYearId == $input['id']) {
         return json_encode(array('info' => $this->Lang->get('decima-accounting::period-management.invalidFiscalYear')));
     }
     $period = json_decode($this->getLastPeriodOfFiscalYear($input), true);
     $FiscalYear = $this->FiscalYear->byId($input['id']);
     $FiscalYear = $this->FiscalYear->byYearAndByOrganization($FiscalYear->year + 1, $organizationId);
     $period2 = $this->Period->firstPeriodbyOrganizationAndByFiscalYear($this->AuthenticationManager->getCurrentUserOrganizationId(), $FiscalYear->id)->toArray();
     $period2['endDate'] = $this->Carbon->createFromFormat('Y-m-d', $period2['end_date'])->format($this->Lang->get('form.phpShortDateFormat'));
     unset($period2['end_date']);
     $period2['month'] = $period2['month'] . ' - ' . $this->Lang->get('decima-accounting::period-management.' . $period2['month']);
     return json_encode(array('id0' => $period['id'], 'month0' => $period['month'], 'endDate0' => $period['endDate'], 'id1' => $period2['id'], 'month1' => $period2['month'], 'endDate1' => $period2['endDate'], 'fiscalYearId' => $FiscalYear->id));
 }
 public function getUpdatedAtAttribute($date)
 {
     return Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $date)->format('Y-m-d');
 }
 /**
  * Update an existing journal voucher
  *
  * @param array $input
  * 	An array as follows: array( 'id' => $id, '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.defaultSuccessUpdateMessage}
  */
 public function updateJournalVoucher(array $input)
 {
     $fieldLabels['voucher_type'] = $input['voucher_type'];
     $fieldLabels['document_type_label'] = $input['document_type_label'];
     $fieldLabels['supplier_label'] = $input['supplier_label'];
     $fieldLabels['client_label'] = $input['client_label'];
     $fieldLabels['employee_label'] = $input['employee_label'];
     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_update($input);
     $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');
     }
     $periodIsChanged = $periodIsClosed = $newPeriodIsClosed = false;
     $this->DB->transaction(function () use(&$input, &$Period, &$periodIsChanged, &$periodIsClosed, &$newPeriodIsClosed, $fieldLabels) {
         $JournalVoucher = $this->JournalVoucher->byId($input['id']);
         $unchangedJournalVoucherValues = $JournalVoucher->toArray();
         $Period = $this->Period->byId($JournalVoucher->period_id);
         if ($Period->is_closed) {
             $periodIsClosed = true;
             return;
         }
         if ($JournalVoucher->period_id != $input['period_id']) {
             $periodIsChanged = true;
             return;
         }
         $Period = $this->Period->byId($input['period_id']);
         if ($Period->is_closed) {
             $newPeriodIsClosed = true;
             return;
         }
         $this->JournalVoucher->update($input, $JournalVoucher);
         $diff = 0;
         foreach ($input as $key => $value) {
             if ($unchangedJournalVoucherValues[$key] != $value) {
                 $diff++;
                 if ($diff == 1) {
                     $Journal = $this->Journal->create(array('journalized_id' => $JournalVoucher->id, 'journalized_type' => $this->JournalVoucher->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId()));
                 }
                 if ($key == 'voucher_type_id') {
                     $VoucherType = $this->VoucherType->byId($value);
                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.voucherType'), 'field_lang_key' => 'decima-accounting::journal-management.voucherType', 'old_value' => $VoucherType->name, 'new_value' => $fieldLabels['voucher_type']), $Journal);
                 } else {
                     if ($key == 'document_type_id') {
                         if (!empty($unchangedJournalVoucherValues[$key])) {
                             $DocumentType = $this->DocumentType->byId($unchangedJournalVoucherValues[$key]);
                             $documentType = $DocumentType->name;
                         } else {
                             $documentType = '';
                         }
                         $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $documentType, 'new_value' => $fieldLabels['document_type_label']), $Journal);
                     } else {
                         if ($key == 'supplier_id') {
                             if (!empty($unchangedJournalVoucherValues[$key])) {
                                 $Supplier = $this->Supplier->byId($unchangedJournalVoucherValues[$key]);
                                 $supplier = $Supplier->name;
                             } else {
                                 $supplier = '';
                             }
                             $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $supplier, 'new_value' => $fieldLabels['supplier_label']), $Journal);
                         } else {
                             if ($key == 'client_id') {
                                 if (!empty($unchangedJournalVoucherValues[$key])) {
                                     $Client = $this->Client->byId($unchangedJournalVoucherValues[$key]);
                                     $client = $Client->name;
                                 } else {
                                     $client = '';
                                 }
                                 $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $client, 'new_value' => $fieldLabels['client_label']), $Journal);
                             } else {
                                 if ($key == 'employee_id') {
                                     if (!empty($unchangedJournalVoucherValues[$key])) {
                                         $Employee = $this->Employee->byId($unchangedJournalVoucherValues[$key]);
                                         $employee = $Employee->names . ' ' . $Employee->surnames;
                                     } else {
                                         $employee = '';
                                     }
                                     $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $employee, 'new_value' => $fieldLabels['employee_label']), $Journal);
                                 } else {
                                     if ($key == 'status') {
                                         $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $this->Lang->get('decima-accounting::journal-management.' . $unchangedJournalVoucherValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::journal-management.' . $value)), $Journal);
                                     } else {
                                         if ($key == 'date' || $key == 'document_date') {
                                             $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $this->Carbon->createFromFormat('Y-m-d', $unchangedJournalVoucherValues[$key], 'UTC')->format($this->Lang->get('form.phpShortDateFormat')), 'new_value' => $this->Carbon->createFromFormat('Y-m-d', $value, 'UTC')->format($this->Lang->get('form.phpShortDateFormat'))), $Journal);
                                         } else {
                                             $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::journal-management.' . camel_case($key), 'old_value' => $unchangedJournalVoucherValues[$key], 'new_value' => $value), $Journal);
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
         }
     });
     if ($periodIsClosed) {
         return json_encode(array('success' => false, 'info' => $this->Lang->get('decima-accounting::journal-management.closedPeriodValidationMessage3', array('period' => $this->Lang->get('decima-accounting::period-management.' . $Period->month)))));
     }
     if ($periodIsChanged) {
         return json_encode(array('success' => false, 'info' => $this->Lang->get('decima-accounting::journal-management.changedPeriodValidationMessage', array('period' => $this->Lang->get('decima-accounting::period-management.' . $Period->month)))));
     }
     if ($newPeriodIsClosed) {
         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))))));
     }
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage')));
 }