add tax to order
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
import {
|
import {
|
||||||
IsInt, IsArray, IsNumber,
|
IsInt, IsArray, IsNumber,
|
||||||
IsNotEmpty,
|
IsNotEmpty,
|
||||||
ArrayMinSize
|
ArrayMinSize,
|
||||||
|
IsBoolean
|
||||||
} from 'class-validator';
|
} from 'class-validator';
|
||||||
import { ApiProperty } from '@nestjs/swagger';
|
import { ApiProperty } from '@nestjs/swagger';
|
||||||
import { Type } from 'class-transformer';
|
import { Type } from 'class-transformer';
|
||||||
@@ -36,6 +37,9 @@ export class CreateInvoiceDto {
|
|||||||
@Type(() => InvoiceItemDto)
|
@Type(() => InvoiceItemDto)
|
||||||
items: InvoiceItemDto[];
|
items: InvoiceItemDto[];
|
||||||
|
|
||||||
|
@ApiProperty()
|
||||||
|
@IsBoolean()
|
||||||
|
enableTax: boolean
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,9 @@ export class Order extends BaseEntity {
|
|||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
subTotal!: number;
|
subTotal!: number;
|
||||||
|
|
||||||
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
|
taxAmount: number;
|
||||||
|
|
||||||
|
|
||||||
// @Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
// @Property({ type: 'decimal', precision: 10, scale: 0, default: 0 })
|
||||||
// deliveryFee: number = 0;
|
// deliveryFee: number = 0;
|
||||||
@@ -75,6 +78,9 @@ export class Order extends BaseEntity {
|
|||||||
|
|
||||||
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
@Property({ type: 'decimal', precision: 10, scale: 0 })
|
||||||
balance!: number;
|
balance!: number;
|
||||||
|
|
||||||
|
@Property({ type: 'decimal', precision: 10, scale: 0, nullable: true })
|
||||||
|
dueDate?: number;
|
||||||
// get balance(): number {
|
// get balance(): number {
|
||||||
// return this._balance
|
// return this._balance
|
||||||
// }
|
// }
|
||||||
@@ -88,6 +94,12 @@ export class Order extends BaseEntity {
|
|||||||
@Enum(() => OrderStatusEnum)
|
@Enum(() => OrderStatusEnum)
|
||||||
status!: OrderStatusEnum;
|
status!: OrderStatusEnum;
|
||||||
|
|
||||||
|
@Property({ nullable: true, columnType: 'timestamptz' })
|
||||||
|
confirmedAt?: Date;
|
||||||
|
|
||||||
|
@Property({ type: 'json', nullable: true })
|
||||||
|
attachments?: string[]
|
||||||
|
|
||||||
@BeforeCreate()
|
@BeforeCreate()
|
||||||
async generateOrderNumber(args: EventArgs<Order>) {
|
async generateOrderNumber(args: EventArgs<Order>) {
|
||||||
const em = args.em;
|
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 { EntityManager } from '@mikro-orm/postgresql';
|
||||||
import { OrderItem } from '../entities/order-item.entity';
|
import { OrderItem } from '../entities/order-item.entity';
|
||||||
import { OrderRepository } from '../repositories/order.repository';
|
import { OrderRepository } from '../repositories/order.repository';
|
||||||
@@ -53,7 +53,8 @@ export class OrderService {
|
|||||||
total: 0,
|
total: 0,
|
||||||
paidAmount: 0,
|
paidAmount: 0,
|
||||||
balance: 0,
|
balance: 0,
|
||||||
status: OrderStatusEnum.DRAFT
|
status: OrderStatusEnum.DRAFT,
|
||||||
|
taxAmount: 0
|
||||||
})
|
})
|
||||||
|
|
||||||
em.persist(order)
|
em.persist(order)
|
||||||
@@ -128,7 +129,8 @@ export class OrderService {
|
|||||||
total,
|
total,
|
||||||
paidAmount: 0,
|
paidAmount: 0,
|
||||||
balance: total,
|
balance: total,
|
||||||
status: OrderStatusEnum.INVOICED
|
status: OrderStatusEnum.INVOICED,
|
||||||
|
taxAmount: 0
|
||||||
})
|
})
|
||||||
|
|
||||||
em.persist(order)
|
em.persist(order)
|
||||||
@@ -200,7 +202,7 @@ export class OrderService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async createInvoice(orderId: string, dto: CreateInvoiceDto) {
|
async createInvoice(orderId: string, dto: CreateInvoiceDto) {
|
||||||
const { items } = dto
|
const { items, enableTax } = dto
|
||||||
|
|
||||||
const targetOrder = await this.findOneOrFail(orderId)
|
const targetOrder = await this.findOneOrFail(orderId)
|
||||||
|
|
||||||
@@ -236,8 +238,17 @@ export class OrderService {
|
|||||||
targetOrder.subTotal = subTotal
|
targetOrder.subTotal = subTotal
|
||||||
targetOrder.discount = totalDiscount
|
targetOrder.discount = totalDiscount
|
||||||
|
|
||||||
targetOrder.total = subTotal - totalDiscount
|
const total = subTotal - totalDiscount
|
||||||
targetOrder.balance = subTotal - totalDiscount
|
let tax = 0
|
||||||
|
|
||||||
|
if (enableTax) {
|
||||||
|
tax = 0.1 * total
|
||||||
|
}
|
||||||
|
|
||||||
|
targetOrder.taxAmount = tax
|
||||||
|
targetOrder.total = total + tax
|
||||||
|
targetOrder.balance = total + tax
|
||||||
|
|
||||||
|
|
||||||
// change status
|
// change status
|
||||||
targetOrder.status = OrderStatusEnum.INVOICED
|
targetOrder.status = OrderStatusEnum.INVOICED
|
||||||
|
|||||||
@@ -31,6 +31,9 @@ export class Ticket {
|
|||||||
@ManyToOne(() => Order)
|
@ManyToOne(() => Order)
|
||||||
order!: Order
|
order!: Order
|
||||||
|
|
||||||
|
@ManyToOne(() => Ticket, { nullable: true })
|
||||||
|
parent?: Ticket
|
||||||
|
|
||||||
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
|
@Property({ defaultRaw: 'now()', columnType: 'timestamptz' })
|
||||||
createdAt: Date = new Date();
|
createdAt: Date = new Date();
|
||||||
|
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ export class UploaderController {
|
|||||||
return this.uploaderService.uploadMultiple(files);
|
return this.uploaderService.uploadMultiple(files);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
@ApiBearerAuth()
|
// @ApiBearerAuth()
|
||||||
@ApiOperation({ summary: 'Upload a file (admin)' })
|
@ApiOperation({ summary: 'Upload a file (admin)' })
|
||||||
@ApiConsumes('multipart/form-data')
|
@ApiConsumes('multipart/form-data')
|
||||||
@UseInterceptors(FileInterceptor('file'))
|
@UseInterceptors(FileInterceptor('file'))
|
||||||
@@ -44,8 +44,8 @@ export class UploaderController {
|
|||||||
return this.uploaderService.uploadFile(file);
|
return this.uploaderService.uploadFile(file);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AdminAuthGuard)
|
// @UseGuards(AdminAuthGuard)
|
||||||
@ApiBearerAuth()
|
// @ApiBearerAuth()
|
||||||
@ApiOperation({ summary: 'Uploads multiple files' })
|
@ApiOperation({ summary: 'Uploads multiple files' })
|
||||||
@ApiConsumes('multipart/form-data')
|
@ApiConsumes('multipart/form-data')
|
||||||
@UseInterceptors(FilesInterceptor('files'))
|
@UseInterceptors(FilesInterceptor('files'))
|
||||||
|
|||||||
Reference in New Issue
Block a user