feat: enhance sales invoice processing with correction and return functionalities

This commit is contained in:
2026-06-15 17:14:54 +03:30
parent 5f70b95589
commit 9170d8cd5a
7 changed files with 141 additions and 152 deletions
@@ -18,7 +18,6 @@ import {
getOriginalResendAttemptNumber,
getRelatedInvoiceForCorrection,
onResult,
trySend,
} from './utils/sales-invoice-tsp.utils'
type ItemTspRow = {
@@ -228,43 +227,9 @@ export class SalesInvoiceTspService {
},
)
const result = await this.runProviderCall(() =>
trySend(this.tspSwitchService, correctionPayload),
)
if (result?.hasError) {
result.provider_request_payload =
result.provider_request_payload || JSON.parse(JSON.stringify(correctionPayload))
result.sent_at = result.sent_at || new Date().toISOString()
result.received_at = result.received_at || new Date().toISOString()
}
const result = await this.tspSwitchService.correction(correctionPayload)
return onResult(this.prisma, result, attempt.id)
// const countByGoodId = (goodIds: string[]): Map<string, number> => {
// const counts = new Map<string, number>()
// for (const goodId of goodIds) {
// counts.set(goodId, (counts.get(goodId) ?? 0) + 1)
// }
// return counts
// }
// const updatedGoodIds = dataToUpdate.items.map(item => item.good_id!)
// const previousGoodIds = lastAttempt.invoice.items.map(item => item.good_id)
// const updatedCounts = countByGoodId(updatedGoodIds)
// const previousCounts = countByGoodId(previousGoodIds)
// let isBackFromSale = false
// if (updatedCounts.size !== previousCounts.size) {
// isBackFromSale = true
// } else {
// for (const [goodId, count] of updatedCounts) {
// if (previousCounts.get(goodId) !== count) {
// isBackFromSale = true
// break
// }
// }
// }
return await onResult(this.prisma, result, newInvoice.id)
}
async revoke(
@@ -317,71 +282,72 @@ export class SalesInvoiceTspService {
)
}
const payments = relatedInvoice.payments.reduce(
(acc, payment) => {
const amount = Number(payment.amount)
switch (payment.payment_method) {
case 'CASH':
acc.cash = (acc.cash || 0) + amount
break
case 'SET_OFF':
acc.set_off = (acc.set_off || 0) + amount
break
case 'CARD':
acc.card = (acc.card || 0) + amount
break
case 'BANK':
acc.bank = (acc.bank || 0) + amount
break
case 'CHEQUE':
acc.check = (acc.check || 0) + amount
break
case 'OTHER':
acc.other = (acc.other || 0) + amount
break
case 'TERMINAL':
acc.terminals = payment.terminal_info
? {
amount,
terminalId: payment.terminal_info.terminal_id,
stan: payment.terminal_info.stan,
rrn: payment.terminal_info.rrn,
customer_card_no:
payment.terminal_info.customer_card_no || undefined,
transaction_date_time: payment.terminal_info.transaction_date_time,
description: payment.terminal_info.description || undefined,
}
: acc.terminals
break
default:
break
}
return acc
},
{} as {
terminals?: {
amount?: number
terminalId: string
stan: string
rrn: string
customer_card_no?: string
transaction_date_time: Date
description?: string
}
cash?: number
set_off?: number
card?: number
bank?: number
check?: number
other?: number
},
)
// const payments = relatedInvoice.payments.reduce(
// (acc, payment) => {
// const amount = Number(payment.amount)
// switch (payment.payment_method) {
// case 'CASH':
// acc.cash = (acc.cash || 0) + amount
// break
// case 'SET_OFF':
// acc.set_off = (acc.set_off || 0) + amount
// break
// case 'CARD':
// acc.card = (acc.card || 0) + amount
// break
// case 'BANK':
// acc.bank = (acc.bank || 0) + amount
// break
// case 'CHEQUE':
// acc.check = (acc.check || 0) + amount
// break
// case 'OTHER':
// acc.other = (acc.other || 0) + amount
// break
// case 'TERMINAL':
// acc.terminals = payment.terminal_info
// ? {
// amount,
// terminal_id: payment.terminal_info.terminal_id,
// stan: payment.terminal_info.stan,
// rrn: payment.terminal_info.rrn,
// customer_card_no:
// payment.terminal_info.customer_card_no || undefined,
// transaction_date_time: payment.terminal_info.transaction_date_time,
// description: payment.terminal_info.description || undefined,
// }
// : acc.terminals
// break
// default:
// break
// }
// return acc
// },
// {} as {
// terminals?: {
// amount?: number
// terminalId: string
// stan: string
// rrn: string
// customer_card_no?: string
// transaction_date_time: Date
// description?: string
// }
// cash?: number
// set_off?: number
// card?: number
// bank?: number
// check?: number
// other?: number
// },
// )
const newInvoice = await this.sharedSaleInvoiceCreateService.create({
data: {
customer_type: relatedInvoice.customer?.type || CustomerType.UNKNOWN,
invoice_date: new Date(),
payments,
// @ts-ignore
payments: undefined,
settlement_type: relatedInvoice.settlement_type,
items: relatedInvoice.items.map(item => ({
unit_price: Number(item.unit_price),