/** * Update accounting setting * * @param array $input * An array as follows: array('courses'=> array($id0, $id1,…), 'costCenters'=> array($key0, $key1,…)); * * @return JSON encoded string * A string as follows: * { * totalEmployees: $totalEmployees * payrollEmployees: $payrollEmployees * serviceEmployees: $serviceEmployees * } */ public function updateAccountingSettings(array $input = array()) { $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id'); $loggedUserId = $this->AuthenticationManager->getLoggedUserId(); $Setting = $this->Setting->byOrganization($organizationId)->first(); $this->DB->transaction(function () use($Setting, $input, $organizationId, $loggedUserId) { $SettingJournal = $this->Journal->create(array('journalized_id' => $Setting->id, 'journalized_type' => $this->Setting->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); if (empty($Setting->is_configured)) { $organizationAccountsType = array(); $this->SystemAccountType->all()->each(function ($AccountType) use($organizationId, &$organizationAccountsType, $loggedUserId) { $accountType = $AccountType->toArray(); $accountType = array_add($accountType, 'organization_id', $organizationId); $accountType['name'] = $this->Lang->has($accountType['lang_key']) ? $this->Lang->get($accountType['lang_key']) : $accountType['name']; unset($accountType['id'], $accountType['created_at'], $accountType['updated_at'], $accountType['deleted_at']); $AccountType = $this->AccountType->create($accountType); $organizationAccountsType = array_add($organizationAccountsType, $AccountType->key, $AccountType->id); $Journal = $this->Journal->create(array('journalized_id' => $AccountType->id, 'journalized_type' => $this->AccountType->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.accountTypeAddedJournal', array('type' => $this->Lang->has($AccountType->lang_key) ? $this->Lang->get($AccountType->lang_key) : $AccountType->name))), $Journal); }); $this->Journal->attachDetail($SettingJournal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.accountTypeSettingAddedJournal')), $SettingJournal); $this->SystemVoucherType->all()->each(function ($VoucherType) use($organizationId, $loggedUserId) { $voucherType = $VoucherType->toArray(); $voucherType = array_add($voucherType, 'organization_id', $organizationId); $voucherType['name'] = $this->Lang->has($voucherType['lang_key']) ? $this->Lang->get($voucherType['lang_key']) : $voucherType['name']; unset($voucherType['id'], $voucherType['created_at'], $voucherType['updated_at'], $voucherType['deleted_at']); $VoucherType = $this->VoucherType->create($voucherType); $Journal = $this->Journal->create(array('journalized_id' => $VoucherType->id, 'journalized_type' => $this->VoucherType->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.voucherTypeAddedJournal', array('type' => $this->Lang->has($VoucherType->lang_key) ? $this->Lang->get($VoucherType->lang_key) : $VoucherType->name))), $Journal); }); $this->Journal->attachDetail($SettingJournal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.voucherTypeSettingAddedJournal')), $SettingJournal); $firstDayYear = date($input['year'] . '-01-01'); $lastDayYear = date($input['year'] . '-12-31'); $FiscalYear = $this->FiscalYear->create(array('year' => $input['year'], 'start_date' => $firstDayYear, 'end_date' => $lastDayYear, 'organization_id' => $organizationId)); $Journal = $this->Journal->create(array('journalized_id' => $FiscalYear->id, 'journalized_type' => $this->FiscalYear->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.fiscalYearAddedJournal', array('year' => $input['year']))), $Journal); $this->Journal->attachDetail($SettingJournal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.fiscalYearAddedJournal', array('year' => $input['year']))), $SettingJournal); if ($input['create_opening_period'] == '1') { $Period = $this->Period->create(array('month' => 0, 'start_date' => $firstDayYear, 'end_date' => $firstDayYear, 'fiscal_year_id' => $FiscalYear->id, 'organization_id' => $organizationId)); $Journal = $this->Journal->create(array('journalized_id' => $Period->id, 'journalized_type' => $this->Period->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.periodAddedJournal', array('period' => 0))), $Journal); } for ($i = 1; $i <= 12; $i++) { $Date = new \DateTime($input['year'] . '-' . $i . '-01'); $firstDay = $Date->format('Y-m-d'); $Date->modify('last day of this month'); $lastDay = $Date->format('Y-m-d'); $Period = $this->Period->create(array('month' => $i, 'start_date' => $firstDay, 'end_date' => $lastDay, 'fiscal_year_id' => $FiscalYear->id, 'organization_id' => $organizationId)); $Journal = $this->Journal->create(array('journalized_id' => $Period->id, 'journalized_type' => $this->Period->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.periodAddedJournal', array('period' => $i))), $Journal); } if ($input['create_closing_period'] == '1') { $Period = $this->Period->create(array('month' => 13, 'start_date' => $lastDayYear, 'end_date' => $lastDayYear, 'fiscal_year_id' => $FiscalYear->id, 'organization_id' => $organizationId)); $Journal = $this->Journal->create(array('journalized_id' => $Period->id, 'journalized_type' => $this->Period->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.periodAddedJournal', array('period' => 13))), $Journal); } $this->Journal->attachDetail($SettingJournal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.periodAddedSettingJournal')), $SettingJournal); $CostCenter = $this->CostCenter->create(array('name' => $this->AuthenticationManager->getCurrentUserOrganizationName(), 'key' => $this->Config->get('accounting-general.default_organization_cc_key'), 'is_group' => true, 'organization_id' => $organizationId)); $Journal = $this->Journal->create(array('journalized_id' => $CostCenter->id, 'journalized_type' => $this->CostCenter->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.costCenterAddedJournal', array('costCenter' => $CostCenter->key . ' ' . $CostCenter->name))), $Journal); $CostCenter = $this->CostCenter->create(array('name' => $this->Lang->get('decima-accounting::cost-center.main'), 'key' => $this->Config->get('accounting-general.default_main_cc_key'), 'is_group' => false, 'parent_cc_id' => $CostCenter->id, 'organization_id' => $organizationId)); $Journal = $this->Journal->create(array('journalized_id' => $CostCenter->id, 'journalized_type' => $this->CostCenter->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.costCenterAddedJournal', array('costCenter' => $CostCenter->key . ' ' . $CostCenter->name))), $Journal); $this->Journal->attachDetail($SettingJournal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.costCenterAddedSettingJournal')), $SettingJournal); $organizationAccounts = array(); $this->SystemAccount->accountsByAccountChartsTypes($input['account_chart_type_id'])->each(function ($SystemAccount) use($organizationId, $organizationAccountsType, &$organizationAccounts, &$systemAccountsCounter, $loggedUserId) { $account = $SystemAccount->toArray(); $account = array_add($account, 'organization_id', $organizationId); $account = array_add($account, 'account_type_id', $organizationAccountsType[$account['account_type_key']]); if (!empty($account['parent_key'])) { $account = array_add($account, 'parent_account_id', $organizationAccounts[$account['parent_key']]); } unset($account['id'], $account['parent_key'], $account['account_type_key'], $account['account_chart_type_id'], $account['created_at'], $account['updated_at'], $account['deleted_at']); $OrganizationAccount = $this->Account->create($account); $Journal = $this->Journal->create(array('journalized_id' => $OrganizationAccount->id, 'journalized_type' => $this->Account->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.accountAddedJournal', array('account' => $OrganizationAccount->key . ' ' . $OrganizationAccount->name))), $Journal); $organizationAccounts[$OrganizationAccount->key] = $OrganizationAccount->id; }); if (!empty($organizationAccounts)) { $AccountChartType = $this->AccountChartType->byId($input['account_chart_type_id']); $this->Journal->attachDetail($SettingJournal->id, array('note' => $this->Lang->get('decima-accounting::initial-accounting-setup.accountAddedSettingJournal', array('catalog' => $AccountChartType->name))), $SettingJournal); } $input['id'] = $Setting->id; $input['initial_year'] = $input['year']; $input['is_configured'] = true; unset($input['_token'], $input['year']); $this->Setting->update($input); } else { $input['id'] = $Setting->id; $unchangedSettingValues = $Setting->toArray(); unset($input['_token'], $input['year'], $input['account_chart_type_id']); $this->Setting->update($input); $diff = 0; foreach ($input as $key => $value) { if ($unchangedSettingValues[$key] != $value) { $diff++; if ($diff == 1) { $Journal = $this->Journal->create(array('journalized_id' => $Setting->id, 'journalized_type' => $this->Setting->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); } $fieldLangKey = 'decima-accounting::initial-accounting-setup.' . camel_case($key); if ($key == 'voucher_numeration_type') { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get($fieldLangKey), 'field_lang_key' => $fieldLangKey, 'old_value' => $this->Lang->get('decima-accounting::initial-accounting-setup.' . $unchangedSettingValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::initial-accounting-setup.' . $value)), $Journal); } else { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get($fieldLangKey), 'field_lang_key' => $fieldLangKey, 'old_value' => $this->Lang->get('journal.' . $unchangedSettingValues[$key]), 'new_value' => $this->Lang->get('journal.' . $value)), $Journal); } } } } }); return json_encode(array('success' => $this->Lang->get('decima-accounting::initial-accounting-setup.successUpdateMessage'))); }
/** * 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'))); }