2025-12-04 21:05:57 +03:30
|
|
|
import { Injectable } from '@nestjs/common'
|
2025-12-06 17:48:10 +03:30
|
|
|
import { ResponseMapper } from '../common/response/response-mapper'
|
2025-12-04 21:05:57 +03:30
|
|
|
import { PrismaService } from '../prisma/prisma.service'
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
2025-12-05 00:01:44 +03:30
|
|
|
export class SuppliersService {
|
2025-12-04 21:05:57 +03:30
|
|
|
constructor(private prisma: PrismaService) {}
|
2025-12-06 17:48:10 +03:30
|
|
|
async create(data: any) {
|
|
|
|
|
const item = await this.prisma.supplier.create({ data })
|
|
|
|
|
return ResponseMapper.create(item)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async findAll() {
|
|
|
|
|
const items = await this.prisma.supplier.findMany()
|
|
|
|
|
return ResponseMapper.list(items)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async findOne(id: number) {
|
|
|
|
|
const item = await this.prisma.supplier.findUnique({ where: { id } })
|
|
|
|
|
if (!item) return null
|
2025-12-21 19:09:41 +03:30
|
|
|
const receiptsInfo = await this.prisma.purchaseReceipt.aggregate({
|
|
|
|
|
where: {
|
|
|
|
|
supplierId: id,
|
|
|
|
|
},
|
|
|
|
|
_count: true,
|
|
|
|
|
_sum: {
|
|
|
|
|
totalAmount: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return ResponseMapper.single({
|
|
|
|
|
...item,
|
|
|
|
|
receiptsOverview: {
|
|
|
|
|
totalPayment: receiptsInfo._sum.totalAmount,
|
|
|
|
|
count: receiptsInfo._count,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getInvoices(supplierId: number) {
|
|
|
|
|
const items = await this.prisma.purchaseReceipt.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
supplierId,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
inventory: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
items: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
count: true,
|
|
|
|
|
fee: true,
|
|
|
|
|
total: true,
|
|
|
|
|
product: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
sku: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-12-26 22:09:46 +03:30
|
|
|
payments: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
amount: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-12-21 19:09:41 +03:30
|
|
|
},
|
|
|
|
|
omit: {
|
|
|
|
|
inventoryId: true,
|
|
|
|
|
supplierId: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.list(items)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getInvoice(supplierId: number, invoiceId: number) {
|
|
|
|
|
const invoice = await this.prisma.purchaseReceipt.findUnique({
|
|
|
|
|
where: {
|
|
|
|
|
id: invoiceId,
|
|
|
|
|
supplierId,
|
|
|
|
|
},
|
|
|
|
|
include: {
|
|
|
|
|
inventory: {
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
items: true,
|
|
|
|
|
},
|
|
|
|
|
omit: {
|
|
|
|
|
inventoryId: true,
|
|
|
|
|
supplierId: true,
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
return ResponseMapper.single(invoice)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async update(id: number, data: any) {
|
|
|
|
|
const item = await this.prisma.supplier.update({ where: { id }, data })
|
|
|
|
|
return ResponseMapper.update(item)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
|
2025-12-06 17:48:10 +03:30
|
|
|
async remove(id: number) {
|
|
|
|
|
const item = await this.prisma.supplier.delete({ where: { id } })
|
|
|
|
|
return ResponseMapper.single(item)
|
2025-12-04 21:05:57 +03:30
|
|
|
}
|
|
|
|
|
}
|