This commit is contained in:
2026-01-25 15:05:01 +03:30
parent bca1932ae3
commit 55856a40f1
6 changed files with 62 additions and 46 deletions
+32 -35
View File
@@ -15,6 +15,8 @@ import { TicketRepository } from 'src/modules/ticket/repositories/tickets.reposi
import { CreateInvoiceDto } from '../dto/create-invoice.dto';
import { AdminRepository } from 'src/modules/admin/repositories/admin.repository';
import { CreateOrderAsAdminDto } from '../dto/create-order-as-admin.dto';
import { Order } from '../entities/order.entity';
import { PaymentRepository } from 'src/modules/payment/repositories/payment.repository';
@Injectable()
@@ -33,6 +35,7 @@ export class OrderService {
private readonly ticketService: TicketService,
private readonly eventEmitter: EventEmitter2,
private readonly adminRepository: AdminRepository,
private readonly paymentRepository: PaymentRepository,
) { }
async createNewOrder(userId: string, dto: CreateOrderDto) {
@@ -94,7 +97,7 @@ export class OrderService {
return order
}
async createOrdeAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
async createOrderAsAdmin(adminId: string, dto: CreateOrderAsAdminDto) {
const { items, userPhone } = dto
const user = await this.userService.findByPhone(userPhone)
@@ -131,7 +134,6 @@ export class OrderService {
throw new BadRequestException("some products not found")
}
items.forEach(item => {
const product = products.find(p => p.id == item.productId)
@@ -146,8 +148,10 @@ export class OrderService {
quantity: item.quantity,
unitPrice: item.unitPrice,
total: item.unitPrice * item.quantity,
discount: 0,
subTotal: 0
discount: item.discount,
subTotal: item.unitPrice * item.quantity - item.discount,
attachments: item.attachments,
description: item.description
})
em.persist(orderItem)
@@ -155,6 +159,8 @@ export class OrderService {
// TODO : calculation must be done after create order
await this.calculateOrder(order, em)
await em.flush()
return order
@@ -178,42 +184,33 @@ export class OrderService {
return order
}
async calculateOrder(orderId: string, em: EntityManager) {
async calculateOrder(order: Order, em: EntityManager) {
const targetOrder = await this.findOneOrFail(orderId)
// calculate order financials
let subTotal = 0
let totalDiscount = 0
const order = await this.em.transactional(async (em) => {
// TODO : use reduce
for (let orderItem of order.items) {
subTotal += orderItem.total
totalDiscount += orderItem.discount
}
// calculate order financials
let subTotal = 0
let totalDiscount = 0
const totalBeforeTax = subTotal - totalDiscount
let tax = 0
// TODO : use reduce
if (order.enableTax) {
tax = 0.1 * totalBeforeTax
}
for (let orderItem of targetOrder.items) {
subTotal += orderItem.total
totalDiscount += orderItem.discount
}
const totalBeforeTax = subTotal - totalDiscount
let tax = 0
if (targetOrder.enableTax) {
tax = 0.1 * totalBeforeTax
}
const total = totalBeforeTax + tax
// Update Order financial values
targetOrder.subTotal = subTotal
targetOrder.discount = totalDiscount
targetOrder.taxAmount = tax
targetOrder.total = total
em.persist(targetOrder)
return targetOrder
})
const total = totalBeforeTax + tax
const paidAmount = await this.paymentRepository.getActualPaidAmount(order.id)
// Update Order financial values
order.subTotal = subTotal
order.discount = totalDiscount
order.taxAmount = tax
order.total = total
order.balance = total - paidAmount
return order
}