purchase slice 1
This commit is contained in:
@@ -1,11 +1,12 @@
|
||||
import { permissions, purchaseOrderStatuses } from "@mrp/shared";
|
||||
import { inventoryUnitsOfMeasure } from "@mrp/shared/dist/inventory/types.js";
|
||||
import { purchaseOrderStatuses } from "@mrp/shared";
|
||||
import { Router } from "express";
|
||||
import { z } from "zod";
|
||||
|
||||
import { fail, ok } from "../../lib/http.js";
|
||||
import { requirePermissions } from "../../lib/rbac.js";
|
||||
import {
|
||||
createPurchaseReceipt,
|
||||
createPurchaseOrder,
|
||||
getPurchaseOrderById,
|
||||
listPurchaseOrders,
|
||||
@@ -42,6 +43,19 @@ const purchaseStatusUpdateSchema = z.object({
|
||||
status: z.enum(purchaseOrderStatuses),
|
||||
});
|
||||
|
||||
const purchaseReceiptLineSchema = z.object({
|
||||
purchaseOrderLineId: z.string().trim().min(1),
|
||||
quantity: z.number().int().positive(),
|
||||
});
|
||||
|
||||
const purchaseReceiptSchema = z.object({
|
||||
receivedAt: z.string().datetime(),
|
||||
warehouseId: z.string().trim().min(1),
|
||||
locationId: z.string().trim().min(1),
|
||||
notes: z.string(),
|
||||
lines: z.array(purchaseReceiptLineSchema),
|
||||
});
|
||||
|
||||
function getRouteParam(value: unknown) {
|
||||
return typeof value === "string" ? value : null;
|
||||
}
|
||||
@@ -126,3 +140,26 @@ purchasingRouter.patch("/orders/:orderId/status", requirePermissions(["purchasin
|
||||
|
||||
return ok(response, result.document);
|
||||
});
|
||||
|
||||
purchasingRouter.post(
|
||||
"/orders/:orderId/receipts",
|
||||
requirePermissions([permissions.purchasingWrite, permissions.inventoryWrite]),
|
||||
async (request, response) => {
|
||||
const orderId = getRouteParam(request.params.orderId);
|
||||
if (!orderId) {
|
||||
return fail(response, 400, "INVALID_INPUT", "Purchase order id is invalid.");
|
||||
}
|
||||
|
||||
const parsed = purchaseReceiptSchema.safeParse(request.body);
|
||||
if (!parsed.success) {
|
||||
return fail(response, 400, "INVALID_INPUT", "Purchase receipt payload is invalid.");
|
||||
}
|
||||
|
||||
const result = await createPurchaseReceipt(orderId, parsed.data, request.authUser?.id);
|
||||
if (!result.ok) {
|
||||
return fail(response, 400, "INVALID_INPUT", result.reason);
|
||||
}
|
||||
|
||||
return ok(response, result.document, 201);
|
||||
}
|
||||
);
|
||||
|
||||
@@ -1,15 +1,10 @@
|
||||
import type {
|
||||
PurchaseLineInput,
|
||||
PurchaseOrderDetailDto,
|
||||
PurchaseOrderInput,
|
||||
PurchaseOrderStatus,
|
||||
PurchaseOrderSummaryDto,
|
||||
PurchaseVendorOptionDto,
|
||||
} from "@mrp/shared";
|
||||
import { Prisma } from "@prisma/client";
|
||||
import type { PurchaseLineInput, PurchaseOrderDetailDto, PurchaseOrderInput, PurchaseOrderStatus, PurchaseOrderSummaryDto, PurchaseVendorOptionDto } from "@mrp/shared";
|
||||
import type { PurchaseReceiptDto, PurchaseReceiptInput } from "@mrp/shared/dist/purchasing/types.js";
|
||||
|
||||
import { prisma } from "../../lib/prisma.js";
|
||||
|
||||
const purchaseOrderModel = (prisma as any).purchaseOrder;
|
||||
const purchaseOrderModel = prisma.purchaseOrder;
|
||||
|
||||
type PurchaseLineRecord = {
|
||||
id: string;
|
||||
@@ -18,6 +13,9 @@ type PurchaseLineRecord = {
|
||||
unitOfMeasure: string;
|
||||
unitCost: number;
|
||||
position: number;
|
||||
receiptLines?: {
|
||||
quantity: number;
|
||||
}[];
|
||||
item: {
|
||||
id: string;
|
||||
sku: string;
|
||||
@@ -25,6 +23,42 @@ type PurchaseLineRecord = {
|
||||
};
|
||||
};
|
||||
|
||||
type PurchaseReceiptLineRecord = {
|
||||
id: string;
|
||||
purchaseOrderLineId: string;
|
||||
quantity: number;
|
||||
purchaseOrderLine: {
|
||||
item: {
|
||||
id: string;
|
||||
sku: string;
|
||||
name: string;
|
||||
};
|
||||
};
|
||||
};
|
||||
|
||||
type PurchaseReceiptRecord = {
|
||||
id: string;
|
||||
receiptNumber: string;
|
||||
receivedAt: Date;
|
||||
notes: string;
|
||||
createdAt: Date;
|
||||
warehouse: {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
};
|
||||
location: {
|
||||
id: string;
|
||||
code: string;
|
||||
name: string;
|
||||
};
|
||||
createdBy: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
} | null;
|
||||
lines: PurchaseReceiptLineRecord[];
|
||||
};
|
||||
|
||||
type PurchaseOrderRecord = {
|
||||
id: string;
|
||||
documentNumber: string;
|
||||
@@ -43,6 +77,7 @@ type PurchaseOrderRecord = {
|
||||
currencyCode: string | null;
|
||||
};
|
||||
lines: PurchaseLineRecord[];
|
||||
receipts: PurchaseReceiptRecord[];
|
||||
};
|
||||
|
||||
function roundMoney(value: number) {
|
||||
@@ -65,6 +100,40 @@ function calculateTotals(subtotal: number, taxPercent: number, freightAmount: nu
|
||||
};
|
||||
}
|
||||
|
||||
function getCreatedByName(createdBy: PurchaseReceiptRecord["createdBy"]) {
|
||||
return createdBy ? `${createdBy.firstName} ${createdBy.lastName}`.trim() : "System";
|
||||
}
|
||||
|
||||
function mapPurchaseReceipt(record: PurchaseReceiptRecord, purchaseOrderId: string): PurchaseReceiptDto {
|
||||
const lines = record.lines.map((line: PurchaseReceiptLineRecord) => ({
|
||||
id: line.id,
|
||||
purchaseOrderLineId: line.purchaseOrderLineId,
|
||||
itemId: line.purchaseOrderLine.item.id,
|
||||
itemSku: line.purchaseOrderLine.item.sku,
|
||||
itemName: line.purchaseOrderLine.item.name,
|
||||
quantity: line.quantity,
|
||||
}));
|
||||
|
||||
return {
|
||||
id: record.id,
|
||||
receiptNumber: record.receiptNumber,
|
||||
purchaseOrderId,
|
||||
receivedAt: record.receivedAt.toISOString(),
|
||||
notes: record.notes,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
createdByName: getCreatedByName(record.createdBy),
|
||||
warehouseId: record.warehouse.id,
|
||||
warehouseCode: record.warehouse.code,
|
||||
warehouseName: record.warehouse.name,
|
||||
locationId: record.location.id,
|
||||
locationCode: record.location.code,
|
||||
locationName: record.location.name,
|
||||
totalQuantity: lines.reduce((sum: number, line: PurchaseReceiptDto["lines"][number]) => sum + line.quantity, 0),
|
||||
lineCount: lines.length,
|
||||
lines,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeLines(lines: PurchaseLineInput[]) {
|
||||
return lines
|
||||
.map((line, index) => ({
|
||||
@@ -111,6 +180,14 @@ async function validateLines(lines: PurchaseLineInput[]) {
|
||||
}
|
||||
|
||||
function mapPurchaseOrder(record: PurchaseOrderRecord): PurchaseOrderDetailDto {
|
||||
const receivedByLineId = new Map<string, number>();
|
||||
|
||||
for (const receipt of record.receipts) {
|
||||
for (const line of receipt.lines) {
|
||||
receivedByLineId.set(line.purchaseOrderLineId, (receivedByLineId.get(line.purchaseOrderLineId) ?? 0) + line.quantity);
|
||||
}
|
||||
}
|
||||
|
||||
const lines = record.lines
|
||||
.slice()
|
||||
.sort((left, right) => left.position - right.position)
|
||||
@@ -124,6 +201,8 @@ function mapPurchaseOrder(record: PurchaseOrderRecord): PurchaseOrderDetailDto {
|
||||
unitOfMeasure: line.unitOfMeasure as PurchaseOrderDetailDto["lines"][number]["unitOfMeasure"],
|
||||
unitCost: line.unitCost,
|
||||
lineTotal: line.quantity * line.unitCost,
|
||||
receivedQuantity: receivedByLineId.get(line.id) ?? 0,
|
||||
remainingQuantity: Math.max(0, line.quantity - (receivedByLineId.get(line.id) ?? 0)),
|
||||
position: line.position,
|
||||
}));
|
||||
const totals = calculateTotals(
|
||||
@@ -131,6 +210,10 @@ function mapPurchaseOrder(record: PurchaseOrderRecord): PurchaseOrderDetailDto {
|
||||
record.taxPercent,
|
||||
record.freightAmount
|
||||
);
|
||||
const receipts = record.receipts
|
||||
.slice()
|
||||
.sort((left, right) => right.receivedAt.getTime() - left.receivedAt.getTime())
|
||||
.map((receipt) => mapPurchaseReceipt(receipt, record.id));
|
||||
|
||||
return {
|
||||
id: record.id,
|
||||
@@ -152,6 +235,7 @@ function mapPurchaseOrder(record: PurchaseOrderRecord): PurchaseOrderDetailDto {
|
||||
updatedAt: record.updatedAt.toISOString(),
|
||||
lineCount: lines.length,
|
||||
lines,
|
||||
receipts,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -160,29 +244,183 @@ async function nextDocumentNumber() {
|
||||
return `PO-${String(next).padStart(5, "0")}`;
|
||||
}
|
||||
|
||||
function buildInclude() {
|
||||
return {
|
||||
vendor: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
paymentTerms: true,
|
||||
currencyCode: true,
|
||||
async function nextReceiptNumber(transaction: Prisma.TransactionClient | typeof prisma = prisma) {
|
||||
const next = (await transaction.purchaseReceipt.count()) + 1;
|
||||
return `PR-${String(next).padStart(5, "0")}`;
|
||||
}
|
||||
|
||||
const purchaseOrderInclude = Prisma.validator<Prisma.PurchaseOrderInclude>()({
|
||||
vendor: {
|
||||
select: {
|
||||
id: true,
|
||||
name: true,
|
||||
email: true,
|
||||
paymentTerms: true,
|
||||
currencyCode: true,
|
||||
},
|
||||
},
|
||||
lines: {
|
||||
include: {
|
||||
item: {
|
||||
select: {
|
||||
id: true,
|
||||
sku: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
lines: {
|
||||
include: {
|
||||
item: {
|
||||
select: {
|
||||
id: true,
|
||||
sku: true,
|
||||
name: true,
|
||||
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
receipts: {
|
||||
include: {
|
||||
warehouse: {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
location: {
|
||||
select: {
|
||||
id: true,
|
||||
code: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
createdBy: {
|
||||
select: {
|
||||
firstName: true,
|
||||
lastName: true,
|
||||
},
|
||||
},
|
||||
lines: {
|
||||
include: {
|
||||
purchaseOrderLine: {
|
||||
include: {
|
||||
item: {
|
||||
select: {
|
||||
id: true,
|
||||
sku: true,
|
||||
name: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ createdAt: "asc" }],
|
||||
},
|
||||
},
|
||||
orderBy: [{ receivedAt: "desc" }, { createdAt: "desc" }],
|
||||
},
|
||||
});
|
||||
|
||||
function normalizeReceiptLines(lines: PurchaseReceiptInput["lines"]) {
|
||||
return lines
|
||||
.map((line) => ({
|
||||
purchaseOrderLineId: line.purchaseOrderLineId.trim(),
|
||||
quantity: Number(line.quantity),
|
||||
}))
|
||||
.filter((line) => line.purchaseOrderLineId.length > 0 && line.quantity > 0);
|
||||
}
|
||||
|
||||
async function validateReceipt(orderId: string, payload: PurchaseReceiptInput) {
|
||||
const normalizedLines = normalizeReceiptLines(payload.lines);
|
||||
|
||||
if (normalizedLines.length === 0) {
|
||||
return { ok: false as const, reason: "At least one receipt line is required." };
|
||||
}
|
||||
|
||||
if (normalizedLines.some((line) => !Number.isInteger(line.quantity) || line.quantity <= 0)) {
|
||||
return { ok: false as const, reason: "Receipt quantity must be a whole number greater than zero." };
|
||||
}
|
||||
|
||||
const order = await purchaseOrderModel.findUnique({
|
||||
where: { id: orderId },
|
||||
select: {
|
||||
id: true,
|
||||
documentNumber: true,
|
||||
status: true,
|
||||
lines: {
|
||||
select: {
|
||||
id: true,
|
||||
quantity: true,
|
||||
itemId: true,
|
||||
item: {
|
||||
select: {
|
||||
sku: true,
|
||||
},
|
||||
},
|
||||
receiptLines: {
|
||||
select: {
|
||||
quantity: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
orderBy: [{ position: "asc" }, { createdAt: "asc" }],
|
||||
},
|
||||
});
|
||||
|
||||
if (!order) {
|
||||
return { ok: false as const, reason: "Purchase order was not found." };
|
||||
}
|
||||
|
||||
if (order.status === "CLOSED") {
|
||||
return { ok: false as const, reason: "Closed purchase orders cannot receive additional stock." };
|
||||
}
|
||||
|
||||
const location = await prisma.warehouseLocation.findUnique({
|
||||
where: { id: payload.locationId },
|
||||
select: {
|
||||
id: true,
|
||||
warehouseId: true,
|
||||
},
|
||||
});
|
||||
|
||||
if (!location || location.warehouseId !== payload.warehouseId) {
|
||||
return { ok: false as const, reason: "Warehouse location is invalid for the selected warehouse." };
|
||||
}
|
||||
|
||||
const orderLinesById = new Map<string, { id: string; quantity: number; itemId: string; itemSku: string; receivedQuantity: number }>(
|
||||
order.lines.map((line: { id: string; quantity: number; itemId: string; item: { sku: string }; receiptLines: { quantity: number }[] }) => [
|
||||
line.id,
|
||||
{
|
||||
id: line.id,
|
||||
quantity: line.quantity,
|
||||
itemId: line.itemId,
|
||||
itemSku: line.item.sku,
|
||||
receivedQuantity: line.receiptLines.reduce((sum: number, receiptLine: { quantity: number }) => sum + receiptLine.quantity, 0),
|
||||
},
|
||||
])
|
||||
);
|
||||
|
||||
const mergedQuantities = new Map<string, number>();
|
||||
for (const line of normalizedLines) {
|
||||
const current = mergedQuantities.get(line.purchaseOrderLineId) ?? 0;
|
||||
mergedQuantities.set(line.purchaseOrderLineId, current + line.quantity);
|
||||
}
|
||||
|
||||
for (const [lineId, quantity] of mergedQuantities.entries()) {
|
||||
const orderLine = orderLinesById.get(lineId);
|
||||
if (!orderLine) {
|
||||
return { ok: false as const, reason: "Receipt lines must reference lines on the selected purchase order." };
|
||||
}
|
||||
|
||||
if (orderLine.receivedQuantity + quantity > orderLine.quantity) {
|
||||
return {
|
||||
ok: false as const,
|
||||
reason: `Receipt for ${orderLine.itemSku} exceeds the remaining ordered quantity.`,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true as const,
|
||||
order,
|
||||
lines: [...mergedQuantities.entries()].map(([purchaseOrderLineId, quantity]) => ({
|
||||
purchaseOrderLineId,
|
||||
quantity,
|
||||
itemId: orderLinesById.get(purchaseOrderLineId)!.itemId,
|
||||
})),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -220,7 +458,7 @@ export async function listPurchaseOrders(filters: { q?: string; status?: Purchas
|
||||
}
|
||||
: {}),
|
||||
},
|
||||
include: buildInclude(),
|
||||
include: purchaseOrderInclude,
|
||||
orderBy: [{ issueDate: "desc" }, { createdAt: "desc" }],
|
||||
});
|
||||
|
||||
@@ -249,10 +487,10 @@ export async function listPurchaseOrders(filters: { q?: string; status?: Purchas
|
||||
export async function getPurchaseOrderById(documentId: string) {
|
||||
const record = await purchaseOrderModel.findUnique({
|
||||
where: { id: documentId },
|
||||
include: buildInclude(),
|
||||
include: purchaseOrderInclude,
|
||||
});
|
||||
|
||||
return record ? mapPurchaseOrder(record as PurchaseOrderRecord) : null;
|
||||
return record ? mapPurchaseOrder(record as unknown as PurchaseOrderRecord) : null;
|
||||
}
|
||||
|
||||
export async function createPurchaseOrder(payload: PurchaseOrderInput) {
|
||||
@@ -356,3 +594,51 @@ export async function updatePurchaseOrderStatus(documentId: string, status: Purc
|
||||
const detail = await getPurchaseOrderById(documentId);
|
||||
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load updated purchase order." };
|
||||
}
|
||||
|
||||
export async function createPurchaseReceipt(orderId: string, payload: PurchaseReceiptInput, createdById?: string | null) {
|
||||
const validated = await validateReceipt(orderId, payload);
|
||||
if (!validated.ok) {
|
||||
return { ok: false as const, reason: validated.reason };
|
||||
}
|
||||
|
||||
await prisma.$transaction(async (transaction) => {
|
||||
const receiptNumber = await nextReceiptNumber(transaction);
|
||||
|
||||
const receipt = await transaction.purchaseReceipt.create({
|
||||
data: {
|
||||
receiptNumber,
|
||||
purchaseOrderId: orderId,
|
||||
warehouseId: payload.warehouseId,
|
||||
locationId: payload.locationId,
|
||||
receivedAt: new Date(payload.receivedAt),
|
||||
notes: payload.notes,
|
||||
createdById: createdById ?? null,
|
||||
lines: {
|
||||
create: validated.lines.map((line) => ({
|
||||
purchaseOrderLineId: line.purchaseOrderLineId,
|
||||
quantity: line.quantity,
|
||||
})),
|
||||
},
|
||||
},
|
||||
select: {
|
||||
receiptNumber: true,
|
||||
},
|
||||
});
|
||||
|
||||
await transaction.inventoryTransaction.createMany({
|
||||
data: validated.lines.map((line) => ({
|
||||
itemId: line.itemId,
|
||||
warehouseId: payload.warehouseId,
|
||||
locationId: payload.locationId,
|
||||
transactionType: "RECEIPT",
|
||||
quantity: line.quantity,
|
||||
reference: receipt.receiptNumber,
|
||||
notes: `Purchase receipt for ${validated.order.documentNumber}`,
|
||||
createdById: createdById ?? null,
|
||||
})),
|
||||
});
|
||||
});
|
||||
|
||||
const detail = await getPurchaseOrderById(orderId);
|
||||
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load updated purchase order." };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user