diff --git a/src/transaction/ppob_callback.controller.ts b/src/transaction/ppob_callback.controller.ts index d415de3..983d833 100644 --- a/src/transaction/ppob_callback.controller.ts +++ b/src/transaction/ppob_callback.controller.ts @@ -66,7 +66,10 @@ export class PpobCallbackController { ); // } else { + console.log('statusapani', response['status']); + console.log('statusapani2', response.toString()); if (response['status'].toString() != '20') { + console.log("masukkesiniga", "msk") //TODO: UPDATE GAGAL const updateTransaction = await this.transactionService.callbackOrderFailed( @@ -79,16 +82,22 @@ export class PpobCallbackController { statusCode: HttpStatus.BAD_REQUEST, message: 'failed to proccess', }; + } else { + + //TODO: UPDATE BERHASIL + const updateTransaction = + await this.transactionService.callbackOrderSuccess( + response['refid'], + response, + ); + + return { + updateTransaction, + statusCode: HttpStatus.OK, + message: 'success', + }; } - - //TODO: UPDATE BERHASIL - const updateTransaction = - await this.transactionService.callbackOrderSuccess( - response['refid'], - response, - ); } - this.logger.log({ requestQuery: request.query, }); diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 48f2b1c..bba3198 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -299,184 +299,216 @@ export class TransactionService { orderTransactionDto: OrderTransactionDto, currentUser: any, ) { - const trxId = Array(6) - .fill(null) - .map(() => { - return Math.round(Math.random() * 16).toString(16); - }) - .join(''); - //GET USER - const userData = await this.userService.findByUsername( - currentUser.username, + const productData = await this.productService.findOne( + orderTransactionDto.productCode, + 'prepaid', ); - //GET PRODUCT - const product = await this.productService.findOne( - orderTransactionDto.productCode, - 'prepaid' - ); - - const product_price = await this.productHistoryPriceService.findOne( - product.id, - userData.partner?.id, - ); - - let supervisorData = []; - 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`, - ); - - if (!userData.partner) { - //GET SALES - supervisorData = await this.calculateCommission( - supervisorData, - profit, - userData, - ); - profit = supervisorData - .map((item) => { - return item.credit; - }) - .reduce((prev, curr) => { - return prev + curr; - }, 0); - - supervisorData = supervisorData.concat([ - { - coa_id: coaExpense.id, - debit: profit, - }, - ]); - } - - if (coaAccount.amount < product_price.mark_up_price + product_price.price) { + if (productData.status == 'NOT ACTIVE') { throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: `Transaction Failed because saldo not enough`, - }, - HttpStatus.INTERNAL_SERVER_ERROR, + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because product is not active`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, ); - } + } else { - try { - await this.connection.transaction(async (manager) => { - const transactionData = new Transactions(); + const trxId = Array(6) + .fill(null) + .map(() => { + return Math.round(Math.random() * 16).toString(16); + }) + .join(''); - transactionData.id = uuid.v4(); - transactionData.amount = - product_price.mark_up_price + product_price.price; - transactionData.user = userData.id; - transactionData.status = statusTransaction.SUCCESS; - 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; - await manager.insert(Transactions, transactionData); + //GET USER + const userData = await this.userService.findByUsername( + currentUser.username, + ); - await this.accountingTransaction({ - createTransaction: false, - transactionalEntityManager: manager, - transaction: transactionData, - amount: transactionData.amount, - journals: [ + //GET PRODUCT + const product = await this.productService.findOne( + orderTransactionDto.productCode, + 'prepaid' + ); + + const product_price = await this.productHistoryPriceService.findOne( + product.id, + userData.partner?.id, + ); + + let supervisorData = []; + 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`, + ); + + if (!userData.partner) { + //GET SALES + supervisorData = await this.calculateCommission( + supervisorData, + profit, + userData, + ); + profit = supervisorData + .map((item) => { + return item.credit; + }) + .reduce((prev, curr) => { + return prev + curr; + }, 0); + + supervisorData = supervisorData.concat([ + { + coa_id: coaExpense.id, + debit: profit, + }, + ]); + } + + if (coaAccount.amount < product_price.mark_up_price + product_price.price) { + throw new HttpException( { - coa_id: coaInventory.id, - credit: product_price.price, + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because saldo not enough`, }, - { - 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, - }, - ].concat(supervisorData), + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } + + try { + 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.status = statusTransaction.SUCCESS; + 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; + 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, + }, + ].concat(supervisorData), + }); }); - }); - } catch (e) { - throw e; - } + } catch (e) { + throw e; + } - return { - trx_id: trxId, - client_trx_id: orderTransactionDto.trx_id, - product: orderTransactionDto.productCode, - amount: product_price.mark_up_price + product_price.price, - status: statusTransaction[statusTransaction.SUCCESS], - }; + return { + trx_id: trxId, + client_trx_id: orderTransactionDto.trx_id, + product: orderTransactionDto.productCode, + amount: product_price.mark_up_price + product_price.price, + status: statusTransaction[statusTransaction.SUCCESS], + }; + } } async orderTransactionProd( orderTransactionDto: OrderTransactionDto, currentUser: any, ) { + + const productData = await this.productService.findOne( + orderTransactionDto.productCode, + 'prepaid', + ); + + if (productData.status == 'NOT ACTIVE') { + throw new HttpException( + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because product is not active`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, + ); + } else { let status; const amount = 0; //GET USER DATA const userData = await this.userService.findByUsername( - currentUser.username, + currentUser.username, ); //GET PRODUCT AND PRICE const product = await this.productService.findOne( - orderTransactionDto.productCode, - 'prepaid', + orderTransactionDto.productCode, + 'prepaid', ); const supplier = await this.supplierService.findByCode( - product.supplier.code, + product.supplier.code, ); let product_price = await this.productHistoryPriceService.findOne( - product.id, - userData.partner?.id, + product.id, + userData.partner?.id, ); //GET COA const coaAccount = await this.coaService.findByUser( - userData.id, - coaType.WALLET, + userData.id, + coaType.WALLET, ); const coaInventory = await this.coaService.findByName( - `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, + `${coaType[coaType.INVENTORY]}-${product.supplier.code}`, ); const coaCostOfSales = await this.coaService.findByName( - `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, + `${coaType[coaType.COST_OF_SALES]}-${product.supplier.code}`, ); const coaSales = await this.coaService.findByName( - `${coaType[coaType.SALES]}-SYSTEM`, + `${coaType[coaType.SALES]}-SYSTEM`, ); if (orderTransactionDto.bill_trx_id) { @@ -490,11 +522,11 @@ export class TransactionService { } catch (e) { if (e instanceof EntityNotFoundError) { throw new HttpException( - { - statusCode: HttpStatus.NOT_FOUND, - error: 'Bill not found', - }, - HttpStatus.NOT_FOUND, + { + statusCode: HttpStatus.NOT_FOUND, + error: 'Bill not found', + }, + HttpStatus.NOT_FOUND, ); } else { throw e; @@ -504,42 +536,41 @@ export class TransactionService { if (coaAccount.amount < product_price.mark_up_price + product_price.price) { throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: `Transaction Failed because saldo not enough`, - }, - HttpStatus.INTERNAL_SERVER_ERROR, + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: `Transaction Failed because saldo not enough`, + }, + HttpStatus.INTERNAL_SERVER_ERROR, ); } //HIT API SUPPLIER const trxId = Array(6) - .fill(null) - .map(() => { - return Math.round(Math.random() * 16).toString(16); - }) - .join(''); + .fill(null) + .map(() => { + return Math.round(Math.random() * 16).toString(16); + }) + .join(''); let hitSupplier = await doTransaction( - orderTransactionDto.productCode, - orderTransactionDto.destination, - trxId, - supplier, + orderTransactionDto.productCode, + orderTransactionDto.destination, + trxId, + supplier, ); // let hitSupplier; if (supplier.code != 'IRS') { const parsingResponse = hitSupplier.split(' '); - console.log const newHitSupplier = { success: hitSupplier.includes('diproses'), harga: parseInt( - parsingResponse[parsingResponse.length - 2].replace(/\./g,' '), + parsingResponse[parsingResponse.length - 2].replace(/\./g, ' '), ), msg: hitSupplier, }; hitSupplier = newHitSupplier; - if(orderTransactionDto.bill_trx_id !== null){ + if (orderTransactionDto.bill_trx_id !== null) { hitSupplier.harga = product_price.price; } } @@ -559,9 +590,9 @@ export class TransactionService { product_price.endDate = new Date(); costInventory = hitSupplier.harga; const listActivePrice = - await this.productHistoryPriceService.getAllActivePriceByProduct( - product.id, - ); + await this.productHistoryPriceService.getAllActivePriceByProduct( + product.id, + ); await this.productHistoryPriceService.updateEndDate(product.id); @@ -588,7 +619,7 @@ export class TransactionService { transactionData.id = uuid.v4(); transactionData.amount = - product_price.mark_up_price + product_price.price; + product_price.mark_up_price + product_price.price; transactionData.user = userData.id; transactionData.type = typeTransaction.ORDER; transactionData.product_price = product_price; @@ -602,11 +633,11 @@ export class TransactionService { status = statusTransaction[transactionData.status]; await this.transactionRepository.insert(transactionData); throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: hitSupplier.msg, - }, - HttpStatus.INTERNAL_SERVER_ERROR, + { + statusCode: HttpStatus.INTERNAL_SERVER_ERROR, + error: hitSupplier.msg, + }, + HttpStatus.INTERNAL_SERVER_ERROR, ); } else { transactionData.status = statusTransaction.PENDING; @@ -653,6 +684,7 @@ export class TransactionService { status: status, }; } + } async orderTransactionBillProd( orderTransactionDto: OrderTransactionDto, @@ -1169,12 +1201,12 @@ export class TransactionService { relations: ['product_price'], }); - const dataMsg = callback.msg; - const failedReason = dataMsg.split('.'); + // const dataMsg = callback.msg; + // const failedReason = dataMsg.split('.'); - dataTransaction.status = statusTransaction.FAILED; - dataTransaction.callback_json = callback; - dataTransaction.failed_reason = `${failedReason[0]}, ${failedReason[1]}`; + // dataTransaction.status = statusTransaction.FAILED; + // dataTransaction.callback_json = callback; + // dataTransaction.failed_reason = `Trx ${product.code} ke ${dataTransaction.destination} gagal`; const userData = await this.userService.findExist(dataTransaction.user); @@ -1186,6 +1218,13 @@ export class TransactionService { product_price.product.id, ); + dataTransaction.status = statusTransaction.FAILED; + dataTransaction.callback_json = callback; + dataTransaction.failed_reason = `Trx ${product.code} ke ${dataTransaction.destination} gagal`; + if (callback['price'] != dataTransaction.product_price.price) { + dataTransaction.product_price.price = callback['price'] + } + //GET COA const coaAccount = await this.coaService.findByUser( userData.id, @@ -1271,6 +1310,10 @@ export class TransactionService { } dataTransaction.callback_json = callback; + if (callback['price'] != dataTransaction.product_price.price) { + dataTransaction.product_price.price = callback['price'] + } + const userData = await this.userService.findExist(dataTransaction.user); let supervisorData = []; @@ -1570,7 +1613,8 @@ export class TransactionService { ) .leftJoin('transaction.product_price', 'product_price') .leftJoin('product_price.product', 'product') - .addSelect('transaction.amount', 'price') + .leftJoin('product.supplier', 'supplier') + .addSelect('transaction.amount', 'price') .addSelect('transaction.destination') .addSelect('transaction.seri_number', 'seri_number') .addSelect('transaction.supplier_trx_id', 'transaction_code') @@ -1579,8 +1623,10 @@ export class TransactionService { .addSelect('transaction.failed_reason', 'failed_reason') .addSelect('userData.name', 'buyer') .addSelect('product.name', 'name') - .addSelect('product.id', 'product_id') - .orderBy('transaction.created_at', 'DESC'); + .addSelect('product_price.price', 'product_price') + .addSelect('product.name', 'supplier_name') + .addSelect('supplier.name', 'supplier_name') + .orderBy('transaction.created_at', 'DESC'); if (startDate && endDate) { baseQuery.andWhere( @@ -1881,16 +1927,27 @@ export class TransactionService { ); const data = await baseQuery - .select('SUM(transactions.amount) as total_amount') - .addSelect('SUM(product_price.price) as total_modal') - .addSelect('SUM(product_price.mark_up_price) as total_profit') - .addSelect('COUNT(transactions.id) as total_transaction') - .getRawOne(); + .select('SUM(transactions.amount) as total_amount') + .addSelect('SUM(product_price.price) as total_modal') + .addSelect('SUM(product_price.mark_up_price) as total_profit') + .addSelect('COUNT(transactions.id) as total_transaction') + .getRawOne(); + + // const dataCoa = this.coaRepository + // .createQueryBuilder('coa') + // .innerJoin('user', 'user', 'coa.user = user.id') + // .where( + // `coa.type = '0' and user.partner_id is not NULL and user.is_active = true and is_rejected = false` + // ); + // + // const coa = await dataCoa + // .select('SUM(coa.amount) as total_modal') + // .getRawOne(); return { + total_modal: parseInt(data.total_modal), total_amount: parseInt(data.total_amount), total_transaction: parseInt(data.total_transaction), - total_modal: parseInt(data.total_modal), total_profit: parseInt(data.total_profit), }; }