feat(inventories): add cardex retrieval for inventory and product
feat(pos): update sale invoice creation to include customer and item details feat(pos): enhance POS account service to include today's sales information feat(pos): refactor order DTO to create sale invoice with detailed item structure feat(products): add minimum stock alert level to product creation and update DTOs fix(triggers): implement stock validation and movement logging for sales invoice items refactor(database): update sales invoices schema to include posAccountId and remove inventoryId
This commit is contained in:
@@ -1,29 +1,14 @@
|
||||
import { Injectable } from '@nestjs/common'
|
||||
import { ResponseMapper } from '../../common/response/response-mapper'
|
||||
import { Prisma } from '../../generated/prisma/client'
|
||||
import { SalesInvoiceCreateInput } from '../../generated/prisma/models'
|
||||
import { PrismaService } from '../../prisma/prisma.service'
|
||||
import { CreateOrderDto } from './dto/create-pos.dto'
|
||||
import { CreateSaleInvoiceDto } from './dto/create-pos.dto'
|
||||
|
||||
@Injectable()
|
||||
export class PosService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
|
||||
async getDefaultInventoryId() {
|
||||
const inventory = await this.prisma.inventory.findFirst({
|
||||
where: { isPointOfSale: true },
|
||||
select: { id: true },
|
||||
})
|
||||
return inventory?.id || null
|
||||
}
|
||||
|
||||
async getDefaultPosId(inventoryId: number) {
|
||||
const pos = await this.prisma.posAccount.findFirst({
|
||||
where: { inventoryId },
|
||||
select: { id: true },
|
||||
})
|
||||
return pos?.id || null
|
||||
}
|
||||
|
||||
async getInfo(posId: number) {
|
||||
const info = await this.prisma.posAccount.findUniqueOrThrow({
|
||||
where: { id: posId },
|
||||
@@ -173,41 +158,34 @@ export class PosService {
|
||||
return ResponseMapper.list(categories)
|
||||
}
|
||||
|
||||
async createOrder(posId: number, data: CreateOrderDto) {
|
||||
const pos = await this.prisma.posAccount.findUniqueOrThrow({
|
||||
where: { id: posId },
|
||||
select: { inventoryId: true },
|
||||
})
|
||||
const inventoryId = pos.inventoryId
|
||||
|
||||
const { customerId, ...res } = data
|
||||
const preparedOrder = { ...res } as any
|
||||
async createOrder(posId: number, data: CreateSaleInvoiceDto) {
|
||||
const { customerId, items, ...res } = data
|
||||
const lastCode = await this.prisma.salesInvoice
|
||||
.findFirst({
|
||||
where: { posAccountId: posId },
|
||||
orderBy: { code: 'desc' },
|
||||
select: { code: true },
|
||||
})
|
||||
.then(si => si?.code || '0')
|
||||
|
||||
preparedOrder.code = String(Number(lastCode) + 1)
|
||||
const newCode = String(Number(lastCode) + 1)
|
||||
|
||||
preparedOrder.totalAmount = data.items.reduce(
|
||||
(acc, item) => acc + Number(item.fee),
|
||||
0,
|
||||
)
|
||||
const totalAmount = data.items.reduce((acc, item) => acc + item.count * item.fee, 0)
|
||||
|
||||
preparedOrder.inventory = { connect: { id: inventoryId } }
|
||||
preparedOrder.customer = { connect: { id: customerId } }
|
||||
|
||||
if (Object.prototype.hasOwnProperty.call(preparedOrder, 'items')) {
|
||||
preparedOrder.items = {
|
||||
create: preparedOrder.items.map((item: any) => ({
|
||||
const preparedOrder: SalesInvoiceCreateInput = {
|
||||
...res,
|
||||
code: newCode,
|
||||
posAccount: { connect: { id: posId } },
|
||||
customer: customerId ? { connect: { id: customerId } } : undefined,
|
||||
totalAmount: totalAmount,
|
||||
items: {
|
||||
create: items.map(item => ({
|
||||
product: { connect: { id: Number(item.productId) } },
|
||||
count: item.count,
|
||||
fee: item.fee,
|
||||
total: item.count * item.fee,
|
||||
})),
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
const item = await this.prisma.salesInvoice.create({ data: preparedOrder })
|
||||
|
||||
Reference in New Issue
Block a user