import Decimal from "decimal.js"; import { Column, Entity, ManyToOne, OneToMany } from "typeorm"; import { InvoiceItem } from "./invoice-item.entity"; import { BaseEntity } from "../../../common/entities/base.entity"; import { DecimalTransformer } from "../../../common/transformers/decimal.transformer"; import { Discount } from "../../discounts/entities/discount.entity"; import { User } from "../../users/entities/user.entity"; import { InvoiceStatus } from "../enums/invoice-status.enum"; @Entity() export class Invoice extends BaseEntity { @Column({ type: "int", generated: "identity", insert: false }) numericId: number; @ManyToOne(() => User, (user) => user.invoices, { nullable: true, onDelete: "RESTRICT" }) user: User; @Column({ type: "decimal", precision: 16, scale: 2, nullable: false, transformer: new DecimalTransformer() }) totalPrice: Decimal; @Column({ type: "decimal", precision: 16, scale: 2, nullable: true, transformer: new DecimalTransformer() }) originalPrice?: Decimal; @Column({ type: "enum", enum: InvoiceStatus, default: InvoiceStatus.PENDING }) status: InvoiceStatus; @Column({ type: "timestamptz", nullable: false }) dueDate: Date; @Column({ type: "timestamptz", nullable: true }) paidAt?: Date; @Column({ type: "decimal", precision: 16, scale: 2, nullable: false, transformer: new DecimalTransformer() }) tax: Decimal; @Column({ type: "decimal", precision: 16, scale: 2, nullable: true, transformer: new DecimalTransformer() }) lateFee: Decimal | null; //--------------------------------------------- @OneToMany(() => InvoiceItem, (invoiceItem) => invoiceItem.invoice, { cascade: true }) items: InvoiceItem[]; @ManyToOne(() => Discount, (discount) => discount.invoices, { nullable: true, onDelete: "RESTRICT" }) discount: Discount | null; get isOverdue(): boolean { return this.status === InvoiceStatus.PENDING && new Date() > this.dueDate; } }