fix: bugs bashing

- Added English translations for customer types in `customer.json`.
- Updated `Model.ts` to improve deletion logic by filtering dependent relations.
- Introduced `BillPaymentBillSyncSubscriber` to handle bill payment events.
- Enhanced `CreateBillPaymentService` and `EditBillPaymentService` to fetch entries after insertion/updating.
- Updated `SaleInvoiceCostGLEntries` to include item entry details in GL entries.
- Refactored various components in the webapp for consistency in naming conventions.
This commit is contained in:
Ahmed Bouhuolia
2026-01-04 01:24:10 +02:00
parent 987ad992a4
commit 0475ce136a
14 changed files with 167 additions and 32 deletions

View File

@@ -14,6 +14,7 @@ import { BranchesSettingsService } from '../Branches/BranchesSettings';
import { BillPaymentsController } from './BillPayments.controller';
import { BillPaymentGLEntries } from './commands/BillPaymentGLEntries';
import { BillPaymentGLEntriesSubscriber } from './subscribers/BillPaymentGLEntriesSubscriber';
import { BillPaymentBillSyncSubscriber } from './subscribers/BillPaymentBillSyncSubscriber';
import { LedgerModule } from '../Ledger/Ledger.module';
import { AccountsModule } from '../Accounts/Accounts.module';
import { BillPaymentsExportable } from './queries/BillPaymentsExportable';
@@ -39,6 +40,7 @@ import { BillPaymentsPages } from './commands/BillPaymentsPages.service';
TenancyContext,
BillPaymentGLEntries,
BillPaymentGLEntriesSubscriber,
BillPaymentBillSyncSubscriber,
BillPaymentsExportable,
BillPaymentsImportable,
GetBillPaymentsService,
@@ -52,4 +54,4 @@ import { BillPaymentsPages } from './commands/BillPaymentsPages.service';
],
controllers: [BillPaymentsController],
})
export class BillPaymentsModule {}
export class BillPaymentsModule { }

View File

@@ -38,7 +38,7 @@ export class CreateBillPaymentService {
@Inject(BillPayment.name)
private readonly billPaymentModel: TenantModelProxy<typeof BillPayment>,
) {}
) { }
/**
* Creates a new bill payment transcations and store it to the storage
@@ -103,11 +103,19 @@ export class CreateBillPaymentService {
} as IBillPaymentCreatingPayload);
// Writes the bill payment graph to the storage.
const billPayment = await this.billPaymentModel()
const insertedBillPayment = await this.billPaymentModel()
.query(trx)
.insertGraphAndFetch({
...billPaymentObj,
});
// Fetch the bill payment with entries to ensure they're loaded for the subscriber.
const billPayment = await this.billPaymentModel()
.query(trx)
.withGraphFetched('entries')
.findById(insertedBillPayment.id)
.throwIfNotFound();
// Triggers `onBillPaymentCreated` event.
await this.eventPublisher.emitAsync(events.billPayment.onCreated, {
billPayment,

View File

@@ -29,7 +29,7 @@ export class EditBillPayment {
@Inject(Vendor.name)
private readonly vendorModel: TenantModelProxy<typeof Vendor>,
) {}
) { }
/**
* Edits the details of the given bill payment.
@@ -116,12 +116,20 @@ export class EditBillPayment {
} as IBillPaymentEditingPayload);
// Edits the bill payment transaction graph on the storage.
const billPayment = await this.billPaymentModel()
await this.billPaymentModel()
.query(trx)
.upsertGraphAndFetch({
.upsertGraph({
id: billPaymentId,
...billPaymentObj,
});
// Fetch the bill payment with entries to ensure they're loaded for the subscriber.
const billPayment = await this.billPaymentModel()
.query(trx)
.withGraphFetched('entries')
.findById(billPaymentId)
.throwIfNotFound();
// Triggers `onBillPaymentEdited` event.
await this.eventPublisher.emitAsync(events.billPayment.onEdited, {
billPaymentId,

View File

@@ -0,0 +1,80 @@
import {
IBillPaymentEventCreatedPayload,
IBillPaymentEventDeletedPayload,
IBillPaymentEventEditedPayload,
} from '../types/BillPayments.types';
import { BillPaymentBillSync } from '../commands/BillPaymentBillSync.service';
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
@Injectable()
export class BillPaymentBillSyncSubscriber {
/**
* @param {BillPaymentBillSync} billPaymentBillSync - Bill payment bill sync service.
*/
constructor(private readonly billPaymentBillSync: BillPaymentBillSync) { }
/**
* Handle bill increment/decrement payment amount
* once created, edited or deleted.
*/
@OnEvent(events.billPayment.onCreated)
async handleBillIncrementPaymentOnceCreated({
billPayment,
trx,
}: IBillPaymentEventCreatedPayload) {
// Ensure entries are available - they should be included in insertGraphAndFetch
const entries = billPayment.entries || [];
await this.billPaymentBillSync.saveChangeBillsPaymentAmount(
entries.map((entry) => ({
billId: entry.billId,
paymentAmount: entry.paymentAmount,
})),
null,
trx,
);
}
/**
* Handle bill increment/decrement payment amount once edited.
*/
@OnEvent(events.billPayment.onEdited)
async handleBillIncrementPaymentOnceEdited({
billPayment,
oldBillPayment,
trx,
}: IBillPaymentEventEditedPayload) {
const entries = billPayment.entries || [];
const oldEntries = oldBillPayment?.entries || null;
await this.billPaymentBillSync.saveChangeBillsPaymentAmount(
entries.map((entry) => ({
billId: entry.billId,
paymentAmount: entry.paymentAmount,
})),
oldEntries,
trx,
);
}
/**
* Handle revert bills payment amount once bill payment deleted.
*/
@OnEvent(events.billPayment.onDeleted)
async handleBillDecrementPaymentAmount({
oldBillPayment,
trx,
}: IBillPaymentEventDeletedPayload) {
const oldEntries = oldBillPayment.entries || [];
await this.billPaymentBillSync.saveChangeBillsPaymentAmount(
oldEntries.map((entry) => ({
billId: entry.billId,
paymentAmount: 0,
})),
oldEntries,
trx,
);
}
}

