product
This commit is contained in:
@@ -15,7 +15,7 @@ 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';
|
||||
|
||||
|
||||
|
||||
@Injectable()
|
||||
export class OrderService {
|
||||
@@ -52,8 +52,9 @@ export class OrderService {
|
||||
total: 0,
|
||||
paidAmount: 0,
|
||||
balance: 0,
|
||||
status: OrderStatusEnum.DRAFT,
|
||||
status: OrderStatusEnum.CREATED,
|
||||
taxAmount: 0,
|
||||
enableTax: false
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
@@ -198,7 +199,47 @@ export class OrderService {
|
||||
return order
|
||||
}
|
||||
|
||||
async createInvoice(orderId: string, dto: CreateInvoiceDto) {
|
||||
async calculateOrder(orderId: string, em: EntityManager) {
|
||||
|
||||
const targetOrder = await this.findOneOrFail(orderId)
|
||||
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
|
||||
// calculate order financials
|
||||
let subTotal = 0
|
||||
let totalDiscount = 0
|
||||
|
||||
// TODO : use reduce
|
||||
|
||||
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
|
||||
})
|
||||
|
||||
return order
|
||||
}
|
||||
|
||||
async updateOrder(orderId: string, dto: CreateInvoiceDto) {
|
||||
const { items, enableTax, attachments, newItems } = dto
|
||||
|
||||
const targetOrder = await this.findOneOrFail(orderId)
|
||||
@@ -222,7 +263,7 @@ export class OrderService {
|
||||
}
|
||||
// TODO use Deciaml js to calculation
|
||||
orderItem.unitPrice = found.unitPrice
|
||||
orderItem.subTotal = (found.unitPrice) * orderItem.quantity
|
||||
// orderItem.subTotal = (found.unitPrice) * orderItem.quantity
|
||||
|
||||
|
||||
subTotal += orderItem.subTotal
|
||||
@@ -279,6 +320,7 @@ export class OrderService {
|
||||
|
||||
// change status
|
||||
targetOrder.status = OrderStatusEnum.INVOICED
|
||||
targetOrder.invoicedAt = new Date()
|
||||
|
||||
em.persist(targetOrder)
|
||||
|
||||
@@ -321,7 +363,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
order.designer = designer
|
||||
order.status = OrderStatusEnum.IN_DESIGN
|
||||
// order.status = OrderStatusEnum.IN_DESIGN
|
||||
|
||||
|
||||
await this.em.persistAndFlush(order)
|
||||
@@ -335,7 +377,7 @@ export class OrderService {
|
||||
if (!order) {
|
||||
throw new BadRequestException("Order not found")
|
||||
}
|
||||
if (![OrderStatusEnum.DRAFT, OrderStatusEnum.INVOICED].includes(order.status)) {
|
||||
if (![OrderStatusEnum.CREATED, OrderStatusEnum.INVOICED].includes(order.status)) {
|
||||
throw new BadRequestException("Order status must be of of Drfat or Invoiced")
|
||||
}
|
||||
if (order.payments.length > 0) {
|
||||
@@ -352,6 +394,18 @@ export class OrderService {
|
||||
|
||||
}
|
||||
|
||||
async updateStatus(orderId: string, newStatus: OrderStatusEnum) {
|
||||
const order = await this.orderRepository.findOne({ id: orderId })
|
||||
if (!order) {
|
||||
throw new BadRequestException("Order not found")
|
||||
}
|
||||
|
||||
order.status = newStatus
|
||||
|
||||
await this.em.flush()
|
||||
|
||||
return order
|
||||
}
|
||||
// async addOrderItem(orderId: string, dto: AddOrderItemDto) {
|
||||
// const order = await this.orderRepository.findOne({ id: orderId })
|
||||
// if (!order) {
|
||||
|
||||
Reference in New Issue
Block a user