Пример #1
0
 /**
  * Close an existing period
  *
  * @param array $input
  * 	An array as follows: array(id => $id);
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessOperationMessage}
  */
 public function closePeriod(array $input)
 {
     $canBeClosed = true;
     $this->DB->transaction(function () use($input, &$canBeClosed) {
         $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
         $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id');
         if ($this->JournalVoucher->getByOrganizationByPeriodAndByStatus($organizationId, array($input['id']), 'A')->count() > 0) {
             $canBeClosed = false;
             return;
         }
         $Period = $this->Period->byId($input['id']);
         $Journal = $this->Journal->create(array('journalized_id' => $input['id'], 'journalized_type' => $this->Period->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
         // $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::journal-management.status'), 'field_lang_key' => 'decima-accounting::journal-management.status', 'old_value' => $this->Lang->get('decima-accounting::period-management.opened'), 'new_value' => $this->Lang->get('decima-accounting::period-management.closed')), $Journal);
         $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::period-management.periodClosedJournal', array('period' => $this->Lang->get('decima-accounting::period-management.' . $Period->month))), $Journal));
         $this->Period->update(array('is_closed' => true), $Period);
     });
     if (!$canBeClosed) {
         return json_encode(array('success' => false, 'info' => $this->Lang->get('decima-accounting::period-management.voucherException')));
     }
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessOperationMessage')));
 }
 /**
  * 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')));
 }
 /**
  * Delete an existing journal entry (soft delete)
  *
  * @param array $input
  * 	An array as follows: array($id0, $id1,…);
  *
  * @return JSON encoded string
  *  A string as follows:
  *	In case of success: {"success" : form.defaultSuccessDeleteMessage}
  */
 public function deleteJournalEntry(array $input)
 {
     $count = 0;
     $periodIsClosed = false;
     $this->DB->transaction(function () use($input, &$count, &$status, &$periodIsClosed, &$Period) {
         $loggedUserId = $this->AuthenticationManager->getLoggedUserId();
         $organizationId = $this->AuthenticationManager->getCurrentUserOrganization('id');
         foreach ($input['id'] as $key => $id) {
             $count++;
             $JournalEntry = $this->JournalEntry->byId($id);
             if ($count == 1) {
                 $JournalVoucher = $this->JournalVoucher->byId($JournalEntry->journal_voucher_id);
                 $Period = $this->Period->byId($JournalVoucher->period_id);
                 if ($Period->is_closed) {
                     $periodIsClosed = true;
                     return;
                 }
                 $JournalVoucher = $this->JournalVoucher->byId($JournalEntry->journal_voucher_id);
                 $Journal = $this->Journal->create(array('journalized_id' => $JournalEntry->journal_voucher_id, 'journalized_type' => $this->JournalVoucher->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId));
             }
             $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::journal-management.journalEntryDeletedJournal', array('number' => $JournalVoucher->number)), $Journal));
             $this->JournalEntry->delete($input['id']);
         }
         $status = $this->updateJournalVoucherStatus($JournalVoucher->id, $Journal, $JournalVoucher);
     });
     if ($periodIsClosed) {
         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)))));
     }
     return json_encode(array('success' => $this->Lang->get('form.defaultSuccessDeleteMessage'), 'status' => $status, 'statusLabel' => $this->Lang->get('decima-accounting::journal-management.' . $status)));
 }