update return from sale invoices jurney and set nama return from sale

This commit is contained in:
2026-06-17 08:50:35 +03:30
parent f87e5b9d8e
commit 826041b07a
17 changed files with 1085 additions and 80 deletions
@@ -9,12 +9,13 @@ import {
} from 'generated/prisma/client'
import {
goldTypePayload,
TspProviderCorrectionSendPayloadDto,
TspProviderGetResponseDto,
TspProviderOriginalResponseDto,
TspProviderOriginalSendPayloadDto,
TspProviderReturnSendPayloadDto,
TspProviderRevokePayloadDto,
} from '../dto'
import { TspProviderCorrectionSendPayloadDto } from '../dto/correction.dto'
import { TspProviderActionResponseDto } from '../dto/provider-switch.dto'
export async function getOriginalResendAttemptNumber(
@@ -160,10 +161,12 @@ export async function buildRevokePayload(
export async function buildCorrectionPayload(
tx: Prisma.TransactionClient,
invoice_id: string,
pos_id: string,
): Promise<TspProviderCorrectionSendPayloadDto> {
const invoice = await tx.salesInvoice.findUnique({
where: {
id: invoice_id,
pos_id,
},
select: {
id: true,
@@ -323,6 +326,217 @@ export async function buildCorrectionPayload(
}
}
export async function buildReturnFromSalePayload(
prisma: PrismaService,
invoiceId: string,
posId: string,
): Promise<TspProviderReturnSendPayloadDto> {
const invoice = await prisma.salesInvoice.findUnique({
where: {
id: invoiceId,
pos_id: posId,
},
select: {
id: true,
code: true,
total_amount: true,
invoice_date: true,
invoice_number: true,
settlement_type: true,
discount_amount: true,
tax_amount: true,
tax_id: true,
items: {
select: {
id: true,
quantity: true,
unit_price: true,
total_amount: true,
measure_unit_code: true,
measure_unit_text: true,
sku_code: true,
sku_vat: true,
good_id: true,
service_id: true,
discount_amount: true,
tax_amount: true,
payload: true,
good_snapshot: true,
},
},
pos: {
select: {
complex: {
select: {
business_activity: {
select: {
fiscal_id: true,
economic_code: true,
partner_token: true,
guild: {
select: {
invoice_template: true,
},
},
consumer: {
select: {
legal: {
select: {
partner: {
select: {
tsp_provider: true,
},
},
},
},
individual: {
select: {
partner: {
select: {
tsp_provider: true,
},
},
},
},
},
},
},
},
},
},
},
},
unknown_customer: true,
customer: {
select: {
type: true,
individual: true,
legal: true,
},
},
payments: {
select: {
amount: true,
payment_method: true,
paid_at: true,
terminal_info: {
select: {
stan: true,
rrn: true,
transaction_date_time: true,
customer_card_no: true,
terminal_id: true,
},
},
},
},
reference_invoice: {
select: {
tax_id: true,
},
},
},
})
if (!invoice) {
throw new NotFoundException('صورت‌حساب مورد نظر یافت نشد.')
}
if (!invoice.reference_invoice) {
throw new NotFoundException('صورت‌حساب مرجع برای برگشت از فروش یافت نشد.')
}
const {
pos,
id,
invoice_number,
invoice_date,
total_amount,
discount_amount,
tax_amount,
settlement_type,
} = invoice
const { business_activity: ba } = pos.complex
const unknown_customer = (invoice.unknown_customer || {}) as Record<string, string>
const { partner } = (ba.consumer.legal || ba.consumer.individual)!
return {
id,
invoice_number,
invoice_date,
settlement_type,
total_amount: Number(total_amount),
tax_amount: Number(tax_amount),
discount_amount: Number(discount_amount),
economic_code: ba.economic_code,
fiscal_id: ba.fiscal_id,
template: ba.guild.invoice_template,
token: ba.partner_token,
tsp_provider: partner.tsp_provider!,
last_tax_id: invoice.reference_invoice.tax_id!,
payments: invoice.payments.map(payment => ({
amount: Number(payment.amount),
payment_method: payment.payment_method,
paid_at: payment.paid_at,
terminal_info: {
card_number: payment.terminal_info?.customer_card_no
? payment.terminal_info.customer_card_no
: undefined,
tracking_code: payment.terminal_info ? payment.terminal_info.rrn : undefined,
},
})),
customer:
invoice.customer && invoice.customer.type !== CustomerType.UNKNOWN
? {
type: invoice.customer.type,
legal_info: invoice.customer.legal
? {
name: invoice.customer.legal.name ?? undefined,
registration_number:
invoice.customer.legal.registration_number ?? undefined,
postal_code: invoice.customer.legal.postal_code ?? undefined,
economic_code: invoice.customer.legal.economic_code ?? undefined,
}
: undefined,
individual_info: invoice.customer.individual
? {
first_name: invoice.customer.individual.first_name ?? undefined,
last_name: invoice.customer.individual.last_name ?? undefined,
national_id: invoice.customer.individual.national_id ?? undefined,
mobile_number: invoice.customer.individual.mobile_number ?? undefined,
postal_code: invoice.customer.individual.postal_code ?? undefined,
economic_code: invoice.customer.individual.economic_code ?? undefined,
}
: undefined,
}
: {
type: CustomerType.UNKNOWN,
unknown_info: {
name: unknown_customer?.name || '',
economic_code: unknown_customer?.economic_code || '',
},
},
items: invoice.items.map(item => ({
invoice_item_id: item.id,
quantity: Number(item.quantity),
unit_price: Number(item.unit_price),
total_amount: Number(item.total_amount),
tax_amount: Number(tax_amount),
discount_amount: Number(discount_amount),
measure_unit: item.measure_unit_code,
sku: item.sku_code,
sku_vat: String(item.sku_vat),
discount: String((item.payload as Record<string, any>)?.discount || '0'),
good_id: item.good_id,
service_id: item.service_id,
good_snapshot: item.good_snapshot,
gold_type_payload: isGoldTypePayload(item.payload) ? item.payload : undefined,
})),
}
}
export async function buildOriginalPayload(
prisma: PrismaService,
invoiceId: string,