add tax to order
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
import {
|
||||
IsInt, IsArray, IsNumber,
|
||||
IsNotEmpty,
|
||||
ArrayMinSize
|
||||
ArrayMinSize,
|
||||
IsBoolean
|
||||
} from 'class-validator';
|
||||
import { ApiProperty } from '@nestjs/swagger';
|
||||
import { Type } from 'class-transformer';
|
||||
@@ -36,6 +37,9 @@ export class CreateInvoiceDto {
|
||||
@Type(() => InvoiceItemDto)
|
||||
items: InvoiceItemDto[];
|
||||
|
||||
@ApiProperty()
|
||||
@IsBoolean()
|
||||
enableTax: boolean
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -60,6 +60,9 @@ export class Order extends BaseEntity {
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||
subTotal!: number;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||
taxAmount: number;
|
||||
|
||||
|
||||
// @Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||
// deliveryFee: number = 0;
|
||||
@@ -75,6 +78,9 @@ export class Order extends BaseEntity {
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||
balance!: number;
|
||||
|
||||
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true })
|
||||
dueDate?: number;
|
||||
// get balance(): number {
|
||||
// return this._balance
|
||||
// }
|
||||
@@ -88,6 +94,12 @@ export class Order extends BaseEntity {
|
||||
@Enum(() => OrderStatusEnum)
|
||||
status!: OrderStatusEnum;
|
||||
|
||||
@Property({ nullable: true, columnType: 'timestamptz' })
|
||||
confirmedAt?: Date;
|
||||
|
||||
@Property({ type: 'json', nullable: true })
|
||||
attachments?: string[]
|
||||
|
||||
@BeforeCreate()
|
||||
async generateOrderNumber(args: EventArgs<Order>) {
|
||||
const em = args.em;
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Injectable, NotFoundException, BadRequestException, Logger } from '@nestjs/common';
|
||||
import { Injectable, BadRequestException, Logger } from '@nestjs/common';
|
||||
import { EntityManager } from '@mikro-orm/postgresql';
|
||||
import { OrderItem } from '../entities/order-item.entity';
|
||||
import { OrderRepository } from '../repositories/order.repository';
|
||||
@@ -53,7 +53,8 @@ export class OrderService {
|
||||
total: 0,
|
||||
paidAmount: 0,
|
||||
balance: 0,
|
||||
status: OrderStatusEnum.DRAFT
|
||||
status: OrderStatusEnum.DRAFT,
|
||||
taxAmount: 0
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
@@ -128,7 +129,8 @@ export class OrderService {
|
||||
total,
|
||||
paidAmount: 0,
|
||||
balance: total,
|
||||
status: OrderStatusEnum.INVOICED
|
||||
status: OrderStatusEnum.INVOICED,
|
||||
taxAmount: 0
|
||||
})
|
||||
|
||||
em.persist(order)
|
||||
@@ -200,7 +202,7 @@ export class OrderService {
|
||||
}
|
||||
|
||||
async createInvoice(orderId: string, dto: CreateInvoiceDto) {
|
||||
const { items } = dto
|
||||
const { items, enableTax } = dto
|
||||
|
||||
const targetOrder = await this.findOneOrFail(orderId)
|
||||
|
||||
@@ -236,8 +238,17 @@ export class OrderService {
|
||||
targetOrder.subTotal = subTotal
|
||||
targetOrder.discount = totalDiscount
|
||||
|
||||
targetOrder.total = subTotal - totalDiscount
|
||||
targetOrder.balance = subTotal - totalDiscount
|
||||
const total = subTotal - totalDiscount
|
||||
let tax = 0
|
||||
|
||||
if (enableTax) {
|
||||
tax = 0.1 * total
|
||||
}
|
||||
|
||||
targetOrder.taxAmount = tax
|
||||
targetOrder.total = total + tax
|
||||
targetOrder.balance = total + tax
|
||||
|
||||
|
||||
// change status
|
||||
targetOrder.status = OrderStatusEnum.INVOICED
|
||||
|
||||
Reference in New Issue
Block a user