feat: change logic to save formula
parent
c06a2a0a2b
commit
23043fb7f9
|
@ -3,7 +3,6 @@ import {
|
|||
Param,
|
||||
RelationParam,
|
||||
} from 'src/core/modules/domain/entities/base-filter.entity';
|
||||
import { FormulaType } from 'src/modules/transaction/sales-price-formula/constants';
|
||||
import { SalesPriceFormulaEntity } from 'src/modules/transaction/sales-price-formula/domain/entities/sales-price-formula.entity';
|
||||
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
|
@ -17,12 +16,7 @@ export class IndexProfitShareFormulaManager extends BaseIndexManager<SalesPriceF
|
|||
}
|
||||
|
||||
get specificFilter(): Param[] {
|
||||
return [
|
||||
{
|
||||
cols: `${this.tableName}.type::text`,
|
||||
data: [FormulaType.PROFIT_SHARE],
|
||||
},
|
||||
];
|
||||
return [];
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
|
@ -51,6 +45,12 @@ export class IndexProfitShareFormulaManager extends BaseIndexManager<SalesPriceF
|
|||
}
|
||||
|
||||
get selects(): string[] {
|
||||
return [];
|
||||
// return [];
|
||||
return [
|
||||
`${this.tableName}.id`,
|
||||
`${this.tableName}.formula_render`,
|
||||
`${this.tableName}.formula_string`,
|
||||
`${this.tableName}.value_for`,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import {
|
||||
Param,
|
||||
RelationParam,
|
||||
} from 'src/core/modules/domain/entities/base-filter.entity';
|
||||
import { BaseIndexManager } from 'src/core/modules/domain/usecase/managers/base-index.manager';
|
||||
import { SelectQueryBuilder } from 'typeorm';
|
||||
import { TaxEntity } from 'src/modules/transaction/tax/domain/entities/tax.entity';
|
||||
|
||||
@Injectable()
|
||||
export class IndexTaxFormulaManager extends BaseIndexManager<TaxEntity> {
|
||||
setQueryFilter(
|
||||
queryBuilder: SelectQueryBuilder<TaxEntity>,
|
||||
): SelectQueryBuilder<TaxEntity> {
|
||||
return queryBuilder;
|
||||
}
|
||||
|
||||
get specificFilter(): Param[] {
|
||||
return [
|
||||
{
|
||||
cols: `${this.tableName}.status::text`,
|
||||
isStatus: true,
|
||||
data: [`'active'`],
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
async prepareData(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async beforeProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
async afterProcess(): Promise<void> {
|
||||
return;
|
||||
}
|
||||
|
||||
get relations(): RelationParam {
|
||||
return {
|
||||
// relation only join (for query purpose)
|
||||
joinRelations: [],
|
||||
|
||||
// relation join and select (relasi yang ingin ditampilkan),
|
||||
selectRelations: [],
|
||||
|
||||
// relation yang hanya ingin dihitung (akan return number)
|
||||
countRelations: [],
|
||||
};
|
||||
}
|
||||
|
||||
get selects(): string[] {
|
||||
// return [];
|
||||
return [
|
||||
`${this.tableName}.id`,
|
||||
`${this.tableName}.formula_render`,
|
||||
`${this.tableName}.formula_string`,
|
||||
`${this.tableName}.name`,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -8,21 +8,21 @@ import {
|
|||
import { SalesPriceFormulaModel } from 'src/modules/transaction/sales-price-formula/data/models/sales-price-formula.model';
|
||||
import { ProfitShareFormulaUpdatedEvent } from '../../entities/event/profit-share-formula-updated.event';
|
||||
import { SalesPriceFormulaEntity } from 'src/modules/transaction/sales-price-formula/domain/entities/sales-price-formula.entity';
|
||||
import { In } from 'typeorm';
|
||||
import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
import { calculateProfitFormula } from 'src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper';
|
||||
import { FormulaType } from 'src/modules/transaction/sales-price-formula/constants';
|
||||
// import { In } from 'typeorm';
|
||||
// import { STATUS } from 'src/core/strings/constants/base.constants';
|
||||
// import { calculateProfitFormula } from 'src/modules/transaction/sales-price-formula/domain/usecases/managers/helpers/calculation-formula.helper';
|
||||
|
||||
@Injectable()
|
||||
export class UpdateProfitShareFormulaManager extends BaseUpdateManager<SalesPriceFormulaEntity> {
|
||||
async validateProcess(): Promise<void> {
|
||||
const taxes = await this.dataServiceFirstOpt.getManyByOptions({
|
||||
where: {
|
||||
status: In([STATUS.ACTIVE]),
|
||||
},
|
||||
});
|
||||
// const taxes = await this.dataServiceFirstOpt.getManyByOptions({
|
||||
// where: {
|
||||
// status: In([STATUS.ACTIVE]),
|
||||
// },
|
||||
// });
|
||||
|
||||
calculateProfitFormula(this.data.formula_string, taxes, 10000, 50, true);
|
||||
// TODO: Save Validation
|
||||
// calculateProfitFormula(this.data.formula_string, taxes, 10000, 50, true);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -32,34 +32,9 @@ export class UpdateProfitShareFormulaManager extends BaseUpdateManager<SalesPric
|
|||
|
||||
async afterProcess(): Promise<void> {
|
||||
const additionalFormula = this.data.additional;
|
||||
for (const additional of additionalFormula) {
|
||||
/**
|
||||
* Find formula for variable
|
||||
* If the formula doesn't exist, then create data for save
|
||||
*/
|
||||
const formula = (await this.dataService.getOneByOptions({
|
||||
where: {
|
||||
value_for: additional.value_for,
|
||||
},
|
||||
})) ?? {
|
||||
editor_id: this.user.id,
|
||||
editor_name: this.user.name,
|
||||
updated_at: new Date().getTime(),
|
||||
created_at: new Date().getTime(),
|
||||
type: FormulaType.PROFIT_SHARE,
|
||||
};
|
||||
|
||||
// Update formula value to exist formula or new formula
|
||||
formula.formula_render = additional.formula_render;
|
||||
formula.formula_string = additional.formula_string;
|
||||
formula.value_for = additional.value_for;
|
||||
|
||||
/**
|
||||
* This function is create, but inside function is save
|
||||
* So, if the id is provide, the data will be update instead create new data
|
||||
*/
|
||||
this.dataService.create(null, null, formula);
|
||||
}
|
||||
const taxFormula = this.data.taxes;
|
||||
this.dataService.create(null, null, additionalFormula);
|
||||
this.dataServiceFirstOpt.create(null, null, taxFormula);
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ export class ProfitShareFormulaDataOrchestrator {
|
|||
async update(data): Promise<SalesPriceFormulaEntity> {
|
||||
const formula = await this.serviceData.getOneByOptions({
|
||||
where: {
|
||||
type: FormulaType.PROFIT_SHARE,
|
||||
type: FormulaType.SALES_PRICE,
|
||||
},
|
||||
});
|
||||
|
||||
|
|
|
@ -4,13 +4,17 @@ import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
|
|||
import { SalesPriceFormulaReadService } from 'src/modules/transaction/sales-price-formula/data/services/sales-price-formula-read.service';
|
||||
import { SalesPriceFormulaEntity } from 'src/modules/transaction/sales-price-formula/domain/entities/sales-price-formula.entity';
|
||||
import { IndexProfitShareFormulaManager } from './managers/index-profit-share-formula.manager';
|
||||
import { IndexTaxFormulaManager } from './managers/tax-formula.manager';
|
||||
import { TaxReadService } from 'src/modules/transaction/tax/data/services/tax-read.service';
|
||||
|
||||
@Injectable()
|
||||
export class ProfitShareFormulaReadOrchestrator {
|
||||
constructor(
|
||||
private detailManager: DetailProfitShareFormulaManager,
|
||||
private indexManager: IndexProfitShareFormulaManager,
|
||||
private taxManager: IndexTaxFormulaManager,
|
||||
private serviceData: SalesPriceFormulaReadService,
|
||||
private taxServiceData: TaxReadService,
|
||||
) {}
|
||||
|
||||
async index(): Promise<SalesPriceFormulaEntity[]> {
|
||||
|
@ -21,6 +25,19 @@ export class ProfitShareFormulaReadOrchestrator {
|
|||
return data;
|
||||
}
|
||||
|
||||
async tax(): Promise<Partial<SalesPriceFormulaEntity>[]> {
|
||||
this.taxManager.setFilterParam({});
|
||||
this.taxManager.setService(this.taxServiceData, TABLE_NAME.TAX);
|
||||
await this.taxManager.execute();
|
||||
const { data } = this.taxManager.getResult();
|
||||
return data.map((tax) => {
|
||||
return {
|
||||
...tax,
|
||||
value_for: tax.name,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
async detail(): Promise<SalesPriceFormulaEntity> {
|
||||
this.detailManager.setData('');
|
||||
this.detailManager.setService(this.serviceData, TABLE_NAME.PRICE_FORMULA);
|
||||
|
|
|
@ -20,4 +20,9 @@ export class ProfitShareFormulaReadController {
|
|||
async breakdown(): Promise<SalesPriceFormulaEntity[]> {
|
||||
return await this.orchestrator.index();
|
||||
}
|
||||
|
||||
@Get('tax')
|
||||
async tax(): Promise<Partial<SalesPriceFormulaEntity>[]> {
|
||||
return await this.orchestrator.tax();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,8 @@ import { SalesPriceFormulaModel } from '../sales-price-formula/data/models/sales
|
|||
import { TaxDataService } from '../tax/data/services/tax-data.service';
|
||||
import { TaxModel } from '../tax/data/models/tax.model';
|
||||
import { IndexProfitShareFormulaManager } from './domain/usecases/managers/index-profit-share-formula.manager';
|
||||
import { IndexTaxFormulaManager } from './domain/usecases/managers/tax-formula.manager';
|
||||
import { TaxReadService } from '../tax/data/services/tax-read.service';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -31,11 +33,13 @@ import { IndexProfitShareFormulaManager } from './domain/usecases/managers/index
|
|||
DetailProfitShareFormulaManager,
|
||||
UpdateProfitShareFormulaManager,
|
||||
IndexProfitShareFormulaManager,
|
||||
IndexTaxFormulaManager,
|
||||
|
||||
ProfitShareFormulaDataOrchestrator,
|
||||
ProfitShareFormulaReadOrchestrator,
|
||||
|
||||
TaxDataService,
|
||||
TaxReadService,
|
||||
],
|
||||
})
|
||||
export class ProfitShareFormulaModule {}
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
import { Injectable } from '@nestjs/common';
|
||||
import { IndexTaxManager } from './index-tax.manager';
|
||||
import { Param } from 'src/core/modules/domain/entities/base-filter.entity';
|
||||
|
||||
@Injectable()
|
||||
export class IndexTaxFormulaManager extends IndexTaxManager {
|
||||
get selects(): string[] {
|
||||
return [
|
||||
`${this.tableName}.id`,
|
||||
`${this.tableName}.name`,
|
||||
`${this.tableName}.value`,
|
||||
];
|
||||
}
|
||||
|
||||
get specificFilter(): Param[] {
|
||||
return [
|
||||
{
|
||||
cols: `${this.tableName}.status::text`,
|
||||
data: [`'active'`],
|
||||
isStatus: true,
|
||||
},
|
||||
...super.specificFilter,
|
||||
];
|
||||
}
|
||||
}
|
|
@ -6,11 +6,13 @@ import { PaginationResponse } from 'src/core/response/domain/ok-response.interfa
|
|||
import { BaseReadOrchestrator } from 'src/core/modules/domain/usecase/orchestrators/base-read.orchestrator';
|
||||
import { DetailTaxManager } from './managers/detail-tax.manager';
|
||||
import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
|
||||
import { IndexTaxFormulaManager } from './managers/index-tax-formula.manager';
|
||||
|
||||
@Injectable()
|
||||
export class TaxReadOrchestrator extends BaseReadOrchestrator<TaxEntity> {
|
||||
constructor(
|
||||
private indexManager: IndexTaxManager,
|
||||
private formulaManager: IndexTaxFormulaManager,
|
||||
private detailManager: DetailTaxManager,
|
||||
private serviceData: TaxReadService,
|
||||
) {
|
||||
|
@ -24,6 +26,13 @@ export class TaxReadOrchestrator extends BaseReadOrchestrator<TaxEntity> {
|
|||
return this.indexManager.getResult();
|
||||
}
|
||||
|
||||
async formula(params): Promise<PaginationResponse<TaxEntity>> {
|
||||
this.formulaManager.setFilterParam(params);
|
||||
this.formulaManager.setService(this.serviceData, TABLE_NAME.TAX);
|
||||
await this.formulaManager.execute();
|
||||
return this.formulaManager.getResult();
|
||||
}
|
||||
|
||||
async detail(dataId: string): Promise<TaxEntity> {
|
||||
this.detailManager.setData(dataId);
|
||||
this.detailManager.setService(this.serviceData, TABLE_NAME.TAX);
|
||||
|
|
|
@ -23,6 +23,14 @@ export class TaxReadController {
|
|||
return await this.orchestrator.index(params);
|
||||
}
|
||||
|
||||
@Get('formula')
|
||||
@Pagination()
|
||||
async formula(
|
||||
@Query() params: FilterTaxDto,
|
||||
): Promise<PaginationResponse<TaxEntity>> {
|
||||
return await this.orchestrator.formula(params);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
async detail(@Param('id') id: string): Promise<TaxEntity> {
|
||||
return await this.orchestrator.detail(id);
|
||||
|
|
|
@ -24,6 +24,7 @@ import { BatchInactiveTaxManager } from './domain/usecases/managers/batch-inacti
|
|||
import { TaxModel } from './data/models/tax.model';
|
||||
import { SalesPriceFormulaReadService } from '../sales-price-formula/data/services/sales-price-formula-read.service';
|
||||
import { SalesPriceFormulaModel } from '../sales-price-formula/data/models/sales-price-formula.model';
|
||||
import { IndexTaxFormulaManager } from './domain/usecases/managers/index-tax-formula.manager';
|
||||
|
||||
@Module({
|
||||
imports: [
|
||||
|
@ -37,6 +38,7 @@ import { SalesPriceFormulaModel } from '../sales-price-formula/data/models/sales
|
|||
controllers: [TaxDataController, TaxReadController],
|
||||
providers: [
|
||||
IndexTaxManager,
|
||||
IndexTaxFormulaManager,
|
||||
DetailTaxManager,
|
||||
CreateTaxManager,
|
||||
DeleteTaxManager,
|
||||
|
|
Loading…
Reference in New Issue