Merge branch 'development' into 'devops-staging'
fix: transaction See merge request empatnusabangsa/ppob/ppob-backend!94
This commit is contained in:
commit
8a43f80689
|
@ -367,10 +367,7 @@ export class TransactionService {
|
|||
]);
|
||||
}
|
||||
|
||||
if (
|
||||
coaAccount.amount <=
|
||||
product_price.mark_up_price + product_price.price
|
||||
) {
|
||||
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
|
@ -439,12 +436,14 @@ export class TransactionService {
|
|||
orderTransactionDto: OrderTransactionDto,
|
||||
currentUser: any,
|
||||
) {
|
||||
//TODO GET USER DATA
|
||||
let status;
|
||||
let amount = 0;
|
||||
//GET USER DATA
|
||||
const userData = await this.userService.findByUsername(
|
||||
currentUser.username,
|
||||
);
|
||||
|
||||
//TODO GET PRODUCT AND PRICE
|
||||
//GET PRODUCT AND PRICE
|
||||
const product = await this.productService.findOne(
|
||||
orderTransactionDto.productCode,
|
||||
);
|
||||
|
@ -454,15 +453,25 @@ export class TransactionService {
|
|||
userData.partner?.id,
|
||||
);
|
||||
|
||||
//GET COA
|
||||
const coaAccount = await this.coaService.findByUser(
|
||||
userData.id,
|
||||
coaType.WALLET,
|
||||
);
|
||||
|
||||
if (
|
||||
coaAccount.amount <=
|
||||
product_price.mark_up_price + product_price.price
|
||||
) {
|
||||
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`,
|
||||
);
|
||||
|
||||
if (coaAccount.amount < product_price.mark_up_price + product_price.price) {
|
||||
throw new HttpException(
|
||||
{
|
||||
statusCode: HttpStatus.INTERNAL_SERVER_ERROR,
|
||||
|
@ -472,7 +481,7 @@ export class TransactionService {
|
|||
);
|
||||
}
|
||||
|
||||
//TODO HIT API SUPPLIER
|
||||
//HIT API SUPPLIER
|
||||
const trxId = Array(6)
|
||||
.fill(null)
|
||||
.map(() => {
|
||||
|
@ -486,38 +495,17 @@ export class TransactionService {
|
|||
trxId,
|
||||
);
|
||||
|
||||
// const hitSupplier = {
|
||||
// harga: product_price.price,
|
||||
// success: true,
|
||||
// msg: 'Berhasil',
|
||||
// };
|
||||
|
||||
this.logger.log({
|
||||
responseAPISupplier: hitSupplier,
|
||||
});
|
||||
|
||||
//TODO TRANSACTION DAT
|
||||
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) {
|
||||
if (hitSupplier.harga != product_price.price) {
|
||||
product_price.endDate = new Date();
|
||||
|
||||
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 {
|
||||
trx_id: trxId,
|
||||
client_trx_id: orderTransactionDto.trx_id,
|
||||
product: orderTransactionDto.productCode,
|
||||
amount: transactionData.amount,
|
||||
status: statusTransaction[transactionData.status],
|
||||
amount: product_price.mark_up_price + product_price.price,
|
||||
status: status,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -706,7 +756,9 @@ export class TransactionService {
|
|||
where: {
|
||||
supplier_trx_id: supplier_trx_id,
|
||||
},
|
||||
relations: ['product_price'],
|
||||
});
|
||||
|
||||
const dataMsg = callback.msg;
|
||||
const failedReason = dataMsg.split('.');
|
||||
|
||||
|
@ -714,7 +766,66 @@ export class TransactionService {
|
|||
dataTransaction.callback_json = callback;
|
||||
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) {
|
||||
|
@ -744,23 +855,6 @@ export class TransactionService {
|
|||
let profit = product_price.mark_up_price;
|
||||
|
||||
//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(
|
||||
`${coaType[coaType.EXPENSE]}-SYSTEM`,
|
||||
);
|
||||
|
@ -797,24 +891,7 @@ export class TransactionService {
|
|||
transactionalEntityManager: manager,
|
||||
transaction: dataTransaction,
|
||||
amount: dataTransaction.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,
|
||||
},
|
||||
{
|
||||
coa_id: coaSales.id,
|
||||
credit: product_price.mark_up_price + product_price.price,
|
||||
},
|
||||
].concat(supervisorData),
|
||||
journals: supervisorData,
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
|
|
Loading…
Reference in New Issue
Block a user