View File

@@ -10,6 +10,7 @@ import {
import { FinancialSheet } from '../../common/FinancialSheet';
import { JournalSheetRepository } from './JournalSheetRepository';
import { ILedgerEntry } from '@/modules/Ledger/types/Ledger.types';
import { getTransactionTypeLabel } from '@/modules/BankingTransactions/utils';
export class JournalSheet extends FinancialSheet {
readonly query: IJournalReportQuery;
@@ -97,10 +98,13 @@ export class JournalSheet extends FinancialSheet {
transactionType: groupEntry.transactionType,
referenceId: groupEntry.transactionId,
referenceTypeFormatted: this.i18n.t(groupEntry.transactionType),
referenceTypeFormatted: this.i18n.t(
getTransactionTypeLabel(
groupEntry.transactionType,
groupEntry.transactionSubType,
),
),
entries: this.entriesMapper(entriesGroup),
currencyCode: this.baseCurrency,
credit: totalCredit,

View File

@@ -6,6 +6,7 @@ import { SaleInvoice } from '@/modules/SaleInvoices/models/SaleInvoice';
import { SaleReceipt } from '@/modules/SaleReceipts/models/SaleReceipt';
import { Item } from '@/modules/Items/models/Item';
import { BaseModel } from '@/models/Model';
import { ItemEntry } from '@/modules/TransactionItemEntry/models/ItemEntry';
export class InventoryCostLotTracker extends BaseModel {
date: Date;
@@ -27,6 +28,7 @@ export class InventoryCostLotTracker extends BaseModel {
warehouseId: number;
item?: Item;
itemEntry?: ItemEntry;
invoice?: SaleInvoice;
receipt?: SaleReceipt;

View File

@@ -20,7 +20,7 @@ export class InventoryCostSubscriber {
private readonly itemsQuantitySync: InventoryItemsQuantitySyncService,
private readonly inventoryService: InventoryComputeCostService,
private readonly importAls: ImportAls,
) {}
) { }
/**
* Sync inventory items quantity once inventory transactions created.
@@ -60,7 +60,7 @@ export class InventoryCostSubscriber {
* Marks items cost compute running state.
*/
@OnEvent(events.inventory.onInventoryTransactionsCreated)
async markGlobalSettingsComputeItems({}) {
async markGlobalSettingsComputeItems({ }) {
await this.inventoryService.markItemsCostComputeRunning(true);
}
@@ -68,7 +68,7 @@ export class InventoryCostSubscriber {
* Marks items cost compute as completed.
*/
@OnEvent(events.inventory.onInventoryCostEntriesWritten)
async markGlobalSettingsComputeItemsCompeted({}) {
async markGlobalSettingsComputeItemsCompeted({ }) {
await this.inventoryService.markItemsCostComputeRunning(false);
}
@@ -80,15 +80,13 @@ export class InventoryCostSubscriber {
itemId,
startingDate,
}: IComputeItemCostJobCompletedPayload) {
// const dependsComputeJobs = await this.agenda.jobs({
// name: 'compute-item-cost',
// nextRunAt: { $ne: null },
// 'data.tenantId': tenantId,
// });
// // There is no scheduled compute jobs waiting.
// if (dependsComputeJobs.length === 0) {
// await this.saleInvoicesCost.scheduleWriteJournalEntries(startingDate);
// }
// Convert startingDate to Date if it's a string
const startingDateObj = startingDate instanceof Date
? startingDate
: new Date(startingDate);
// Write GL entries for inventory cost lots after cost computation completes
await this.saleInvoicesCost.writeCostLotsGLEntries(startingDateObj);
}
/**

View File

@@ -38,7 +38,8 @@ export class SaleInvoiceCostGLEntries {
.modify('filterDateRange', startingDate)
.orderBy('date', 'ASC')
.withGraphFetched('invoice')
.withGraphFetched('item');
.withGraphFetched('item')
.withGraphFetched('itemEntry');
const ledger = this.getInventoryCostLotsLedger(inventoryCostLotTrans);
@@ -79,6 +80,9 @@ export class SaleInvoiceCostGLEntries {
transactionType: inventoryCostLot.transactionType,
transactionId: inventoryCostLot.transactionId,
transactionNumber: inventoryCostLot.invoice.invoiceNo,
referenceNumber: inventoryCostLot.invoice.referenceNo,
date: inventoryCostLot.date,
indexGroup: 20,
costable: true,
@@ -105,6 +109,9 @@ export class SaleInvoiceCostGLEntries {
const costAccountId =
inventoryCostLot.costAccountId || inventoryCostLot.item.costAccountId;
// Get description from item entry if available
const description = inventoryCostLot.itemEntry?.description || null;
// XXX Debit - Cost account.
const costEntry = {
...commonEntry,
@@ -112,6 +119,7 @@ export class SaleInvoiceCostGLEntries {
accountId: costAccountId,
accountNormal: AccountNormal.DEBIT,
itemId: inventoryCostLot.itemId,
note: description,
index: getIndexIncrement(),
};
// XXX Credit - Inventory account.
@@ -121,6 +129,7 @@ export class SaleInvoiceCostGLEntries {
accountId: inventoryCostLot.item.inventoryAccountId,
accountNormal: AccountNormal.DEBIT,
itemId: inventoryCostLot.itemId,
note: description,
index: getIndexIncrement(),
};
return [costEntry, inventoryEntry];

View File

@@ -1,15 +1,18 @@
import { Injectable } from '@nestjs/common';
import { OnEvent } from '@nestjs/event-emitter';
import { events } from '@/common/events/events';
import { IInventoryCostLotsGLEntriesWriteEvent } from '@/modules/InventoryCost/types/InventoryCost.types';
import { SaleInvoiceCostGLEntries } from '../SaleInvoiceCostGLEntries';
@Injectable()
export class InvoiceCostGLEntriesSubscriber {
constructor(private readonly invoiceCostEntries: SaleInvoiceCostGLEntries) {}
constructor(private readonly invoiceCostEntries: SaleInvoiceCostGLEntries) { }
/**
* Writes the invoices cost GL entries once the inventory cost lots be written.
* @param {IInventoryCostLotsGLEntriesWriteEvent}
*/
@OnEvent(events.inventory.onCostLotsGLEntriesWrite)
async writeInvoicesCostEntriesOnCostLotsWritten({
trx,
startingDate,