fda190f902
- Removed old DTOs for creating and updating purchase receipts. - Updated purchase receipts controller and service to use new DTOs and workflows. - Introduced transaction helper for managing database transactions. - Added new workflows for handling purchase receipt payments and supplier ledgers. - Implemented new logic for managing purchase receipt items and payments. - Enhanced error handling for payment processing in workflows. - Updated supplier ledger management to reflect changes in purchase receipts.
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { Injectable } from '@nestjs/common'
|
|
import { Prisma } from '../../../generated/prisma/client'
|
|
import { LedgerSourceType } from '../../../generated/prisma/enums'
|
|
|
|
@Injectable()
|
|
export class SupplierLedgerWorkflow {
|
|
async updateLedgerBalance(
|
|
tx: Prisma.TransactionClient,
|
|
supplierId: number,
|
|
amount: number,
|
|
sourceId: number,
|
|
sourceType: LedgerSourceType,
|
|
) {
|
|
const lastSupplierLedgerBalance = await tx.supplierLedger
|
|
.findFirstOrThrow({
|
|
where: { supplierId },
|
|
orderBy: { createdAt: 'desc' },
|
|
})
|
|
.then(sl => (sl ? sl.balance : 0))
|
|
|
|
let newBalance = Number(lastSupplierLedgerBalance)
|
|
let credit = 0
|
|
let debit = 0
|
|
let description = ''
|
|
|
|
switch (sourceType) {
|
|
case 'PURCHASE':
|
|
newBalance += amount
|
|
credit = amount
|
|
description = `خرید به مبلغ ${amount} بابت خرید شماره ${sourceId}`
|
|
break
|
|
case 'REFUND':
|
|
case 'PAYMENT':
|
|
newBalance -= amount
|
|
debit = amount
|
|
description = `پرداخت به مبلغ ${amount} بابت پرداخت شماره ${sourceId}`
|
|
break
|
|
case 'ADJUSTMENT':
|
|
newBalance = amount
|
|
description = `اصلاح حساب به مبلغ ${amount} بابت پرداخت شماره ${sourceId}`
|
|
|
|
break
|
|
}
|
|
|
|
return await tx.supplierLedger.create({
|
|
data: {
|
|
supplierId,
|
|
debit,
|
|
credit,
|
|
balance: newBalance,
|
|
sourceId,
|
|
sourceType,
|
|
description,
|
|
},
|
|
})
|
|
}
|
|
}
|