refactor: migrate ledger writer to nestjs

This commit is contained in:
Ahmed Bouhuolia
2025-01-01 12:11:58 +02:00
parent 3ad34ba56f
commit 505c4b28a5
45 changed files with 880 additions and 777 deletions

View File

@@ -19,10 +19,10 @@ export class BillsApplication {
private createBillService: CreateBill,
private editBillService: EditBillService,
private getBillService: GetBill,
// private getBillsService: GetBills,
private deleteBillService: DeleteBill,
private getDueBillsService: GetDueBills,
private openBillService: OpenBillService,
// private getBillsService: GetBills,
// private getBillPaymentsService: GetBillPayments,
) {}

View File

@@ -18,9 +18,12 @@ import { TenancyContext } from '../Tenancy/TenancyContext.service';
import { BillsController } from './Bills.controller';
import { BillLandedCostsModule } from '../BillLandedCosts/BillLandedCosts.module';
import { BillGLEntriesSubscriber } from './subscribers/BillGLEntriesSubscriber';
import { BillGLEntries } from './commands/BillsGLEntries';
import { LedgerModule } from '../Ledger/Ledger.module';
import { AccountsModule } from '../Accounts/Accounts.module';
@Module({
imports: [BillLandedCostsModule],
imports: [BillLandedCostsModule, LedgerModule, AccountsModule],
providers: [
TenancyContext,
BillsApplication,
@@ -37,8 +40,9 @@ import { BillGLEntriesSubscriber } from './subscribers/BillGLEntriesSubscriber';
DeleteBill,
BillDTOTransformer,
BillsValidators,
BillGLEntries,
ItemsEntriesService,
BillGLEntriesSubscriber
BillGLEntriesSubscriber,
],
controllers: [BillsController],
})

View File

@@ -6,7 +6,7 @@ import * as composeAsync from 'async/compose';
import { formatDateFields } from '@/utils/format-date-fields';
import { BranchTransactionDTOTransformer } from '@/modules/Branches/integrations/BranchTransactionDTOTransform';
import { WarehouseTransactionDTOTransform } from '@/modules/Warehouses/Integrations/WarehouseTransactionDTOTransform';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
import { Item } from '@/modules/Items/models/Item';
import { Vendor } from '@/modules/Vendors/models/Vendor';
import { ItemEntriesTaxTransactions } from '@/modules/TaxRates/ItemEntriesTaxTransactions.service';

View File

@@ -1,6 +1,6 @@
import { sumBy } from 'lodash';
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
import { Bill } from '../models/Bill';
import { AccountNormal } from '@/modules/Accounts/Accounts.types';
import { Ledger } from '@/modules/Ledger/Ledger';
@@ -85,7 +85,6 @@ export class BillGL {
index: index + 1,
indexGroup: 10,
itemId: entry.itemId,
itemQuantity: entry.quantity,
accountNormal: AccountNormal.DEBIT,
};
}

View File

@@ -2,7 +2,7 @@ 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 { Inject, Injectable } from '@nestjs/common';
import { BillGL } from './BillsGL';
@Injectable()
@@ -15,6 +15,8 @@ export class BillGLEntries {
constructor(
private readonly ledgerStorage: LedgerStorageService,
private readonly accountRepository: AccountRepository,
@Inject(Bill.name)
private readonly billModel: typeof Bill,
) {}

View File

@@ -8,7 +8,7 @@ import {
import { BillsValidators } from './BillsValidators.service';
import { UnitOfWork } from '@/modules/Tenancy/TenancyDB/UnitOfWork.service';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
import { Bill } from '../models/Bill';
@Injectable()

View File

@@ -24,6 +24,7 @@ export class EditBillService {
private eventPublisher: EventEmitter2,
private transactionLandedCostEntries: TransactionLandedCostEntriesService,
private transformerDTO: BillDTOTransformer,
@Inject(Bill.name) private billModel: typeof Bill,
@Inject(Vendor.name) private contactModel: typeof Vendor,
) {}

View File

@@ -1,6 +1,7 @@
import { Model, raw, mixin } from 'objection';
import { castArray, difference } from 'lodash';
import moment from 'moment';
import { castArray, difference, defaultTo } from 'lodash';
import * as moment from 'moment';
import * as R from 'ramda';
// import TenantModel from 'models/TenantModel';
// import BillSettings from './Bill.Settings';
// import ModelSetting from './ModelSetting';
@@ -8,10 +9,11 @@ import moment from 'moment';
// import { DEFAULT_VIEWS } from '@/services/Purchases/Bills/constants';
// import ModelSearchable from './ModelSearchable';
import { BaseModel } from '@/models/Model';
import { ItemEntry } from '@/modules/Items/models/ItemEntry';
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
import { BillLandedCost } from '@/modules/BillLandedCosts/models/BillLandedCost';
import { DiscountType } from '@/common/types/Discount';
export class Bill extends BaseModel{
export class Bill extends BaseModel {
public amount: number;
public paymentAmount: number;
public landedCostAmount: number;
@@ -33,6 +35,10 @@ export class Bill extends BaseModel{
public openedAt: Date | string;
public userId: number;
public discountType: DiscountType;
public discount: number;
public adjustment: number;
public branchId: number;
public warehouseId: number;
public projectId: number;
@@ -68,9 +74,16 @@ export class Bill extends BaseModel{
'localAllocatedCostAmount',
'billableAmount',
'amountLocal',
'discountAmount',
'discountAmountLocal',
'discountPercentage',
'adjustmentLocal',
'subtotal',
'subtotalLocal',
'subtotalExcludingTax',
'subtotalExludingTax',
'taxAmountWithheldLocal',
'total',
'totalLocal',
@@ -119,14 +132,53 @@ export class Bill extends BaseModel{
return this.taxAmountWithheld * this.exchangeRate;
}
/**
* Discount amount.
* @returns {number}
*/
get discountAmount() {
return this.discountType === DiscountType.Amount
? this.discount
: this.subtotal * (this.discount / 100);
}
/**
* Discount amount in local currency.
* @returns {number | null}
*/
get discountAmountLocal() {
return this.discountAmount ? this.discountAmount * this.exchangeRate : null;
}
/**
/**
* Discount percentage.
* @returns {number | null}
*/
get discountPercentage(): number | null {
return this.discountType === DiscountType.Percentage ? this.discount : null;
}
/**
* Adjustment amount in local currency.
* @returns {number | null}
*/
get adjustmentLocal() {
return this.adjustment ? this.adjustment * this.exchangeRate : null;
}
/**
* Invoice total. (Tax included)
* @returns {number}
*/
get total() {
return this.isInclusiveTax
? this.subtotal
: this.subtotal + this.taxAmountWithheld;
const adjustmentAmount = defaultTo(this.adjustment, 0);
return R.compose(
R.add(adjustmentAmount),
R.subtract(R.__, this.discountAmount),
R.when(R.always(this.isInclusiveTax), R.add(this.taxAmountWithheld)),
)(this.subtotal);
}
/**
@@ -183,7 +235,7 @@ export class Bill extends BaseModel{
raw(`COALESCE(AMOUNT, 0) -
COALESCE(PAYMENT_AMOUNT, 0) -
COALESCE(CREDITED_AMOUNT, 0) > 0
`)
`),
);
},
/**

View File

@@ -8,6 +8,7 @@ import { TransformerInjectable } from '@/modules/Transformer/TransformerInjectab
export class GetBill {
constructor(
@Inject(Bill.name) private billModel: typeof Bill,
private transformer: TransformerInjectable,
private validators: BillsValidators,
) {}