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:
@@ -1,4 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ShortEntity } from '../common/interfaces/response-models'
|
||||
import { ResponseMapper } from '../common/response/response-mapper'
|
||||
import { PrismaService } from '../prisma/prisma.service'
|
||||
|
||||
@@ -11,14 +12,52 @@ export class InventoriesService {
|
||||
}
|
||||
|
||||
async findAll() {
|
||||
const items = await this.prisma.inventory.findMany()
|
||||
return ResponseMapper.list(items)
|
||||
const items = await this.prisma.inventory.findMany({
|
||||
include: { productCharges: { include: { product: true } } },
|
||||
})
|
||||
|
||||
const mapped = items.map(i => {
|
||||
const productsMap: Record<number, ShortEntity> = {}
|
||||
for (const pc of i.productCharges ?? []) {
|
||||
if (pc.product) {
|
||||
productsMap[pc.product.id] = { id: pc.product.id, name: pc.product.name }
|
||||
}
|
||||
}
|
||||
const groupedProducts = Object.values(productsMap)
|
||||
const { productCharges, ...rest } = i as any
|
||||
return { ...rest, groupedProducts }
|
||||
})
|
||||
|
||||
return ResponseMapper.list(mapped)
|
||||
}
|
||||
|
||||
async findOne(id: number) {
|
||||
const item = await this.prisma.inventory.findUnique({ where: { id } })
|
||||
const item = await this.prisma.inventory.findUnique({
|
||||
where: { id },
|
||||
include: { productCharges: { include: { product: true } } },
|
||||
})
|
||||
if (!item) return null
|
||||
return ResponseMapper.single(item)
|
||||
const productsMap: Record<number, { id: number; name: string; count: number }> = {}
|
||||
for (const pc of item.productCharges ?? []) {
|
||||
const pid = pc.productId
|
||||
const pname = pc.product?.name ?? `#${pid}`
|
||||
if (!productsMap[pid]) productsMap[pid] = { id: pid, name: pname, count: 0 }
|
||||
productsMap[pid].count += Number(pc.count)
|
||||
}
|
||||
|
||||
const productsWithCount = Object.values(productsMap)
|
||||
const groupedProducts: ShortEntity[] = productsWithCount.map(p => ({
|
||||
id: p.id,
|
||||
name: p.name,
|
||||
}))
|
||||
|
||||
const { productCharges, ...rest } = item as any
|
||||
const itemWithCounts = {
|
||||
...rest,
|
||||
products: productsWithCount,
|
||||
groupedProducts,
|
||||
}
|
||||
return ResponseMapper.single(itemWithCounts)
|
||||
}
|
||||
|
||||
async update(id: number, data: any) {
|
||||
|
||||
Reference in New Issue
Block a user