mirror of
https://github.com/bigcapitalhq/bigcapital.git
synced 2026-02-20 06:40:31 +00:00
refactor: GL entries
This commit is contained in:
@@ -1,288 +0,0 @@
|
||||
// import moment from 'moment';
|
||||
// import { sumBy } from 'lodash';
|
||||
// import { Knex } from 'knex';
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import * as R from 'ramda';
|
||||
// import { AccountNormal, IBill, IItemEntry, ILedgerEntry } from '@/interfaces';
|
||||
// import HasTenancyService from '@/services/Tenancy/TenancyService';
|
||||
// import Ledger from '@/services/Accounting/Ledger';
|
||||
// import LedgerStorageService from '@/services/Accounting/LedgerStorageService';
|
||||
// import ItemsEntriesService from '@/services/Items/ItemsEntriesService';
|
||||
|
||||
// @Service()
|
||||
// export class BillGLEntries {
|
||||
// @Inject()
|
||||
// private tenancy: HasTenancyService;
|
||||
|
||||
// @Inject()
|
||||
// private ledgerStorage: LedgerStorageService;
|
||||
|
||||
// @Inject()
|
||||
// private itemsEntriesService: ItemsEntriesService;
|
||||
|
||||
// /**
|
||||
// * Creates bill GL entries.
|
||||
// * @param {number} tenantId -
|
||||
// * @param {number} billId -
|
||||
// * @param {Knex.Transaction} trx -
|
||||
// */
|
||||
// public writeBillGLEntries = async (
|
||||
// tenantId: number,
|
||||
// billId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// const { accountRepository } = this.tenancy.repositories(tenantId);
|
||||
// const { Bill } = this.tenancy.models(tenantId);
|
||||
|
||||
// // Retrieves bill with associated entries and landed costs.
|
||||
// const bill = await Bill.query(trx)
|
||||
// .findById(billId)
|
||||
// .withGraphFetched('entries.item')
|
||||
// .withGraphFetched('entries.allocatedCostEntries')
|
||||
// .withGraphFetched('locatedLandedCosts.allocateEntries');
|
||||
|
||||
// // Finds or create a A/P account based on the given currency.
|
||||
// const APAccount = await accountRepository.findOrCreateAccountsPayable(
|
||||
// bill.currencyCode,
|
||||
// {},
|
||||
// trx
|
||||
// );
|
||||
// // Find or create tax payable account.
|
||||
// const taxPayableAccount = await accountRepository.findOrCreateTaxPayable(
|
||||
// {},
|
||||
// trx
|
||||
// );
|
||||
// const billLedger = this.getBillLedger(
|
||||
// bill,
|
||||
// APAccount.id,
|
||||
// taxPayableAccount.id
|
||||
// );
|
||||
// // Commit the GL enties on the storage.
|
||||
// await this.ledgerStorage.commit(tenantId, billLedger, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Reverts the given bill GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} billId
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public revertBillGLEntries = async (
|
||||
// tenantId: number,
|
||||
// billId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// await this.ledgerStorage.deleteByReference(tenantId, billId, 'Bill', trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Rewrites the given bill GL entries.
|
||||
// * @param {number} tenantId
|
||||
// * @param {number} billId
|
||||
// * @param {Knex.Transaction} trx
|
||||
// */
|
||||
// public rewriteBillGLEntries = async (
|
||||
// tenantId: number,
|
||||
// billId: number,
|
||||
// trx?: Knex.Transaction
|
||||
// ) => {
|
||||
// // Reverts the bill GL entries.
|
||||
// await this.revertBillGLEntries(tenantId, billId, trx);
|
||||
|
||||
// // Writes the bill GL entries.
|
||||
// await this.writeBillGLEntries(tenantId, billId, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the bill common entry.
|
||||
// * @param {IBill} bill
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getBillCommonEntry = (bill: IBill) => {
|
||||
// return {
|
||||
// debit: 0,
|
||||
// credit: 0,
|
||||
// currencyCode: bill.currencyCode,
|
||||
// exchangeRate: bill.exchangeRate || 1,
|
||||
|
||||
// transactionId: bill.id,
|
||||
// transactionType: 'Bill',
|
||||
|
||||
// date: moment(bill.billDate).format('YYYY-MM-DD'),
|
||||
// userId: bill.userId,
|
||||
|
||||
// referenceNumber: bill.referenceNo,
|
||||
// transactionNumber: bill.billNumber,
|
||||
|
||||
// branchId: bill.branchId,
|
||||
// projectId: bill.projectId,
|
||||
|
||||
// createdAt: bill.createdAt,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the bill item inventory/cost entry.
|
||||
// * @param {IBill} bill -
|
||||
// * @param {IItemEntry} entry -
|
||||
// * @param {number} index -
|
||||
// */
|
||||
// private getBillItemEntry = R.curry(
|
||||
// (bill: IBill, entry: IItemEntry, index: number): ILedgerEntry => {
|
||||
// const commonJournalMeta = this.getBillCommonEntry(bill);
|
||||
|
||||
// const localAmount = bill.exchangeRate * entry.amountExludingTax;
|
||||
// const landedCostAmount = sumBy(entry.allocatedCostEntries, 'cost');
|
||||
|
||||
// return {
|
||||
// ...commonJournalMeta,
|
||||
// debit: localAmount + landedCostAmount,
|
||||
// accountId:
|
||||
// ['inventory'].indexOf(entry.item.type) !== -1
|
||||
// ? entry.item.inventoryAccountId
|
||||
// : entry.costAccountId,
|
||||
// index: index + 1,
|
||||
// indexGroup: 10,
|
||||
// itemId: entry.itemId,
|
||||
// itemQuantity: entry.quantity,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// };
|
||||
// }
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * Retrieves the bill landed cost entry.
|
||||
// * @param {IBill} bill -
|
||||
// * @param {} landedCost -
|
||||
// * @param {number} index -
|
||||
// */
|
||||
// private getBillLandedCostEntry = R.curry(
|
||||
// (bill: IBill, landedCost, index: number): ILedgerEntry => {
|
||||
// const commonJournalMeta = this.getBillCommonEntry(bill);
|
||||
|
||||
// return {
|
||||
// ...commonJournalMeta,
|
||||
// credit: landedCost.amount,
|
||||
// accountId: landedCost.costAccountId,
|
||||
// accountNormal: AccountNormal.DEBIT,
|
||||
// index: 1,
|
||||
// indexGroup: 20,
|
||||
// };
|
||||
// }
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * Retrieves the bill payable entry.
|
||||
// * @param {number} payableAccountId
|
||||
// * @param {IBill} bill
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getBillPayableEntry = (
|
||||
// payableAccountId: number,
|
||||
// bill: IBill
|
||||
// ): ILedgerEntry => {
|
||||
// const commonJournalMeta = this.getBillCommonEntry(bill);
|
||||
|
||||
// return {
|
||||
// ...commonJournalMeta,
|
||||
// credit: bill.totalLocal,
|
||||
// accountId: payableAccountId,
|
||||
// contactId: bill.vendorId,
|
||||
// accountNormal: AccountNormal.CREDIT,
|
||||
// index: 1,
|
||||
// indexGroup: 5,
|
||||
// };
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the bill tax GL entry.
|
||||
// * @param {IBill} bill -
|
||||
// * @param {number} taxPayableAccountId -
|
||||
// * @param {IItemEntry} entry -
|
||||
// * @param {number} index -
|
||||
// * @returns {ILedgerEntry}
|
||||
// */
|
||||
// private getBillTaxEntry = R.curry(
|
||||
// (
|
||||
// bill: IBill,
|
||||
// taxPayableAccountId: number,
|
||||
// entry: IItemEntry,
|
||||
// index: number
|
||||
// ): ILedgerEntry => {
|
||||
// const commonJournalMeta = this.getBillCommonEntry(bill);
|
||||
|
||||
// return {
|
||||
// ...commonJournalMeta,
|
||||
// debit: entry.taxAmount,
|
||||
// index,
|
||||
// indexGroup: 30,
|
||||
// accountId: taxPayableAccountId,
|
||||
// accountNormal: AccountNormal.CREDIT,
|
||||
// taxRateId: entry.taxRateId,
|
||||
// taxRate: entry.taxRate,
|
||||
// };
|
||||
// }
|
||||
// );
|
||||
|
||||
// /**
|
||||
// * Retrieves the bill tax GL entries.
|
||||
// * @param {IBill} bill
|
||||
// * @param {number} taxPayableAccountId
|
||||
// * @returns {ILedgerEntry[]}
|
||||
// */
|
||||
// private getBillTaxEntries = (bill: IBill, taxPayableAccountId: number) => {
|
||||
// // Retrieves the non-zero tax entries.
|
||||
// const nonZeroTaxEntries = this.itemsEntriesService.getNonZeroEntries(
|
||||
// bill.entries
|
||||
// );
|
||||
// const transformTaxEntry = this.getBillTaxEntry(bill, taxPayableAccountId);
|
||||
|
||||
// return nonZeroTaxEntries.map(transformTaxEntry);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the given bill GL entries.
|
||||
// * @param {IBill} bill
|
||||
// * @param {number} payableAccountId
|
||||
// * @returns {ILedgerEntry[]}
|
||||
// */
|
||||
// private getBillGLEntries = (
|
||||
// bill: IBill,
|
||||
// payableAccountId: number,
|
||||
// taxPayableAccountId: number
|
||||
// ): ILedgerEntry[] => {
|
||||
// const payableEntry = this.getBillPayableEntry(payableAccountId, bill);
|
||||
|
||||
// const itemEntryTransformer = this.getBillItemEntry(bill);
|
||||
// const landedCostTransformer = this.getBillLandedCostEntry(bill);
|
||||
|
||||
// const itemsEntries = bill.entries.map(itemEntryTransformer);
|
||||
// const landedCostEntries = bill.locatedLandedCosts.map(
|
||||
// landedCostTransformer
|
||||
// );
|
||||
// const taxEntries = this.getBillTaxEntries(bill, taxPayableAccountId);
|
||||
|
||||
// // Allocate cost entries journal entries.
|
||||
// return [payableEntry, ...itemsEntries, ...landedCostEntries, ...taxEntries];
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Retrieves the given bill ledger.
|
||||
// * @param {IBill} bill
|
||||
// * @param {number} payableAccountId
|
||||
// * @returns {Ledger}
|
||||
// */
|
||||
// private getBillLedger = (
|
||||
// bill: IBill,
|
||||
// payableAccountId: number,
|
||||
// taxPayableAccountId: number
|
||||
// ) => {
|
||||
// const entries = this.getBillGLEntries(
|
||||
// bill,
|
||||
// payableAccountId,
|
||||
// taxPayableAccountId
|
||||
// );
|
||||
|
||||
// return new Ledger(entries);
|
||||
// };
|
||||
// }
|
||||
@@ -1,75 +0,0 @@
|
||||
// import { Inject, Service } from 'typedi';
|
||||
// import events from '@/subscribers/events';
|
||||
// import {
|
||||
// IBillCreatedPayload,
|
||||
// IBillEditedPayload,
|
||||
// IBIllEventDeletedPayload,
|
||||
// IBillOpenedPayload,
|
||||
// } from '@/interfaces';
|
||||
// import { BillGLEntries } from './BillGLEntries';
|
||||
|
||||
// @Service()
|
||||
// export class BillGLEntriesSubscriber {
|
||||
// @Inject()
|
||||
// private billGLEntries: BillGLEntries;
|
||||
|
||||
// /**
|
||||
// * Attaches events with handles.
|
||||
// */
|
||||
// public attach(bus) {
|
||||
// bus.subscribe(
|
||||
// events.bill.onCreated,
|
||||
// this.handlerWriteJournalEntriesOnCreate
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.bill.onOpened,
|
||||
// this.handlerWriteJournalEntriesOnCreate
|
||||
// );
|
||||
// bus.subscribe(
|
||||
// events.bill.onEdited,
|
||||
// this.handleOverwriteJournalEntriesOnEdit
|
||||
// );
|
||||
// bus.subscribe(events.bill.onDeleted, this.handlerDeleteJournalEntries);
|
||||
// }
|
||||
|
||||
// /**
|
||||
// * Handles writing journal entries once bill created.
|
||||
// * @param {IBillCreatedPayload} payload -
|
||||
// */
|
||||
// private handlerWriteJournalEntriesOnCreate = async ({
|
||||
// tenantId,
|
||||
// bill,
|
||||
// trx,
|
||||
// }: IBillCreatedPayload | IBillOpenedPayload) => {
|
||||
// if (!bill.openedAt) return null;
|
||||
|
||||
// await this.billGLEntries.writeBillGLEntries(tenantId, bill.id, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Handles the overwriting journal entries once bill edited.
|
||||
// * @param {IBillEditedPayload} payload -
|
||||
// */
|
||||
// private handleOverwriteJournalEntriesOnEdit = async ({
|
||||
// tenantId,
|
||||
// billId,
|
||||
// bill,
|
||||
// trx,
|
||||
// }: IBillEditedPayload) => {
|
||||
// if (!bill.openedAt) return null;
|
||||
|
||||
// await this.billGLEntries.rewriteBillGLEntries(tenantId, billId, trx);
|
||||
// };
|
||||
|
||||
// /**
|
||||
// * Handles revert journal entries on bill deleted.
|
||||
// * @param {IBIllEventDeletedPayload} payload -
|
||||
// */
|
||||
// private handlerDeleteJournalEntries = async ({
|
||||
// tenantId,
|
||||
// billId,
|
||||
// trx,
|
||||
// }: IBIllEventDeletedPayload) => {
|
||||
// await this.billGLEntries.revertBillGLEntries(tenantId, billId, trx);
|
||||
// };
|
||||
// }
|
||||
243
packages/server-nest/src/modules/Bills/commands/BillsGL.ts
Normal file
243
packages/server-nest/src/modules/Bills/commands/BillsGL.ts
Normal file
@@ -0,0 +1,243 @@
|
||||
import { sumBy } from 'lodash';
|
||||
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
|
||||
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { AccountNormal } from '@/modules/Accounts/Accounts.types';
|
||||
import { Ledger } from '@/modules/Ledger/Ledger';
|
||||
import { BillLandedCost } from '@/modules/BillLandedCosts/models/BillLandedCost';
|
||||
|
||||
export class BillGL {
|
||||
private bill: Bill;
|
||||
private payableAccountId: number;
|
||||
private taxPayableAccountId: number;
|
||||
private purchaseDiscountAccountId: number;
|
||||
private otherExpensesAccountId: number;
|
||||
|
||||
constructor(bill: Bill) {
|
||||
this.bill = bill;
|
||||
}
|
||||
|
||||
setPayableAccountId(payableAccountId: number) {
|
||||
this.payableAccountId = payableAccountId;
|
||||
return this;
|
||||
}
|
||||
|
||||
setTaxPayableAccountId(taxPayableAccountId: number) {
|
||||
this.taxPayableAccountId = taxPayableAccountId;
|
||||
return this;
|
||||
}
|
||||
|
||||
setPurchaseDiscountAccountId(purchaseDiscountAccountId: number) {
|
||||
this.purchaseDiscountAccountId = purchaseDiscountAccountId;
|
||||
return this;
|
||||
}
|
||||
|
||||
setOtherExpensesAccountId(otherExpensesAccountId: number) {
|
||||
this.otherExpensesAccountId = otherExpensesAccountId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bill common entry.
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private get billCommonEntry() {
|
||||
return {
|
||||
debit: 0,
|
||||
credit: 0,
|
||||
|
||||
currencyCode: this.bill.currencyCode,
|
||||
exchangeRate: this.bill.exchangeRate || 1,
|
||||
|
||||
transactionId: this.bill.id,
|
||||
transactionType: 'Bill',
|
||||
|
||||
date: moment(this.bill.billDate).format('YYYY-MM-DD'),
|
||||
userId: this.bill.userId,
|
||||
|
||||
referenceNumber: this.bill.referenceNo,
|
||||
transactionNumber: this.bill.billNumber,
|
||||
|
||||
branchId: this.bill.branchId,
|
||||
projectId: this.bill.projectId,
|
||||
|
||||
createdAt: this.bill.createdAt,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bill item inventory/cost entry.
|
||||
* @param {ItemEntry} entry -
|
||||
* @param {number} index -
|
||||
*/
|
||||
private getBillItemEntry(entry: ItemEntry, index: number): ILedgerEntry {
|
||||
const commonJournalMeta = this.billCommonEntry;
|
||||
const totalLocal = this.bill.exchangeRate * entry.totalExcludingTax;
|
||||
const landedCostAmount = sumBy(entry.allocatedCostEntries, 'cost');
|
||||
|
||||
return {
|
||||
...commonJournalMeta,
|
||||
debit: totalLocal + landedCostAmount,
|
||||
accountId:
|
||||
['inventory'].indexOf(entry.item.type) !== -1
|
||||
? entry.item.inventoryAccountId
|
||||
: entry.costAccountId,
|
||||
index: index + 1,
|
||||
indexGroup: 10,
|
||||
itemId: entry.itemId,
|
||||
itemQuantity: entry.quantity,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bill landed cost entry.
|
||||
* @param {BillLandedCost} landedCost - Landed cost
|
||||
* @param {number} index - Index
|
||||
*/
|
||||
private getBillLandedCostEntry(
|
||||
landedCost: BillLandedCost,
|
||||
index: number,
|
||||
): ILedgerEntry {
|
||||
const commonJournalMeta = this.billCommonEntry;
|
||||
|
||||
return {
|
||||
...commonJournalMeta,
|
||||
credit: landedCost.amount,
|
||||
accountId: landedCost.costAccountId,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
index: 1,
|
||||
indexGroup: 20,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bill payable entry.
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private get billPayableEntry(): ILedgerEntry {
|
||||
const commonJournalMeta = this.billCommonEntry;
|
||||
|
||||
return {
|
||||
...commonJournalMeta,
|
||||
credit: this.bill.totalLocal,
|
||||
accountId: this.payableAccountId,
|
||||
contactId: this.bill.vendorId,
|
||||
accountNormal: AccountNormal.CREDIT,
|
||||
index: 1,
|
||||
indexGroup: 5,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bill tax GL entry.
|
||||
* @param {IBill} bill -
|
||||
* @param {number} taxPayableAccountId -
|
||||
* @param {IItemEntry} entry -
|
||||
* @param {number} index -
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private getBillTaxEntry(entry: ItemEntry, index: number): ILedgerEntry {
|
||||
const commonJournalMeta = this.billCommonEntry;
|
||||
|
||||
return {
|
||||
...commonJournalMeta,
|
||||
debit: entry.taxAmount,
|
||||
index,
|
||||
indexGroup: 30,
|
||||
accountId: this.taxPayableAccountId,
|
||||
accountNormal: AccountNormal.CREDIT,
|
||||
taxRateId: entry.taxRateId,
|
||||
taxRate: entry.taxRate,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the bill tax GL entries.
|
||||
* @param {IBill} bill
|
||||
* @param {number} taxPayableAccountId
|
||||
* @returns {ILedgerEntry[]}
|
||||
*/
|
||||
// private getBillTaxEntries = () => {
|
||||
// // Retrieves the non-zero tax entries.
|
||||
// const nonZeroTaxEntries = this.itemsEntriesService.getNonZeroEntries(
|
||||
// this.bill.entries,
|
||||
// );
|
||||
// const transformTaxEntry = this.getBillTaxEntry(
|
||||
// this.bill,
|
||||
// this.taxPayableAccountId,
|
||||
// );
|
||||
|
||||
// return nonZeroTaxEntries.map(transformTaxEntry);
|
||||
// };
|
||||
|
||||
/**
|
||||
* Retrieves the purchase discount GL entry.
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private get purchaseDiscountEntry(): ILedgerEntry {
|
||||
const commonEntry = this.billCommonEntry;
|
||||
|
||||
return {
|
||||
...commonEntry,
|
||||
credit: this.bill.discountAmountLocal,
|
||||
accountId: this.purchaseDiscountAccountId,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
index: 1,
|
||||
indexGroup: 40,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the purchase other charges GL entry.
|
||||
* @returns {ILedgerEntry}
|
||||
*/
|
||||
private get adjustmentEntry(): ILedgerEntry {
|
||||
const commonEntry = this.billCommonEntry;
|
||||
const adjustmentAmount = Math.abs(this.bill.adjustmentLocal);
|
||||
|
||||
return {
|
||||
...commonEntry,
|
||||
debit: this.bill.adjustmentLocal > 0 ? adjustmentAmount : 0,
|
||||
credit: this.bill.adjustmentLocal < 0 ? adjustmentAmount : 0,
|
||||
accountId: this.otherExpensesAccountId,
|
||||
accountNormal: AccountNormal.DEBIT,
|
||||
index: 1,
|
||||
indexGroup: 40,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the given bill GL entries.
|
||||
* @returns {ILedgerEntry[]}
|
||||
*/
|
||||
private getBillGLEntries = (): ILedgerEntry[] => {
|
||||
const payableEntry = this.billPayableEntry;
|
||||
|
||||
const itemsEntries = this.bill.entries.map((entry, index) =>
|
||||
this.getBillItemEntry(entry, index),
|
||||
);
|
||||
const landedCostEntries = this.bill.locatedLandedCosts.map(
|
||||
(landedCost, index) => this.getBillLandedCostEntry(landedCost, index),
|
||||
);
|
||||
|
||||
// Allocate cost entries journal entries.
|
||||
return [
|
||||
payableEntry,
|
||||
...itemsEntries,
|
||||
...landedCostEntries,
|
||||
this.purchaseDiscountEntry,
|
||||
this.adjustmentEntry,
|
||||
];
|
||||
};
|
||||
|
||||
/**
|
||||
* Retrieves the given bill ledger.
|
||||
* @returns {Ledger}
|
||||
*/
|
||||
public getBillLedger = () => {
|
||||
const entries = this.getBillGLEntries();
|
||||
|
||||
return new Ledger(entries);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
import { Knex } from 'knex';
|
||||
import { LedgerStorageService } from '@/modules/Ledger/LedgerStorage.service';
|
||||
import { AccountRepository } from '@/modules/Accounts/repositories/Account.repository';
|
||||
import { Bill } from '../models/Bill';
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { BillGL } from './BillsGL';
|
||||
|
||||
@Injectable()
|
||||
export class BillGLEntries {
|
||||
/**
|
||||
* @param {LedgerStorageService} ledgerStorage - Ledger storage service.
|
||||
* @param {AccountRepository} accountRepository - Account repository.
|
||||
* @param {typeof Bill} billModel - Bill model.
|
||||
*/
|
||||
constructor(
|
||||
private readonly ledgerStorage: LedgerStorageService,
|
||||
private readonly accountRepository: AccountRepository,
|
||||
private readonly billModel: typeof Bill,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Creates bill GL entries.
|
||||
* @param {number} billId - Bill id.
|
||||
* @param {Knex.Transaction} trx - Knex transaction.
|
||||
*/
|
||||
public writeBillGLEntries = async (
|
||||
billId: number,
|
||||
trx?: Knex.Transaction,
|
||||
) => {
|
||||
// Retrieves bill with associated entries and landed costs.
|
||||
const bill = await this.billModel
|
||||
.query(trx)
|
||||
.findById(billId)
|
||||
.withGraphFetched('entries.item')
|
||||
.withGraphFetched('entries.allocatedCostEntries')
|
||||
.withGraphFetched('locatedLandedCosts.allocateEntries');
|
||||
|
||||
// Finds or create a A/P account based on the given currency.
|
||||
const APAccount = await this.accountRepository.findOrCreateAccountsPayable(
|
||||
bill.currencyCode,
|
||||
{},
|
||||
trx,
|
||||
);
|
||||
// Find or create tax payable account.
|
||||
const taxPayableAccount =
|
||||
await this.accountRepository.findOrCreateTaxPayable({}, trx);
|
||||
|
||||
// Find or create other expenses account.
|
||||
const otherExpensesAccount =
|
||||
await this.accountRepository.findOrCreateOtherExpensesAccount({}, trx);
|
||||
|
||||
// Find or create purchase discount account.
|
||||
const purchaseDiscountAccount =
|
||||
await this.accountRepository.findOrCreatePurchaseDiscountAccount({}, trx);
|
||||
|
||||
// Retrieves the bill ledger.
|
||||
const billLedger = new BillGL(bill)
|
||||
.setPayableAccountId(APAccount.id)
|
||||
.setTaxPayableAccountId(taxPayableAccount.id)
|
||||
.setPurchaseDiscountAccountId(purchaseDiscountAccount.id)
|
||||
.setOtherExpensesAccountId(otherExpensesAccount.id)
|
||||
.getBillLedger();
|
||||
|
||||
// Commit the GL enties on the storage.
|
||||
await this.ledgerStorage.commit(billLedger, trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Reverts the given bill GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public revertBillGLEntries = async (
|
||||
billId: number,
|
||||
trx?: Knex.Transaction,
|
||||
) => {
|
||||
await this.ledgerStorage.deleteByReference(billId, 'Bill', trx);
|
||||
};
|
||||
|
||||
/**
|
||||
* Rewrites the given bill GL entries.
|
||||
* @param {number} tenantId
|
||||
* @param {number} billId
|
||||
* @param {Knex.Transaction} trx
|
||||
*/
|
||||
public rewriteBillGLEntries = async (
|
||||
billId: number,
|
||||
trx?: Knex.Transaction,
|
||||
) => {
|
||||
// Reverts the bill GL entries.
|
||||
await this.revertBillGLEntries(billId, trx);
|
||||
|
||||
// Writes the bill GL entries.
|
||||
await this.writeBillGLEntries(billId, trx);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user