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) {}
|
|
|
|
|
|
|
|
|
|
defaultSelect: SalesInvoiceSelect = {
|
|
|
|
|
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: {
|
|
|
|
|
first_name: true,
|
|
|
|
|
last_name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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-26 09:54:48 +03:30
|
|
|
const [accounts, 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,
|
|
|
|
|
}),
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
const mappedAccounts = accounts.map(account => {
|
|
|
|
|
const { _count, ...rest } = account
|
|
|
|
|
return {
|
|
|
|
|
...rest,
|
|
|
|
|
items_count: _count.items,
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
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-08 18:15:44 +03:30
|
|
|
const account = await this.prisma.salesInvoice.findUniqueOrThrow({
|
|
|
|
|
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,
|
|
|
|
|
unit_type: true,
|
|
|
|
|
category: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
image_url: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
payments: {
|
|
|
|
|
select: {
|
|
|
|
|
amount: true,
|
|
|
|
|
paid_at: true,
|
|
|
|
|
payment_method: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.single(account)
|
|
|
|
|
}
|
|
|
|
|
}
|