fix invoice number
This commit is contained in:
@@ -2347,9 +2347,9 @@
|
|||||||
},
|
},
|
||||||
"invoice_number": {
|
"invoice_number": {
|
||||||
"name": "invoice_number",
|
"name": "invoice_number",
|
||||||
"type": "int",
|
"type": "serial",
|
||||||
"unsigned": false,
|
"unsigned": true,
|
||||||
"autoincrement": false,
|
"autoincrement": true,
|
||||||
"primary": false,
|
"primary": false,
|
||||||
"nullable": false,
|
"nullable": false,
|
||||||
"mappedType": "integer"
|
"mappedType": "integer"
|
||||||
|
|||||||
@@ -3,7 +3,20 @@ import { Migration } from '@mikro-orm/migrations';
|
|||||||
export class Migration20260516121012 extends Migration {
|
export class Migration20260516121012 extends Migration {
|
||||||
|
|
||||||
override async up(): Promise<void> {
|
override async up(): Promise<void> {
|
||||||
this.addSql(`alter table "invoice_items" add column "sub_total" int generated always as (COALESCE(unit_price, 0) * quantity) STORED not null, add column "total" int generated always as ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED not null;`);
|
this.addSql(`
|
||||||
|
DO $$ BEGIN
|
||||||
|
ALTER TABLE "invoice_items"
|
||||||
|
ADD COLUMN "sub_total" int GENERATED ALWAYS AS (COALESCE(unit_price, 0) * quantity) STORED NOT NULL,
|
||||||
|
ADD COLUMN "total" int GENERATED ALWAYS AS ((COALESCE(unit_price, 0) * quantity) - COALESCE(discount, 0)) STORED NOT NULL;
|
||||||
|
EXCEPTION
|
||||||
|
WHEN duplicate_column THEN NULL;
|
||||||
|
END $$;
|
||||||
|
`);
|
||||||
|
}
|
||||||
|
|
||||||
|
override async down(): Promise<void> {
|
||||||
|
this.addSql(`alter table "invoice_items" drop column if exists "sub_total";`);
|
||||||
|
this.addSql(`alter table "invoice_items" drop column if exists "total";`);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { Migration } from '@mikro-orm/migrations';
|
||||||
|
|
||||||
|
export class Migration20260517190050 extends Migration {
|
||||||
|
|
||||||
|
override async up(): Promise<void> {
|
||||||
|
this.addSql(`CREATE SEQUENCE IF NOT EXISTS invoice_number_seq;`);
|
||||||
|
this.addSql(`SELECT setval('invoice_number_seq', COALESCE((SELECT MAX(invoice_number) FROM invoices), 0) + 1, false);`);
|
||||||
|
this.addSql(`ALTER TABLE "invoices" ALTER COLUMN "invoice_number" SET DEFAULT nextval('invoice_number_seq');`);
|
||||||
|
}
|
||||||
|
|
||||||
|
override async down(): Promise<void> {
|
||||||
|
this.addSql(`ALTER TABLE "invoices" ALTER COLUMN "invoice_number" DROP DEFAULT;`);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -24,7 +24,7 @@ import { IAttachment } from 'src/modules/order/interface/order.interface';
|
|||||||
@Entity({ tableName: 'invoices' })
|
@Entity({ tableName: 'invoices' })
|
||||||
@Index({ properties: ['user'] })
|
@Index({ properties: ['user'] })
|
||||||
export class Invoice extends BaseEntity {
|
export class Invoice extends BaseEntity {
|
||||||
[OptionalProps]?: 'createdAt' | 'deletedAt'
|
[OptionalProps]?: 'createdAt' | 'deletedAt' | 'invoiceNumber'
|
||||||
|
|
||||||
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
|
@PrimaryKey({ type: 'string', columnType: 'char(26)' })
|
||||||
id: string = ulid()
|
id: string = ulid()
|
||||||
@@ -50,9 +50,10 @@ export class Invoice extends BaseEntity {
|
|||||||
|
|
||||||
@Property({
|
@Property({
|
||||||
type: 'int',
|
type: 'int',
|
||||||
unique: true
|
unique: true,
|
||||||
|
autoincrement: true
|
||||||
})
|
})
|
||||||
invoiceNumber!: number | Opt;
|
invoiceNumber: number & Opt;
|
||||||
|
|
||||||
|
|
||||||
@Property({ type: 'int' })
|
@Property({ type: 'int' })
|
||||||
|
|||||||
@@ -64,7 +64,6 @@ export class InvoiceService {
|
|||||||
balance: 0,
|
balance: 0,
|
||||||
attachments: dto.attachments ?? [],
|
attachments: dto.attachments ?? [],
|
||||||
paymentMethod: dto.paymentMethod,
|
paymentMethod: dto.paymentMethod,
|
||||||
invoiceNumber: 20, // TODO : invoice number
|
|
||||||
description: dto.description,
|
description: dto.description,
|
||||||
subTotal: 0,
|
subTotal: 0,
|
||||||
taxAmount: 0,
|
taxAmount: 0,
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ export class RequestRepository extends EntityRepository<Request> {
|
|||||||
limit,
|
limit,
|
||||||
offset,
|
offset,
|
||||||
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
orderBy: { [orderBy]: order.toLowerCase() as 'asc' | 'desc' },
|
||||||
populate: ['items', 'items.product', 'user'],
|
populate: ['items', 'items.product', 'user','invoices'],
|
||||||
});
|
});
|
||||||
|
|
||||||
const totalPages = Math.ceil(total / limit);
|
const totalPages = Math.ceil(total / limit);
|
||||||
|
|||||||
Reference in New Issue
Block a user