import { ResponseMapper } from '@/common/response/response-mapper' import { SharedSaleInvoiceActionsService } from '@/common/services/saleInvoices/sale-invoice-actions.service' import { SalesInvoiceWhereInput } from '@/generated/prisma/models' import { PrismaService } from '@/prisma/prisma.service' import { Injectable, NotFoundException } from '@nestjs/common' @Injectable() export class SalesInvoicesService { constructor( private prisma: PrismaService, private readonly sharedSaleInvoiceActionsService: SharedSaleInvoiceActionsService, ) {} async findAll(consumer_id: string, complex_id: string, pos_id: string) { const defaultWhere: SalesInvoiceWhereInput = { pos_id, pos: { complex: { id: complex_id, business_activity: { consumer_id, }, }, }, } const perPage = 10 const page = 1 const [items, total] = await this.prisma.$transaction([ this.prisma.salesInvoice.findMany({ where: defaultWhere, select: { id: true, code: true, invoice_date: true, notes: true, total_amount: true, items: { select: { measure_unit_code: true, measure_unit_text: true, sku_code: true, discount_amount: true, tax_amount: true, notes: true, quantity: true, total_amount: true, unit_price: true, payload: true, good: { select: { id: true, name: true, sku: { select: { id: true, name: true, }, }, barcode: true, local_sku: true, pricing_model: true, measure_unit: { select: { id: true, name: true, }, }, category: { select: { id: true, name: true, }, }, }, }, }, }, payments: { select: { amount: true, paid_at: true, payment_method: true, }, }, customer: { select: { type: true, individual: { select: { economic_code: true, first_name: true, last_name: true, postal_code: true, national_id: true, }, }, legal: { select: { economic_code: true, postal_code: true, registration_number: true, }, }, }, }, unknown_customer: true, }, skip: (page - 1) * perPage, take: perPage, }), this.prisma.salesInvoice.count({ where: defaultWhere, }), ]) return ResponseMapper.paginate(items, { total, page, perPage }) } findOne(complex_id: string, pos_id: string, id: string) { // TODO: Implement fetching a single sales invoice return {} } 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) } }