/** * Update an existing ... * * @param array $input * An array as follows: array('id' => $id, 'field0'=>$field0, 'field1'=>$field1 * * @return JSON encoded string * A string as follows: * In case of success: {"success" : form.defaultSuccessUpdateMessage} */ public function update(array $input) { unset($input['_token']); $input = eloquent_array_filter_for_update($input); // $input['date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['date'])->format('Y-m-d'); $this->DB->transaction(function () use(&$input) { $VoucherType = $this->VoucherType->byId($input['id']); $unchangedVoucherTypeValues = $VoucherType->toArray(); $this->VoucherType->update($input, $VoucherType); $diff = 0; foreach ($input as $key => $value) { if ($unchangedVoucherTypeValues[$key] != $value) { $diff++; if ($diff == 1) { $Journal = $this->Journal->create(array('journalized_id' => $VoucherType->id, 'journalized_type' => $this->VoucherType->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId())); } if ($key == 'name') { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::voucher-type-management.name'), 'field_lang_key' => 'decima-accounting::voucher-type-management.name', 'old_value' => $this->Lang->get($unchangedVoucherTypeValues[$key]), 'new_value' => $this->Lang->get($value)), $Journal); } else { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::voucher-type-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::voucher-type-management.' . camel_case($key), 'old_value' => $unchangedVoucherTypeValues[$key], 'new_value' => $value), $Journal); } } } }); return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'))); }
/** * Update an existing cost center * * @param array $input * An array as follows: array('id' => $id, 'key'=>$key, 'name'=>$name, 'balance_type'=>$balanceType, 'is_group'=>$isGroup, * 'parent_cc_id'=>$parentCostCenterId, 'parent_cc' => $parentCostCenter * ); * * @return JSON encoded string * A string as follows: * In case of success: {"success" : form.defaultSuccessUpdateMessage} */ public function update(array $input) { $newParentCostCenter = $input['parent_cc']; $groupsCostCenters = $info = false; unset($input['_token'], $input['parent_cc']); $input = eloquent_array_filter_for_update($input); if ($this->getCostCenterChildrenCount($input['id']) > 0 && $input['is_group'] == 0) { return json_encode(array('info' => $this->Lang->get('decima-accounting::cost-center-management.isGroupException'))); // return json_encode(array('validationFailed' => true , 'fieldValidationMessages' => array('is-group' => $this->Lang->get('decima-accounting::cost-center-management.isGroupException')))); } $this->DB->transaction(function () use(&$input, $newParentCostCenter, &$groupsCostCenters, &$info) { $CostCenter = $this->CostCenter->byId($input['id']); $unchangedCostCenterValues = $CostCenter->toArray(); if (rtrim($input['key']) != rtrim($CostCenter->key) && $this->CostCenter->byOrganizationAndByKey($this->AuthenticationManager->getCurrentUserOrganizationId(), $input['key'])->count() > 0) { $info = $this->Lang->get('decima-accounting::cost-center-management.keyValidationMessage', array('key' => $input['key'])); return; } $this->CostCenter->update($input, $CostCenter); if (!empty($CostCenter->is_group)) { $groupsCostCenters = $this->getGroupsCostCenters(); } $diff = 0; foreach ($input as $key => $value) { // var_dump($unchangedCostCenterValues);die(); if ($unchangedCostCenterValues[$key] != $value) { $diff++; if ($diff == 1) { $Journal = $this->Journal->create(array('journalized_id' => $CostCenter->id, 'journalized_type' => $this->CostCenter->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId())); } if ($key == 'parent_cc_id') { if (!empty($unchangedCostCenterValues[$key])) { $OldParentcostCenter = $this->CostCenter->byId($unchangedCostCenterValues[$key]); $oldParentcostCenter = $OldParentcostCenter->key . ' ' . $OldParentcostCenter->name; } else { $oldParentcostCenter = ''; } $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::cost-center-management.parentCc'), 'field_lang_key' => 'decima-accounting::cost-center-management.parentCc', 'old_value' => $oldParentcostCenter, 'new_value' => $newParentCostCenter), $Journal); } else { if ($key == 'is_group') { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::cost-center-management.isGroupLong'), 'field_lang_key' => 'decima-accounting::cost-center-management.isGroupLong', 'old_value' => $this->Lang->get('decima-accounting::account-management.' . $unchangedCostCenterValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::account-management.' . $value)), $Journal); } else { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::account-management.' . camel_case($key), 'old_value' => $unchangedCostCenterValues[$key], 'new_value' => $value), $Journal); } } } } }); if (!$info) { return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'), 'groupsCostCenters' => $groupsCostCenters)); } else { return json_encode(array('info' => $info)); } }
/** * 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))); }
/** * 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)); }
/** * Create closing balance voucher * * @param array $input * An array as follows: array('fiscal_year_id'=>$id, 'date'=> $date, 'period_id' => periodId, 'voucher_type_id' => $voucherTypeId, 'remark' => $remark, 'cost_center_id' => $costCenterId, 'account_id' => $accountId ); * * @return JSON encoded string * A string as follows: * In case of success: {"success" : form.defaultSuccessSaveMessage} */ public function createClosingBalanceVoucher(array $input) { $loggedUserId = $this->AuthenticationManager->getLoggedUserId(); $organizationId = $this->AuthenticationManager->getCurrentUserOrganizationId(); $input = eloquent_array_filter_for_insert($input); $input = array_add($input, 'organization_id', $organizationId); $this->DB->transaction(function () use($input, $loggedUserId, $organizationId, &$resultClosing, &$resultOpening) { $resultClosing = json_decode($this->JournalManagerService->saveJournalVoucher(array('notransaction' => '', 'status' => 'B', 'date' => $input['date_closing'], 'remark' => $input['remark_closing'], 'period_id' => $input['period_id_closing'], 'voucher_type_id' => $input['voucher_type_id_closing'])), true); $resultOpening = json_decode($this->JournalManagerService->saveJournalVoucher(array('notransaction' => '', 'status' => 'B', 'date' => $input['date_opening'], 'remark' => $input['remark_opening'], 'period_id' => $input['period_id_opening'], 'voucher_type_id' => $input['voucher_type_id_opening'])), true); $entries = $this->JournalEntry->getJournalEntriesGroupedByPlBsCategoryByOrganizationAndByFiscalYear(array('D', 'E'), $organizationId, $input['fiscal_year_id']); $voucherClosingEntries = array(); $voucherOpeningEntries = array(); foreach ($entries as $Entry) { //$Entry->balance_type //Deudor,Receivable:D and Acreedor,Payable: A if ($Entry->balance_type == 'D') { $credit = round($Entry->debit - $Entry->credit, 2); $debit = 0; } else { $debit = round($Entry->credit - $Entry->debit, 2); $credit = 0; } if ($debit != 0 || $credit != 0) { array_push($voucherClosingEntries, array('debit' => $debit, 'credit' => $credit, 'cost_center_id' => $Entry->cost_center_id, 'account_id' => $Entry->account_id, 'journal_voucher_id' => $resultClosing['id'])); array_push($voucherOpeningEntries, array('debit' => $credit, 'credit' => $debit, 'cost_center_id' => $Entry->cost_center_id, 'account_id' => $Entry->account_id, 'journal_voucher_id' => $resultOpening['id'])); } } $this->JournalEntry->massCreate($voucherClosingEntries); $this->JournalEntry->massCreate($voucherOpeningEntries); $Journal = $this->Journal->create(array('journalized_id' => $input['fiscal_year_id'], 'journalized_type' => $this->FiscalYear->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::close-fiscal-year.closingBalanceAddedJournal', array('number' => $resultClosing['number'], 'period' => $input['period_label_closing'])), $Journal)); $this->JournalManagerService->updateJournalVoucherStatus($resultClosing['id']); $Journal = $this->Journal->create(array('journalized_id' => $input['fiscal_year_id_opening'], 'journalized_type' => $this->FiscalYear->getTable(), 'user_id' => $loggedUserId, 'organization_id' => $organizationId)); $this->Journal->attachDetail($Journal->id, array('note' => $this->Lang->get('decima-accounting::close-fiscal-year.openingBalanceAddedJournal', array('number' => $resultOpening['number'], 'period' => $input['period_label_opening'])), $Journal)); $this->JournalManagerService->updateJournalVoucherStatus($resultOpening['id']); }); return json_encode(array('success' => $this->Lang->get('decima-accounting::close-fiscal-year.closingBalanceAddedJournal', array('number' => $resultClosing['number'], 'period' => $input['period_label_closing'])) . '<br>' . $this->Lang->get('decima-accounting::close-fiscal-year.closingBalanceAddedJournal', array('number' => $resultOpening['number'], 'period' => $input['period_label_opening'])))); }
/** * Update an existing account * * @param array $input * An array as follows: array('id' => $id, 'key'=>$key, 'name'=>$name, 'balance_type'=>$balanceType, 'is_group'=>$isGroup, * 'account_type_id'=>$accountTypeId, 'parent_account_id'=>$parentAccountId, 'parent_account' => $parentAccount * 'account_type' => $accountType, 'parent_account' => $parentAccount * ); * * @return JSON encoded string * A string as follows: * In case of success: {"success" : form.defaultSuccessUpdateMessage} */ public function update(array $input) { $newParentAccount = $input['parent_account']; $newAccountType = $input['account_type']; $groupsAccounts = $info = false; unset($input['_token'], $input['parent_account'], $input['balance_type_name'], $input['account_type']); $input = eloquent_array_filter_for_update($input); // $input['date'] = $this->Carbon->createFromFormat($this->Lang->get('form.phpShortDateFormat'), $input['date'])->format('Y-m-d'); if ($this->getAccountChildrenCount($input['id']) > 0 && $input['is_group'] == 0) { return json_encode(array('info' => $this->Lang->get('decima-accounting::account-management.isGroupException'))); } $this->DB->transaction(function () use(&$input, $newParentAccount, $newAccountType, &$groupsAccounts, &$info) { $Account = $this->Account->byId($input['id']); $unchangedAccountValues = $Account->toArray(); if (rtrim($input['key']) != rtrim($Account->key) && $this->Account->byOrganizationAndByKey($this->AuthenticationManager->getCurrentUserOrganizationId(), $input['key'])->count() > 0) { $info = $this->Lang->get('decima-accounting::account-management.keyValidationMessage', array('key' => $input['key'])); return; } $this->Account->update($input, $Account); if (!empty($input['is_group'])) { $groupsAccounts = $this->getGroupsAccounts(); } $diff = 0; foreach ($input as $key => $value) { if ($unchangedAccountValues[$key] != $value) { $diff++; if ($diff == 1) { $Journal = $this->Journal->create(array('journalized_id' => $Account->id, 'journalized_type' => $this->Account->getTable(), 'user_id' => $this->AuthenticationManager->getLoggedUserId(), 'organization_id' => $this->AuthenticationManager->getCurrentUserOrganizationId())); } if ($key == 'balance_type') { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.balanceType'), 'field_lang_key' => 'decima-accounting::account-management.balanceType', 'old_value' => $this->Lang->get('decima-accounting::account-management.' . $unchangedAccountValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::account-management.' . $value)), $Journal); } else { if ($key == 'account_type_id') { $oldAccountType = $this->AccountType->byId($unchangedAccountValues[$key]); $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.accountType'), 'field_lang_key' => 'decima-accounting::account-management.accountType', 'old_value' => $oldAccountType->name, 'new_value' => $newAccountType), $Journal); } else { if ($key == 'parent_account_id') { if (!empty($unchangedAccountValues[$key])) { $OldParentAccount = $this->Account->byId($unchangedAccountValues[$key]); $oldParentAccount = $OldParentAccount->key . ' ' . $OldParentAccount->name; } else { $oldParentAccount = ''; } $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.parentAccount'), 'field_lang_key' => 'decima-accounting::account-management.parentAccount', 'old_value' => $oldParentAccount, 'new_value' => $newParentAccount), $Journal); } else { if ($key == 'is_group') { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.isGroupLong'), 'field_lang_key' => 'decima-accounting::account-management.isGroupLong', 'old_value' => $this->Lang->get('decima-accounting::account-management.' . $unchangedAccountValues[$key]), 'new_value' => $this->Lang->get('decima-accounting::account-management.' . $value)), $Journal); } else { $this->Journal->attachDetail($Journal->id, array('field' => $this->Lang->get('decima-accounting::account-management.' . camel_case($key)), 'field_lang_key' => 'decima-accounting::account-management.' . camel_case($key), 'old_value' => $unchangedAccountValues[$key], 'new_value' => $value), $Journal); } } } } } } }); if (!$info) { return json_encode(array('success' => $this->Lang->get('form.defaultSuccessUpdateMessage'), 'groupsAccounts' => $groupsAccounts)); } else { return json_encode(array('info' => $info)); } }