152 lines
5.8 KiB
TypeScript
152 lines
5.8 KiB
TypeScript
import { Injectable, NotFoundException } from '@nestjs/common';
|
|||
|
|
import { OrderStatus, Prisma } from '@prisma/client';
|
||
|
|
import { PrismaService } from '../../prisma/prisma.service';
|
||
|
|
import { CatalogService } from '../catalog/catalog.service';
|
||
|
|
import { LedgerService } from '../accounting/ledger.service';
|
||
|
|
import { CreateOrderDto } from './dto/create-order.dto';
|
||
|
|
|
||
|
|
interface ComputedItem {
|
||
|
|
productVariantId: string;
|
||
|
|
quantity: number;
|
||
|
|
rashWeightGramsSnapshot: Prisma.Decimal;
|
||
|
|
wagePercentSnapshot: Prisma.Decimal;
|
||
|
|
discountPercentSnapshot: Prisma.Decimal;
|
||
|
|
computedWeightGrams: Prisma.Decimal;
|
||
|
|
flatRialCharge: Prisma.Decimal;
|
||
|
|
}
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class OrdersService {
|
||
|
|
constructor(
|
||
|
|
private readonly prisma: PrismaService,
|
||
|
|
private readonly catalog: CatalogService,
|
||
|
|
private readonly ledger: LedgerService,
|
||
|
|
) {}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Places a stock order. Per line item:
|
||
|
|
* w = rashWeightGrams * (1 + (wagePercent - discountPercent) / 100)
|
||
|
|
* mirrors legacy ApiMobileController::setOrder. Debt is NOT posted here —
|
||
|
|
* only once the order reaches DELIVERED_TO_AGENT (see updateStatus).
|
||
|
|
*/
|
||
|
|
async createOrder(dto: CreateOrderDto) {
|
||
|
|
const customer = await this.prisma.customer.findUnique({ where: { id: dto.customerId } });
|
||
|
|
if (!customer) throw new NotFoundException(`Customer ${dto.customerId} not found`);
|
||
|
|
|
||
|
|
const computedItems: ComputedItem[] = await Promise.all(
|
||
|
|
dto.items.map(async (item) => {
|
||
|
|
const variant = await this.catalog.getVariantOrThrow(item.productVariantId);
|
||
|
|
const rashWeight = new Prisma.Decimal(variant.rashWeightGrams);
|
||
|
|
const wagePercent = new Prisma.Decimal(item.wagePercent);
|
||
|
|
const discountPercent = new Prisma.Decimal(item.discountPercent ?? 0);
|
||
|
|
const netPercent = wagePercent.sub(discountPercent);
|
||
|
|
const computedWeightGrams = rashWeight.mul(netPercent.div(100).add(1));
|
||
|
|
|
||
|
|
return {
|
||
|
|
productVariantId: variant.id,
|
||
|
|
quantity: item.quantity,
|
||
|
|
rashWeightGramsSnapshot: rashWeight,
|
||
|
|
wagePercentSnapshot: wagePercent,
|
||
|
|
discountPercentSnapshot: discountPercent,
|
||
|
|
computedWeightGrams,
|
||
|
|
flatRialCharge: new Prisma.Decimal(item.flatRialCharge ?? 0),
|
||
|
|
};
|
||
|
|
}),
|
||
|
|
);
|
||
|
|
|
||
|
|
const totalGoldWeightGrams = computedItems.reduce(
|
||
|
|
(sum, item) => sum.add(item.computedWeightGrams.mul(item.quantity)),
|
||
|
|
new Prisma.Decimal(0),
|
||
|
|
);
|
||
|
|
const totalRashWeightGrams = computedItems.reduce(
|
||
|
|
(sum, item) => sum.add(item.rashWeightGramsSnapshot.mul(item.quantity)),
|
||
|
|
new Prisma.Decimal(0),
|
||
|
|
);
|
||
|
|
const wageWeightGrams = totalGoldWeightGrams.sub(totalRashWeightGrams);
|
||
|
|
const flatRialCharge = computedItems.reduce(
|
||
|
|
(sum, item) => sum.add(item.flatRialCharge.mul(item.quantity)),
|
||
|
|
new Prisma.Decimal(0),
|
||
|
|
);
|
||
|
|
|
||
|
|
// legacy: inValidOrder = (allGram + customer.remain) > customer.gram_level
|
||
|
|
const isSuspendedOverCreditLimit = totalGoldWeightGrams
|
||
|
|
.add(customer.goldDebtGrams)
|
||
|
|
.gt(customer.goldCreditLimitGrams);
|
||
|
|
const initialStatus = isSuspendedOverCreditLimit ? OrderStatus.SUSPENDED : OrderStatus.PLACED;
|
||
|
|
|
||
|
|
return this.prisma.$transaction(async (tx) => {
|
||
|
|
const order = await tx.order.create({
|
||
|
|
data: {
|
||
|
|
orderNumber: this.generateOrderNumber(),
|
||
|
|
customerId: dto.customerId,
|
||
|
|
branchId: dto.branchId,
|
||
|
|
status: initialStatus,
|
||
|
|
totalGoldWeightGrams,
|
||
|
|
wageWeightGrams,
|
||
|
|
flatRialCharge,
|
||
|
|
isSuspendedOverCreditLimit,
|
||
|
|
items: {
|
||
|
|
create: computedItems.map((item) => ({
|
||
|
|
productVariantId: item.productVariantId,
|
||
|
|
quantity: item.quantity,
|
||
|
|
rashWeightGramsSnapshot: item.rashWeightGramsSnapshot,
|
||
|
|
wagePercentSnapshot: item.wagePercentSnapshot,
|
||
|
|
discountPercentSnapshot: item.discountPercentSnapshot,
|
||
|
|
computedWeightGrams: item.computedWeightGrams,
|
||
|
|
flatRialCharge: item.flatRialCharge,
|
||
|
|
})),
|
||
|
|
},
|
||
|
|
statusEvents: {
|
||
|
|
create: { toStatus: initialStatus, note: isSuspendedOverCreditLimit ? 'Credit limit exceeded' : undefined },
|
||
|
|
},
|
||
|
|
},
|
||
|
|
include: { items: true, statusEvents: true },
|
||
|
|
});
|
||
|
|
|
||
|
|
return order;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
async updateStatus(orderId: string, newStatus: OrderStatus, actorAdminUserId?: string, note?: string) {
|
||
|
|
return this.prisma.$transaction(async (tx) => {
|
||
|
|
const order = await tx.order.findUnique({ where: { id: orderId } });
|
||
|
|
if (!order) throw new NotFoundException(`Order ${orderId} not found`);
|
||
|
|
|
||
|
|
const updated = await tx.order.update({
|
||
|
|
where: { id: orderId },
|
||
|
|
data: {
|
||
|
|
status: newStatus,
|
||
|
|
deliveredAt: newStatus === OrderStatus.DELIVERED_TO_CUSTOMER ? new Date() : order.deliveredAt,
|
||
|
|
},
|
||
|
|
});
|
||
|
|
|
||
|
|
await tx.orderStatusEvent.create({
|
||
|
|
data: { orderId, fromStatus: order.status, toStatus: newStatus, note, actorAdminUserId },
|
||
|
|
});
|
||
|
|
|
||
|
|
// Debt recognition point — mirrors legacy sts>4, which first becomes true
|
||
|
|
// at sts=5 ("تحویل مسئول فروش" / delivered to agent), not sts=6 (delivered
|
||
|
|
// to customer). Only fires once: a second transition into this status
|
||
|
|
// (e.g. a data-fix re-save) would double-post debt, so guard on the order
|
||
|
|
// not already having deliveredAt.
|
||
|
|
if (newStatus === OrderStatus.DELIVERED_TO_AGENT && !order.deliveredAt) {
|
||
|
|
await this.ledger.postOrderDebt(orderId, tx);
|
||
|
|
}
|
||
|
|
|
||
|
|
return updated;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
getById(orderId: string) {
|
||
|
|
return this.prisma.order.findUnique({
|
||
|
|
where: { id: orderId },
|
||
|
|
include: { items: true, statusEvents: true, ledgerEntries: true },
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
private generateOrderNumber(): string {
|
||
|
|
const random = Math.random().toString(36).slice(2, 8).toUpperCase();
|
||
|
|
return `ORD-${Date.now()}-${random}`;
|
||
|
|
}
|
||
|
|
}
|