Merge branch 'development' into 'devops-staging'
fix: transaction See merge request empatnusabangsa/ppob/ppob-backend!94
This commit is contained in:
commit
9fb168a70f
|
@ -367,10 +367,7 @@ export class TransactionService {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (
|
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
|
||||||
coaAccount.amount <=
|
|
||||||
product_price.mark_up_price + product_price.price
|
|
||||||
) {
|
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
{
|
{
|
||||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
@ -439,12 +436,14 @@ export class TransactionService {
|
||||||
orderTransactionDto: OrderTransactionDto,
|
orderTransactionDto: OrderTransactionDto,
|
||||||
currentUser: any,
|
currentUser: any,
|
||||||
) {
|
) {
|
||||||
//TODO GET USER DATA
|
let status;
|
||||||
|
let amount = 0;
|
||||||
|
//GET USER DATA
|
||||||
const userData = await this.userService.findByUsername(
|
const userData = await this.userService.findByUsername(
|
||||||
currentUser.username,
|
currentUser.username,
|
||||||
);
|
);
|
||||||
|
|
||||||
//TODO GET PRODUCT AND PRICE
|
//GET PRODUCT AND PRICE
|
||||||
const product = await this.productService.findOne(
|
const product = await this.productService.findOne(
|
||||||
orderTransactionDto.productCode,
|
orderTransactionDto.productCode,
|
||||||
);
|
);
|
||||||
|
@ -454,15 +453,25 @@ export class TransactionService {
|
||||||
userData.partner?.id,
|
userData.partner?.id,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
//GET COA
|
||||||
const coaAccount = await this.coaService.findByUser(
|
const coaAccount = await this.coaService.findByUser(
|
||||||
userData.id,
|
userData.id,
|
||||||
coaType.WALLET,
|
coaType.WALLET,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (
|
const coaInventory = await this.coaService.findByName(
|
||||||
coaAccount.amount <=
|
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
|
||||||
product_price.mark_up_price + product_price.price
|
);
|
||||||
) {
|
|
||||||
|
const coaCostOfSales = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaSales = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.SALES]}-SYSTEM`,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
|
||||||
throw new HttpException(
|
throw new HttpException(
|
||||||
{
|
{
|
||||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
@ -472,7 +481,7 @@ export class TransactionService {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO HIT API SUPPLIER
|
//HIT API SUPPLIER
|
||||||
const trxId = Array(6)
|
const trxId = Array(6)
|
||||||
.fill(null)
|
.fill(null)
|
||||||
.map(() => {
|
.map(() => {
|
||||||
|
@ -486,38 +495,17 @@ export class TransactionService {
|
||||||
trxId,
|
trxId,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// const hitSupplier = {
|
||||||
|
// harga: product_price.price,
|
||||||
|
// success: true,
|
||||||
|
// msg: 'Berhasil',
|
||||||
|
// };
|
||||||
|
|
||||||
this.logger.log({
|
this.logger.log({
|
||||||
responseAPISupplier: hitSupplier,
|
responseAPISupplier: hitSupplier,
|
||||||
});
|
});
|
||||||
|
|
||||||
//TODO TRANSACTION DAT
|
if (hitSupplier.harga != product_price.price) {
|
||||||
const transactionData = new Transactions();
|
|
||||||
|
|
||||||
transactionData.id = uuid.v4();
|
|
||||||
transactionData.amount = product_price.mark_up_price + product_price.price;
|
|
||||||
transactionData.user = userData.id;
|
|
||||||
transactionData.type = typeTransaction.ORDER;
|
|
||||||
transactionData.product_price = product_price;
|
|
||||||
transactionData.destination = orderTransactionDto.destination;
|
|
||||||
transactionData.partner_trx_id = orderTransactionDto.trx_id;
|
|
||||||
transactionData.supplier_trx_id = trxId;
|
|
||||||
|
|
||||||
if (!hitSupplier.success) {
|
|
||||||
transactionData.status = statusTransaction.FAILED;
|
|
||||||
await this.transactionRepository.insert(transactionData);
|
|
||||||
throw new HttpException(
|
|
||||||
{
|
|
||||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
error: hitSupplier.msg,
|
|
||||||
},
|
|
||||||
HttpStatus.INTERNAL_SERVER_ERROR,
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
transactionData.status = statusTransaction.PENDING;
|
|
||||||
await this.transactionRepository.insert(transactionData);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hitSupplier.harga > product_price.price) {
|
|
||||||
product_price.endDate = new Date();
|
product_price.endDate = new Date();
|
||||||
|
|
||||||
const listActivePrice =
|
const listActivePrice =
|
||||||
|
@ -541,12 +529,74 @@ export class TransactionService {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
//TRANSACTION DATA
|
||||||
|
await this.connection.transaction(async (manager) => {
|
||||||
|
const transactionData = new Transactions();
|
||||||
|
|
||||||
|
transactionData.id = uuid.v4();
|
||||||
|
transactionData.amount =
|
||||||
|
product_price.mark_up_price + product_price.price;
|
||||||
|
transactionData.user = userData.id;
|
||||||
|
transactionData.type = typeTransaction.ORDER;
|
||||||
|
transactionData.product_price = product_price;
|
||||||
|
transactionData.destination = orderTransactionDto.destination;
|
||||||
|
transactionData.partner_trx_id = orderTransactionDto.trx_id;
|
||||||
|
transactionData.supplier_trx_id = trxId;
|
||||||
|
|
||||||
|
if (!hitSupplier.success) {
|
||||||
|
transactionData.status = statusTransaction.FAILED;
|
||||||
|
status = statusTransaction[transactionData.status];
|
||||||
|
await this.transactionRepository.insert(transactionData);
|
||||||
|
throw new HttpException(
|
||||||
|
{
|
||||||
|
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
error: hitSupplier.msg,
|
||||||
|
},
|
||||||
|
HttpStatus.INTERNAL_SERVER_ERROR,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
transactionData.status = statusTransaction.PENDING;
|
||||||
|
status = statusTransaction[transactionData.status];
|
||||||
|
}
|
||||||
|
|
||||||
|
await manager.insert(Transactions, transactionData);
|
||||||
|
|
||||||
|
await this.accountingTransaction({
|
||||||
|
createTransaction: false,
|
||||||
|
transactionalEntityManager: manager,
|
||||||
|
transaction: transactionData,
|
||||||
|
amount: transactionData.amount,
|
||||||
|
journals: [
|
||||||
|
{
|
||||||
|
coa_id: coaInventory.id,
|
||||||
|
credit: product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaCostOfSales.id,
|
||||||
|
debit: product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaAccount.id,
|
||||||
|
debit: product_price.mark_up_price + product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
// eslint-disable-next-line camelcase
|
||||||
|
coa_id: coaSales.id,
|
||||||
|
credit: product_price.mark_up_price + product_price.price,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
return {
|
return {
|
||||||
trx_id: trxId,
|
trx_id: trxId,
|
||||||
client_trx_id: orderTransactionDto.trx_id,
|
client_trx_id: orderTransactionDto.trx_id,
|
||||||
product: orderTransactionDto.productCode,
|
product: orderTransactionDto.productCode,
|
||||||
amount: transactionData.amount,
|
amount: product_price.mark_up_price + product_price.price,
|
||||||
status: statusTransaction[transactionData.status],
|
status: status,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -706,7 +756,9 @@ export class TransactionService {
|
||||||
where: {
|
where: {
|
||||||
supplier_trx_id: supplier_trx_id,
|
supplier_trx_id: supplier_trx_id,
|
||||||
},
|
},
|
||||||
|
relations: ['product_price'],
|
||||||
});
|
});
|
||||||
|
|
||||||
const dataMsg = callback.msg;
|
const dataMsg = callback.msg;
|
||||||
const failedReason = dataMsg.split('.');
|
const failedReason = dataMsg.split('.');
|
||||||
|
|
||||||
|
@ -714,7 +766,66 @@ export class TransactionService {
|
||||||
dataTransaction.callback_json = callback;
|
dataTransaction.callback_json = callback;
|
||||||
dataTransaction.failed_reason = failedReason[0] + ', ' + failedReason[1];
|
dataTransaction.failed_reason = failedReason[0] + ', ' + failedReason[1];
|
||||||
|
|
||||||
await this.transactionRepository.save(dataTransaction);
|
const userData = await this.userService.findExist(dataTransaction.user);
|
||||||
|
|
||||||
|
const product_price = await this.productHistoryPriceService.findById(
|
||||||
|
dataTransaction.product_price.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
const product = await this.productService.findOneById(
|
||||||
|
product_price.product.id,
|
||||||
|
);
|
||||||
|
|
||||||
|
//GET COA
|
||||||
|
const coaAccount = await this.coaService.findByUser(
|
||||||
|
userData.id,
|
||||||
|
coaType.WALLET,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaInventory = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaCostOfSales = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
|
||||||
|
);
|
||||||
|
|
||||||
|
const coaSales = await this.coaService.findByName(
|
||||||
|
`${coaType[coaType.SALES]}-SYSTEM`,
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
await this.connection.transaction(async (manager) => {
|
||||||
|
await manager.save(dataTransaction);
|
||||||
|
|
||||||
|
await this.accountingTransaction({
|
||||||
|
createTransaction: false,
|
||||||
|
transactionalEntityManager: manager,
|
||||||
|
transaction: dataTransaction,
|
||||||
|
amount: dataTransaction.amount,
|
||||||
|
journals: [
|
||||||
|
{
|
||||||
|
coa_id: coaInventory.id,
|
||||||
|
debit: product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaCostOfSales.id,
|
||||||
|
credit: product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaAccount.id,
|
||||||
|
credit: product_price.mark_up_price + product_price.price,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
coa_id: coaSales.id,
|
||||||
|
debit: product_price.mark_up_price + product_price.price,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
});
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async callbackOrderSuccess(supplier_trx_id: string, callback: any) {
|
async callbackOrderSuccess(supplier_trx_id: string, callback: any) {
|
||||||
|
@ -744,23 +855,6 @@ export class TransactionService {
|
||||||
let profit = product_price.mark_up_price;
|
let profit = product_price.mark_up_price;
|
||||||
|
|
||||||
//GET COA
|
//GET COA
|
||||||
const coaAccount = await this.coaService.findByUser(
|
|
||||||
userData.id,
|
|
||||||
coaType.WALLET,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaInventory = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.INVENTORY]}-${product.supplier.code}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaCostOfSales = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaSales = await this.coaService.findByName(
|
|
||||||
`${coaType[coaType.SALES]}-SYSTEM`,
|
|
||||||
);
|
|
||||||
|
|
||||||
const coaExpense = await this.coaService.findByName(
|
const coaExpense = await this.coaService.findByName(
|
||||||
`${coaType[coaType.EXPENSE]}-SYSTEM`,
|
`${coaType[coaType.EXPENSE]}-SYSTEM`,
|
||||||
);
|
);
|
||||||
|
@ -797,24 +891,7 @@ export class TransactionService {
|
||||||
transactionalEntityManager: manager,
|
transactionalEntityManager: manager,
|
||||||
transaction: dataTransaction,
|
transaction: dataTransaction,
|
||||||
amount: dataTransaction.amount,
|
amount: dataTransaction.amount,
|
||||||
journals: [
|
journals: supervisorData,
|
||||||
{
|
|
||||||
coa_id: coaInventory.id,
|
|
||||||
credit: product_price.price,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
coa_id: coaCostOfSales.id,
|
|
||||||
debit: product_price.price,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
coa_id: coaAccount.id,
|
|
||||||
debit: product_price.mark_up_price + product_price.price,
|
|
||||||
},
|
|
||||||
{
|
|
||||||
coa_id: coaSales.id,
|
|
||||||
credit: product_price.mark_up_price + product_price.price,
|
|
||||||
},
|
|
||||||
].concat(supervisorData),
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user