feat: implement purchase receipt items management with create, update, find, and delete functionalities
feat: add purchase receipts management with create, update, find, and delete functionalities feat: implement sales invoice items management with create, update, find, and delete functionalities feat: add sales invoices management with create, update, find, and delete functionalities feat: implement stock adjustments management with create, update, find, and delete functionalities feat: add stock balance retrieval functionality feat: implement stock movements management with create, update, find, and delete functionalities
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from '../common/response/response-mapper'
|
||||
import { PrismaService } from '../prisma/prisma.service'
|
||||
import { CreateSalesInvoiceItemDto } from './dto/create-sales-invoice-item.dto'
|
||||
|
||||
@Injectable()
|
||||
export class SalesInvoiceItemsService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async create(dto: CreateSalesInvoiceItemDto) {
|
||||
const payload: any = { ...dto }
|
||||
if (Object.prototype.hasOwnProperty.call(payload, 'invoiceId')) {
|
||||
payload.invoice = { connect: { id: Number(payload.invoiceId) } }
|
||||
delete payload.invoiceId
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(payload, 'productId')) {
|
||||
payload.product = { connect: { id: Number(payload.productId) } }
|
||||
delete payload.productId
|
||||
}
|
||||
|
||||
const item = await this.prisma.salesInvoiceItem.create({ data: payload })
|
||||
return ResponseMapper.create(item)
|
||||
}
|
||||
async findAll(invoiceId?: number) {
|
||||
const where = invoiceId ? { invoiceId: Number(invoiceId) } : undefined
|
||||
const items = await this.prisma.salesInvoiceItem.findMany({
|
||||
where,
|
||||
include: { product: true },
|
||||
})
|
||||
return ResponseMapper.list(items)
|
||||
}
|
||||
|
||||
async findOne(id: number) {
|
||||
const item = await this.prisma.salesInvoiceItem.findUnique({
|
||||
where: { id },
|
||||
include: { product: true, invoice: true },
|
||||
})
|
||||
if (!item) return null
|
||||
return ResponseMapper.single(item)
|
||||
}
|
||||
|
||||
async createForInvoice(invoiceId: number, dto: CreateSalesInvoiceItemDto) {
|
||||
const payload: any = { ...dto, invoiceId }
|
||||
return this.create(payload)
|
||||
}
|
||||
|
||||
async findAllForInvoice(invoiceId: number) {
|
||||
return this.findAll(invoiceId)
|
||||
}
|
||||
|
||||
async findOneForInvoice(invoiceId: number, id: number) {
|
||||
const item = await this.prisma.salesInvoiceItem.findFirst({
|
||||
where: { id, invoiceId },
|
||||
include: { product: true, invoice: true },
|
||||
})
|
||||
if (!item) return null
|
||||
return ResponseMapper.single(item)
|
||||
}
|
||||
|
||||
async update(id: number, data: any) {
|
||||
const payload: any = { ...data }
|
||||
if (Object.prototype.hasOwnProperty.call(payload, 'productId')) {
|
||||
payload.product = { connect: { id: Number(payload.productId) } }
|
||||
delete payload.productId
|
||||
}
|
||||
if (Object.prototype.hasOwnProperty.call(payload, 'invoiceId')) {
|
||||
payload.invoice = { connect: { id: Number(payload.invoiceId) } }
|
||||
delete payload.invoiceId
|
||||
}
|
||||
|
||||
const item = await this.prisma.salesInvoiceItem.update({
|
||||
where: { id },
|
||||
data: payload,
|
||||
})
|
||||
return ResponseMapper.update(item)
|
||||
}
|
||||
|
||||
async updateForInvoice(invoiceId: number, id: number, data: any) {
|
||||
const existing = await this.prisma.salesInvoiceItem.findFirst({
|
||||
where: { id, invoiceId },
|
||||
})
|
||||
if (!existing) return null
|
||||
return this.update(id, data)
|
||||
}
|
||||
|
||||
async remove(id: number) {
|
||||
const item = await this.prisma.salesInvoiceItem.delete({ where: { id } })
|
||||
return ResponseMapper.single(item)
|
||||
}
|
||||
|
||||
async removeForInvoice(invoiceId: number, id: number) {
|
||||
const existing = await this.prisma.salesInvoiceItem.findFirst({
|
||||
where: { id, invoiceId },
|
||||
})
|
||||
if (!existing) return null
|
||||
return this.remove(id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user