fix(SPG-595) Pemesanan - Penyesuaian flow booking

pull/27/head
Aswin Ashar Abdullah 2024-07-11 18:14:05 +07:00
parent de2041ae95
commit 9242f43760
5 changed files with 78 additions and 33 deletions

View File

@ -144,6 +144,9 @@ export class TransactionModel
payment_change: number; payment_change: number;
// share and profit data // share and profit data
@Column('decimal', { name: 'payment_total_dpp', nullable: true })
payment_total_dpp: number;
@Column('decimal', { name: 'payment_total_share', nullable: true }) @Column('decimal', { name: 'payment_total_share', nullable: true })
payment_total_share: number; payment_total_share: number;

View File

@ -60,6 +60,7 @@ export interface TransactionEntity extends BaseStatusEntity {
payment_total_share: number; // total share untuk para tenant payment_total_share: number; // total share untuk para tenant
payment_total_tax: number; // total untuk tax payment_total_tax: number; // total untuk tax
payment_total_profit: number; // total untuk profit perusahan payment_total_profit: number; // total untuk profit perusahan
payment_total_dpp: number;
profit_share_formula: string; profit_share_formula: string;
sales_price_formula: string; sales_price_formula: string;

View File

@ -8,6 +8,10 @@ import { Equation, parse } from 'algebra.js';
import { STATUS } from 'src/core/strings/constants/base.constants'; import { STATUS } from 'src/core/strings/constants/base.constants';
import { TransactionDataService } from '../../../data/services/transaction-data.service'; import { TransactionDataService } from '../../../data/services/transaction-data.service';
import { TransactionModel } from '../../../data/models/transaction.model'; import { TransactionModel } from '../../../data/models/transaction.model';
import {
generateCodeDate,
generateRandom,
} from 'src/modules/transaction/vip-code/domain/usecases/managers/helpers/generate-random.helper';
@EventsHandler(TransactionChangeStatusEvent) @EventsHandler(TransactionChangeStatusEvent)
export class SettledTransactionHandler export class SettledTransactionHandler
@ -21,14 +25,21 @@ export class SettledTransactionHandler
async handle(event: TransactionChangeStatusEvent) { async handle(event: TransactionChangeStatusEvent) {
const old_data = event.data.old; const old_data = event.data.old;
const data = event.data.data; const current_data = event.data.data;
if ( if (
old_data.status == data.status || old_data.status == current_data.status ||
![STATUS.ACTIVE, STATUS.SETTLED].includes(data.status) ![STATUS.ACTIVE, STATUS.SETTLED].includes(current_data.status)
) )
return; return;
const data = await this.dataService.getOneByOptions({
where: {
id: current_data.id,
},
relations: ['items'],
});
const profit_formula = await this.formulaService.getOneByOptions({ const profit_formula = await this.formulaService.getOneByOptions({
where: { where: {
type: FormulaType.PROFIT_SHARE, type: FormulaType.PROFIT_SHARE,
@ -52,17 +63,30 @@ export class SettledTransactionHandler
.manager.connection.createQueryRunner(); .manager.connection.createQueryRunner();
// const profit_share_value = this.calculateFormula(profit_formula.formula_string, taxes, data.payment_total_net_profit ?? 0); // const profit_share_value = this.calculateFormula(profit_formula.formula_string, taxes, data.payment_total_net_profit ?? 0);
// const sale_price_value = this.calculateFormula(sales_price.formula_string, taxes, data.payment_total_net_profit ?? 0); const { dpp_value, tax_datas } = this.calculateSalesFormula(
sales_price.formula_string,
taxes,
data.payment_total_net_profit ?? 0,
);
const month_year = generateCodeDate();
const char = generateRandom(1);
const number = generateRandom(1, true);
Object.assign(data, { Object.assign(data, {
payment_total_dpp: dpp_value,
invoice_code: `${month_year}${char}${number}`,
profit_share_formula: profit_formula.formula_string, profit_share_formula: profit_formula.formula_string,
sales_price_formula: sales_price.formula_string, sales_price_formula: sales_price.formula_string,
taxes: tax_datas,
}); });
await this.dataService.create(queryRunner, TransactionModel, data); await this.dataService.create(queryRunner, TransactionModel, data);
} }
calculateFormula(formula, taxes, total) { calculateSalesFormula(formula, taxes, total) {
let value = 0;
let tax_datas = [];
const regex = /([a-zA-Z0-9_]+)/g; const regex = /([a-zA-Z0-9_]+)/g;
const variable = {}; const variable = {};
@ -73,6 +97,13 @@ export class SettledTransactionHandler
for (const key of keys) { for (const key of keys) {
const keyData = taxes.find((tax) => tax.name == key); const keyData = taxes.find((tax) => tax.name == key);
variable[key] = keyData.value / 100; variable[key] = keyData.value / 100;
tax_datas.push({
tax_id: keyData.id,
tax_name: keyData.name,
tax_value: keyData.value,
tax_total_value: (keyData.value / 100) * Number(total),
});
} }
try { try {
@ -86,11 +117,15 @@ export class SettledTransactionHandler
const result = equation.solveFor('dpp').toString(); const result = equation.solveFor('dpp').toString();
console.log(result); console.log(result);
const value = math.evaluate(result); value = math.evaluate(result);
console.log(value); console.log(value);
return value;
} catch (e) { } catch (e) {
console.log(e); console.log(e);
} }
return {
dpp_value: value,
tax_datas: tax_datas,
};
} }
} }

View File

@ -2,6 +2,10 @@ import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { VipCodeEntity } from '../../entities/vip-code.entity'; import { VipCodeEntity } from '../../entities/vip-code.entity';
import { EventTopics } from 'src/core/strings/constants/interface.constants'; import { EventTopics } from 'src/core/strings/constants/interface.constants';
import {
generateCodeDate,
generateRandom,
} from './helpers/generate-random.helper';
@Injectable() @Injectable()
export class GenerateVipCodeManager extends BaseCustomManager<VipCodeEntity> { export class GenerateVipCodeManager extends BaseCustomManager<VipCodeEntity> {
@ -14,19 +18,11 @@ export class GenerateVipCodeManager extends BaseCustomManager<VipCodeEntity> {
} }
process(): Promise<void> { process(): Promise<void> {
const date = new Date(); const month_year = generateCodeDate();
const char = generateRandom(1);
const number = generateRandom(2, true);
// get month dan year (dua digit) this.result = `${month_year}${char}${number}`;
const month =
date.getMonth() < 10
? `0${date.getMonth() + 1}`
: (date.getMonth() + 1).toString();
const year = date.getFullYear().toString().slice(-2);
const char = this.generateRandom(1);
const number = this.generateRandom(2, true);
this.result = `${month}${year}${char}${number}`;
return; return;
} }
@ -45,18 +41,4 @@ export class GenerateVipCodeManager extends BaseCustomManager<VipCodeEntity> {
get eventTopics(): EventTopics[] { get eventTopics(): EventTopics[] {
return []; return [];
} }
generateRandom(length: number, is_number?: boolean): string {
let result = '';
let base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (is_number) base = '123456789';
let counter = 0;
while (counter < length) {
result += base.charAt(Math.floor(Math.random() * base.length));
counter += 1;
}
return result;
}
} }

View File

@ -0,0 +1,24 @@
export function generateRandom(length: number, is_number?: boolean): string {
let result = '';
let base = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if (is_number) base = '123456789';
let counter = 0;
while (counter < length) {
result += base.charAt(Math.floor(Math.random() * base.length));
counter += 1;
}
return result;
}
export function generateCodeDate() {
const date = new Date();
const month =
date.getMonth() < 10
? `0${date.getMonth() + 1}`
: (date.getMonth() + 1).toString();
const year = date.getFullYear().toString().slice(-2);
return `${month}${year}`;
}