update switch providers and nama provider. fix original send
This commit is contained in:
@@ -41,13 +41,17 @@ export class SalesInvoicesService {
|
||||
await tx.salesInvoice.count({ where }),
|
||||
])
|
||||
|
||||
const summaryItems = items.map(invoice => ({
|
||||
...invoice,
|
||||
status: translateEnumValue(
|
||||
'TspProviderResponseStatus',
|
||||
invoice.tsp_attempts?.[0]?.status || TspProviderResponseStatus.NOT_SEND,
|
||||
),
|
||||
}))
|
||||
const summaryItems = items.map(invoice => {
|
||||
const { tsp_attempts, type, ...rest } = invoice
|
||||
return {
|
||||
...rest,
|
||||
type: translateEnumValue('TspProviderRequestType', type),
|
||||
status: translateEnumValue(
|
||||
'TspProviderResponseStatus',
|
||||
invoice.tsp_attempts?.[0]?.status || TspProviderResponseStatus.NOT_SEND,
|
||||
),
|
||||
}
|
||||
})
|
||||
|
||||
return ResponseMapper.paginate(summaryItems, { total, page, perPage })
|
||||
}
|
||||
@@ -69,15 +73,17 @@ export class SalesInvoicesService {
|
||||
})
|
||||
|
||||
if (invoice) {
|
||||
const { tsp_attempts, ...rest } = invoice || {}
|
||||
|
||||
return ResponseMapper.single({
|
||||
const { tsp_attempts, type, ...rest } = invoice
|
||||
const mappedInvoice = {
|
||||
...rest,
|
||||
type: translateEnumValue('TspProviderRequestType', type),
|
||||
status: translateEnumValue(
|
||||
'TspProviderResponseStatus',
|
||||
invoice.tsp_attempts?.[0]?.status || TspProviderResponseStatus.NOT_SEND,
|
||||
),
|
||||
})
|
||||
}
|
||||
|
||||
return ResponseMapper.single(mappedInvoice)
|
||||
} else throw new NotFoundException('فاکتور مورد نظر شما یافت نشد.')
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
import { ResponseMapper } from '@/common/response/response-mapper'
|
||||
import { getCurrentJalaliSeasonRange } from '@/common/utils'
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import {
|
||||
TspProviderRequestType,
|
||||
TspProviderResponseStatus,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { Injectable } from '@nestjs/common'
|
||||
|
||||
@@ -23,12 +26,13 @@ export class StatisticsService {
|
||||
pending_count: number | null
|
||||
not_sended_amount_sum: number | null
|
||||
not_sended_count: number | null
|
||||
credit_amount_sum: number | null
|
||||
credit_count: number | null
|
||||
not_original_amount_sum: number | null
|
||||
not_original_count: number | null
|
||||
}>
|
||||
>`
|
||||
SELECT
|
||||
SUM(si.total_amount) AS all_amount_sum,
|
||||
si.type,
|
||||
COUNT(*) AS all_count,
|
||||
SUM(CASE WHEN la.status = ${TspProviderResponseStatus.SUCCESS} THEN si.total_amount ELSE 0 END) AS success_amount_sum,
|
||||
SUM(CASE WHEN la.status = ${TspProviderResponseStatus.SUCCESS} THEN 1 ELSE 0 END) AS success_count,
|
||||
@@ -38,8 +42,8 @@ export class StatisticsService {
|
||||
SUM(CASE WHEN la.status = ${TspProviderResponseStatus.QUEUED} THEN 1 ELSE 0 END) AS pending_count,
|
||||
SUM(CASE WHEN la.status IS NULL THEN si.total_amount ELSE 0 END) AS not_sended_amount_sum,
|
||||
SUM(CASE WHEN la.status IS NULL THEN 1 ELSE 0 END) AS not_sended_count,
|
||||
SUM(CASE WHEN si.settlement_type = 'CREDIT' THEN si.total_amount ELSE 0 END) AS credit_amount_sum,
|
||||
SUM(CASE WHEN si.settlement_type = 'CREDIT' THEN 1 ELSE 0 END) AS credit_count
|
||||
SUM(CASE WHEN si.type <> ${TspProviderRequestType.ORIGINAL} THEN si.total_amount ELSE 0 END) AS not_original_amount_sum,
|
||||
SUM(CASE WHEN si.type <> ${TspProviderRequestType.ORIGINAL} THEN 1 ELSE 0 END) AS not_original_count
|
||||
FROM sales_invoices si
|
||||
INNER JOIN poses p ON p.id = si.pos_id
|
||||
LEFT JOIN (
|
||||
@@ -64,6 +68,7 @@ export class StatisticsService {
|
||||
FROM sales_invoices child
|
||||
WHERE child.ref_id = si.id
|
||||
)
|
||||
GROUP BY si.type
|
||||
`
|
||||
|
||||
return ResponseMapper.single({
|
||||
@@ -87,15 +92,15 @@ export class StatisticsService {
|
||||
total_tax: Number(0),
|
||||
count: Number(item?.pending_count || 0),
|
||||
},
|
||||
notSended: {
|
||||
not_sended: {
|
||||
total_amount: Number(item?.not_sended_amount_sum || 0),
|
||||
total_tax: Number(0),
|
||||
count: Number(item?.not_sended_count || 0),
|
||||
},
|
||||
credit: {
|
||||
total_amount: Number(item?.credit_amount_sum || 0),
|
||||
not_original: {
|
||||
total_amount: Number(item?.not_original_amount_sum || 0),
|
||||
total_tax: Number(0),
|
||||
count: Number(item?.credit_count || 0),
|
||||
count: Number(item?.not_original_count || 0),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import { CustomerType, PaymentMethodType } from '@/generated/prisma/enums'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import {
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
|
||||
export class CustomerUnknownInfoDto {
|
||||
@ApiProperty({})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
name: string
|
||||
|
||||
@ApiProperty({})
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
economic_code: string
|
||||
}
|
||||
|
||||
export class CustomerLegalInfoDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
economic_code: string
|
||||
}
|
||||
|
||||
export class CustomerIndividualInfoDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
first_name: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
last_name: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
national_id: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
mobile_number: string
|
||||
}
|
||||
|
||||
export class CustomerInfoDto {
|
||||
@ApiProperty({ required: true, enum: CustomerType })
|
||||
@IsEnum(CustomerType)
|
||||
type: CustomerType
|
||||
|
||||
@ApiProperty({})
|
||||
@Type(() => CustomerLegalInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
legal_info?: CustomerLegalInfoDto
|
||||
|
||||
@ApiProperty({})
|
||||
@Type(() => CustomerIndividualInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
individual_info?: CustomerIndividualInfoDto
|
||||
|
||||
@ApiProperty({})
|
||||
@Type(() => CustomerUnknownInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
unknown_info?: CustomerUnknownInfoDto
|
||||
}
|
||||
|
||||
export class TerminalInfoDto {
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tracking_code?: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
card_number?: string
|
||||
}
|
||||
|
||||
export class PaymentInfoDto {
|
||||
@ApiProperty({ required: true, enum: PaymentMethodType })
|
||||
@IsEnum(PaymentMethodType)
|
||||
payment_method: PaymentMethodType
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsNumber({ allowInfinity: false, allowNaN: false })
|
||||
@Min(0)
|
||||
amount: number
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
paid_at?: Date
|
||||
|
||||
@ApiProperty({ type: TerminalInfoDto })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
terminal_info: TerminalInfoDto
|
||||
}
|
||||
|
||||
export class goldTypePayload {
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
karat: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
profit: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
wages: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
commission: string
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import {
|
||||
SharedCreateSalesInvoiceItemDto,
|
||||
SharedCreateSalesInvoicePaymentsDto,
|
||||
} from '@/common/services/saleInvoices/sale-invoice-create.dto'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import {
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
import { TspProviderOriginalSendPayloadDto } from './original.dto'
|
||||
|
||||
export class TspProviderCorrectionInvoicePayloadDto {
|
||||
@ApiProperty({ type: [SharedCreateSalesInvoiceItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => SharedCreateSalesInvoiceItemDto)
|
||||
@ArrayMinSize(1)
|
||||
items: SharedCreateSalesInvoiceItemDto[]
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty({ required: true, type: SharedCreateSalesInvoicePaymentsDto })
|
||||
@IsObject()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => SharedCreateSalesInvoicePaymentsDto)
|
||||
payments: SharedCreateSalesInvoicePaymentsDto
|
||||
}
|
||||
export class TspProviderCorrectionSendPayloadDto extends TspProviderOriginalSendPayloadDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
last_tax_id: string
|
||||
}
|
||||
|
||||
export class TspProviderCorrectionSendResponseDto {}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import {
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator'
|
||||
|
||||
export class TspProviderGetResultDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsBoolean()
|
||||
hasError: boolean
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
message?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_response_payload?: Object
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
sent_at: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
received_at: string
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
export * from './common.dto'
|
||||
export * from './correction.dto'
|
||||
export * from './get.dto'
|
||||
export * from './original.dto'
|
||||
export * from './provider-switch.dto'
|
||||
export * from './revoke.dto'
|
||||
@@ -0,0 +1,193 @@
|
||||
import {
|
||||
InvoiceSettlementType,
|
||||
InvoiceTemplateType,
|
||||
TspProviderResponseStatus,
|
||||
TspProviderType,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import {
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
import { CustomerInfoDto, goldTypePayload, PaymentInfoDto } from './common.dto'
|
||||
|
||||
export class TspProviderOriginalItemPayloadDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
sku: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
sku_vat: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsNumber()
|
||||
unit_price: number
|
||||
|
||||
// @ApiProperty()
|
||||
// @IsString()
|
||||
// invoice_item_id: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
quantity: number
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
discount: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
measure_unit: string
|
||||
|
||||
@ApiProperty({ nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
good_id?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
service_id?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: goldTypePayload })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
gold_type_payload?: goldTypePayload
|
||||
|
||||
// @ApiProperty({ required: false, nullable: true, type: Object })
|
||||
// @IsOptional()
|
||||
// @IsObject()
|
||||
// payload?: unknown
|
||||
}
|
||||
|
||||
export class TspProviderOriginalSendPayloadDto {
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
id: string
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsNumber()
|
||||
invoice_number: number
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsDateString()
|
||||
invoice_date: Date
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
|
||||
@ApiProperty({ required: true, enum: TspProviderType })
|
||||
@IsEnum(TspProviderType)
|
||||
tsp_provider: TspProviderType
|
||||
|
||||
@ApiProperty({ required: true, enum: InvoiceTemplateType })
|
||||
@IsEnum(InvoiceTemplateType)
|
||||
template: InvoiceTemplateType
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
token: string
|
||||
|
||||
@ApiProperty({ required: true, enum: InvoiceSettlementType })
|
||||
@IsEnum(InvoiceSettlementType)
|
||||
settlement_type: InvoiceSettlementType
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty({ type: [TspProviderOriginalItemPayloadDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TspProviderOriginalItemPayloadDto)
|
||||
items: TspProviderOriginalItemPayloadDto[]
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@Type(() => CustomerInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
customer?: CustomerInfoDto
|
||||
|
||||
@ApiProperty({ required: true, type: [PaymentInfoDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => PaymentInfoDto)
|
||||
payments: PaymentInfoDto[]
|
||||
}
|
||||
|
||||
export class TspProviderOriginalSendItemResultDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tax_id?: string | null
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
nullable: true,
|
||||
type: TspProviderOriginalSendPayloadDto,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TspProviderOriginalSendPayloadDto)
|
||||
provider_request_payload: TspProviderOriginalSendPayloadDto
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_response?: Object
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsBoolean()
|
||||
hasError: boolean
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
message?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
error_message?: string
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
fiscal_warnings?: Object
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
validation_errors?: Object
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
received_at: string
|
||||
}
|
||||
@@ -1,412 +1,30 @@
|
||||
import { TspProviderCustomerType } from '@/common/enums/enums'
|
||||
import {
|
||||
SharedCreateSalesInvoiceItemDto,
|
||||
SharedCreateSalesInvoicePaymentsDto,
|
||||
} from '@/common/services/saleInvoices/sale-invoice-create.dto'
|
||||
import {
|
||||
CustomerType,
|
||||
InvoiceSettlementType,
|
||||
InvoiceTemplateType,
|
||||
PaymentMethodType,
|
||||
TspProviderRequestType,
|
||||
TspProviderResponseStatus,
|
||||
TspProviderType,
|
||||
} from '@/generated/prisma/enums'
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import { IsArray, IsEnum, IsObject, IsString, ValidateNested } from 'class-validator'
|
||||
import {
|
||||
ArrayMinSize,
|
||||
IsArray,
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
|
||||
export class CustomerLegalInfoDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
economic_code: string
|
||||
}
|
||||
|
||||
export class CustomerIndividualInfoDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
first_name: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
last_name: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
national_id: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
mobile_number: string
|
||||
}
|
||||
|
||||
export class CustomerInfoDto {
|
||||
@ApiProperty({ required: true, enum: CustomerType })
|
||||
@IsEnum(CustomerType)
|
||||
type: CustomerType
|
||||
|
||||
@ApiProperty({})
|
||||
@Type(() => CustomerLegalInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
legal_info?: CustomerLegalInfoDto
|
||||
|
||||
@ApiProperty({})
|
||||
@Type(() => CustomerIndividualInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
individual_info?: CustomerIndividualInfoDto
|
||||
}
|
||||
|
||||
export class PaymentInfoDto {
|
||||
@ApiProperty({ required: true, enum: PaymentMethodType })
|
||||
@IsEnum(PaymentMethodType)
|
||||
payment_method: PaymentMethodType
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsNumber({ allowInfinity: false, allowNaN: false })
|
||||
@Min(10_000)
|
||||
amount: number
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tracking_code?: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
card_number?: string
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsDateString()
|
||||
paid_at?: Date
|
||||
}
|
||||
|
||||
export class TspProviderSendItemPayloadDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
sku: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
sku_vat: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
discount: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsNumber()
|
||||
unit_price: number
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
invoice_item_id: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
quantity: number
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
measure_unit: string
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
good_id?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
service_id?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
payload?: unknown
|
||||
}
|
||||
|
||||
export class TspProviderOriginalSendPayloadDto {
|
||||
@ApiProperty({ required: true, enum: TspProviderRequestType })
|
||||
@IsEnum(TspProviderRequestType)
|
||||
type: TspProviderRequestType
|
||||
|
||||
@ApiProperty({ required: true, enum: TspProviderCustomerType })
|
||||
@IsEnum(TspProviderCustomerType)
|
||||
customer_type: TspProviderCustomerType
|
||||
|
||||
@ApiProperty({ type: [TspProviderSendItemPayloadDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TspProviderSendItemPayloadDto)
|
||||
items: TspProviderSendItemPayloadDto[]
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@Type(() => CustomerInfoDto)
|
||||
@ValidateNested({ each: true })
|
||||
customer?: CustomerInfoDto
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsNumber()
|
||||
invoice_number: number
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsDateString()
|
||||
invoice_date: Date
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty({ required: true, enum: TspProviderType })
|
||||
@IsEnum(TspProviderType)
|
||||
tsp_provider: TspProviderType
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
|
||||
@ApiProperty({ required: true, type: [PaymentInfoDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => PaymentInfoDto)
|
||||
payments: PaymentInfoDto[]
|
||||
|
||||
@ApiProperty({ required: true, enum: InvoiceTemplateType })
|
||||
@IsEnum(InvoiceTemplateType)
|
||||
invoice_template: InvoiceTemplateType
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
tsp_token: string
|
||||
|
||||
@ApiProperty({ required: true, enum: InvoiceSettlementType })
|
||||
@IsEnum(InvoiceSettlementType)
|
||||
settlement_type: InvoiceSettlementType
|
||||
}
|
||||
|
||||
export class TspProviderSendItemResultDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tax_id?: string | null
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
nullable: true,
|
||||
type: TspProviderOriginalSendPayloadDto,
|
||||
})
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_request_payload: Object
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_response_payload?: Object
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsBoolean()
|
||||
hasError: boolean
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
message?: string | null
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
sent_at: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
received_at: string
|
||||
}
|
||||
TspProviderCorrectionSendPayloadDto,
|
||||
TspProviderCorrectionSendResponseDto,
|
||||
} from './correction.dto'
|
||||
import { TspProviderGetResultDto } from './get.dto'
|
||||
import {
|
||||
TspProviderOriginalSendItemResultDto,
|
||||
TspProviderOriginalSendPayloadDto,
|
||||
} from './original.dto'
|
||||
import { TspProviderRevokePayloadDto, TspProviderRevokeResponseDto } from './revoke.dto'
|
||||
|
||||
export class TspProviderBulkSendResultDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ type: [TspProviderSendItemResultDto] })
|
||||
@ApiProperty({ type: [TspProviderOriginalSendItemResultDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => TspProviderSendItemResultDto)
|
||||
items: TspProviderSendItemResultDto[]
|
||||
@Type(() => TspProviderOriginalSendItemResultDto)
|
||||
items: TspProviderOriginalSendItemResultDto[]
|
||||
}
|
||||
|
||||
///////////// GET RELATED DTOs /////////////
|
||||
|
||||
export class TspProviderGetResultDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsBoolean()
|
||||
hasError: boolean
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
message?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_response_payload?: Object
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
sent_at: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
received_at: string
|
||||
}
|
||||
|
||||
///////////// REVOKE RELATED DTOs /////////////
|
||||
|
||||
export class TspProviderRevokePayloadDto {
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsNumber()
|
||||
invoice_number: number
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ required: true, enum: TspProviderType })
|
||||
@IsEnum(TspProviderType)
|
||||
tsp_provider: TspProviderType
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
tsp_token: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
last_tax_id: string
|
||||
}
|
||||
|
||||
export class TspProviderRevokeResponseDto {
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsBoolean()
|
||||
hasError: boolean
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
message?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_request_payload?: unknown
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_response_payload?: unknown
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
sent_at: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
received_at: string
|
||||
}
|
||||
|
||||
///////////// CORRECTION RELATED DTOs /////////////
|
||||
export class TspProviderCorrectionInvoicePayloadDto {
|
||||
@ApiProperty({ type: [SharedCreateSalesInvoiceItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => SharedCreateSalesInvoiceItemDto)
|
||||
@ArrayMinSize(1)
|
||||
items: SharedCreateSalesInvoiceItemDto[]
|
||||
|
||||
@ApiProperty()
|
||||
@IsNumber()
|
||||
total_amount: number
|
||||
|
||||
@ApiProperty({ required: true, type: SharedCreateSalesInvoicePaymentsDto })
|
||||
@IsObject()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => SharedCreateSalesInvoicePaymentsDto)
|
||||
payments: SharedCreateSalesInvoicePaymentsDto
|
||||
}
|
||||
export class TspProviderCorrectionSendPayloadDto extends TspProviderOriginalSendPayloadDto {
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
last_tax_id: string
|
||||
}
|
||||
|
||||
export class TspProviderCorrectionSendResponseDto {}
|
||||
|
||||
export class TspProviderActionResponseDto {
|
||||
@IsObject()
|
||||
invoice: object
|
||||
@@ -422,7 +40,7 @@ export interface IProviderSwitchAdapter {
|
||||
readonly code: string
|
||||
originalSend(
|
||||
payload: TspProviderOriginalSendPayloadDto,
|
||||
): Promise<TspProviderSendItemResultDto>
|
||||
): Promise<TspProviderOriginalSendItemResultDto>
|
||||
sendBulk(
|
||||
payloads: TspProviderOriginalSendPayloadDto[],
|
||||
): Promise<TspProviderBulkSendResultDto[]>
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
import { TspProviderResponseStatus, TspProviderType } from '@/generated/prisma/enums'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import {
|
||||
IsBoolean,
|
||||
IsDateString,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsObject,
|
||||
IsOptional,
|
||||
IsString,
|
||||
} from 'class-validator'
|
||||
|
||||
export class TspProviderRevokePayloadDto {
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsNumber()
|
||||
invoice_number: number
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ required: true, enum: TspProviderType })
|
||||
@IsEnum(TspProviderType)
|
||||
tsp_provider: TspProviderType
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
tsp_token: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
last_tax_id: string
|
||||
}
|
||||
|
||||
export class TspProviderRevokeResponseDto {
|
||||
@ApiProperty({ required: true, nullable: true })
|
||||
@IsString()
|
||||
invoice_id: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsBoolean()
|
||||
hasError: boolean
|
||||
|
||||
@ApiProperty({ required: false, nullable: true })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
message?: string | null
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_request_payload?: unknown
|
||||
|
||||
@ApiProperty({ required: false, nullable: true, type: Object })
|
||||
@IsOptional()
|
||||
@IsObject()
|
||||
provider_response_payload?: unknown
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
sent_at: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsDateString()
|
||||
received_at: string
|
||||
}
|
||||
@@ -4,11 +4,11 @@ import {
|
||||
TspProviderCorrectionSendPayloadDto,
|
||||
TspProviderCorrectionSendResponseDto,
|
||||
TspProviderGetResultDto,
|
||||
TspProviderOriginalSendItemResultDto,
|
||||
TspProviderOriginalSendPayloadDto,
|
||||
TspProviderRevokePayloadDto,
|
||||
TspProviderRevokeResponseDto,
|
||||
TspProviderSendItemResultDto,
|
||||
} from './dto/provider-switch.dto'
|
||||
} from './dto'
|
||||
import { NamaProviderSwitchAdapter } from './switch/nama/nama-provider.adapter'
|
||||
|
||||
@Injectable()
|
||||
@@ -34,7 +34,7 @@ export class SalesInvoiceTspSwitchService {
|
||||
|
||||
async send(
|
||||
payload: TspProviderOriginalSendPayloadDto,
|
||||
): Promise<TspProviderSendItemResultDto> {
|
||||
): Promise<TspProviderOriginalSendItemResultDto> {
|
||||
const adapter = this.resolveSwitch(payload.tsp_provider)
|
||||
|
||||
return adapter.originalSend(payload)
|
||||
|
||||
@@ -9,11 +9,11 @@ import {
|
||||
import {
|
||||
TspProviderActionResponseDto,
|
||||
TspProviderCorrectionInvoicePayloadDto,
|
||||
} from './dto/provider-switch.dto'
|
||||
} from './dto'
|
||||
import { SalesInvoiceTspSwitchService } from './sales-invoice-tsp-switch.service'
|
||||
import {
|
||||
buildCorrectionPayload,
|
||||
buildPayload,
|
||||
buildOriginalPayload,
|
||||
buildRevokePayload,
|
||||
getOriginalResendAttemptNumber,
|
||||
onResult,
|
||||
@@ -33,81 +33,27 @@ export class SalesInvoiceTspService {
|
||||
private sharedSaleInvoiceCreateService: SharedSaleInvoiceCreateService,
|
||||
) {}
|
||||
|
||||
private mapProviderError(error: any) {
|
||||
const isProviderFailureObject =
|
||||
error &&
|
||||
typeof error === 'object' &&
|
||||
('provider_request_payload' in error ||
|
||||
'provider_response_payload' in error ||
|
||||
'sent_at' in error ||
|
||||
'received_at' in error)
|
||||
|
||||
if (isProviderFailureObject) {
|
||||
return {
|
||||
hasError: true,
|
||||
status: error.status || TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
error.message?.toString() ||
|
||||
'خطا در ارتباط با سامانه مالیاتی. لطفا مجددا تلاش کنید.',
|
||||
provider_request_payload: error.provider_request_payload,
|
||||
provider_response_payload: error.provider_response_payload,
|
||||
sent_at: error.sent_at || new Date().toISOString(),
|
||||
received_at: error.received_at || new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
hasError: true,
|
||||
status: TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
error?.message?.toString() ||
|
||||
'خطا در ارتباط با سامانه مالیاتی. لطفا مجددا تلاش کنید.',
|
||||
provider_response_payload: {
|
||||
error: error?.message || 'fetch failed',
|
||||
cause: error?.cause || null,
|
||||
},
|
||||
sent_at: new Date().toISOString(),
|
||||
received_at: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
private async runProviderCall(fn: () => Promise<any>) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (error: any) {
|
||||
return this.mapProviderError(error)
|
||||
}
|
||||
}
|
||||
|
||||
async originalSend(
|
||||
posId: string,
|
||||
invoice_id: string,
|
||||
): Promise<TspProviderActionResponseDto> {
|
||||
const payload = await buildPayload(this.prisma, invoice_id, posId)
|
||||
const payload = await buildOriginalPayload(this.prisma, invoice_id, posId)
|
||||
|
||||
const attemptNumber = await getOriginalResendAttemptNumber(this.prisma, invoice_id)
|
||||
console.log('attemptNumber', attemptNumber)
|
||||
|
||||
const attempt = await this.prisma.saleInvoiceTspAttempts.create({
|
||||
data: {
|
||||
attempt_no: attemptNumber,
|
||||
invoice_id,
|
||||
provider_request_payload: {},
|
||||
status: TspProviderResponseStatus.QUEUED,
|
||||
raw_request_payload: JSON.parse(JSON.stringify(payload)),
|
||||
sent_at: new Date().toISOString(),
|
||||
message: 'در حال ارسال به سامانه مالیاتی...',
|
||||
},
|
||||
})
|
||||
|
||||
const result = await this.runProviderCall(() =>
|
||||
trySend(this.tspSwitchService, payload),
|
||||
)
|
||||
if (result?.hasError) {
|
||||
result.provider_request_payload =
|
||||
result.provider_request_payload || JSON.parse(JSON.stringify(payload))
|
||||
result.sent_at = result.sent_at || new Date().toISOString()
|
||||
result.received_at = result.received_at || new Date().toISOString()
|
||||
}
|
||||
|
||||
const result = await trySend(this.tspSwitchService, payload)
|
||||
return await onResult(this.prisma, result, attempt.id)
|
||||
}
|
||||
|
||||
@@ -206,8 +152,6 @@ export class SalesInvoiceTspService {
|
||||
business_activity.partner_token,
|
||||
)
|
||||
|
||||
console.log('getResult', result)
|
||||
|
||||
if (!result) {
|
||||
throw new NotFoundException('نتیجه ارسال فاکتور یافت نشد.')
|
||||
}
|
||||
@@ -217,7 +161,7 @@ export class SalesInvoiceTspService {
|
||||
id: attempt.id,
|
||||
},
|
||||
data: {
|
||||
provider_response_payload: JSON.parse(JSON.stringify(result)),
|
||||
provider_response: JSON.parse(JSON.stringify(result)),
|
||||
status: result.status,
|
||||
received_at: result.received_at,
|
||||
},
|
||||
@@ -309,6 +253,7 @@ export class SalesInvoiceTspService {
|
||||
status: TspProviderResponseStatus.QUEUED,
|
||||
sent_at: new Date().toISOString(),
|
||||
raw_request_payload: JSON.parse(JSON.stringify(correctionPayload)),
|
||||
provider_request_payload: {},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -326,8 +271,6 @@ export class SalesInvoiceTspService {
|
||||
result.received_at = result.received_at || new Date().toISOString()
|
||||
}
|
||||
|
||||
console.log('sendResult')
|
||||
console.log(result)
|
||||
return onResult(this.prisma, result, attempt.id)
|
||||
|
||||
// const countByGoodId = (goodIds: string[]): Map<string, number> => {
|
||||
@@ -505,6 +448,7 @@ export class SalesInvoiceTspService {
|
||||
message: 'در حال ارسال به سامانه مالیاتی...',
|
||||
status: TspProviderResponseStatus.QUEUED,
|
||||
raw_request_payload: JSON.parse(JSON.stringify(payload)),
|
||||
provider_request_payload: {},
|
||||
},
|
||||
})
|
||||
|
||||
@@ -524,4 +468,28 @@ export class SalesInvoiceTspService {
|
||||
|
||||
return await onResult(this.prisma, result, attempt.id)
|
||||
}
|
||||
|
||||
private mapProviderError(error: any) {
|
||||
return {
|
||||
hasError: true,
|
||||
status: error.status || TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
error.message?.toString() ||
|
||||
'خطا در ارتباط با سامانه مالیاتی. لطفا مجددا تلاش کنید.',
|
||||
provider_response_payload: error.provider_response_payload || {
|
||||
error: error?.message || 'fetch failed',
|
||||
cause: error?.cause || null,
|
||||
},
|
||||
sent_at: error.sent_at || new Date().toISOString(),
|
||||
received_at: error.received_at || new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
private async runProviderCall(fn: () => Promise<any>) {
|
||||
try {
|
||||
return await fn()
|
||||
} catch (error: any) {
|
||||
return this.mapProviderError(error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,257 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import {
|
||||
IsArray,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
|
||||
export enum NamaProviderResponseStatus {
|
||||
POSTED = 'POSTED',
|
||||
PENDING = 'PENDING',
|
||||
IN_PROGRESS = 'IN_PROGRESS',
|
||||
SUCCESS = 'SUCCESS',
|
||||
VALIDATION_ERROR = 'VALIDATION_ERROR',
|
||||
FAILED = 'FAILED',
|
||||
}
|
||||
|
||||
export class NamaProviderPaymentInfoDto {
|
||||
@ApiProperty({ required: true, description: 'روش پرداخت با توجه به PaymentMethodType' })
|
||||
@IsNumber()
|
||||
pmt: number
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ پرداختی. در صورتی که روش تسویه نسیه باشد برابر با ۰ است',
|
||||
})
|
||||
@IsNumber({ allowInfinity: false, allowNaN: false })
|
||||
@Min(0)
|
||||
pv: number
|
||||
|
||||
@ApiProperty({ required: false, description: 'شمارهی پیگیری' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
trn?: string
|
||||
|
||||
@ApiProperty({ required: false, description: 'شمارهی کارت پرداخت کنندهی صورتحساب' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
pcn?: string
|
||||
|
||||
@ApiProperty({ required: false, description: 'تاریخ و زمان پرداخت صورتحساب unix' })
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
pdt?: string
|
||||
}
|
||||
|
||||
export class NamaProviderCommonHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'الگوی صورتحساب' })
|
||||
@IsString()
|
||||
inp: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'نوع صورتحساب' })
|
||||
@IsString()
|
||||
inty: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'سریال صورتحساب داخلی حافظه مالیاتی' })
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
// @ApiProperty({ required: true })
|
||||
// @IsString()
|
||||
// tins: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تاریخ و زمان صدور صورتحساب به صورت timestamp',
|
||||
})
|
||||
@IsString()
|
||||
indatim: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'نام مشتری' })
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@ApiProperty({
|
||||
required: false,
|
||||
description:
|
||||
'نوع شخصیت خریدار (در صورتیکه خریدار نوع ۲ باشه باید ۱ یا ۲ گذاشته بشه)',
|
||||
})
|
||||
@IsString()
|
||||
tob: string
|
||||
|
||||
@ApiProperty({ description: 'آدرس' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
address?: string
|
||||
|
||||
@ApiProperty({ description: 'شماره موبایل' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobile?: string
|
||||
|
||||
@ApiProperty({
|
||||
description: `شماره ملی / شناسه ملی / شناسه مشارکت مدنی / کد فراگیر خریدار`,
|
||||
})
|
||||
@IsString()
|
||||
bid: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'روش تسویه (۱.نقدی، ۲.نسیه، ۳.نقدی-نسیه)' })
|
||||
@IsString()
|
||||
setm: '1' | '2' | '3'
|
||||
|
||||
@ApiProperty({ description: 'سهم پرداخت نقدی در صورتی که نقدی - نسیه باشد (۳)' })
|
||||
@IsString()
|
||||
cap: string
|
||||
|
||||
@ApiProperty({ description: 'سهم پرداخت نسیه در صورتی که نقدی - نسیه باشد (۳)' })
|
||||
@IsString()
|
||||
insp: string
|
||||
}
|
||||
|
||||
export class NamaProviderCommonBodyDto {
|
||||
@ApiProperty({ required: true, description: 'شناسه کالا / خدمت' })
|
||||
@IsString()
|
||||
sstid: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: `نرخ مالیات بر ارزش افزوده`,
|
||||
})
|
||||
@IsString()
|
||||
vra: string
|
||||
|
||||
// @ApiProperty()
|
||||
// @IsString()
|
||||
// sstt: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ واحد',
|
||||
})
|
||||
@IsString()
|
||||
fee: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ تخفیف',
|
||||
})
|
||||
@IsString()
|
||||
dis: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'واحد اندازه گیری',
|
||||
})
|
||||
@IsString()
|
||||
mu: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تعداد / مقدار',
|
||||
})
|
||||
@IsString()
|
||||
am: string
|
||||
|
||||
@ApiProperty({
|
||||
description: ' اجرت ساخت (طلا)',
|
||||
})
|
||||
@IsString()
|
||||
consfee: string
|
||||
|
||||
@ApiProperty({
|
||||
description: 'حق العمل',
|
||||
})
|
||||
@IsString()
|
||||
bros: string
|
||||
|
||||
@ApiProperty({
|
||||
description: 'سود فروشنده',
|
||||
})
|
||||
@IsString()
|
||||
spro: string
|
||||
}
|
||||
|
||||
export class NamaProviderApiErrorItemDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
}
|
||||
|
||||
export class NamaProviderTSPValidationErrorsDto {
|
||||
@ApiProperty({ type: [NamaProviderApiErrorItemDto], required: false })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderApiErrorItemDto)
|
||||
error?: NamaProviderApiErrorItemDto[]
|
||||
|
||||
@ApiProperty({ type: [NamaProviderApiErrorItemDto], required: false })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderApiErrorItemDto)
|
||||
warning?: NamaProviderApiErrorItemDto[]
|
||||
}
|
||||
|
||||
export class NamaProviderValidationErrorDto {
|
||||
@IsString()
|
||||
key: string
|
||||
|
||||
@IsString()
|
||||
errorKeyPosition: string
|
||||
|
||||
@IsString()
|
||||
message: string
|
||||
|
||||
@IsNumber()
|
||||
errorKeyIndex: number
|
||||
}
|
||||
|
||||
export class NamaProviderResponseDto {
|
||||
@ApiProperty({ description: 'شناسه پیگیری فاکتور' })
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
status: NamaProviderResponseStatus
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
reference_number: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
tax_id?: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
error_message: string
|
||||
|
||||
@ApiProperty({ type: [NamaProviderValidationErrorDto] })
|
||||
@IsArray()
|
||||
@IsOptional()
|
||||
validation_error: NamaProviderValidationErrorDto[]
|
||||
|
||||
@ApiProperty({ type: [NamaProviderTSPValidationErrorsDto], required: false })
|
||||
@IsOptional()
|
||||
tax_api_response?: NamaProviderTSPValidationErrorsDto[]
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import { IsArray, IsString, Length, ValidateNested } from 'class-validator'
|
||||
import {
|
||||
NamaProviderCommonBodyDto,
|
||||
NamaProviderCommonHeaderDto,
|
||||
NamaProviderPaymentInfoDto,
|
||||
NamaProviderResponseDto,
|
||||
} from './common.dto'
|
||||
|
||||
export class NamaProviderCorrectionHeaderDto extends NamaProviderCommonHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'موضوع صورتحساب (2- اصلاحی)' })
|
||||
@IsString()
|
||||
ins: '2'
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'شناسه مالیاتی صورتحساب مرجع',
|
||||
minLength: 22,
|
||||
maxLength: 22,
|
||||
})
|
||||
@IsString()
|
||||
@Length(22, 22)
|
||||
irtaxid: string
|
||||
}
|
||||
|
||||
export class NamaProviderCorrectionBodyItemDto extends NamaProviderCommonBodyDto {}
|
||||
|
||||
export class NamaProviderCorrectionRequestDto {
|
||||
@ApiProperty({ type: [NamaProviderCorrectionBodyItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderCorrectionBodyItemDto)
|
||||
body: NamaProviderCorrectionBodyItemDto[]
|
||||
|
||||
@ApiProperty({ type: [NamaProviderPaymentInfoDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderPaymentInfoDto)
|
||||
payment: NamaProviderPaymentInfoDto[]
|
||||
|
||||
@ApiProperty({ type: NamaProviderCorrectionHeaderDto })
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderCorrectionHeaderDto)
|
||||
header: NamaProviderCorrectionHeaderDto
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
}
|
||||
|
||||
export class NamaProviderCorrectionResponseDto extends NamaProviderResponseDto {}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { ApiProperty, OmitType } from '@nestjs/swagger'
|
||||
import { IsString } from 'class-validator'
|
||||
import { NamaProviderResponseDto } from './common.dto'
|
||||
|
||||
export class NamaProviderGetResponseDto extends OmitType(NamaProviderResponseDto, [
|
||||
'tax_id',
|
||||
]) {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export * from './common.dto'
|
||||
export * from './correction.dto'
|
||||
export * from './get.dto'
|
||||
export * from './original.dto'
|
||||
export * from './revoke.dto'
|
||||
@@ -0,0 +1,50 @@
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import { IsArray, IsString, ValidateNested } from 'class-validator'
|
||||
import {
|
||||
NamaProviderCommonBodyDto,
|
||||
NamaProviderCommonHeaderDto,
|
||||
NamaProviderPaymentInfoDto,
|
||||
NamaProviderResponseDto,
|
||||
} from './common.dto'
|
||||
|
||||
export class NamaProviderOriginalHeaderDto extends NamaProviderCommonHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'موضوع صورتحساب (1- اصلی)' })
|
||||
@IsString()
|
||||
ins: '1'
|
||||
}
|
||||
|
||||
export class NamaProviderOriginalBodyItemDto extends NamaProviderCommonBodyDto {}
|
||||
|
||||
export class NamaProviderOriginalRequestDto {
|
||||
@ApiProperty({ type: [NamaProviderOriginalBodyItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderOriginalBodyItemDto)
|
||||
body: NamaProviderOriginalBodyItemDto[]
|
||||
|
||||
@ApiProperty({ type: [NamaProviderPaymentInfoDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderPaymentInfoDto)
|
||||
payment: NamaProviderPaymentInfoDto[]
|
||||
|
||||
@ApiProperty({ type: NamaProviderOriginalHeaderDto })
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderOriginalHeaderDto)
|
||||
header: NamaProviderOriginalHeaderDto
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
}
|
||||
|
||||
export class NamaProviderOriginalResponseDto extends NamaProviderResponseDto {}
|
||||
@@ -0,0 +1,54 @@
|
||||
import { ApiProperty, OmitType } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import { IsString, ValidateNested } from 'class-validator'
|
||||
import { NamaProviderResponseDto } from './common.dto'
|
||||
|
||||
export class NamaProviderRevokeHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'موضوع صورتحساب (3- ابطالی)' })
|
||||
@IsString()
|
||||
ins: '3'
|
||||
|
||||
@ApiProperty({ required: true, description: 'سریال صورتحساب داخلی حافظه مالیاتی' })
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تاریخ و زمان صدور صورتحساب به صورت timestamp',
|
||||
})
|
||||
@IsString()
|
||||
indatim: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'آخرین شماره مالیاتی دریافت شده مربوط به فاکتور',
|
||||
})
|
||||
@IsString()
|
||||
irtaxid: string
|
||||
}
|
||||
export class NamaProviderRevokeRequestDto {
|
||||
@ApiProperty({ type: NamaProviderRevokeHeaderDto })
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderRevokeHeaderDto)
|
||||
header: NamaProviderRevokeHeaderDto
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
}
|
||||
|
||||
export class NamaProviderRevokeResponseDto extends OmitType(NamaProviderResponseDto, [
|
||||
'tax_id',
|
||||
]) {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
}
|
||||
@@ -7,23 +7,25 @@ import { HttpClientUtil } from '@/common/utils/http-client.util'
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import { Injectable, Logger } from '@nestjs/common'
|
||||
import {
|
||||
IProviderSwitchAdapter,
|
||||
TspProviderBulkSendResultDto,
|
||||
TspProviderCorrectionSendPayloadDto,
|
||||
TspProviderCorrectionSendResponseDto,
|
||||
TspProviderGetResultDto,
|
||||
TspProviderOriginalSendItemResultDto,
|
||||
TspProviderOriginalSendPayloadDto,
|
||||
TspProviderRevokePayloadDto,
|
||||
TspProviderRevokeResponseDto,
|
||||
TspProviderSendItemResultDto,
|
||||
} from '../../dto'
|
||||
import {
|
||||
IProviderSwitchAdapter,
|
||||
TspProviderBulkSendResultDto,
|
||||
} from '../../dto/provider-switch.dto'
|
||||
import {
|
||||
NamaProviderGetResponseDto,
|
||||
NamaProviderOriginalResponseDto,
|
||||
NamaProviderResponseStatus,
|
||||
NamaProviderRevokeRequestDto,
|
||||
NamaProviderRevokeResponseDto,
|
||||
NamaProviderSendItemResponseDto,
|
||||
} from './nama-provider.dto'
|
||||
} from './dto'
|
||||
import { NamaProviderUtils } from './nama-provider.util'
|
||||
|
||||
@Injectable()
|
||||
@@ -77,8 +79,8 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
|
||||
async originalSend(
|
||||
payload: TspProviderOriginalSendPayloadDto,
|
||||
): Promise<TspProviderSendItemResultDto> {
|
||||
const mappedRequest = this.namaProviderUtils.mapToNamaRequestDto(payload)
|
||||
): Promise<TspProviderOriginalSendItemResultDto> {
|
||||
const mappedRequest = this.namaProviderUtils.mapToNamaOriginalRequestDto(payload)
|
||||
try {
|
||||
const response = await this.httpClient.request(
|
||||
this.buildSendUrl(),
|
||||
@@ -86,20 +88,23 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(mappedRequest),
|
||||
},
|
||||
this.createRequestInterceptors(payload.tsp_token),
|
||||
this.createRequestInterceptors(payload.token),
|
||||
)
|
||||
const providerResponse: NamaProviderSendItemResponseDto = await response.json()
|
||||
this.logger.debug('NAMA provider response', providerResponse)
|
||||
const providerResponse: NamaProviderOriginalResponseDto = await response.json()
|
||||
this.logger.debug('NAMA provider response', response)
|
||||
this.logger.debug('NAMA provider providerResponse', providerResponse)
|
||||
|
||||
const result: TspProviderSendItemResultDto = {
|
||||
invoice_id: payload.invoice_id,
|
||||
const result: TspProviderOriginalSendItemResultDto = {
|
||||
invoice_id: payload.id,
|
||||
provider_request_payload: JSON.parse(JSON.stringify(mappedRequest)),
|
||||
hasError: !response.ok,
|
||||
message: providerResponse.message,
|
||||
provider_response_payload: JSON.parse(JSON.stringify(providerResponse)),
|
||||
received_at: providerResponse.tsp_update_time,
|
||||
sent_at: new Date().toISOString(),
|
||||
received_at: new Date().toISOString(),
|
||||
provider_response: JSON.parse(JSON.stringify(providerResponse)),
|
||||
tax_id: providerResponse.tax_id,
|
||||
error_message: providerResponse.error_message,
|
||||
fiscal_warnings: providerResponse.tax_api_response,
|
||||
validation_errors: providerResponse.validation_error,
|
||||
status: this.namaProviderUtils.mapResponseStatus(
|
||||
providerResponse.status as NamaProviderResponseStatus,
|
||||
),
|
||||
@@ -108,24 +113,22 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
return result
|
||||
} catch (err) {
|
||||
this.logger.error('NAMA send failed', err)
|
||||
const failure: TspProviderSendItemResultDto = {
|
||||
invoice_id: payload.invoice_id,
|
||||
const failure: TspProviderOriginalSendItemResultDto = {
|
||||
invoice_id: payload.id,
|
||||
provider_request_payload: JSON.parse(JSON.stringify(mappedRequest)),
|
||||
hasError: true,
|
||||
message: (err as Error).message,
|
||||
sent_at: new Date().toISOString(),
|
||||
received_at: new Date().toISOString(),
|
||||
tax_id: null,
|
||||
status: TspProviderResponseStatus.NOT_SEND,
|
||||
status: TspProviderResponseStatus.SEND_FAILURE,
|
||||
}
|
||||
throw failure
|
||||
return failure
|
||||
}
|
||||
}
|
||||
|
||||
async correctionSend(
|
||||
payload: TspProviderCorrectionSendPayloadDto,
|
||||
): Promise<TspProviderCorrectionSendResponseDto> {
|
||||
const mappedRequest = this.namaProviderUtils.mapToNamaRequestDto(payload)
|
||||
const mappedRequest = this.namaProviderUtils.mapToNamaOriginalRequestDto(payload)
|
||||
|
||||
try {
|
||||
const response = await this.httpClient.request(
|
||||
@@ -134,19 +137,18 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(mappedRequest),
|
||||
},
|
||||
this.createRequestInterceptors(payload.tsp_token),
|
||||
this.createRequestInterceptors(payload.token),
|
||||
)
|
||||
const providerResponse: NamaProviderSendItemResponseDto = await response.json()
|
||||
const providerResponse: NamaProviderOriginalResponseDto = await response.json()
|
||||
this.logger.debug('NAMA provider response', providerResponse)
|
||||
|
||||
const result: TspProviderSendItemResultDto = {
|
||||
invoice_id: payload.invoice_id,
|
||||
const result: TspProviderOriginalSendItemResultDto = {
|
||||
invoice_id: payload.id,
|
||||
provider_request_payload: JSON.parse(JSON.stringify(mappedRequest)),
|
||||
hasError: !response.ok,
|
||||
message: providerResponse.message,
|
||||
sent_at: new Date().toISOString(),
|
||||
provider_response_payload: JSON.parse(JSON.stringify(providerResponse)),
|
||||
received_at: providerResponse.tsp_update_time,
|
||||
provider_response: JSON.parse(JSON.stringify(providerResponse)),
|
||||
received_at: new Date().toISOString(),
|
||||
tax_id: providerResponse.tax_id,
|
||||
status: this.namaProviderUtils.mapResponseStatus(
|
||||
providerResponse.status as NamaProviderResponseStatus,
|
||||
@@ -156,12 +158,11 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
return result
|
||||
} catch (err) {
|
||||
this.logger.error('NAMA send failed', err)
|
||||
const failure: TspProviderSendItemResultDto = {
|
||||
invoice_id: payload.invoice_id,
|
||||
const failure: TspProviderOriginalSendItemResultDto = {
|
||||
invoice_id: payload.id,
|
||||
provider_request_payload: JSON.parse(JSON.stringify(mappedRequest)),
|
||||
hasError: true,
|
||||
message: (err as Error).message,
|
||||
sent_at: new Date().toISOString(),
|
||||
received_at: new Date().toISOString(),
|
||||
tax_id: null,
|
||||
status: TspProviderResponseStatus.NOT_SEND,
|
||||
@@ -177,7 +178,7 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
for (const payload of payloads) {
|
||||
const itemResults = await this.originalSend(payload)
|
||||
result.push({
|
||||
invoice_id: payload.invoice_id,
|
||||
invoice_id: payload.id,
|
||||
items: [itemResults],
|
||||
})
|
||||
}
|
||||
@@ -201,7 +202,7 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
message: providerResponse.message,
|
||||
provider_response_payload: providerResponse,
|
||||
sent_at: new Date().toISOString(),
|
||||
received_at: providerResponse.tsp_update_time || new Date().toISOString(),
|
||||
received_at: new Date().toISOString(),
|
||||
tax_id: providerResponse.tax_id,
|
||||
status: this.namaProviderUtils.mapResponseStatus(
|
||||
providerResponse.status as NamaProviderResponseStatus,
|
||||
@@ -228,7 +229,7 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
payload: TspProviderRevokePayloadDto,
|
||||
): Promise<TspProviderRevokeResponseDto> {
|
||||
const mappedRequest: NamaProviderRevokeRequestDto =
|
||||
this.namaProviderUtils.mapRevokeToNamaRequestDto(payload)
|
||||
this.namaProviderUtils.mapToNamaRevokeRequestDto(payload)
|
||||
|
||||
this.logger.debug('NAMA provider response', mappedRequest)
|
||||
|
||||
@@ -251,7 +252,7 @@ export class NamaProviderSwitchAdapter implements IProviderSwitchAdapter {
|
||||
message: providerResponse.message,
|
||||
sent_at: new Date().toISOString(),
|
||||
provider_response_payload: providerResponse,
|
||||
received_at: providerResponse.tsp_update_time,
|
||||
received_at: new Date().toISOString(),
|
||||
tax_id: providerResponse.tax_id,
|
||||
status: this.namaProviderUtils.mapResponseStatus(
|
||||
providerResponse.status as NamaProviderResponseStatus,
|
||||
|
||||
@@ -1,561 +0,0 @@
|
||||
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
||||
import { ApiProperty } from '@nestjs/swagger'
|
||||
import { Type } from 'class-transformer'
|
||||
import {
|
||||
IsArray,
|
||||
IsEnum,
|
||||
IsNumber,
|
||||
IsOptional,
|
||||
IsString,
|
||||
Min,
|
||||
ValidateNested,
|
||||
} from 'class-validator'
|
||||
|
||||
export enum NamaProviderResponseStatus {
|
||||
POSTED = 'SUCCESS',
|
||||
PENDING = 'PENDING',
|
||||
IN_PROGRESS = 'IN_PROGRESS',
|
||||
SUCCESS = 'SUCCESS',
|
||||
FAILED = 'FAILED',
|
||||
}
|
||||
|
||||
export class NamaProviderPaymentInfoDto {
|
||||
@ApiProperty({ required: true, description: 'روش پرداخت' })
|
||||
@IsNumber()
|
||||
pmt: number
|
||||
|
||||
@ApiProperty({ required: true, description: 'مبلغ پرداختی' })
|
||||
@IsNumber()
|
||||
@Min(10_000)
|
||||
pv: number
|
||||
|
||||
@ApiProperty({ required: false, description: 'شمارهی پیگیری' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
trn?: string
|
||||
|
||||
@ApiProperty({ required: false, description: 'شمارهی کارت پرداخت کنندهی صورتحساب' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
pcn?: string
|
||||
|
||||
@ApiProperty({ required: false, description: 'تاریخ و زمان پرداخت صورتحساب unix' })
|
||||
@IsOptional()
|
||||
@IsNumber()
|
||||
pdt?: string
|
||||
}
|
||||
|
||||
export class NamaProviderBodyItemDto {
|
||||
@ApiProperty({ required: true, description: 'شناسه کالا / خدمت' })
|
||||
@IsString()
|
||||
sstid: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: `نرخ مالیات بر ارزش افزوده`,
|
||||
})
|
||||
@IsString()
|
||||
vra: string
|
||||
|
||||
// @ApiProperty()
|
||||
// @IsString()
|
||||
// sstt: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ واحد',
|
||||
})
|
||||
@IsString()
|
||||
fee: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ تخفیف',
|
||||
})
|
||||
@IsString()
|
||||
dis: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'واحد اندازه گیری',
|
||||
})
|
||||
@IsString()
|
||||
mu: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تعداد / مقدار',
|
||||
})
|
||||
@IsString()
|
||||
am: string
|
||||
|
||||
@ApiProperty({
|
||||
description: ' اجرت ساخت (طلا)',
|
||||
})
|
||||
@IsString()
|
||||
consfee: string
|
||||
|
||||
@ApiProperty({
|
||||
description: 'حق العمل',
|
||||
})
|
||||
@IsString()
|
||||
bros: string
|
||||
|
||||
@ApiProperty({
|
||||
description: 'سود فروشنده',
|
||||
})
|
||||
@IsString()
|
||||
spro: string
|
||||
}
|
||||
|
||||
export class NamaProviderHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'موضوع صورتحساب' })
|
||||
@IsString()
|
||||
ins: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'الگوی صورتحساب' })
|
||||
@IsString()
|
||||
inp: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'نوع صورتحساب' })
|
||||
@IsString()
|
||||
inty: string
|
||||
|
||||
// @ApiProperty({required: true})
|
||||
// @IsString()
|
||||
// unique_tax_code: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'سریال صورتحساب داخلی حافظه مالیاتی' })
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
// @ApiProperty({ required: true })
|
||||
// @IsString()
|
||||
// tins: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تاریخ و زمان صدور صورتحساب به صورت timestamp',
|
||||
})
|
||||
@IsString()
|
||||
indatim: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'نام مشتری' })
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@ApiProperty({
|
||||
required: false,
|
||||
description:
|
||||
'نوع شخصیت خریدار (در صورتیکه خریدار نوع ۲ باشه باید ۱ یا ۲ گذاشته بشه)',
|
||||
})
|
||||
@IsString()
|
||||
tob: string
|
||||
|
||||
@ApiProperty({ description: 'آدرس' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
address?: string
|
||||
|
||||
@ApiProperty({ description: 'شماره موبایل' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobile?: string
|
||||
|
||||
@ApiProperty({
|
||||
description: `شماره ملی / شناسه ملی / شناسه مشارکت مدنی / کد فراگیر خریدار`,
|
||||
})
|
||||
@IsString()
|
||||
bid: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'روش تسویه (۱.نقدی، ۲.نسیه، ۳.نقدی-نسیه)' })
|
||||
@IsString()
|
||||
setm: '1' | '2' | '3'
|
||||
|
||||
@ApiProperty({ description: 'سهم پرداخت نقدی در صورتی که نقدی - نسیه باشد (۳)' })
|
||||
@IsString()
|
||||
cap: string
|
||||
|
||||
@ApiProperty({ description: 'سهم پرداخت نسیه در صورتی که نقدی - نسیه باشد (۳)' })
|
||||
@IsString()
|
||||
insp: string
|
||||
}
|
||||
|
||||
export class NamaProviderRequestDto {
|
||||
@ApiProperty({ type: [NamaProviderBodyItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderBodyItemDto)
|
||||
body: NamaProviderBodyItemDto[]
|
||||
|
||||
@ApiProperty({ type: [NamaProviderPaymentInfoDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderPaymentInfoDto)
|
||||
payment: NamaProviderPaymentInfoDto[]
|
||||
|
||||
@ApiProperty({ type: NamaProviderHeaderDto })
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderHeaderDto)
|
||||
header: NamaProviderHeaderDto
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
}
|
||||
|
||||
export class NamaProviderApiErrorItemDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
}
|
||||
|
||||
export class NamaProviderApiErrorResponseDto {
|
||||
@ApiProperty({ type: [NamaProviderApiErrorItemDto], required: false })
|
||||
@IsOptional()
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderApiErrorItemDto)
|
||||
error?: NamaProviderApiErrorItemDto
|
||||
}
|
||||
|
||||
export class NamaProviderSendItemResponseDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
status: NamaProviderResponseStatus
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
reference_number: string
|
||||
|
||||
@ApiProperty({ type: () => NamaProviderApiErrorResponseDto, required: false })
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderApiErrorResponseDto)
|
||||
tax_api_response?: NamaProviderApiErrorResponseDto
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tsp_update_time: string
|
||||
}
|
||||
|
||||
export class NamaProviderGetResponseDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
|
||||
@ApiProperty({ enum: TspProviderResponseStatus })
|
||||
@IsEnum(TspProviderResponseStatus)
|
||||
status: TspProviderResponseStatus
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
reference_number: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tsp_update_time: string
|
||||
}
|
||||
|
||||
export class NamaProviderRevokeHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'موضوع صورتحساب' })
|
||||
@IsString()
|
||||
ins: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'سریال صورتحساب داخلی حافظه مالیاتی' })
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تاریخ و زمان صدور صورتحساب به صورت timestamp',
|
||||
})
|
||||
@IsString()
|
||||
indatim: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'آخرین شماره مالیاتی دریافت شده مربوط به فاکتور',
|
||||
})
|
||||
@IsString()
|
||||
irtaxid: string
|
||||
}
|
||||
export class NamaProviderRevokeRequestDto {
|
||||
// @ApiProperty({ type: [NamaProviderBodyItemDto] })
|
||||
// @IsArray()
|
||||
// @ValidateNested({ each: true })
|
||||
// @Type(() => NamaProviderBodyItemDto)
|
||||
// body: NamaProviderBodyItemDto[]
|
||||
|
||||
// @ApiProperty({ type: [NamaProviderPaymentInfoDto] })
|
||||
// @IsArray()
|
||||
// @ValidateNested({ each: true })
|
||||
// @Type(() => NamaProviderPaymentInfoDto)
|
||||
// payment: NamaProviderPaymentInfoDto[]
|
||||
// @ApiProperty({ type: Object, required: true })
|
||||
// @IsObject()
|
||||
// body: {}
|
||||
|
||||
@ApiProperty({ type: NamaProviderRevokeHeaderDto })
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderRevokeHeaderDto)
|
||||
header: NamaProviderRevokeHeaderDto
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
}
|
||||
|
||||
export class NamaProviderRevokeResponseDto {
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
message: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
status: NamaProviderResponseStatus
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
tax_id: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
@ApiProperty({ required: true })
|
||||
@IsString()
|
||||
reference_number: string
|
||||
|
||||
@ApiProperty({ type: () => NamaProviderApiErrorResponseDto, required: false })
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderApiErrorResponseDto)
|
||||
tax_api_response?: NamaProviderApiErrorResponseDto
|
||||
|
||||
@ApiProperty({ required: false })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
tsp_update_time: string
|
||||
}
|
||||
|
||||
/////////////// Correction DTOs ///////////////
|
||||
export class NamaProviderCorrectionBodyItemDto {
|
||||
@ApiProperty({ required: true, description: 'شناسه کالا / خدمت' })
|
||||
@IsString()
|
||||
sstid: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: `نرخ مالیات بر ارزش افزوده`,
|
||||
})
|
||||
@IsString()
|
||||
vra: string
|
||||
|
||||
// @ApiProperty()
|
||||
// @IsString()
|
||||
// sstt: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ واحد',
|
||||
})
|
||||
@IsString()
|
||||
fee: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'مبلغ تخفیف',
|
||||
})
|
||||
@IsString()
|
||||
dis: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'واحد اندازه گیری',
|
||||
})
|
||||
@IsString()
|
||||
mu: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تعداد / مقدار',
|
||||
})
|
||||
@IsString()
|
||||
am: string
|
||||
|
||||
@ApiProperty({
|
||||
description: ' اجرت ساخت (طلا)',
|
||||
})
|
||||
@IsString()
|
||||
consfee: string
|
||||
|
||||
@ApiProperty({
|
||||
description: 'حق العمل',
|
||||
})
|
||||
@IsString()
|
||||
bros: string
|
||||
|
||||
@ApiProperty({
|
||||
description: 'سود فروشنده',
|
||||
})
|
||||
@IsString()
|
||||
spro: string
|
||||
}
|
||||
|
||||
export class NamaProviderCorrectionHeaderDto {
|
||||
@ApiProperty({ required: true, description: 'موضوع صورتحساب' })
|
||||
@IsString()
|
||||
ins: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'الگوی صورتحساب' })
|
||||
@IsString()
|
||||
inp: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'نوع صورتحساب' })
|
||||
@IsString()
|
||||
inty: string
|
||||
|
||||
// @ApiProperty({required: true})
|
||||
// @IsString()
|
||||
// unique_tax_code: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'سریال صورتحساب داخلی حافظه مالیاتی' })
|
||||
@IsString()
|
||||
inno: string
|
||||
|
||||
// @ApiProperty({ required: true })
|
||||
// @IsString()
|
||||
// tins: string
|
||||
|
||||
@ApiProperty({
|
||||
required: true,
|
||||
description: 'تاریخ و زمان صدور صورتحساب به صورت timestamp',
|
||||
})
|
||||
@IsString()
|
||||
indatim: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'نام مشتری' })
|
||||
@IsString()
|
||||
name: string
|
||||
|
||||
@ApiProperty({
|
||||
required: false,
|
||||
description:
|
||||
'نوع شخصیت خریدار (در صورتیکه خریدار نوع ۲ باشه باید ۱ یا ۲ گذاشته بشه)',
|
||||
})
|
||||
@IsString()
|
||||
tob: string
|
||||
|
||||
@ApiProperty({ description: 'آدرس' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
address?: string
|
||||
|
||||
@ApiProperty({ description: 'شماره موبایل' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
mobile?: string
|
||||
|
||||
@ApiProperty({
|
||||
description: `شماره ملی / شناسه ملی / شناسه مشارکت مدنی / کد فراگیر خریدار`,
|
||||
})
|
||||
@IsString()
|
||||
bid: string
|
||||
|
||||
@ApiProperty({ required: true, description: 'روش تسویه (۱.نقدی، ۲.نسیه، ۳.نقدی-نسیه)' })
|
||||
@IsString()
|
||||
setm: '1' | '2' | '3'
|
||||
|
||||
@ApiProperty({ description: 'سهم پرداخت نقدی در صورتی که نقدی - نسیه باشد (۳)' })
|
||||
@IsString()
|
||||
cap: string
|
||||
|
||||
@ApiProperty({ description: 'سهم پرداخت نسیه در صورتی که نقدی - نسیه باشد (۳)' })
|
||||
@IsString()
|
||||
insp: string
|
||||
}
|
||||
|
||||
export class NamaProviderCorrectionRequestDto {
|
||||
@ApiProperty({ type: [NamaProviderCorrectionBodyItemDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderCorrectionBodyItemDto)
|
||||
body: NamaProviderCorrectionBodyItemDto[]
|
||||
|
||||
@ApiProperty({ type: [NamaProviderPaymentInfoDto] })
|
||||
@IsArray()
|
||||
@ValidateNested({ each: true })
|
||||
@Type(() => NamaProviderPaymentInfoDto)
|
||||
payment: NamaProviderPaymentInfoDto[]
|
||||
|
||||
@ApiProperty({ type: NamaProviderCorrectionHeaderDto })
|
||||
@ValidateNested()
|
||||
@Type(() => NamaProviderCorrectionHeaderDto)
|
||||
header: NamaProviderCorrectionHeaderDto
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
uuid: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
economic_code: string
|
||||
|
||||
@ApiProperty()
|
||||
@IsString()
|
||||
fiscal_id: string
|
||||
}
|
||||
@@ -13,45 +13,45 @@ import {
|
||||
TspProviderCorrectionSendPayloadDto,
|
||||
TspProviderOriginalSendPayloadDto,
|
||||
TspProviderRevokePayloadDto,
|
||||
} from '../../dto/provider-switch.dto'
|
||||
} from '../../dto'
|
||||
import {
|
||||
NamaProviderCorrectionRequestDto,
|
||||
NamaProviderOriginalRequestDto,
|
||||
NamaProviderPaymentInfoDto,
|
||||
NamaProviderRequestDto,
|
||||
NamaProviderResponseStatus,
|
||||
NamaProviderRevokeRequestDto,
|
||||
} from './nama-provider.dto'
|
||||
} from './dto'
|
||||
|
||||
export class NamaProviderUtils {
|
||||
mapResponseStatus(status: NamaProviderResponseStatus): TspProviderResponseStatus {
|
||||
console.log('NAMA status', status)
|
||||
|
||||
switch (status.toUpperCase()) {
|
||||
case NamaProviderResponseStatus.SUCCESS:
|
||||
case NamaProviderResponseStatus.POSTED:
|
||||
return TspProviderResponseStatus.FISCAL_QUEUED
|
||||
case NamaProviderResponseStatus.SUCCESS:
|
||||
return TspProviderResponseStatus.SUCCESS
|
||||
case NamaProviderResponseStatus.PENDING:
|
||||
case NamaProviderResponseStatus.IN_PROGRESS:
|
||||
return TspProviderResponseStatus.QUEUED
|
||||
case NamaProviderResponseStatus.FAILED:
|
||||
return TspProviderResponseStatus.FAILURE
|
||||
case NamaProviderResponseStatus.PENDING:
|
||||
case NamaProviderResponseStatus.IN_PROGRESS:
|
||||
default:
|
||||
return TspProviderResponseStatus.QUEUED
|
||||
}
|
||||
}
|
||||
|
||||
mapToNamaRequestDto(
|
||||
mapToNamaOriginalRequestDto(
|
||||
payload: TspProviderOriginalSendPayloadDto,
|
||||
): NamaProviderRequestDto {
|
||||
): NamaProviderOriginalRequestDto {
|
||||
return {
|
||||
uuid: payload.invoice_id,
|
||||
uuid: payload.id,
|
||||
economic_code: payload.economic_code,
|
||||
fiscal_id: payload.fiscal_id,
|
||||
payment: this.mapPayments(payload.payments),
|
||||
header: {
|
||||
ins: this.mapTspProviderRequestType(payload.type),
|
||||
inp: this.mapInvoiceTemplate(payload.invoice_template),
|
||||
inty: this.mapTspProviderCustomerType(payload.customer_type),
|
||||
ins: '1',
|
||||
inp: this.mapInvoiceTemplate(payload.template),
|
||||
inty: this.mapTspProviderCustomerType(payload.customer?.type),
|
||||
inno: payload.invoice_number.toString(),
|
||||
tins: '',
|
||||
indatim: payload.invoice_date.getTime() + '',
|
||||
@@ -81,14 +81,14 @@ export class NamaProviderUtils {
|
||||
payload: TspProviderCorrectionSendPayloadDto,
|
||||
): NamaProviderCorrectionRequestDto {
|
||||
return {
|
||||
uuid: payload.invoice_id,
|
||||
uuid: payload.id,
|
||||
economic_code: payload.economic_code,
|
||||
fiscal_id: payload.fiscal_id,
|
||||
payment: this.mapPayments(payload.payments),
|
||||
header: {
|
||||
ins: this.mapTspProviderRequestType(TspProviderRequestType.CORRECTION),
|
||||
inp: this.mapInvoiceTemplate(payload.invoice_template),
|
||||
inty: this.mapTspProviderCustomerType(payload.customer_type),
|
||||
ins: this.mapRequestType(TspProviderRequestType.CORRECTION),
|
||||
inp: this.mapInvoiceTemplate(payload.template),
|
||||
inty: this.mapTspProviderCustomerType(payload.customer?.type),
|
||||
inno: payload.invoice_number.toString(),
|
||||
tins: '',
|
||||
indatim: payload.invoice_date.getTime() + '',
|
||||
@@ -110,7 +110,7 @@ export class NamaProviderUtils {
|
||||
}
|
||||
}
|
||||
|
||||
mapRevokeToNamaRequestDto(
|
||||
mapToNamaRevokeRequestDto(
|
||||
payload: TspProviderRevokePayloadDto,
|
||||
): NamaProviderRevokeRequestDto {
|
||||
return {
|
||||
@@ -119,14 +119,14 @@ export class NamaProviderUtils {
|
||||
fiscal_id: payload.fiscal_id,
|
||||
header: {
|
||||
inno: payload.invoice_number.toString(),
|
||||
ins: this.mapTspProviderRequestType(TspProviderRequestType.REVOKE),
|
||||
ins: '3',
|
||||
irtaxid: payload.last_tax_id,
|
||||
indatim: new Date().getTime() + '',
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
private mapTspProviderRequestType(type: TspProviderRequestType) {
|
||||
private mapRequestType(type: TspProviderRequestType) {
|
||||
switch (type) {
|
||||
case TspProviderRequestType.ORIGINAL:
|
||||
return '1'
|
||||
@@ -206,9 +206,9 @@ export class NamaProviderUtils {
|
||||
case TspProviderCustomerType.KNOWN:
|
||||
return '1'
|
||||
case TspProviderCustomerType.UNKNOWN:
|
||||
return '2'
|
||||
default:
|
||||
return '3'
|
||||
return '2'
|
||||
// return '3'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -267,8 +267,8 @@ export class NamaProviderUtils {
|
||||
return payments.map(payment => ({
|
||||
pmt: this.mapPaymentMethod(payment.payment_method),
|
||||
pv: payment.amount,
|
||||
trn: payment.tracking_code,
|
||||
pcn: payment.card_number,
|
||||
trn: payment.terminal_info.tracking_code,
|
||||
pcn: payment.terminal_info.card_number,
|
||||
pdt: payment.paid_at ? String(payment.paid_at.getTime()) : undefined,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { SaleInvoiceTspAttemptsUpdateInput } from '@/generated/prisma/models'
|
||||
import { PrismaService } from '@/prisma/prisma.service'
|
||||
import { BadRequestException, NotFoundException } from '@nestjs/common'
|
||||
import { TspProviderCustomerType } from 'common/enums/enums'
|
||||
import {
|
||||
Prisma,
|
||||
TspProviderRequestType,
|
||||
TspProviderResponseStatus,
|
||||
} from 'generated/prisma/client'
|
||||
import {
|
||||
TspProviderActionResponseDto,
|
||||
TspProviderCorrectionSendPayloadDto,
|
||||
TspProviderOriginalSendItemResultDto,
|
||||
TspProviderOriginalSendPayloadDto,
|
||||
TspProviderRevokePayloadDto,
|
||||
TspProviderSendItemResultDto,
|
||||
} from '../dto/provider-switch.dto'
|
||||
} from '../dto'
|
||||
import { TspProviderActionResponseDto } from '../dto/provider-switch.dto'
|
||||
|
||||
export async function getOriginalResendAttemptNumber(
|
||||
prisma: PrismaService,
|
||||
@@ -49,7 +49,10 @@ export async function getOriginalResendAttemptNumber(
|
||||
'فاکتور تایید شده از طرف سازمان مالیاتی قابل ارسال مجدد نیست.',
|
||||
)
|
||||
}
|
||||
if (existingAttempt.status === TspProviderResponseStatus.QUEUED) {
|
||||
if (
|
||||
existingAttempt.status === TspProviderResponseStatus.QUEUED ||
|
||||
existingAttempt.status === TspProviderResponseStatus.FISCAL_QUEUED
|
||||
) {
|
||||
throw new BadRequestException(
|
||||
'در حال حاضر فاکتور شما در حال بررسی توسط سازمان مالیاتی است.',
|
||||
)
|
||||
@@ -164,6 +167,7 @@ export async function buildCorrectionPayload(
|
||||
invoice_date: true,
|
||||
invoice_number: true,
|
||||
settlement_type: true,
|
||||
tax_id: true,
|
||||
items: true,
|
||||
pos: {
|
||||
select: {
|
||||
@@ -207,6 +211,7 @@ export async function buildCorrectionPayload(
|
||||
},
|
||||
},
|
||||
},
|
||||
unknown_customer: true,
|
||||
customer: {
|
||||
select: {
|
||||
type: true,
|
||||
@@ -234,6 +239,8 @@ export async function buildCorrectionPayload(
|
||||
const { partner } = (invoice.pos.complex.business_activity.consumer.legal ||
|
||||
invoice.pos.complex.business_activity.consumer.individual)!
|
||||
|
||||
const unknown_customer = (invoice.unknown_customer || {}) as Record<string, string>
|
||||
|
||||
return {
|
||||
items: invoice.items.map(item => ({
|
||||
invoice_item_id: item.id,
|
||||
@@ -253,36 +260,41 @@ export async function buildCorrectionPayload(
|
||||
amount: Number(payment.amount),
|
||||
payment_method: payment.payment_method,
|
||||
paid_at: payment.paid_at,
|
||||
card_number: payment.terminal_info ? payment.terminal_info.stan : undefined,
|
||||
tracking_code: payment.terminal_info ? payment.terminal_info.rrn : undefined,
|
||||
terminal_info: {
|
||||
card_number: payment.terminal_info ? payment.terminal_info.stan : undefined,
|
||||
tracking_code: payment.terminal_info ? payment.terminal_info.rrn : undefined,
|
||||
},
|
||||
})),
|
||||
total_amount: Number(invoice.total_amount),
|
||||
last_tax_id: invoice.reference_invoice!.tax_id!,
|
||||
invoice_number: invoice.invoice_number,
|
||||
invoice_id: invoice.id,
|
||||
id: invoice.id,
|
||||
economic_code: invoice.pos.complex.business_activity.economic_code,
|
||||
fiscal_id: invoice.pos.complex.business_activity.fiscal_id,
|
||||
tsp_token: invoice.pos.complex.business_activity.partner_token,
|
||||
type: TspProviderRequestType.CORRECTION,
|
||||
token: invoice.pos.complex.business_activity.partner_token,
|
||||
tsp_provider: partner.tsp_provider!,
|
||||
invoice_template: invoice.pos.complex.business_activity.guild.invoice_template,
|
||||
template: invoice.pos.complex.business_activity.guild.invoice_template,
|
||||
invoice_date: new Date(),
|
||||
settlement_type: invoice.settlement_type,
|
||||
|
||||
customer_type: invoice.customer?.type
|
||||
? TspProviderCustomerType.KNOWN
|
||||
: TspProviderCustomerType.UNKNOWN,
|
||||
customer: invoice.customer?.type
|
||||
? {
|
||||
type: invoice.customer.type,
|
||||
legal_info: invoice.customer.legal ?? undefined,
|
||||
individual_info: invoice.customer.individual ?? undefined,
|
||||
}
|
||||
: undefined,
|
||||
customer:
|
||||
invoice.customer && invoice.customer.type !== 'UNKNOWN'
|
||||
? {
|
||||
type: invoice.customer.type,
|
||||
legal_info: invoice.customer.legal ?? undefined,
|
||||
individual_info: invoice.customer.individual ?? undefined,
|
||||
}
|
||||
: {
|
||||
type: 'UNKNOWN',
|
||||
unknown_info: {
|
||||
name: unknown_customer?.name || '',
|
||||
economic_code: unknown_customer?.economic_code || '',
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export async function buildPayload(
|
||||
export async function buildOriginalPayload(
|
||||
prisma: PrismaService,
|
||||
invoiceId: string,
|
||||
posId: string,
|
||||
@@ -311,6 +323,7 @@ export async function buildPayload(
|
||||
sku_vat: true,
|
||||
good_id: true,
|
||||
service_id: true,
|
||||
discount: true,
|
||||
payload: true,
|
||||
good_snapshot: true,
|
||||
},
|
||||
@@ -357,6 +370,7 @@ export async function buildPayload(
|
||||
},
|
||||
},
|
||||
},
|
||||
unknown_customer: true,
|
||||
customer: {
|
||||
select: {
|
||||
type: true,
|
||||
@@ -396,38 +410,48 @@ export async function buildPayload(
|
||||
throw new NotFoundException('فاکتور مورد نظر یافت نشد.')
|
||||
}
|
||||
|
||||
const { partner } = (invoice.pos.complex.business_activity.consumer.legal ||
|
||||
invoice.pos.complex.business_activity.consumer.individual)!
|
||||
const { pos, id, invoice_number, invoice_date, total_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 {
|
||||
invoice_id: invoice.id,
|
||||
invoice_number: invoice.invoice_number,
|
||||
invoice_date: invoice.invoice_date,
|
||||
total_amount: Number(invoice.total_amount),
|
||||
economic_code: invoice.pos.complex.business_activity.economic_code,
|
||||
fiscal_id: invoice.pos.complex.business_activity.fiscal_id,
|
||||
invoice_template: invoice.pos.complex.business_activity.guild.invoice_template,
|
||||
tsp_token: invoice.pos.complex.business_activity.partner_token,
|
||||
settlement_type: invoice.settlement_type,
|
||||
id,
|
||||
invoice_number,
|
||||
invoice_date,
|
||||
settlement_type,
|
||||
total_amount: Number(total_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!,
|
||||
|
||||
payments: invoice.payments.map(payment => ({
|
||||
amount: Number(payment.amount),
|
||||
payment_method: payment.payment_method,
|
||||
paid_at: payment.paid_at,
|
||||
card_number: payment.terminal_info ? payment.terminal_info.stan : undefined,
|
||||
tracking_code: payment.terminal_info ? payment.terminal_info.rrn : undefined,
|
||||
terminal_info: {
|
||||
card_number: payment.terminal_info ? payment.terminal_info.stan : undefined,
|
||||
tracking_code: payment.terminal_info ? payment.terminal_info.rrn : undefined,
|
||||
},
|
||||
})),
|
||||
type: TspProviderRequestType.ORIGINAL,
|
||||
tsp_provider: partner.tsp_provider!,
|
||||
customer_type: invoice.customer?.type
|
||||
? TspProviderCustomerType.KNOWN
|
||||
: TspProviderCustomerType.UNKNOWN,
|
||||
customer: invoice.customer?.type
|
||||
? {
|
||||
type: invoice.customer.type,
|
||||
legal_info: invoice.customer.legal ?? undefined,
|
||||
individual_info: invoice.customer.individual ?? undefined,
|
||||
}
|
||||
: undefined,
|
||||
customer:
|
||||
invoice.customer && invoice.customer.type !== 'UNKNOWN'
|
||||
? {
|
||||
type: invoice.customer.type,
|
||||
legal_info: invoice.customer.legal ?? undefined,
|
||||
individual_info: invoice.customer.individual ?? undefined,
|
||||
}
|
||||
: {
|
||||
type: '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),
|
||||
@@ -449,84 +473,63 @@ export async function trySend(
|
||||
tspSwitchService: {
|
||||
send(
|
||||
payload: TspProviderOriginalSendPayloadDto,
|
||||
): Promise<TspProviderSendItemResultDto | null>
|
||||
): Promise<TspProviderOriginalSendItemResultDto>
|
||||
},
|
||||
payload: TspProviderOriginalSendPayloadDto,
|
||||
): Promise<TspProviderSendItemResultDto | null> {
|
||||
): Promise<TspProviderOriginalSendItemResultDto> {
|
||||
return (await tspSwitchService.send(payload)) || null
|
||||
}
|
||||
|
||||
export async function onResult(
|
||||
prisma: PrismaService,
|
||||
result: any,
|
||||
result: TspProviderOriginalSendItemResultDto,
|
||||
attempt_id: string,
|
||||
): Promise<TspProviderActionResponseDto> {
|
||||
let attemptUpdatedData: SaleInvoiceTspAttemptsUpdateInput = {}
|
||||
|
||||
const resultMessage = result.message
|
||||
|
||||
if (result) {
|
||||
if (result.hasError) {
|
||||
const updatedAttempt = await prisma.saleInvoiceTspAttempts.update({
|
||||
where: {
|
||||
id: attempt_id,
|
||||
attemptUpdatedData = {
|
||||
provider_request_payload: JSON.parse(
|
||||
JSON.stringify(result.provider_request_payload),
|
||||
),
|
||||
provider_response: JSON.parse(JSON.stringify(result.provider_response)),
|
||||
status: result.status,
|
||||
received_at: result.received_at || new Date().toISOString(),
|
||||
message: resultMessage
|
||||
? resultMessage
|
||||
: result.hasError
|
||||
? 'وجود مشکل در ارسال به سامانه مالیاتی'
|
||||
: 'فاکتور با موفقیت به سامانه مالیاتی ارسال شد.',
|
||||
invoice: {
|
||||
update: {
|
||||
tax_id: result.tax_id,
|
||||
},
|
||||
data: {
|
||||
provider_request_payload: result.provider_request_payload,
|
||||
provider_response_payload: JSON.parse(JSON.stringify(result)),
|
||||
status: result.status,
|
||||
sent_at: result.sent_at || new Date().toISOString(),
|
||||
received_at: result.received_at || new Date().toISOString(),
|
||||
message: result.message?.toString() || 'وجود مشکل در ارسال به سامانه مالیاتی',
|
||||
},
|
||||
select: {
|
||||
status: true,
|
||||
invoice: true,
|
||||
message: true,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
invoice: updatedAttempt.invoice,
|
||||
status: updatedAttempt.status,
|
||||
message: updatedAttempt.message,
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const updatedAttempt = await prisma.saleInvoiceTspAttempts.update({
|
||||
where: {
|
||||
id: attempt_id,
|
||||
},
|
||||
data: {
|
||||
provider_request_payload: result.provider_request_payload,
|
||||
provider_response_payload: JSON.parse(JSON.stringify(result)),
|
||||
status: result.status,
|
||||
sent_at: result.sent_at,
|
||||
received_at: result.received_at,
|
||||
message:
|
||||
result.message?.toString() || 'فاکتور با موفقیت به سامانه مالیاتی ارسال شد.',
|
||||
},
|
||||
select: {
|
||||
status: true,
|
||||
invoice: true,
|
||||
message: true,
|
||||
},
|
||||
})
|
||||
|
||||
return {
|
||||
invoice: updatedAttempt.invoice,
|
||||
status: updatedAttempt.status,
|
||||
message: updatedAttempt.message,
|
||||
if (result.hasError) {
|
||||
;((attemptUpdatedData.error_message = result.error_message),
|
||||
(attemptUpdatedData.validation_errors = JSON.parse(
|
||||
JSON.stringify(result.validation_errors),
|
||||
)),
|
||||
(attemptUpdatedData.fiscal_warnings = JSON.parse(
|
||||
JSON.stringify(result.fiscal_warnings),
|
||||
)))
|
||||
}
|
||||
} else {
|
||||
attemptUpdatedData = {
|
||||
status: TspProviderResponseStatus.SEND_FAILURE,
|
||||
message:
|
||||
'متاسفانه امکان ارسال فاکتور به سیستم مالیاتی در حال حاضر وجود ندارد. لطفا بعدا تلاش کنید.',
|
||||
received_at: new Date().toISOString(),
|
||||
}
|
||||
}
|
||||
|
||||
const updatedAttempt = await prisma.saleInvoiceTspAttempts.update({
|
||||
where: {
|
||||
id: attempt_id,
|
||||
},
|
||||
data: {
|
||||
provider_response_payload: {},
|
||||
status: TspProviderResponseStatus.FAILURE,
|
||||
message:
|
||||
'متاسفانه امکان ارسال فاکتور به سیستم مالیاتی در حال حاضر وجود ندارد. لطفا بعدا تلاش کنید.',
|
||||
received_at: new Date().toISOString(),
|
||||
},
|
||||
data: attemptUpdatedData,
|
||||
select: {
|
||||
status: true,
|
||||
invoice: true,
|
||||
|
||||
Reference in New Issue
Block a user