From b34d54e7d049d0812d71c6e0da7179d889c7cfdc Mon Sep 17 00:00:00 2001 From: shancheas Date: Fri, 20 Sep 2024 11:06:09 +0700 Subject: [PATCH 1/3] fix: item bundling replace when edit --- .../helpers/mapping-transaction.helper.ts | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper.ts b/src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper.ts index 5e8a868..9842adf 100644 --- a/src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper.ts +++ b/src/modules/transaction/transaction/domain/usecases/managers/helpers/mapping-transaction.helper.ts @@ -178,6 +178,16 @@ export function mappingRevertTransaction(data, type) { const total_share_tenant = share_margin > 0 ? (Number(share_margin) / 100) * total_price : 0; + item.item.bundling_items = item.item.bundling_items?.map((bundling) => { + if (bundling.item_id) return bundling; + return { + ...bundling, + item_id: bundling.id, + item_name: bundling.name, + id: uuidv4(), + }; + }); + Object.assign(item, { item_id: item.item.id, item_name: item.item.name, @@ -192,15 +202,6 @@ export function mappingRevertTransaction(data, type) { (bundling) => bundling.name, ), breakdown_bundling: item.item.breakdown_bundling, - bundling_items: item.item.bundling_items?.map((bundling) => { - if (bundling.item_id) return bundling; - return { - ...bundling, - item_id: bundling.id, - item_name: bundling.name, - id: uuidv4(), - }; - }), item_tenant_id: item.item.tenant?.id ?? null, item_tenant_name: item.item.tenant?.id ?? null, From b16edb73e31cce3001bfaeac522b373e9e8c6f12 Mon Sep 17 00:00:00 2001 From: shancheas Date: Fri, 20 Sep 2024 13:01:38 +0700 Subject: [PATCH 2/3] fix: report wrong non bundling profit share key --- .../configs/income-per-item-master.ts | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/modules/reports/shared/configs/transaction-report/configs/income-per-item-master.ts b/src/modules/reports/shared/configs/transaction-report/configs/income-per-item-master.ts index 64861c8..4e54307 100644 --- a/src/modules/reports/shared/configs/transaction-report/configs/income-per-item-master.ts +++ b/src/modules/reports/shared/configs/transaction-report/configs/income-per-item-master.ts @@ -155,28 +155,32 @@ export default { }, { column: 'tr_item_bundling__payment_total_dpp', - query: 'tr_item_bundling.payment_total_dpp', + query: + 'CASE WHEN tr_item_bundling.payment_total_dpp is not null THEN tr_item_bundling.payment_total_dpp ELSE tr_item.payment_total_dpp END', label: 'DPP', type: DATA_TYPE.MEASURE, format: DATA_FORMAT.CURRENCY, }, { column: 'tr_item_bundling__payment_total_tax', - query: 'tr_item_bundling.payment_total_tax', + query: + 'CASE WHEN tr_item_bundling.payment_total_tax is not null THEN tr_item_bundling.payment_total_tax ELSE tr_item.payment_total_tax END', label: 'Total Pajak', type: DATA_TYPE.MEASURE, format: DATA_FORMAT.CURRENCY, }, { column: 'tr_item_bundling__total_profit_share', - query: 'tr_item_bundling.total_profit_share', + query: + 'CASE WHEN tr_item_bundling.total_profit_share is not null THEN tr_item_bundling.total_profit_share ELSE tr_item.total_profit_share END', label: 'Profit Share', type: DATA_TYPE.MEASURE, format: DATA_FORMAT.CURRENCY, }, { column: 'tr_item_bundling__total_share_tenant', - query: 'tr_item_bundling.total_share_tenant', + query: + 'CASE WHEN tr_item_bundling.total_share_tenant is not null THEN tr_item_bundling.total_share_tenant ELSE tr_item.total_share_tenant END', label: 'Tenant Share', type: DATA_TYPE.MEASURE, format: DATA_FORMAT.CURRENCY, From b4266d5d6835496485294a02f96a5bf52880dbbb Mon Sep 17 00:00:00 2001 From: shancheas Date: Fri, 20 Sep 2024 18:32:52 +0700 Subject: [PATCH 3/3] fix: add discount to calculator --- ...89989-add-discount-for-item-transaction.ts | 43 +++++++++++++++++++ .../1726830293878-change-column-name.ts | 23 ++++++++++ .../data/models/transaction-item.model.ts | 16 +++++++ .../entities/transaction-item.entity.ts | 4 ++ .../usecases/calculator/price.calculator.ts | 3 +- .../helpers/mapping-transaction.helper.ts | 23 +++++++++- 6 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 src/database/migrations/1726824289989-add-discount-for-item-transaction.ts create mode 100644 src/database/migrations/1726830293878-change-column-name.ts diff --git a/src/database/migrations/1726824289989-add-discount-for-item-transaction.ts b/src/database/migrations/1726824289989-add-discount-for-item-transaction.ts new file mode 100644 index 0000000..4e005a5 --- /dev/null +++ b/src/database/migrations/1726824289989-add-discount-for-item-transaction.ts @@ -0,0 +1,43 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class AddDiscountForItemTransaction1726824289989 + implements MigrationInterface +{ + name = 'AddDiscountForItemTransaction1726824289989'; + + public async up(queryRunner: QueryRunner): Promise { + 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 { + 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"`, + ); + } +} diff --git a/src/database/migrations/1726830293878-change-column-name.ts b/src/database/migrations/1726830293878-change-column-name.ts new file mode 100644 index 0000000..0489ee9 --- /dev/null +++ b/src/database/migrations/1726830293878-change-column-name.ts @@ -0,0 +1,23 @@ +import { MigrationInterface, QueryRunner } from 'typeorm'; + +export class ChangeColumnName1726830293878 implements MigrationInterface { + name = 'ChangeColumnName1726830293878'; + + public async up(queryRunner: QueryRunner): Promise { + 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 { + 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"`, + ); + } +} diff --git a/src/modules/transaction/transaction/data/models/transaction-item.model.ts b/src/modules/transaction/transaction/data/models/transaction-item.model.ts index e472982..034bb47 100644 --- a/src/modules/transaction/transaction/data/models/transaction-item.model.ts +++ b/src/modules/transaction/transaction/data/models/transaction-item.model.ts @@ -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 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,