133 lines
4.2 KiB
TypeScript
133 lines
4.2 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { AppError } from '../common/errors/app-error';
|
|
import { DatabaseService } from '../database/database.service';
|
|
import { Prisma } from '../generated/prisma/client';
|
|
import { AccessStore } from '../identity/access.store';
|
|
import { recordAudit } from '../identity/audit';
|
|
import type { Principal } from '../identity/identity.types';
|
|
import { estimateLineCost, sumCosts } from './cost-math';
|
|
import { latestCostRates } from './latest-cost-rates';
|
|
import type { CostHistoryQuery, CostRateInput } from './costing.schemas';
|
|
|
|
@Injectable()
|
|
export class CostingStore {
|
|
constructor(
|
|
private readonly db: DatabaseService,
|
|
private readonly access: AccessStore,
|
|
) {}
|
|
|
|
async listRates(
|
|
actor: Principal,
|
|
materialId: string,
|
|
query: CostHistoryQuery,
|
|
) {
|
|
const material = await this.db.material.findFirst({
|
|
where: { id: materialId, organizationId: actor.organizationId },
|
|
select: { id: true },
|
|
});
|
|
if (!material) throw new AppError('MATERIAL_NOT_FOUND');
|
|
return this.db.materialCostRate.findMany({
|
|
where: {
|
|
organizationId: actor.organizationId,
|
|
materialId,
|
|
currency: query.currency,
|
|
},
|
|
orderBy: { revision: 'desc' },
|
|
take: query.limit,
|
|
skip: query.offset,
|
|
});
|
|
}
|
|
|
|
createRate(actor: Principal, materialId: string, input: CostRateInput) {
|
|
return this.access.mutate(actor, 'cost.manage', async (tx) => {
|
|
const material = await tx.material.findFirst({
|
|
where: { id: materialId, organizationId: actor.organizationId },
|
|
select: { active: true },
|
|
});
|
|
if (!material) throw new AppError('MATERIAL_NOT_FOUND');
|
|
if (!material.active) throw new AppError('COST_MATERIAL_INACTIVE');
|
|
const latest = await tx.materialCostRate.findFirst({
|
|
where: {
|
|
materialId,
|
|
organizationId: actor.organizationId,
|
|
currency: input.currency,
|
|
},
|
|
orderBy: { revision: 'desc' },
|
|
select: { revision: true },
|
|
});
|
|
const revision = (latest?.revision ?? 0) + 1;
|
|
if (revision > 1000) throw new AppError('COST_RATE_LIMIT');
|
|
const rate = await tx.materialCostRate.create({
|
|
data: {
|
|
organizationId: actor.organizationId,
|
|
materialId,
|
|
recordedById: actor.userId,
|
|
currency: input.currency,
|
|
revision,
|
|
unitCost: input.unitCost,
|
|
},
|
|
});
|
|
await recordAudit(
|
|
tx,
|
|
actor.organizationId,
|
|
actor.userId,
|
|
'material.cost_rate_recorded',
|
|
rate.id,
|
|
);
|
|
return rate;
|
|
});
|
|
}
|
|
|
|
estimate(
|
|
actor: Principal,
|
|
bomId: string,
|
|
kind: 'PRODUCTION' | 'PACKAGING',
|
|
currency: string,
|
|
) {
|
|
return this.db.$transaction(
|
|
async (tx) => {
|
|
const bom = await tx.productBom.findFirst({
|
|
where: { id: bomId, organizationId: actor.organizationId, kind },
|
|
include: { lines: { orderBy: { materialCode: 'asc' } } },
|
|
});
|
|
if (!bom) throw new AppError('BOM_NOT_FOUND');
|
|
const byMaterial = await latestCostRates(
|
|
tx,
|
|
actor.organizationId,
|
|
currency,
|
|
bom.lines.map((line) => line.materialId),
|
|
);
|
|
const lines = bom.lines.map((line) => {
|
|
const rate = byMaterial.get(line.materialId);
|
|
if (!rate) throw new AppError('COST_RATE_MISSING');
|
|
const quantity = line.quantity.toFixed(3);
|
|
const unitCost = rate.unitCost.toFixed(4);
|
|
const estimatedCost = estimateLineCost(quantity, unitCost);
|
|
return {
|
|
bomLineId: line.id,
|
|
materialId: line.materialId,
|
|
materialCode: line.materialCode,
|
|
materialName: line.materialName,
|
|
unit: line.unit,
|
|
quantity,
|
|
rateId: rate.id,
|
|
rateRevision: rate.revision,
|
|
unitCost,
|
|
estimatedCost,
|
|
};
|
|
});
|
|
return {
|
|
bomId: bom.id,
|
|
kind,
|
|
currency,
|
|
outputQuantity: bom.outputQuantity,
|
|
estimatedAt: new Date(),
|
|
lines,
|
|
estimatedTotal: sumCosts(lines.map((line) => line.estimatedCost)),
|
|
};
|
|
},
|
|
{ isolationLevel: Prisma.TransactionIsolationLevel.RepeatableRead },
|
|
);
|
|
}
|
|
}
|