add item to invoice
This commit is contained in:
@@ -54,7 +54,7 @@ export class OrderService {
|
||||
paidAmount: 0,
|
||||
balance: 0,
|
||||
status: OrderStatusEnum.DRAFT,
|
||||
taxAmount: 0
|
||||
taxAmount: 0,
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
@@ -202,14 +202,14 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async createInvoice(orderId: string, dto: CreateInvoiceDto) {
|
||||
const { items, enableTax } = dto
|
||||
const { items, enableTax, attachments, newItems } = dto
|
||||
|
||||
const targetOrder = await this.findOneOrFail(orderId)
|
||||
|
||||
if (items.length !== targetOrder.items.length) {
|
||||
throw new BadRequestException("One or more order items price is not set")
|
||||
}
|
||||
|
||||
//TODO : calc is not correct
|
||||
|
||||
const order = await this.em.transactional(async (em) => {
|
||||
|
||||
@@ -234,6 +234,33 @@ export class OrderService {
|
||||
|
||||
}
|
||||
|
||||
newItems.forEach(async item => {
|
||||
const found = order.items.find((it => Number(it.product.id) === item.productId))
|
||||
|
||||
if (found) {
|
||||
throw new BadRequestException("Product already exist!")
|
||||
}
|
||||
const product = await this.productRepository.findOne({ id: item.productId })
|
||||
if (!product) {
|
||||
throw new BadRequestException("Product not found!")
|
||||
}
|
||||
const newOrderItem = this.orderItemRepository.create({
|
||||
discount: item.discount,
|
||||
product,
|
||||
unitPrice: item.unitPrice,
|
||||
total: (item.unitPrice - item.discount) * item.quantity,
|
||||
subTotal: item.unitPrice * item.quantity,
|
||||
quantity: item.quantity,
|
||||
status: OrderItemStatus.CONFIRMED,
|
||||
attributesValues: item.attributesValues,
|
||||
order
|
||||
})
|
||||
|
||||
subTotal += item.unitPrice
|
||||
|
||||
this.em.persist(newOrderItem)
|
||||
})
|
||||
|
||||
// calculate order financials
|
||||
targetOrder.subTotal = subTotal
|
||||
targetOrder.discount = totalDiscount
|
||||
@@ -249,6 +276,9 @@ export class OrderService {
|
||||
targetOrder.total = total + tax
|
||||
targetOrder.balance = total + tax
|
||||
|
||||
if (attachments.length) {
|
||||
order.attachments = attachments
|
||||
}
|
||||
|
||||
// change status
|
||||
targetOrder.status = OrderStatusEnum.INVOICED
|
||||
@@ -325,40 +355,40 @@ export class OrderService {
|
||||
|
||||
}
|
||||
|
||||
async addOrderItem(orderId: string, dto: AddOrderItemDto) {
|
||||
const order = await this.orderRepository.findOne({ id: orderId })
|
||||
if (!order) {
|
||||
throw new BadRequestException("Order not Found!")
|
||||
}
|
||||
const { items } = dto
|
||||
// async addOrderItem(orderId: string, dto: AddOrderItemDto) {
|
||||
// const order = await this.orderRepository.findOne({ id: orderId })
|
||||
// if (!order) {
|
||||
// throw new BadRequestException("Order not Found!")
|
||||
// }
|
||||
// const { items } = dto
|
||||
|
||||
items.forEach(async item => {
|
||||
const found = order.items.find((it => Number(it.product.id) === item.productId))
|
||||
// items.forEach(async item => {
|
||||
// const found = order.items.find((it => Number(it.product.id) === item.productId))
|
||||
|
||||
if (found) {
|
||||
throw new BadRequestException("Product already exist!")
|
||||
}
|
||||
const product = await this.productRepository.findOne({ id: item.productId })
|
||||
if (!product) {
|
||||
throw new BadRequestException("Product not found!")
|
||||
}
|
||||
const newOrderItem = this.orderItemRepository.create({
|
||||
discount: item.discount,
|
||||
product,
|
||||
unitPrice: item.unitPrice,
|
||||
total: (item.unitPrice - item.discount) * item.quantity,
|
||||
subTotal: item.unitPrice * item.quantity,
|
||||
quantity: item.quantity,
|
||||
status: OrderItemStatus.CONFIRMED,
|
||||
attributesValues: item.attributesValues,
|
||||
order
|
||||
})
|
||||
// if (found) {
|
||||
// throw new BadRequestException("Product already exist!")
|
||||
// }
|
||||
// const product = await this.productRepository.findOne({ id: item.productId })
|
||||
// if (!product) {
|
||||
// throw new BadRequestException("Product not found!")
|
||||
// }
|
||||
// const newOrderItem = this.orderItemRepository.create({
|
||||
// discount: item.discount,
|
||||
// product,
|
||||
// unitPrice: item.unitPrice,
|
||||
// total: (item.unitPrice - item.discount) * item.quantity,
|
||||
// subTotal: item.unitPrice * item.quantity,
|
||||
// quantity: item.quantity,
|
||||
// status: OrderItemStatus.CONFIRMED,
|
||||
// attributesValues: item.attributesValues,
|
||||
// order
|
||||
// })
|
||||
|
||||
this.em.persist(newOrderItem)
|
||||
})
|
||||
// need calculate order finacial
|
||||
await this.em.flush()
|
||||
// this.em.persist(newOrderItem)
|
||||
// })
|
||||
// // need calculate order finacial
|
||||
// await this.em.flush()
|
||||
|
||||
return order
|
||||
}
|
||||
// return order
|
||||
// }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user