/** * Get cost centers children * * @param array $input * An array as follows: array(id => $id); * * @return JSON encoded string * A string as follows: [{"text" : $costCenterKey . " " . $costCenterName, "state" : {"opened" : true }, "icon" : $icon, "children" : [{"text" : $childCostCenterKey0 . " " . $childCostCenterName0, "icon" : $childIcon0}, …]}] */ public function getCostCenterChildren($input) { $CostCenter = $this->CostCenter->byId($input['id']); $costCenterTree = array('text' => $CostCenter->key . ' ' . $CostCenter->name, 'state' => array('opened' => true), 'icon' => 'fa fa-sitemap', 'children' => array()); $this->CostCenter->byParent($input['id'])->each(function ($CostCenter) use(&$costCenterTree) { if ($CostCenter->is_group) { array_push($costCenterTree['children'], array('text' => $CostCenter->key . ' ' . $CostCenter->name, 'icon' => 'fa fa-sitemap')); } else { array_push($costCenterTree['children'], array('text' => $CostCenter->key . ' ' . $CostCenter->name, 'icon' => 'fa fa-leaf')); } }); return json_encode(array($costCenterTree)); }
/** * 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 entry * * @param array $input * An array as follows: array('journal_entry_id' => $journalEntryId, 'debit'=>$debit, 'credit'=>$credit, 'system_reference_type'=>$systemReferenceType, 'system_reference_field'=>$systemReferenceField, * 'journal_voucher_id'=> journalVoucherId, 'cost_center_id'=>$costCenterId, 'account_id' => $accountId * ); * * @return JSON encoded string * A string as follows: * In case of success: {"success" : form.defaultSuccessUpdateMessage, "statusLabel": $statusLabel, "status": $status} */ public function updateJournalEntry(array $input, $Journal = null) { $journalNumber = $input['number']; $unchangedInputValues = $input; $input['id'] = $input['journal_entry_id']; unset($input['_token'], $input['cost_center'], $input['account'], $input['number'], $input['journal_entry_id']); $input = eloquent_array_filter_for_update($input); $input['debit'] = remove_thousands_separator($input['debit']); $input['credit'] = remove_thousands_separator($input['credit']); $JournalVoucher = $this->JournalVoucher->byId($input['journal_voucher_id']); $Period = $this->Period->byId($JournalVoucher->period_id); if ($Period->is_closed) { 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))))); } $this->DB->transaction(function () use(&$input, $journalNumber, $unchangedInputValues, &$status, $Journal) { $JournalEntry = $this->JournalEntry->byId($input['id']); $unchangedJournalEntryValues = $JournalEntry->toArray(); $this->JournalEntry->update($input, $JournalEntry); $diff = 0; foreach ($input as $key => $value) { if ($unchangedJournalEntryValues[$key] != $value) { $diff++; if ($diff == 1) { if (empty($Journal)) { $Journal = $this->Journal->create(array('journalized_id' => $JournalEntry->journal_voucher_id, 'journalized_type' => $this->JournalVoucher->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId())); } } if ($key == 'cost_center_id') { $CostCenter = $this->CostCenter->byId($value); $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.costCenter'), 'field_lang_key' => 'decima-accounting::journal-management.costCenter', 'old_value' => $unchangedInputValues['cost_center'], 'new_value' => $CostCenter->key . ' ' . $CostCenter->name), $Journal); } else { if ($key == 'account_id') { $Account = $this->Account->byId($value); $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.account'), 'field_lang_key' => 'decima-accounting::journal-management.account', 'old_value' => $unchangedInputValues['account'], 'new_value' => $Account->key . ' ' . $Account->name), $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' => $unchangedJournalEntryValues[$key], 'new_value' => $value), $Journal); } } } } if (empty($Journal)) { $status = $this->updateJournalVoucherStatus($JournalEntry->journal_voucher_id); } else { $status = $this->updateJournalVoucherStatus($JournalEntry->journal_voucher_id, $Journal); } }); return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'), 'status' => $status, 'statusLabel' => $this->Lang->get('decima-accounting::journal-management.' . $status))); }