2026-05-10 14:14:01 +03:30
|
|
|
import { SharedSaleInvoiceActionsService } from '@/common/services/saleInvoices/sale-invoice-actions.service'
|
2026-03-30 13:16:36 +03:30
|
|
|
import { ResponseMapper } from '@/common/response/response-mapper'
|
|
|
|
|
import { SalesInvoiceWhereInput } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
2026-05-10 14:14:01 +03:30
|
|
|
import { Injectable, NotFoundException } from '@nestjs/common'
|
2026-03-30 13:16:36 +03:30
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class SalesInvoicesService {
|
2026-05-10 14:14:01 +03:30
|
|
|
constructor(
|
|
|
|
|
private prisma: PrismaService,
|
|
|
|
|
private readonly sharedSaleInvoiceActionsService: SharedSaleInvoiceActionsService,
|
|
|
|
|
) {}
|
2026-03-30 13:16:36 +03:30
|
|
|
|
2026-04-11 14:47:05 +03:30
|
|
|
async findAll(consumer_id: string, complex_id: string, pos_id: string) {
|
2026-03-30 13:16:36 +03:30
|
|
|
const defaultWhere: SalesInvoiceWhereInput = {
|
|
|
|
|
pos_id,
|
|
|
|
|
pos: {
|
|
|
|
|
complex: {
|
|
|
|
|
id: complex_id,
|
|
|
|
|
business_activity: {
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer_id,
|
2026-03-30 13:16:36 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
const perPage = 10
|
2026-03-30 13:16:36 +03:30
|
|
|
const page = 1
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
const [items, total] = await this.prisma.$transaction([
|
2026-03-30 13:16:36 +03:30
|
|
|
this.prisma.salesInvoice.findMany({
|
|
|
|
|
where: defaultWhere,
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
invoice_date: true,
|
|
|
|
|
notes: true,
|
|
|
|
|
total_amount: true,
|
|
|
|
|
|
|
|
|
|
items: {
|
|
|
|
|
select: {
|
2026-05-01 19:43:59 +03:30
|
|
|
measure_unit_code: true,
|
|
|
|
|
measure_unit_text: true,
|
|
|
|
|
sku_code: true,
|
2026-03-30 13:16:36 +03:30
|
|
|
discount: true,
|
|
|
|
|
notes: true,
|
|
|
|
|
quantity: true,
|
|
|
|
|
total_amount: true,
|
|
|
|
|
unit_price: true,
|
|
|
|
|
payload: true,
|
|
|
|
|
good: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
sku: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-30 13:16:36 +03:30
|
|
|
barcode: true,
|
|
|
|
|
local_sku: true,
|
|
|
|
|
pricing_model: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
measure_unit: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-30 13:16:36 +03:30
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
payments: {
|
|
|
|
|
select: {
|
|
|
|
|
amount: true,
|
|
|
|
|
paid_at: true,
|
|
|
|
|
payment_method: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
customer: {
|
|
|
|
|
select: {
|
|
|
|
|
type: true,
|
2026-04-27 10:45:39 +03:30
|
|
|
individual: {
|
2026-03-30 13:16:36 +03:30
|
|
|
select: {
|
|
|
|
|
economic_code: true,
|
|
|
|
|
first_name: true,
|
|
|
|
|
last_name: true,
|
|
|
|
|
postal_code: true,
|
|
|
|
|
national_id: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-27 10:45:39 +03:30
|
|
|
legal: {
|
2026-03-30 13:16:36 +03:30
|
|
|
select: {
|
|
|
|
|
economic_code: true,
|
|
|
|
|
postal_code: true,
|
|
|
|
|
registration_number: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
unknown_customer: true,
|
|
|
|
|
},
|
2026-04-26 09:54:48 +03:30
|
|
|
skip: (page - 1) * perPage,
|
|
|
|
|
take: perPage,
|
2026-03-30 13:16:36 +03:30
|
|
|
}),
|
|
|
|
|
this.prisma.salesInvoice.count({
|
|
|
|
|
where: defaultWhere,
|
|
|
|
|
}),
|
|
|
|
|
])
|
2026-04-26 09:54:48 +03:30
|
|
|
return ResponseMapper.paginate(items, { total, page, perPage })
|
2026-03-30 13:16:36 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
findOne(complex_id: string, pos_id: string, id: string) {
|
|
|
|
|
// TODO: Implement fetching a single sales invoice
|
|
|
|
|
return {}
|
|
|
|
|
}
|
2026-05-10 14:14:01 +03:30
|
|
|
|
|
|
|
|
async send(
|
|
|
|
|
consumerAccountId: string,
|
|
|
|
|
complexId: string,
|
|
|
|
|
posId: string,
|
|
|
|
|
invoiceId: string,
|
|
|
|
|
) {
|
|
|
|
|
const invoice = await this.sharedSaleInvoiceActionsService.send(
|
|
|
|
|
consumerAccountId,
|
|
|
|
|
posId,
|
|
|
|
|
invoiceId,
|
|
|
|
|
)
|
|
|
|
|
return ResponseMapper.single(invoice)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async retry(
|
|
|
|
|
consumerAccountId: string,
|
|
|
|
|
complexId: string,
|
|
|
|
|
posId: string,
|
|
|
|
|
invoiceId: string,
|
|
|
|
|
) {
|
|
|
|
|
const invoice = await this.sharedSaleInvoiceActionsService.retry(
|
|
|
|
|
consumerAccountId,
|
|
|
|
|
posId,
|
|
|
|
|
invoiceId,
|
|
|
|
|
)
|
|
|
|
|
return ResponseMapper.single(invoice)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async revoke(
|
|
|
|
|
consumerAccountId: string,
|
|
|
|
|
complexId: string,
|
|
|
|
|
posId: string,
|
|
|
|
|
invoiceId: string,
|
|
|
|
|
) {
|
|
|
|
|
const pos = await this.prisma.pos.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: posId,
|
|
|
|
|
complex: {
|
|
|
|
|
id: complexId,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
complex: {
|
|
|
|
|
select: {
|
|
|
|
|
business_activity_id: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
if (!pos) throw new NotFoundException('پایانه فروش مورد نظر یافت نشد.')
|
|
|
|
|
|
|
|
|
|
const invoice = await this.sharedSaleInvoiceActionsService.revoke(
|
|
|
|
|
consumerAccountId,
|
|
|
|
|
posId,
|
|
|
|
|
complexId,
|
|
|
|
|
pos.complex.business_activity_id,
|
|
|
|
|
invoiceId,
|
|
|
|
|
)
|
|
|
|
|
return ResponseMapper.single(invoice)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async inquiry(consumerAccountId: string, posId: string, invoiceId: string) {
|
|
|
|
|
const invoice = await this.sharedSaleInvoiceActionsService.inquiry(
|
|
|
|
|
consumerAccountId,
|
|
|
|
|
posId,
|
|
|
|
|
invoiceId,
|
|
|
|
|
)
|
|
|
|
|
return ResponseMapper.single(invoice)
|
|
|
|
|
}
|
2026-03-30 13:16:36 +03:30
|
|
|
}
|