pos-be/src/modules/transaction/reconciliation/domain/usecases/managers/recap-reconciliation.manage...

141 lines
4.6 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { BaseCustomManager } from 'src/core/modules/domain/usecase/managers/base-custom.manager';
import { EventTopics } from 'src/core/strings/constants/interface.constants';
import { TransactionType } from 'src/modules/transaction/transaction/constants';
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
import { TransactionEntity } from 'src/modules/transaction/transaction/domain/entities/transaction.entity';
import { Between, ILike } from 'typeorm';
import * as _ from 'lodash';
import * as moment from 'moment';
import { EMPTY_UUID, STATUS } from 'src/core/strings/constants/base.constants';
@Injectable()
export class RecapReconciliationManager extends BaseCustomManager<TransactionEntity> {
private recapTransactions = {};
private startOfDay = moment().startOf('day').valueOf();
private endOfDay = moment().endOf('day').valueOf();
get entityTarget(): any {
return TransactionModel;
}
getResult() {
return 'Berhasil recap data transaksi';
}
get eventTopics(): EventTopics[] {
return [];
}
async validateProcess(): Promise<void> {
return;
}
async beforeProcess(): Promise<void> {
const transactions = await this.dataService.getManyByOptions({
where: {
is_recap_transaction: false,
type: TransactionType.COUNTER,
status: STATUS.SETTLED,
created_at: Between(this.startOfDay, this.endOfDay),
},
});
const payCounserTransactions = await this.dataService.getManyByOptions({
where: {
is_recap_transaction: false,
payment_code: ILike('%SLS%'),
status: STATUS.SETTLED,
created_at: Between(this.startOfDay, this.endOfDay),
},
});
transactions.push(...payCounserTransactions);
for (const transaction of transactions) {
const { creator_counter_no, payment_type, payment_type_method_id } =
transaction;
const group_by =
creator_counter_no + '-' + payment_type + '-' + payment_type_method_id;
if (!this.recapTransactions[group_by]) {
this.recapTransactions[group_by] = [];
}
this.recapTransactions[group_by].push(transaction);
}
return;
}
async process(): Promise<void> {
const total_recap = Object.keys(this.recapTransactions);
for (const recap of total_recap) {
const first_transaction = this.recapTransactions[recap][0];
const { creator_counter_no, payment_type, payment_type_method_id } =
first_transaction;
let query = {
is_recap_transaction: true,
created_at: Between(this.startOfDay, this.endOfDay),
creator_counter_no: creator_counter_no,
payment_type: payment_type,
};
if (payment_type != 'cash')
query['payment_type_method_id'] = payment_type_method_id ?? EMPTY_UUID;
const exist = await this.dataService.getOneByOptions({
where: query,
});
if (payment_type == 'cash') console.log(exist, 'das', query);
const new_recap = new TransactionModel();
const total = _.sumBy(this.recapTransactions[recap], (recap) =>
parseFloat(recap.payment_total),
);
if (exist) {
Object.assign(exist, {
payment_total: total,
payment_total_net_profit: total,
editor_id: this.user.id,
editor_name: this.user.name,
updated_at: new Date().getTime(),
});
} else {
Object.assign(new_recap, {
is_recap_transaction: true,
payment_total: total,
payment_total_net_profit: total,
reconciliation_status: STATUS.PENDING,
status: STATUS.SETTLED,
type: TransactionType.COUNTER,
booking_date: new Date(),
payment_date: new Date(),
creator_counter_no: first_transaction.creator_counter_no,
payment_type: first_transaction.payment_type,
payment_type_method_id: first_transaction.payment_type_method_id,
payment_type_method_number:
first_transaction.payment_type_method_number,
payment_type_method_name: first_transaction.payment_type_method_name,
payment_type_method_qr: first_transaction.payment_type_method_qr,
creator_id: this.user.id,
creator_name: this.user.name,
created_at: new Date().getTime(),
updated_at: new Date().getTime(),
});
}
await this.dataService.create(
this.queryRunner,
TransactionModel,
exist ?? new_recap,
);
}
return;
}
async afterProcess(): Promise<void> {
return;
}
}