2026-04-27 10:45:39 +03:30
|
|
|
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
2026-06-14 16:34:00 +03:30
|
|
|
import { SharedSaleInvoicePaginationService } from '@/common/services/saleInvoices/sale-invoice-pagination.service'
|
|
|
|
|
import { translateEnumValue } from '@/common/utils'
|
|
|
|
|
import { TspProviderResponseStatus } from '@/generated/prisma/enums'
|
|
|
|
|
import { SalesInvoiceWhereInput } from '@/generated/prisma/models'
|
2026-04-08 18:15:44 +03:30
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-06-14 16:34:00 +03:30
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common'
|
2026-04-08 18:15:44 +03:30
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class CustomerSaleInvoicesService {
|
2026-06-14 16:34:00 +03:30
|
|
|
constructor(
|
|
|
|
|
private readonly prisma: PrismaService,
|
|
|
|
|
private readonly sharedSaleInvoicePaginationService: SharedSaleInvoicePaginationService,
|
|
|
|
|
) {}
|
2026-04-08 18:15:44 +03:30
|
|
|
|
2026-06-14 16:34:00 +03:30
|
|
|
async findAll(
|
|
|
|
|
consumer_id: string,
|
|
|
|
|
customer_id: string,
|
|
|
|
|
requestedPage = 1,
|
|
|
|
|
requestedPerPage = 10,
|
|
|
|
|
) {
|
|
|
|
|
const { page, perPage, skip, take } =
|
|
|
|
|
this.sharedSaleInvoicePaginationService.normalize(requestedPage, requestedPerPage)
|
2026-04-08 18:15:44 +03:30
|
|
|
|
|
|
|
|
const salesWhere: SalesInvoiceWhereInput = {
|
|
|
|
|
customer_id,
|
|
|
|
|
pos: {
|
|
|
|
|
complex: {
|
|
|
|
|
business_activity: {
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
const [saleInvoices, total] = await this.prisma.$transaction(async tx => [
|
2026-04-08 18:15:44 +03:30
|
|
|
await tx.salesInvoice.findMany({
|
|
|
|
|
where: salesWhere,
|
|
|
|
|
select: {
|
2026-06-14 16:34:00 +03:30
|
|
|
...QUERY_CONSTANTS.SALE_INVOICE.summarySelect,
|
|
|
|
|
pos: {
|
|
|
|
|
select: {
|
|
|
|
|
name: true,
|
|
|
|
|
complex: {
|
|
|
|
|
select: {
|
|
|
|
|
name: true,
|
|
|
|
|
business_activity: {
|
|
|
|
|
select: {
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-08 18:15:44 +03:30
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
items: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-06-14 16:34:00 +03:30
|
|
|
skip,
|
|
|
|
|
take,
|
2026-04-08 18:15:44 +03:30
|
|
|
}),
|
|
|
|
|
await tx.salesInvoice.count({
|
|
|
|
|
where: salesWhere,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
const mappedAccounts = saleInvoices.map(saleInvoice => {
|
2026-06-14 16:34:00 +03:30
|
|
|
const { _count, consumer_account, last_tsp_status, ...rest } = saleInvoice
|
2026-04-08 18:15:44 +03:30
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
items_count: _count.items,
|
2026-06-14 16:34:00 +03:30
|
|
|
status: translateEnumValue('TspProviderResponseStatus', last_tsp_status),
|
2026-04-08 18:15:44 +03:30
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.paginate(mappedAccounts, {
|
2026-04-26 09:54:48 +03:30
|
|
|
total,
|
2026-04-08 18:15:44 +03:30
|
|
|
page,
|
2026-04-26 09:54:48 +03:30
|
|
|
perPage,
|
2026-04-08 18:15:44 +03:30
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findOne(consumer_id: string, customer_id: string, id: string) {
|
2026-06-14 16:34:00 +03:30
|
|
|
const invoice = await this.prisma.salesInvoice.findUnique({
|
2026-04-08 18:15:44 +03:30
|
|
|
where: {
|
|
|
|
|
id,
|
|
|
|
|
customer_id,
|
|
|
|
|
pos: {
|
|
|
|
|
complex: {
|
|
|
|
|
business_activity: {
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: {
|
2026-06-14 16:34:00 +03:30
|
|
|
...QUERY_CONSTANTS.SALE_INVOICE.select,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
})
|
2026-04-27 10:45:39 +03:30
|
|
|
|
2026-06-14 16:34:00 +03:30
|
|
|
if (invoice) {
|
|
|
|
|
const { type, ...rest } = invoice
|
|
|
|
|
const mappedInvoice = {
|
|
|
|
|
...rest,
|
|
|
|
|
type: translateEnumValue('TspProviderRequestType', type),
|
|
|
|
|
status: translateEnumValue(
|
|
|
|
|
'TspProviderResponseStatus',
|
|
|
|
|
invoice.last_tsp_status || TspProviderResponseStatus.NOT_SEND,
|
|
|
|
|
),
|
|
|
|
|
settlement_type: translateEnumValue(
|
|
|
|
|
'InvoiceSettlementType',
|
|
|
|
|
invoice.settlement_type,
|
|
|
|
|
),
|
|
|
|
|
}
|
|
|
|
|
return ResponseMapper.single(mappedInvoice)
|
|
|
|
|
}
|
|
|
|
|
throw new NotFoundException('صورتحساب مورد نظر شما یافت نشد.')
|
2026-04-08 18:15:44 +03:30
|
|
|
}
|
|
|
|
|
}
|