2026-04-27 10:45:39 +03:30
|
|
|
import { QUERY_CONSTANTS } from '@/common/queryConstants'
|
|
|
|
|
import consumer_mappersUtil from '@/common/utils/mappers/consumer_mappers.util'
|
2026-04-08 18:15:44 +03:30
|
|
|
import { SalesInvoiceSelect, SalesInvoiceWhereInput } from '@/generated/prisma/models'
|
|
|
|
|
import { PrismaService } from '@/prisma/prisma.service'
|
|
|
|
|
import { Injectable } from '@nestjs/common'
|
|
|
|
|
import { ResponseMapper } from 'common/response/response-mapper'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class CustomerSaleInvoicesService {
|
|
|
|
|
constructor(private readonly prisma: PrismaService) {}
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
private readonly defaultSelect: SalesInvoiceSelect = {
|
2026-04-08 18:15:44 +03:30
|
|
|
id: true,
|
|
|
|
|
code: true,
|
|
|
|
|
invoice_date: true,
|
|
|
|
|
notes: true,
|
|
|
|
|
total_amount: true,
|
|
|
|
|
pos: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
complex: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
business_activity: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-13 13:21:50 +03:30
|
|
|
consumer_account: {
|
2026-04-08 18:15:44 +03:30
|
|
|
select: {
|
|
|
|
|
role: true,
|
2026-04-11 14:47:05 +03:30
|
|
|
consumer: {
|
2026-04-08 18:15:44 +03:30
|
|
|
select: {
|
2026-04-27 10:45:39 +03:30
|
|
|
...QUERY_CONSTANTS.CONSUMER.infoSelect,
|
2026-04-08 18:15:44 +03:30
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
account: {
|
|
|
|
|
select: {
|
|
|
|
|
username: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
created_at: true,
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-26 09:54:48 +03:30
|
|
|
async findAll(consumer_id: string, customer_id: string, page = 1, perPage = 10) {
|
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: {
|
|
|
|
|
...this.defaultSelect,
|
|
|
|
|
_count: {
|
|
|
|
|
select: {
|
|
|
|
|
items: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-26 09:54:48 +03:30
|
|
|
skip: (page - 1) * perPage,
|
2026-04-08 18:15:44 +03:30
|
|
|
take: 10,
|
|
|
|
|
}),
|
|
|
|
|
await tx.salesInvoice.count({
|
|
|
|
|
where: salesWhere,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
2026-04-27 10:45:39 +03:30
|
|
|
const mappedAccounts = saleInvoices.map(saleInvoice => {
|
|
|
|
|
const { _count, consumer_account, ...rest } = saleInvoice
|
|
|
|
|
const { consumer, ...restConsumerAccount } = consumer_account as any
|
|
|
|
|
const mappedConsumer = consumer_mappersUtil(consumer)
|
2026-04-08 18:15:44 +03:30
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
items_count: _count.items,
|
2026-04-27 10:45:39 +03:30
|
|
|
consumer_account: {
|
|
|
|
|
...restConsumerAccount,
|
|
|
|
|
consumer: mappedConsumer,
|
|
|
|
|
},
|
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-04-27 10:45:39 +03:30
|
|
|
const saleInvoice = await this.prisma.salesInvoice.findUniqueOrThrow({
|
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: {
|
|
|
|
|
...this.defaultSelect,
|
|
|
|
|
items: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
notes: true,
|
|
|
|
|
unit_price: true,
|
|
|
|
|
discount: true,
|
|
|
|
|
quantity: true,
|
|
|
|
|
total_amount: true,
|
|
|
|
|
payload: true,
|
|
|
|
|
good: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
pricing_model: true,
|
2026-05-01 19:43:59 +03:30
|
|
|
measure_unit: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-04-08 18:15:44 +03:30
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
image_url: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
payments: {
|
|
|
|
|
select: {
|
|
|
|
|
amount: true,
|
|
|
|
|
paid_at: true,
|
|
|
|
|
payment_method: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-04-27 10:45:39 +03:30
|
|
|
const { _count, consumer_account, ...rest } = saleInvoice
|
|
|
|
|
const { consumer, ...restConsumerAccount } = consumer_account as any
|
|
|
|
|
const mappedConsumer = consumer_mappersUtil(consumer)
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single({
|
|
|
|
|
...rest,
|
|
|
|
|
items_count: _count.items,
|
|
|
|
|
consumer_account: {
|
|
|
|
|
...restConsumerAccount,
|
|
|
|
|
consumer: mappedConsumer,
|
|
|
|
|
},
|
|
|
|
|
})
|
2026-04-08 18:15:44 +03:30
|
|
|
}
|
|
|
|
|
}
|