167 lines
5.7 KiB
TypeScript
167 lines
5.7 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { AccessStore } from '../identity/access.store';
|
|
import { recordAudit } from '../identity/audit';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import type { InspectionInput, ReceiptInput } from './goods-receipt.schemas';
|
|
import { scaled } from './purchase-order-money';
|
|
|
|
const quantity = (value: string) => scaled(value, 3);
|
|
const receiptView = {
|
|
lines: { include: { inspection: true }, orderBy: { id: 'asc' as const } },
|
|
};
|
|
|
|
@Injectable()
|
|
export class GoodsReceiptStore {
|
|
constructor(
|
|
private readonly db: DatabaseService,
|
|
private readonly access: AccessStore,
|
|
) {}
|
|
|
|
async list(actor: Principal, orderId: string) {
|
|
const order = await this.db.purchaseOrder.findFirst({
|
|
where: { id: orderId, organizationId: actor.organizationId },
|
|
select: { id: true },
|
|
});
|
|
if (!order) throw new AppError('PURCHASE_ORDER_NOT_FOUND');
|
|
return this.db.goodsReceipt.findMany({
|
|
where: { organizationId: actor.organizationId, purchaseOrderId: orderId },
|
|
include: receiptView,
|
|
orderBy: [{ createdAt: 'desc' }, { id: 'desc' }],
|
|
take: 100,
|
|
});
|
|
}
|
|
|
|
async find(actor: Principal, id: string) {
|
|
const receipt = await this.db.goodsReceipt.findFirst({
|
|
where: { id, organizationId: actor.organizationId },
|
|
include: receiptView,
|
|
});
|
|
if (!receipt) throw new AppError('RECEIPT_NOT_FOUND');
|
|
return receipt;
|
|
}
|
|
|
|
create(actor: Principal, orderId: string, input: ReceiptInput) {
|
|
return this.access.mutate(actor, 'procurement.manage', async (tx) => {
|
|
const order = await tx.purchaseOrder.findFirst({
|
|
where: { id: orderId, organizationId: actor.organizationId },
|
|
include: { lines: true },
|
|
});
|
|
if (!order) throw new AppError('PURCHASE_ORDER_NOT_FOUND');
|
|
if (order.status !== 'ISSUED') throw new AppError('RECEIPT_ORDER_STATE');
|
|
const orderLines = new Map(order.lines.map((line) => [line.id, line]));
|
|
const prior = await tx.goodsReceiptLine.findMany({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
purchaseOrderId: orderId,
|
|
},
|
|
include: { inspection: true },
|
|
});
|
|
for (const line of input.lines) {
|
|
const ordered = orderLines.get(line.purchaseOrderLineId);
|
|
if (!ordered) throw new AppError('RECEIPT_LINE_INVALID');
|
|
const consumed = prior
|
|
.filter(
|
|
(entry) => entry.purchaseOrderLineId === line.purchaseOrderLineId,
|
|
)
|
|
.reduce(
|
|
(sum, entry) =>
|
|
sum +
|
|
quantity(
|
|
entry.inspection?.acceptedQuantity.toFixed(3) ??
|
|
entry.quantity.toFixed(3),
|
|
),
|
|
0n,
|
|
);
|
|
if (
|
|
consumed + quantity(line.quantity) >
|
|
quantity(ordered.quantity.toFixed(3))
|
|
)
|
|
throw new AppError('RECEIPT_QUANTITY_LIMIT');
|
|
}
|
|
const receipt = await tx.goodsReceipt.create({
|
|
data: {
|
|
organizationId: actor.organizationId,
|
|
purchaseOrderId: orderId,
|
|
createdById: actor.userId,
|
|
deliveryNote: input.deliveryNote,
|
|
},
|
|
});
|
|
await tx.goodsReceiptLine.createMany({
|
|
data: input.lines.map((line) => ({
|
|
organizationId: actor.organizationId,
|
|
purchaseOrderId: orderId,
|
|
receiptId: receipt.id,
|
|
purchaseOrderLineId: line.purchaseOrderLineId,
|
|
quantity: line.quantity,
|
|
})),
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'goods_receipt.created',
|
|
receipt.id,
|
|
);
|
|
return tx.goodsReceipt.findUniqueOrThrow({
|
|
where: { id: receipt.id },
|
|
include: receiptView,
|
|
});
|
|
});
|
|
}
|
|
|
|
inspect(actor: Principal, id: string, input: InspectionInput) {
|
|
return this.access.mutate(actor, 'procurement.manage', async (tx) => {
|
|
const receipt = await tx.goodsReceipt.findFirst({
|
|
where: { id, organizationId: actor.organizationId },
|
|
include: receiptView,
|
|
});
|
|
if (!receipt) throw new AppError('RECEIPT_NOT_FOUND');
|
|
if (receipt.lines.some((line) => line.inspection))
|
|
throw new AppError('RECEIPT_ALREADY_INSPECTED');
|
|
const lines = new Map(receipt.lines.map((line) => [line.id, line]));
|
|
if (
|
|
input.lines.length !== lines.size ||
|
|
input.lines.some((line) => !lines.has(line.receiptLineId))
|
|
)
|
|
throw new AppError('INSPECTION_LINES_INVALID');
|
|
for (const decision of input.lines) {
|
|
const received = lines.get(decision.receiptLineId)!;
|
|
if (
|
|
quantity(decision.acceptedQuantity) +
|
|
quantity(decision.rejectedQuantity) !==
|
|
quantity(received.quantity.toFixed(3))
|
|
)
|
|
throw new AppError('INSPECTION_QUANTITY_INVALID');
|
|
if (
|
|
quantity(decision.rejectedQuantity) > 0n &&
|
|
!decision.rejectionReason
|
|
)
|
|
throw new AppError('INSPECTION_REASON_REQUIRED');
|
|
}
|
|
await tx.receiptInspection.createMany({
|
|
data: input.lines.map((line) => ({
|
|
organizationId: actor.organizationId,
|
|
receiptLineId: line.receiptLineId,
|
|
inspectedById: actor.userId,
|
|
acceptedQuantity: line.acceptedQuantity,
|
|
rejectedQuantity: line.rejectedQuantity,
|
|
rejectionReason: line.rejectionReason,
|
|
})),
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'goods_receipt.inspected',
|
|
id,
|
|
);
|
|
return tx.goodsReceipt.findUniqueOrThrow({
|
|
where: { id },
|
|
include: receiptView,
|
|
});
|
|
});
|
|
}
|
|
}
|