diff --git a/database/migrations/.snapshot-dmenu.json b/database/migrations/.snapshot-dmenu.json index 02a8e79..3903d2b 100644 --- a/database/migrations/.snapshot-dmenu.json +++ b/database/migrations/.snapshot-dmenu.json @@ -4284,6 +4284,18 @@ "default": "0", "mappedType": "decimal" }, + "packing_fee": { + "name": "packing_fee", + "type": "numeric(10,0)", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "precision": 10, + "scale": 0, + "default": "0", + "mappedType": "decimal" + }, "total": { "name": "total", "type": "numeric(10,0)", diff --git a/database/migrations/Migration20260701062227_addPackingFeeToOrders.ts b/database/migrations/Migration20260701062227_addPackingFeeToOrders.ts new file mode 100644 index 0000000..5cec471 --- /dev/null +++ b/database/migrations/Migration20260701062227_addPackingFeeToOrders.ts @@ -0,0 +1,13 @@ +import { Migration } from '@mikro-orm/migrations'; + +export class Migration20260701062227_addPackingFeeToOrders extends Migration { + + override async up(): Promise { + this.addSql(`alter table "orders" add column "packing_fee" numeric(10,0) not null default 0;`); + } + + override async down(): Promise { + this.addSql(`alter table "orders" drop column "packing_fee";`); + } + +} diff --git a/src/modules/orders/dto/admin-create-order.dto.ts b/src/modules/orders/dto/admin-create-order.dto.ts index e354d69..438e13d 100644 --- a/src/modules/orders/dto/admin-create-order.dto.ts +++ b/src/modules/orders/dto/admin-create-order.dto.ts @@ -53,6 +53,12 @@ export class AdminCreateOrderDto { @Min(0) discountAmount?: number; + @ApiPropertyOptional({ description: 'Packing fee amount', minimum: 0, default: 0 }) + @IsOptional() + @IsNumber() + @Min(0) + packingFee?: number; + @ApiPropertyOptional({ description: 'Order description or notes' }) @IsOptional() @IsString() diff --git a/src/modules/orders/entities/order.entity.ts b/src/modules/orders/entities/order.entity.ts index dc75ad2..195ea26 100644 --- a/src/modules/orders/entities/order.entity.ts +++ b/src/modules/orders/entities/order.entity.ts @@ -80,6 +80,9 @@ export class Order extends BaseEntity { @Property({ type: 'decimal', precision: 10, scale: 0, default: 0 }) deliveryFee: number = 0; + @Property({ type: 'decimal', precision: 10, scale: 0, default: 0 }) + packingFee: number = 0; + @Property({ type: 'decimal', precision: 10, scale: 0 }) total!: number; diff --git a/src/modules/orders/providers/orders.service.ts b/src/modules/orders/providers/orders.service.ts index d98298b..aa19fbf 100644 --- a/src/modules/orders/providers/orders.service.ts +++ b/src/modules/orders/providers/orders.service.ts @@ -83,6 +83,7 @@ export class OrdersService { subTotal: cart.subTotal || 0, tax: cart.tax || 0, deliveryFee: cart.deliveryFee || 0, + packingFee: 0, total: cart.total || 0, totalItems: cart.totalItems || 0, description: cart.description, @@ -169,13 +170,14 @@ export class OrdersService { const subTotal = orderItemsData.reduce((sum, item) => sum + item.unitPrice * item.quantity, 0); const itemsDiscount = orderItemsData.reduce((sum, item) => sum + item.discount * item.quantity, 0); const deliveryFee = delivery.deliveryFeeType === DeliveryFeeTypeEnum.FIXED ? Number(delivery.deliveryFee) : 0; + const packingFee = dto.packingFee ?? 0; const adminDiscount = dto.discountAmount ?? 0; const maxDiscount = subTotal - itemsDiscount; if (adminDiscount > maxDiscount) { throw new BadRequestException(OrderMessage.DISCOUNT_AMOUNT_EXCEEDS_ORDER_TOTAL); } const totalDiscount = itemsDiscount + adminDiscount; - const total = subTotal - totalDiscount + deliveryFee; + const total = subTotal - totalDiscount + deliveryFee + packingFee; const totalItems = orderItemsData.reduce((sum, item) => sum + item.quantity, 0); const order = await this.em.transactional(async em => { @@ -193,6 +195,7 @@ export class OrdersService { subTotal, tax: 0, deliveryFee, + packingFee, total, totalItems, description: dto.description,