コード例 #1
0
 /**
  * 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')));
 }