diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 5367dec..4a2222e 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -299,140 +299,157 @@ 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(