/** * Copyright since 2025 Mifos Initiative * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ /** TODO: Separate services for feature modules for cleaner accounting service. */ /** Angular Imports */ import { Injectable, inject } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; /** rxjs Imports */ import { Observable } from 'rxjs'; /** * Accounting service. */ @Injectable({ providedIn: 'root' }) export class AccountingService { private http = inject(HttpClient); /** * @returns {Observable} Offices data ordered by id. */ getOffices(): Observable { const httpParams = new HttpParams().set('orderBy', 'id'); return this.http.get('/offices', { params: httpParams }); } /** * @param {boolean} useHttpParams True if http params should be used. * @returns {Observable} Accounting rules data. */ getAccountingRules(useHttpParams: boolean = false): Observable { let httpParams = new HttpParams(); if (useHttpParams) { httpParams = httpParams.set('associations', 'all'); } return this.http.get('/accountingrules', { params: httpParams }); } /** * @returns {Observable} Currencies data. */ getCurrencies(): Observable { return this.http.get('/currencies'); } /** * @returns {Observable} Payment types data. */ getPaymentTypes(): Observable { return this.http.get('/paymenttypes'); } /** * @param {any} journalEntry Journal entry to be created. * @returns {Observable} */ createJournalEntry(journalEntry: any): Observable { return this.http.post('/journalentries', journalEntry); } /** * @returns {Observable} GL Accounts. */ getGlAccounts(): Observable { const httpParams = new HttpParams().set('manualEntriesAllowed', 'true').set('usage', '1').set('disabled', 'false'); return this.http.get(`/glaccounts`, { params: httpParams }); } /** * @param transactionId Transaction ID of journal entries. * @returns {Observable} Journal Entries. */ getJournalEntry(transactionId: string): Observable { const httpParams = new HttpParams().set('transactionId', transactionId).set('transactionDetails', 'true'); return this.http.get(`/journalentries`, { params: httpParams }); } /** * @param {any} filterBy Properties by which entries should be filtered. * @param {string} orderBy Property by which entries should be sorted. * @param {string} sortOrder Sort order: ascending or descending. * @param {number} offset Page offset. * @param {number} limit Number of entries within the page. * @returns {Observable} Journal Entries. */ getJournalEntries(filterBy: any, orderBy: string, sortOrder: string, offset: number, limit: number): Observable { let httpParams = new HttpParams() .set('offset', offset.toString()) .set('limit', limit.toString()) .set('sortOrder', sortOrder) .set('orderBy', orderBy); // filterBy: officeId, glAccountId, manualEntriesOnly, fromDate, toDate, transactionId filterBy.forEach(function (filter: any) { if (filter.value) { httpParams = httpParams.set(filter.type, filter.value); } }); return this.http.get('/journalentries', { params: httpParams }); } /** * CBS GL Account Ledger Report: returns opening balance, day-wise summary, totals, and paginated entries. */ getGLAccountLedger(params: { glAccountId?: number; officeId?: number; fromDate?: string; toDate?: string; dateFormat?: string; locale?: string; manualEntriesOnly?: boolean; transactionId?: string; offset?: number; limit?: number; orderBy?: string; sortOrder?: string; }): Observable { let httpParams = new HttpParams(); if (params.glAccountId) httpParams = httpParams.set('glAccountId', params.glAccountId.toString()); if (params.officeId) httpParams = httpParams.set('officeId', params.officeId.toString()); if (params.fromDate) httpParams = httpParams.set('fromDate', params.fromDate); if (params.toDate) httpParams = httpParams.set('toDate', params.toDate); if (params.dateFormat) httpParams = httpParams.set('dateFormat', params.dateFormat); if (params.locale) httpParams = httpParams.set('locale', params.locale); if (params.manualEntriesOnly != null) httpParams = httpParams.set('manualEntriesOnly', params.manualEntriesOnly.toString()); if (params.transactionId) httpParams = httpParams.set('transactionId', params.transactionId); if (params.offset != null) httpParams = httpParams.set('offset', params.offset.toString()); if (params.limit != null) httpParams = httpParams.set('limit', params.limit.toString()); if (params.orderBy) httpParams = httpParams.set('orderBy', params.orderBy); if (params.sortOrder) httpParams = httpParams.set('sortOrder', params.sortOrder); return this.http.get('/journalentries/ledger', { params: httpParams }); } /** * @param {string} transactionId Transaction ID to be reverted. * @param {string} comments Any comments. * @returns {Observable} */ revertTransaction(transactionId: string, comments: string): Observable { const httpParams = new HttpParams().set('command', 'reverse'); let data = {}; if (comments) { data = { comments: comments }; } return this.http.post(`/journalentries/${transactionId}`, data, { params: httpParams }); } /** * @returns {Observable} Financial activity accounts. */ getFinancialActivityAccounts(): Observable { return this.http.get('/financialactivityaccounts'); } /** * @returns {Observable} Financial activity accounts template. */ getFinancialActivityAccountsTemplate(): Observable { return this.http.get('/financialactivityaccounts/template'); } /** * @param {any} financialActivityAccount Financial activity account to be created. * @returns {Observable} */ createFinancialActivityAccount(financialActivityAccount: any): Observable { return this.http.post('/financialactivityaccounts', financialActivityAccount); } /** * @param {string} financialActivityAccountId Financial activity account ID of financial activity account. * @param {boolean} template True if template is required. * @returns {Observable} Financial activity account. */ getFinancialActivityAccount(financialActivityAccountId: string, template: boolean = false): Observable { const httpParams = new HttpParams().set('template', template.toString()); return this.http.get(`/financialactivityaccounts/${financialActivityAccountId}`, { params: httpParams }); } /** * @param {string} financialActivityAccountId Financial activity account ID of financial activity account to be updated. * @param {any} financialActivityAccount Financial activity account to be updated. * @returns {Observable} */ updateFinancialActivityAccount(financialActivityAccountId: string, financialActivityAccount: any): Observable { return this.http.put(`/financialactivityaccounts/${financialActivityAccountId}`, financialActivityAccount); } /** * @param {string} financialActivityAccountId Financial activity account ID of financial activity account to be deleted. * @returns {Observable} */ deleteFinancialActivityAccount(financialActivityAccountId: string): Observable { return this.http.delete(`/financialactivityaccounts/${financialActivityAccountId}`); } /** * @param {string} officeId Office ID to retrive opening balances accounts for. * @returns {Observable} */ retrieveOpeningBalances(officeId: string): Observable { const httpParams = new HttpParams().set('officeId', officeId); return this.http.get('/journalentries/openingbalance', { params: httpParams }); } /** * @param {any} openingBalances Opening balances for office accounts to be defined. * @returns {Observable} */ defineOpeningBalances(openingBalances: any): Observable { const httpParams = new HttpParams().set('command', 'defineOpeningBalance'); return this.http.post(`/journalentries`, openingBalances, { params: httpParams }); } /** * @returns {Observable} GL accounts. */ getChartOfAccounts(): Observable { return this.http.get('/glaccounts'); } /** * @returns {Observable} GL accounts template. */ getChartOfAccountsTemplate(): Observable { return this.http.get('/glaccounts/template'); } /** * @param {any} glAccount GL account to be created. * @returns {Observable} */ createGlAccount(glAccount: any): Observable { return this.http.post('/glaccounts', glAccount); } /** * @param {string} glAccountId GL account ID of gl account. * @param {boolean} template True if template is required. * @returns {Observable} GL account. */ getGlAccount(glAccountId: string, template: boolean = false): Observable { const httpParams = new HttpParams().set('template', template.toString()); return this.http.get(`/glaccounts/${glAccountId}`, { params: httpParams }); } /** * @param {string} glAccountId GL account ID of gl account. * @param {any} glAccount GL account to be updated. * @returns {Observable} */ updateGlAccount(glAccountId: string, glAccount: any): Observable { return this.http.put(`/glaccounts/${glAccountId}`, glAccount); } /** * @param glAccountId GL account ID of gl account to be deleted. * @returns {Observable} */ deleteGlAccount(glAccountId: string): Observable { return this.http.delete(`/glaccounts/${glAccountId}`); } /** * @returns {Observable} GL account closures. */ getAccountingClosures(): Observable { return this.http.get('/glclosures'); } /** * @param {any} accountingClosure GL closure to be created. * @returns {Observable} */ createAccountingClosure(accountingClosure: any): Observable { return this.http.post('/glclosures', accountingClosure); } /** * @param {string} accountingClosureId Accounting closure ID of gl closure. * @returns {Observable} Accounting closure. */ getAccountingClosure(accountingClosureId: string): Observable { return this.http.get(`/glclosures/${accountingClosureId}`); } /** * @param {string} accountingClosureId Accounting closure ID of gl closure to be updated. * @param {any} accountingClosure Accounting closure to be updated. * @returns {Observable} */ updateAccountingClosure(accountingClosureId: string, accountingClosure: any): Observable { return this.http.put(`/glclosures/${accountingClosureId}`, accountingClosure); } /** * @param {string} accountingClosureId Accounting closure ID of gl closure to be deleted. * @returns {Observable} */ deleteAccountingClosure(accountingClosureId: string): Observable { return this.http.delete(`/glclosures/${accountingClosureId}`); } /** * @returns {Observable} Accounting rules template. */ getAccountingRulesTemplate(): Observable { return this.http.get('/accountingrules/template'); } /** * @param {any} accountingRule Accounting rule to be created. * @returns {Observable} */ createAccountingRule(accountingRule: any): Observable { return this.http.post('/accountingrules', accountingRule); } /** * @param {string} accountingRuleId Accounting rule ID of accounting rule. * @returns {Observable} Accounting rule. */ getAccountingRule(accountingRuleId: string): Observable { return this.http.get(`/accountingrules/${accountingRuleId}`); } /** * @param {string} accoutingRuleId Accounting rule ID of accounting rule to be updated. * @param {any} accountingRule Accounting rule to be updated. * @returns {Observable} */ updateAccountingRule(accoutingRuleId: string, accountingRule: any): Observable { return this.http.put(`/accountingrules/${accoutingRuleId}`, accountingRule); } /** * @param {string} accountingRuleId Accounting rule ID of accounting rule to be deleted. * @returns {Observable} */ deleteAccountingRule(accountingRuleId: string): Observable { return this.http.delete(`/accountingrules/${accountingRuleId}`); } /** * @param {any} periodicAccruals Accruals to be executed. * @returns {Observable} */ executePeriodicAccruals(periodicAccruals: any): Observable { return this.http.post('/runaccruals', periodicAccruals); } /** * @returns {Observable} Provisioning entries. */ getProvisioningEntries(): Observable { return this.http.get('/provisioningentries'); } /** * @param {any} provisioningEntry Provisioning entry to be created. * @returns {Observable} */ createProvisioningEntry(provisioningEntry: any): Observable { return this.http.post('/provisioningentries', provisioningEntry); } /** * @param {string} provisioningEntryId Provisioning entry ID of provisioning entry. * @returns {Observable} Provisioning entry. */ getProvisioningEntry(provisioningEntryId: string): Observable { return this.http.get(`/provisioningentries/${provisioningEntryId}`); } /** * @returns {Observable} Loan products data. */ getLoanProducts(): Observable { return this.http.get('/loanproducts'); } /** * @returns {Observable} Provisioning categories data. */ getProvisioningCategories(): Observable { return this.http.get('/provisioningcategory'); } /** * @param {string} provisioningEntryId Provisioning entry ID of provisioning entry. * @returns {Observable} Provisioning entry entries. */ getProvisioningEntryEntries(provisioningEntryId: string): Observable { const httpParams = new HttpParams().set('entryId', provisioningEntryId); return this.http.get('/provisioningentries/entries', { params: httpParams }); } /** * @param {string} provisioningEntryId Provisioning entry ID of provisioning entry. * @returns {Observable} Provisioning journal entries. */ getProvisioningJournalEntries(provisioningEntryId: string): Observable { const httpParams = new HttpParams().set('entryId', provisioningEntryId); return this.http.get('/journalentries/provisioning', { params: httpParams }); } /** * @param {string} provisioningEntryId Provisioning entry ID for which journal entries need to be created. * @returns {Observable} */ createProvisioningJournalEntries(provisioningEntryId: string): Observable { const httpParams = new HttpParams().set('command', 'createjournalentry'); return this.http.post(`/provisioningentries/${provisioningEntryId}`, {}, { params: httpParams }); } /** * @param {string} provisioningEntryId Provisioning entry ID for which provisioning entry needs to be recreated. * @returns {Observable} */ recreateProvisioningEntries(provisioningEntryId: string): Observable { const httpParams = new HttpParams().set('command', 'recreateprovisioningentry'); return this.http.post(`/provisioningentries/${provisioningEntryId}`, {}, { params: httpParams }); } }