2026-03-15 09:04:18 -05:00
|
|
|
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";
|
2026-03-15 00:29:41 -05:00
|
|
|
|
|
|
|
|
import { prisma } from "../../lib/prisma.js";
|
|
|
|
|
|
2026-03-15 09:04:18 -05:00
|
|
|
const purchaseOrderModel = prisma.purchaseOrder;
|
2026-03-15 00:29:41 -05:00
|
|
|
|
|
|
|
|
type PurchaseLineRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
description: string;
|
|
|
|
|
quantity: number;
|
|
|
|
|
unitOfMeasure: string;
|
|
|
|
|
unitCost: number;
|
|
|
|
|
position: number;
|
2026-03-15 09:04:18 -05:00
|
|
|
receiptLines?: {
|
|
|
|
|
quantity: number;
|
|
|
|
|
}[];
|
2026-03-15 00:29:41 -05:00
|
|
|
item: {
|
|
|
|
|
id: string;
|
|
|
|
|
sku: string;
|
|
|
|
|
name: string;
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-15 09:04:18 -05:00
|
|
|
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[];
|
|
|
|
|
};
|
|
|
|
|
|
2026-03-15 00:29:41 -05:00
|
|
|
type PurchaseOrderRecord = {
|
|
|
|
|
id: string;
|
|
|
|
|
documentNumber: string;
|
|
|
|
|
status: string;
|
|
|
|
|
issueDate: Date;
|
|
|
|
|
taxPercent: number;
|
|
|
|
|
freightAmount: number;
|
|
|
|
|
notes: string;
|
|
|
|
|
createdAt: Date;
|
|
|
|
|
updatedAt: Date;
|
|
|
|
|
vendor: {
|
|
|
|
|
id: string;
|
|
|
|
|
name: string;
|
|
|
|
|
email: string;
|
|
|
|
|
paymentTerms: string | null;
|
|
|
|
|
currencyCode: string | null;
|
|
|
|
|
};
|
|
|
|
|
lines: PurchaseLineRecord[];
|
2026-03-15 09:04:18 -05:00
|
|
|
receipts: PurchaseReceiptRecord[];
|
2026-03-15 00:29:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function roundMoney(value: number) {
|
|
|
|
|
return Math.round(value * 100) / 100;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function calculateTotals(subtotal: number, taxPercent: number, freightAmount: number) {
|
|
|
|
|
const normalizedSubtotal = roundMoney(subtotal);
|
|
|
|
|
const normalizedTaxPercent = Number.isFinite(taxPercent) ? taxPercent : 0;
|
|
|
|
|
const normalizedFreight = roundMoney(Number.isFinite(freightAmount) ? freightAmount : 0);
|
|
|
|
|
const taxAmount = roundMoney(normalizedSubtotal * (normalizedTaxPercent / 100));
|
|
|
|
|
const total = roundMoney(normalizedSubtotal + taxAmount + normalizedFreight);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
subtotal: normalizedSubtotal,
|
|
|
|
|
taxPercent: normalizedTaxPercent,
|
|
|
|
|
taxAmount,
|
|
|
|
|
freightAmount: normalizedFreight,
|
|
|
|
|
total,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 09:04:18 -05:00
|
|
|
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,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 00:29:41 -05:00
|
|
|
function normalizeLines(lines: PurchaseLineInput[]) {
|
|
|
|
|
return lines
|
|
|
|
|
.map((line, index) => ({
|
|
|
|
|
itemId: line.itemId,
|
|
|
|
|
description: line.description.trim(),
|
|
|
|
|
quantity: Number(line.quantity),
|
|
|
|
|
unitOfMeasure: line.unitOfMeasure,
|
|
|
|
|
unitCost: Number(line.unitCost),
|
|
|
|
|
position: line.position ?? (index + 1) * 10,
|
|
|
|
|
}))
|
|
|
|
|
.filter((line) => line.itemId.trim().length > 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function validateLines(lines: PurchaseLineInput[]) {
|
|
|
|
|
const normalized = normalizeLines(lines);
|
|
|
|
|
|
|
|
|
|
if (normalized.length === 0) {
|
|
|
|
|
return { ok: false as const, reason: "At least one line item is required." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (normalized.some((line) => !Number.isInteger(line.quantity) || line.quantity <= 0)) {
|
|
|
|
|
return { ok: false as const, reason: "Line quantity must be a whole number greater than zero." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (normalized.some((line) => Number.isNaN(line.unitCost) || line.unitCost < 0)) {
|
|
|
|
|
return { ok: false as const, reason: "Unit cost must be zero or greater." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const itemIds = [...new Set(normalized.map((line) => line.itemId))];
|
|
|
|
|
const items = await prisma.inventoryItem.findMany({
|
|
|
|
|
where: { id: { in: itemIds } },
|
|
|
|
|
select: { id: true, isPurchasable: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (items.length !== itemIds.length) {
|
|
|
|
|
return { ok: false as const, reason: "One or more purchase lines reference an invalid inventory item." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (items.some((item) => !item.isPurchasable)) {
|
|
|
|
|
return { ok: false as const, reason: "Purchase orders can only include purchasable inventory items." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { ok: true as const, lines: normalized };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function mapPurchaseOrder(record: PurchaseOrderRecord): PurchaseOrderDetailDto {
|
2026-03-15 09:04:18 -05:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 00:29:41 -05:00
|
|
|
const lines = record.lines
|
|
|
|
|
.slice()
|
|
|
|
|
.sort((left, right) => left.position - right.position)
|
|
|
|
|
.map((line) => ({
|
|
|
|
|
id: line.id,
|
|
|
|
|
itemId: line.item.id,
|
|
|
|
|
itemSku: line.item.sku,
|
|
|
|
|
itemName: line.item.name,
|
|
|
|
|
description: line.description,
|
|
|
|
|
quantity: line.quantity,
|
|
|
|
|
unitOfMeasure: line.unitOfMeasure as PurchaseOrderDetailDto["lines"][number]["unitOfMeasure"],
|
|
|
|
|
unitCost: line.unitCost,
|
|
|
|
|
lineTotal: line.quantity * line.unitCost,
|
2026-03-15 09:04:18 -05:00
|
|
|
receivedQuantity: receivedByLineId.get(line.id) ?? 0,
|
|
|
|
|
remainingQuantity: Math.max(0, line.quantity - (receivedByLineId.get(line.id) ?? 0)),
|
2026-03-15 00:29:41 -05:00
|
|
|
position: line.position,
|
|
|
|
|
}));
|
|
|
|
|
const totals = calculateTotals(
|
|
|
|
|
lines.reduce((sum, line) => sum + line.lineTotal, 0),
|
|
|
|
|
record.taxPercent,
|
|
|
|
|
record.freightAmount
|
|
|
|
|
);
|
2026-03-15 09:04:18 -05:00
|
|
|
const receipts = record.receipts
|
|
|
|
|
.slice()
|
|
|
|
|
.sort((left, right) => right.receivedAt.getTime() - left.receivedAt.getTime())
|
|
|
|
|
.map((receipt) => mapPurchaseReceipt(receipt, record.id));
|
2026-03-15 00:29:41 -05:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
id: record.id,
|
|
|
|
|
documentNumber: record.documentNumber,
|
|
|
|
|
vendorId: record.vendor.id,
|
|
|
|
|
vendorName: record.vendor.name,
|
|
|
|
|
vendorEmail: record.vendor.email,
|
|
|
|
|
paymentTerms: record.vendor.paymentTerms,
|
|
|
|
|
currencyCode: record.vendor.currencyCode,
|
|
|
|
|
status: record.status as PurchaseOrderStatus,
|
|
|
|
|
subtotal: totals.subtotal,
|
|
|
|
|
taxPercent: totals.taxPercent,
|
|
|
|
|
taxAmount: totals.taxAmount,
|
|
|
|
|
freightAmount: totals.freightAmount,
|
|
|
|
|
total: totals.total,
|
|
|
|
|
issueDate: record.issueDate.toISOString(),
|
|
|
|
|
notes: record.notes,
|
|
|
|
|
createdAt: record.createdAt.toISOString(),
|
|
|
|
|
updatedAt: record.updatedAt.toISOString(),
|
|
|
|
|
lineCount: lines.length,
|
|
|
|
|
lines,
|
2026-03-15 09:04:18 -05:00
|
|
|
receipts,
|
2026-03-15 00:29:41 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function nextDocumentNumber() {
|
|
|
|
|
const next = (await purchaseOrderModel.count()) + 1;
|
|
|
|
|
return `PO-${String(next).padStart(5, "0")}`;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-15 09:04:18 -05:00
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
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" }],
|
2026-03-15 00:29:41 -05:00
|
|
|
},
|
|
|
|
|
},
|
2026-03-15 09:04:18 -05:00
|
|
|
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,
|
|
|
|
|
},
|
2026-03-15 00:29:41 -05:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2026-03-15 09:04:18 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
})),
|
2026-03-15 00:29:41 -05:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function listPurchaseVendorOptions(): Promise<PurchaseVendorOptionDto[]> {
|
|
|
|
|
const vendors = await prisma.vendor.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
status: {
|
|
|
|
|
not: "INACTIVE",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: {
|
|
|
|
|
id: true,
|
|
|
|
|
name: true,
|
|
|
|
|
email: true,
|
|
|
|
|
paymentTerms: true,
|
|
|
|
|
currencyCode: true,
|
|
|
|
|
},
|
|
|
|
|
orderBy: [{ name: "asc" }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return vendors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function listPurchaseOrders(filters: { q?: string; status?: PurchaseOrderStatus } = {}) {
|
|
|
|
|
const query = filters.q?.trim();
|
|
|
|
|
const records = await purchaseOrderModel.findMany({
|
|
|
|
|
where: {
|
|
|
|
|
...(filters.status ? { status: filters.status } : {}),
|
|
|
|
|
...(query
|
|
|
|
|
? {
|
|
|
|
|
OR: [
|
|
|
|
|
{ documentNumber: { contains: query } },
|
|
|
|
|
{ vendor: { name: { contains: query } } },
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
: {}),
|
|
|
|
|
},
|
2026-03-15 09:04:18 -05:00
|
|
|
include: purchaseOrderInclude,
|
2026-03-15 00:29:41 -05:00
|
|
|
orderBy: [{ issueDate: "desc" }, { createdAt: "desc" }],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return records.map((record: unknown) => {
|
|
|
|
|
const detail = mapPurchaseOrder(record as PurchaseOrderRecord);
|
|
|
|
|
const summary: PurchaseOrderSummaryDto = {
|
|
|
|
|
id: detail.id,
|
|
|
|
|
documentNumber: detail.documentNumber,
|
|
|
|
|
vendorId: detail.vendorId,
|
|
|
|
|
vendorName: detail.vendorName,
|
|
|
|
|
status: detail.status,
|
|
|
|
|
subtotal: detail.subtotal,
|
|
|
|
|
taxPercent: detail.taxPercent,
|
|
|
|
|
taxAmount: detail.taxAmount,
|
|
|
|
|
freightAmount: detail.freightAmount,
|
|
|
|
|
total: detail.total,
|
|
|
|
|
issueDate: detail.issueDate,
|
|
|
|
|
updatedAt: detail.updatedAt,
|
|
|
|
|
lineCount: detail.lineCount,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return summary;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function getPurchaseOrderById(documentId: string) {
|
|
|
|
|
const record = await purchaseOrderModel.findUnique({
|
|
|
|
|
where: { id: documentId },
|
2026-03-15 09:04:18 -05:00
|
|
|
include: purchaseOrderInclude,
|
2026-03-15 00:29:41 -05:00
|
|
|
});
|
|
|
|
|
|
2026-03-15 09:04:18 -05:00
|
|
|
return record ? mapPurchaseOrder(record as unknown as PurchaseOrderRecord) : null;
|
2026-03-15 00:29:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function createPurchaseOrder(payload: PurchaseOrderInput) {
|
|
|
|
|
const validatedLines = await validateLines(payload.lines);
|
|
|
|
|
if (!validatedLines.ok) {
|
|
|
|
|
return { ok: false as const, reason: validatedLines.reason };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const vendor = await prisma.vendor.findUnique({
|
|
|
|
|
where: { id: payload.vendorId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!vendor) {
|
|
|
|
|
return { ok: false as const, reason: "Vendor was not found." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const documentNumber = await nextDocumentNumber();
|
|
|
|
|
|
|
|
|
|
const created = await purchaseOrderModel.create({
|
|
|
|
|
data: {
|
|
|
|
|
documentNumber,
|
|
|
|
|
vendorId: payload.vendorId,
|
|
|
|
|
status: payload.status,
|
|
|
|
|
issueDate: new Date(payload.issueDate),
|
|
|
|
|
taxPercent: payload.taxPercent,
|
|
|
|
|
freightAmount: payload.freightAmount,
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
lines: {
|
|
|
|
|
create: validatedLines.lines,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const detail = await getPurchaseOrderById(created.id);
|
|
|
|
|
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load saved purchase order." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updatePurchaseOrder(documentId: string, payload: PurchaseOrderInput) {
|
|
|
|
|
const existing = await purchaseOrderModel.findUnique({
|
|
|
|
|
where: { id: documentId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
return { ok: false as const, reason: "Purchase order was not found." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const validatedLines = await validateLines(payload.lines);
|
|
|
|
|
if (!validatedLines.ok) {
|
|
|
|
|
return { ok: false as const, reason: validatedLines.reason };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const vendor = await prisma.vendor.findUnique({
|
|
|
|
|
where: { id: payload.vendorId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!vendor) {
|
|
|
|
|
return { ok: false as const, reason: "Vendor was not found." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await purchaseOrderModel.update({
|
|
|
|
|
where: { id: documentId },
|
|
|
|
|
data: {
|
|
|
|
|
vendorId: payload.vendorId,
|
|
|
|
|
status: payload.status,
|
|
|
|
|
issueDate: new Date(payload.issueDate),
|
|
|
|
|
taxPercent: payload.taxPercent,
|
|
|
|
|
freightAmount: payload.freightAmount,
|
|
|
|
|
notes: payload.notes,
|
|
|
|
|
lines: {
|
|
|
|
|
deleteMany: {},
|
|
|
|
|
create: validatedLines.lines,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const detail = await getPurchaseOrderById(documentId);
|
|
|
|
|
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load saved purchase order." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function updatePurchaseOrderStatus(documentId: string, status: PurchaseOrderStatus) {
|
|
|
|
|
const existing = await purchaseOrderModel.findUnique({
|
|
|
|
|
where: { id: documentId },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!existing) {
|
|
|
|
|
return { ok: false as const, reason: "Purchase order was not found." };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await purchaseOrderModel.update({
|
|
|
|
|
where: { id: documentId },
|
|
|
|
|
data: { status },
|
|
|
|
|
select: { id: true },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const detail = await getPurchaseOrderById(documentId);
|
|
|
|
|
return detail ? { ok: true as const, document: detail } : { ok: false as const, reason: "Unable to load updated purchase order." };
|
|
|
|
|
}
|
2026-03-15 09:04:18 -05:00
|
|
|
|
|
|
|
|
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." };
|
|
|
|
|
}
|