fix: add discount to calculator
parent
b16edb73e3
commit
b4266d5d68
|
@ -0,0 +1,43 @@
|
|||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class AddDiscountForItemTransaction1726824289989
|
||||
implements MigrationInterface
|
||||
{
|
||||
name = 'AddDiscountForItemTransaction1726824289989';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_items" ADD "subtotal" numeric`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_items" ADD "discount_value" numeric`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" ADD "subtotal" numeric`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" ADD "discount_value" numeric`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" ADD "total_price" numeric`,
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "total_price"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "discount_value"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" DROP COLUMN "subtotal"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_items" DROP COLUMN "discount_value"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_items" DROP COLUMN "subtotal"`,
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,23 @@
|
|||
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||
|
||||
export class ChangeColumnName1726830293878 implements MigrationInterface {
|
||||
name = 'ChangeColumnName1726830293878';
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_items" RENAME COLUMN "subtotal" TO "total_net_price"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" RENAME COLUMN "subtotal" TO "total_net_price"`,
|
||||
);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_item_breakdowns" RENAME COLUMN "total_net_price" TO "subtotal"`,
|
||||
);
|
||||
await queryRunner.query(
|
||||
`ALTER TABLE "transaction_items" RENAME COLUMN "total_net_price" TO "subtotal"`,
|
||||
);
|
||||
}
|
||||
}
|
|
@ -52,6 +52,12 @@ export class TransactionItemModel
|
|||
@Column('decimal', { name: 'item_tenant_share_margin', nullable: true })
|
||||
item_tenant_share_margin: number;
|
||||
|
||||
@Column('decimal', { nullable: true })
|
||||
total_net_price: number;
|
||||
|
||||
@Column('decimal', { nullable: true })
|
||||
discount_value: number;
|
||||
|
||||
// calculation data
|
||||
@Column('decimal', { name: 'total_price', nullable: true })
|
||||
total_price: number;
|
||||
|
@ -139,6 +145,16 @@ export class TransactionItemBreakdownModel extends BaseCoreModel<TransactionBund
|
|||
@Column('bigint', { nullable: true })
|
||||
item_rates: number;
|
||||
|
||||
@Column('decimal', { nullable: true })
|
||||
total_net_price: number;
|
||||
|
||||
@Column('decimal', { nullable: true })
|
||||
discount_value: number;
|
||||
|
||||
// calculation data
|
||||
@Column('decimal', { name: 'total_price', nullable: true })
|
||||
total_price: number;
|
||||
|
||||
@Column('decimal', { nullable: true })
|
||||
total_profit_share: number;
|
||||
|
||||
|
|
|
@ -19,6 +19,8 @@ export interface TransactionItemEntity extends BaseCoreEntity {
|
|||
|
||||
// calculation data
|
||||
total_price: number;
|
||||
total_net_price: number;
|
||||
discount_value: number;
|
||||
total_hpp: number;
|
||||
total_profit: number;
|
||||
total_share_tenant: number;
|
||||
|
@ -32,6 +34,8 @@ export interface TransactionBundlingItemEntity extends BaseCoreEntity {
|
|||
item_id: string;
|
||||
item_name: string;
|
||||
hpp: number;
|
||||
total_net_price: number;
|
||||
discount_value: number;
|
||||
base_price: number;
|
||||
item_rates: number;
|
||||
total_price?: number;
|
||||
|
|
|
@ -39,7 +39,6 @@ export class PriceCalculator {
|
|||
if (item.bundling_items) {
|
||||
for (let b = 0; b < item.bundling_items.length; b++) {
|
||||
const bundling = item.bundling_items[b];
|
||||
bundling.total_price = bundling.item_rates ?? bundling.base_price;
|
||||
|
||||
const bundlingPrice = await this.calculateItem(bundling);
|
||||
const bundlingValues = this.calculatePrice(bundlingPrice);
|
||||
|
@ -108,7 +107,7 @@ export class PriceCalculator {
|
|||
const dppValue = calculateFormula(
|
||||
dpp.formula_string,
|
||||
taxes,
|
||||
transaction.total_price,
|
||||
transaction.total_net_price,
|
||||
);
|
||||
|
||||
values['dpp'] = dppValue;
|
||||
|
|
|
@ -161,8 +161,17 @@ export function mappingRevertTransaction(data, type) {
|
|||
});
|
||||
}
|
||||
|
||||
const { payment_sub_total, discount_value, discount_percentage } = data;
|
||||
const discountPercent =
|
||||
discount_value && +discount_value > 0
|
||||
? +discount_value / +payment_sub_total
|
||||
: discount_percentage / 100;
|
||||
const discountValue = payment_sub_total * discountPercent;
|
||||
|
||||
Object.assign(data, {
|
||||
payment_total_net_profit: data.payment_total,
|
||||
discount_value: discountValue,
|
||||
discount_percentage: discountPercent * 100,
|
||||
customer_category_id: data.customer_category?.id ?? null,
|
||||
customer_category_name: data.customer_category?.name ?? null,
|
||||
season_period_id: data.season_period?.id ?? null,
|
||||
|
@ -174,16 +183,26 @@ export function mappingRevertTransaction(data, type) {
|
|||
data.items?.map((item) => {
|
||||
const total_price =
|
||||
Number(item.item.price ?? item.item.base_price) * Number(item.qty);
|
||||
const discount = discountPercent * total_price;
|
||||
const net_price = total_price - discount;
|
||||
const share_margin = item.item.tenant?.share_margin ?? 0;
|
||||
const total_share_tenant =
|
||||
share_margin > 0 ? (Number(share_margin) / 100) * total_price : 0;
|
||||
|
||||
item.item.bundling_items = item.item.bundling_items?.map((bundling) => {
|
||||
item.bundling_items = item.item.bundling_items?.map((bundling) => {
|
||||
if (bundling.item_id) return bundling;
|
||||
|
||||
const basePrice = bundling.item_rates ?? bundling.base_price;
|
||||
const discount = discountPercent * basePrice;
|
||||
const total = basePrice - discount;
|
||||
|
||||
return {
|
||||
...bundling,
|
||||
item_id: bundling.id,
|
||||
item_name: bundling.name,
|
||||
total_net_price: basePrice,
|
||||
discount_value: discount,
|
||||
total_price: total,
|
||||
id: uuidv4(),
|
||||
};
|
||||
});
|
||||
|
@ -207,6 +226,8 @@ export function mappingRevertTransaction(data, type) {
|
|||
item_tenant_name: item.item.tenant?.id ?? null,
|
||||
item_tenant_share_margin: item.item.tenant?.share_margin ?? null,
|
||||
|
||||
total_net_price: net_price,
|
||||
discount_value: discount,
|
||||
total_price: total_price,
|
||||
total_hpp: Number(item.item.hpp) * Number(item.qty),
|
||||
total_share_tenant: total_share_tenant,
|
||||
|
|
Loading…
Reference in New Issue