fix(SPG-743) Refund dengan code invoice yang sama terkena validasi

fix/data
Aswin Ashar Abdullah 2024-08-02 14:38:36 +07:00
parent d14d9101ae
commit 1636f6b930
14 changed files with 122 additions and 34 deletions

View File

@ -0,0 +1,49 @@
import { MigrationInterface, QueryRunner } from 'typeorm';
export class UpdateRelationTableTransaction1722581313837
implements MigrationInterface
{
name = 'UpdateRelationTableTransaction1722581313837';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "refunds" DROP CONSTRAINT "FK_8bb3b7579f49990d2e77684acd4"`,
);
await queryRunner.query(
`ALTER TABLE "refunds" DROP CONSTRAINT "REL_8bb3b7579f49990d2e77684acd"`,
);
await queryRunner.query(
`ALTER TABLE "refund_items" DROP CONSTRAINT "FK_07b481a163c219f5de8fb1c90b3"`,
);
await queryRunner.query(
`ALTER TABLE "refund_items" DROP CONSTRAINT "REL_07b481a163c219f5de8fb1c90b"`,
);
await queryRunner.query(
`ALTER TABLE "refunds" ADD CONSTRAINT "FK_8bb3b7579f49990d2e77684acd4" FOREIGN KEY ("transaction_id") REFERENCES "transactions"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
);
await queryRunner.query(
`ALTER TABLE "refund_items" ADD CONSTRAINT "FK_07b481a163c219f5de8fb1c90b3" FOREIGN KEY ("transaction_item_id") REFERENCES "transaction_items"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE "refund_items" DROP CONSTRAINT "FK_07b481a163c219f5de8fb1c90b3"`,
);
await queryRunner.query(
`ALTER TABLE "refunds" DROP CONSTRAINT "FK_8bb3b7579f49990d2e77684acd4"`,
);
await queryRunner.query(
`ALTER TABLE "refund_items" ADD CONSTRAINT "REL_07b481a163c219f5de8fb1c90b" UNIQUE ("transaction_item_id")`,
);
await queryRunner.query(
`ALTER TABLE "refund_items" ADD CONSTRAINT "FK_07b481a163c219f5de8fb1c90b3" FOREIGN KEY ("transaction_item_id") REFERENCES "transaction_items"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
);
await queryRunner.query(
`ALTER TABLE "refunds" ADD CONSTRAINT "REL_8bb3b7579f49990d2e77684acd" UNIQUE ("transaction_id")`,
);
await queryRunner.query(
`ALTER TABLE "refunds" ADD CONSTRAINT "FK_8bb3b7579f49990d2e77684acd4" FOREIGN KEY ("transaction_id") REFERENCES "transactions"("id") ON DELETE CASCADE ON UPDATE CASCADE`,
);
}
}

View File

@ -86,8 +86,6 @@ export class RecapReconciliationManager extends BaseCustomManager<TransactionEnt
where: query, where: query,
}); });
if (payment_type == 'cash') console.log(exist, 'das', query);
const new_recap = new TransactionModel(); const new_recap = new TransactionModel();
const total = _.sumBy(this.recapTransactions[recap], (recap) => const total = _.sumBy(this.recapTransactions[recap], (recap) =>
parseFloat(recap.payment_total), parseFloat(recap.payment_total),

View File

@ -29,7 +29,7 @@ export class RefundItemModel
// transaction to transaction item // transaction to transaction item
@Column('varchar', { name: 'transaction_item_id', nullable: true }) @Column('varchar', { name: 'transaction_item_id', nullable: true })
transaction_item_id: string; transaction_item_id: string;
@OneToOne(() => TransactionItemModel, (model) => model.refund, { @ManyToOne(() => TransactionItemModel, (model) => model.refunds, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
onUpdate: 'CASCADE', onUpdate: 'CASCADE',
}) })

View File

@ -1,6 +1,6 @@
import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { RefundEntity } from '../../domain/entities/refund.entity'; import { RefundEntity } from '../../domain/entities/refund.entity';
import { Column, Entity, JoinColumn, OneToMany, OneToOne } from 'typeorm'; import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { BaseStatusModel } from 'src/core/modules/data/model/base-status.model'; import { BaseStatusModel } from 'src/core/modules/data/model/base-status.model';
import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model'; import { TransactionModel } from 'src/modules/transaction/transaction/data/models/transaction.model';
import { RefundItemModel } from './refund-item.model'; import { RefundItemModel } from './refund-item.model';
@ -64,7 +64,7 @@ export class RefundModel
// relation to transaction // relation to transaction
@Column('varchar', { name: 'transaction_id', nullable: true }) @Column('varchar', { name: 'transaction_id', nullable: true })
transaction_id: string; transaction_id: string;
@OneToOne(() => TransactionModel, (model) => model.refund, { @ManyToOne(() => TransactionModel, (model) => model.refunds, {
onDelete: 'CASCADE', onDelete: 'CASCADE',
onUpdate: 'CASCADE', onUpdate: 'CASCADE',
}) })

View File

@ -38,7 +38,6 @@ export class BatchConfirmRefundManager extends BaseBatchUpdateStatusManager<Refu
if (this.data.status == STATUS.DRAFT) { if (this.data.status == STATUS.DRAFT) {
Object.assign(this.data, { Object.assign(this.data, {
code: `RF-${data?.['transaction']?.invoice_code.split('-')[1]}`,
request_date: new Date(), request_date: new Date(),
status: STATUS.PENDING, status: STATUS.PENDING,
}); });

View File

@ -53,7 +53,6 @@ export class ConfirmRefundManager extends BaseUpdateStatusManager<RefundEntity>
if (data.status == STATUS.DRAFT) { if (data.status == STATUS.DRAFT) {
Object.assign(this.data, { Object.assign(this.data, {
code: `RF-${data.transaction?.invoice_code?.split('-')[1]}`,
request_date: new Date(), request_date: new Date(),
status: STATUS.PENDING, status: STATUS.PENDING,
}); });

View File

@ -13,6 +13,8 @@ import { RefundModel } from '../../../data/models/refund.model';
import { BaseCreateManager } from 'src/core/modules/domain/usecase/managers/base-create.manager'; import { BaseCreateManager } from 'src/core/modules/domain/usecase/managers/base-create.manager';
import { RefundCreatedEvent } from '../../entities/event/refund-created.event'; import { RefundCreatedEvent } from '../../entities/event/refund-created.event';
import { STATUS } from 'src/core/strings/constants/base.constants'; import { STATUS } from 'src/core/strings/constants/base.constants';
import { In, Not } from 'typeorm';
import { generateInvoiceCodeHelper } from 'src/modules/transaction/transaction/domain/usecases/managers/helpers/generate-invoice-code.helper';
@Injectable() @Injectable()
export class CreateRefundManager extends BaseCreateManager<RefundEntity> { export class CreateRefundManager extends BaseCreateManager<RefundEntity> {
@ -26,13 +28,10 @@ export class CreateRefundManager extends BaseCreateManager<RefundEntity> {
}; };
}); });
Object.assign(this.data, {
refund_items: refund_items,
});
const exist = await this.dataService.getOneByOptions({ const exist = await this.dataService.getOneByOptions({
where: { where: {
transaction_id: this.data.transaction.id, transaction_id: this.data.transaction.id,
status: Not(In([STATUS.CANCEL])),
}, },
}); });
if (exist) { if (exist) {
@ -57,6 +56,11 @@ export class CreateRefundManager extends BaseCreateManager<RefundEntity> {
error: 'Unprocessable Entity', error: 'Unprocessable Entity',
}); });
} }
Object.assign(this.data, {
refund_items: refund_items,
code: await generateInvoiceCodeHelper(this.dataService, 'RF'),
});
return; return;
} }

View File

@ -15,7 +15,7 @@ export class DetailRefundManager extends BaseDetailManager<RefundEntity> {
} }
async afterProcess(): Promise<void> { async afterProcess(): Promise<void> {
mappingTransaction(this.result['transaction']); mappingTransaction(this.result['transaction'], this.dataId);
return; return;
} }
@ -25,7 +25,12 @@ export class DetailRefundManager extends BaseDetailManager<RefundEntity> {
joinRelations: [], joinRelations: [],
// relation join and select (relasi yang ingin ditampilkan), // relation join and select (relasi yang ingin ditampilkan),
selectRelations: ['transaction', 'transaction.items', 'items.refund'], selectRelations: [
'transaction',
'transaction.items',
'items.refunds item_refunds',
'item_refunds.refund item_refunds_refund',
],
// relation yang hanya ingin dihitung (akan return number) // relation yang hanya ingin dihitung (akan return number)
countRelations: [], countRelations: [],
@ -55,7 +60,9 @@ export class DetailRefundManager extends BaseDetailManager<RefundEntity> {
'transaction', 'transaction',
'items', 'items',
'refund', 'item_refunds',
'item_refunds_refund.id',
'item_refunds_refund.status',
]; ];
} }

View File

@ -1,5 +1,5 @@
import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { Column, Entity, JoinColumn, ManyToOne, OneToOne } from 'typeorm'; import { Column, Entity, JoinColumn, ManyToOne, OneToMany } from 'typeorm';
import { BaseCoreModel } from 'src/core/modules/data/model/base-core.model'; import { BaseCoreModel } from 'src/core/modules/data/model/base-core.model';
import { TransactionItemEntity } from '../../domain/entities/transaction-item.entity'; import { TransactionItemEntity } from '../../domain/entities/transaction-item.entity';
import { TransactionModel } from './transaction.model'; import { TransactionModel } from './transaction.model';
@ -80,10 +80,10 @@ export class TransactionItemModel
transaction: TransactionModel; transaction: TransactionModel;
// relations to refund // relations to refund
@OneToOne(() => RefundItemModel, (model) => model.transaction_item, { @OneToMany(() => RefundItemModel, (model) => model.transaction_item, {
cascade: true, cascade: true,
onDelete: 'CASCADE', onDelete: 'CASCADE',
onUpdate: 'CASCADE', onUpdate: 'CASCADE',
}) })
refund: RefundItemModel; refunds: RefundItemModel[];
} }

View File

@ -1,6 +1,6 @@
import { TABLE_NAME } from 'src/core/strings/constants/table.constants'; import { TABLE_NAME } from 'src/core/strings/constants/table.constants';
import { TransactionEntity } from '../../domain/entities/transaction.entity'; import { TransactionEntity } from '../../domain/entities/transaction.entity';
import { Column, Entity, OneToMany, OneToOne } from 'typeorm'; import { Column, Entity, OneToMany } from 'typeorm';
import { BaseStatusModel } from 'src/core/modules/data/model/base-status.model'; import { BaseStatusModel } from 'src/core/modules/data/model/base-status.model';
import { import {
TransactionType, TransactionType,
@ -233,10 +233,10 @@ export class TransactionModel
taxes: TransactionTaxModel[]; taxes: TransactionTaxModel[];
// relations to refund // relations to refund
@OneToOne(() => RefundModel, (model) => model.transaction, { @OneToMany(() => RefundModel, (model) => model.transaction, {
cascade: true, cascade: true,
onDelete: 'CASCADE', onDelete: 'CASCADE',
onUpdate: 'CASCADE', onUpdate: 'CASCADE',
}) })
refund: RefundModel; refunds: RefundModel[];
} }

View File

@ -25,7 +25,12 @@ export class DetailTransactionManager extends BaseDetailManager<TransactionEntit
joinRelations: [], joinRelations: [],
// relation join and select (relasi yang ingin ditampilkan), // relation join and select (relasi yang ingin ditampilkan),
selectRelations: ['items', 'items.refund item_refund', 'refund'], selectRelations: [
'items',
'items.refunds item_refunds',
'item_refunds.refund item_refunds_refund',
'refunds',
],
// relation yang hanya ingin dihitung (akan return number) // relation yang hanya ingin dihitung (akan return number)
countRelations: [], countRelations: [],
@ -76,8 +81,11 @@ export class DetailTransactionManager extends BaseDetailManager<TransactionEntit
`${this.tableName}.payment_total`, `${this.tableName}.payment_total`,
'items', 'items',
'item_refund', 'item_refunds',
'refund', 'item_refunds_refund.id',
'item_refunds_refund.status',
'refunds',
]; ];
} }

View File

@ -12,6 +12,12 @@ export async function generateInvoiceCodeHelper(dataService, code) {
invoice_code: ILike(`%${month_year}%`), invoice_code: ILike(`%${month_year}%`),
}; };
if (code == 'RF') {
query = {
code: ILike(`%${month_year}%`),
};
}
if (code == 'PYM') { if (code == 'PYM') {
query = { query = {
payment_code: ILike(`%${month_year}%`), payment_code: ILike(`%${month_year}%`),

View File

@ -4,7 +4,7 @@ import {
TransactionType, TransactionType,
} from 'src/modules/transaction/transaction/constants'; } from 'src/modules/transaction/transaction/constants';
export function mappingTransaction(data) { export function mappingTransaction(data, refundId?: string) {
let payment_type_bank: any = null; let payment_type_bank: any = null;
const season_period = { const season_period = {
id: data.season_period_id, id: data.season_period_id,
@ -26,6 +26,9 @@ export function mappingTransaction(data) {
const items = data?.['items']?.map((itemData) => { const items = data?.['items']?.map((itemData) => {
let tenant; let tenant;
let refund = itemData.refunds?.find(
(item) => ![STATUS.CANCEL].includes(item.refund.status),
);
if (itemData.item_tenant_id) { if (itemData.item_tenant_id) {
tenant = { tenant = {
@ -35,6 +38,9 @@ export function mappingTransaction(data) {
}; };
} }
if (refundId)
refund = itemData.refunds?.find((item) => item.refund.id == refundId);
return { return {
item: { item: {
id: itemData.item_id, id: itemData.item_id,
@ -49,20 +55,27 @@ export function mappingTransaction(data) {
}, },
}, },
id: itemData.id, id: itemData.id,
refund: itemData.refund, refund: refund,
qty: itemData.qty, qty: itemData.qty,
qty_remaining: itemData.qty_remaining, qty_remaining: itemData.qty_remaining,
total_price_refund: itemData.refund?.refund_total ?? 0, total_price_refund: refund?.refund_total ?? 0,
total_price: itemData.total_price, total_price: itemData.total_price,
}; };
}); });
const refund = data.refunds?.find(
(refund) => ![STATUS.CANCEL].includes(refund.status),
);
Object.assign(data, { Object.assign(data, {
season_period: season_period, season_period: season_period,
items: items, items: items,
payment_type_bank: payment_type_bank, payment_type_bank: payment_type_bank,
refund: refund,
}); });
delete data.refunds;
delete data.season_period_id; delete data.season_period_id;
delete data.season_period_name; delete data.season_period_name;
delete data.season_period_type_id; delete data.season_period_type_id;

View File

@ -7,6 +7,7 @@ import {
RelationParam, RelationParam,
} from 'src/core/modules/domain/entities/base-filter.entity'; } from 'src/core/modules/domain/entities/base-filter.entity';
import { BetweenQueryHelper } from 'src/core/helpers/query/between-query.helper'; import { BetweenQueryHelper } from 'src/core/helpers/query/between-query.helper';
import { STATUS } from 'src/core/strings/constants/base.constants';
@Injectable() @Injectable()
export class IndexTransactionManager extends BaseIndexManager<TransactionEntity> { export class IndexTransactionManager extends BaseIndexManager<TransactionEntity> {
@ -20,12 +21,16 @@ export class IndexTransactionManager extends BaseIndexManager<TransactionEntity>
async afterProcess(): Promise<void> { async afterProcess(): Promise<void> {
this.result?.data?.map((item) => { this.result?.data?.map((item) => {
const activeRefund = item['refunds'].find(
(refund) => ![STATUS.CANCEL].includes(refund.status),
);
Object.assign(item, { Object.assign(item, {
refund_code: item['refund']?.code ?? null, refund_code: activeRefund?.code ?? null,
refund_date: item['refund']?.refund_date ?? null, refund_date: activeRefund?.refund_date ?? null,
}); });
delete item['refund']; delete item['refunds'];
}); });
return; return;
} }
@ -36,7 +41,7 @@ export class IndexTransactionManager extends BaseIndexManager<TransactionEntity>
joinRelations: [], joinRelations: [],
// relation join and select (relasi yang ingin ditampilkan), // relation join and select (relasi yang ingin ditampilkan),
selectRelations: ['items', 'refund'], selectRelations: ['items', 'refunds'],
// relation yang hanya ingin dihitung (akan return number) // relation yang hanya ingin dihitung (akan return number)
countRelations: [], countRelations: [],
@ -76,10 +81,10 @@ export class IndexTransactionManager extends BaseIndexManager<TransactionEntity>
`${this.tableName}.payment_type_method_name`, `${this.tableName}.payment_type_method_name`,
`${this.tableName}.payment_type_method_number`, `${this.tableName}.payment_type_method_number`,
`refund.id`, `refunds.id`,
`refund.code`, `refunds.code`,
`refund.refund_date`, `refunds.refund_date`,
`refund.request_date`, `refunds.request_date`,
]; ];
} }