diff --git a/src/product/dto/product/update-price-product.dto.ts b/src/product/dto/product/update-price-product.dto.ts index 9a51190..b4ec5b3 100644 --- a/src/product/dto/product/update-price-product.dto.ts +++ b/src/product/dto/product/update-price-product.dto.ts @@ -11,6 +11,9 @@ export class UpdatePriceProductDto { @IsNotEmpty() type: productType; + @IsNotEmpty() + productType: string; + startDate: Date; endDate: Date; diff --git a/src/product/entities/product.entity.ts b/src/product/entities/product.entity.ts index 1a9c739..2b2f92b 100644 --- a/src/product/entities/product.entity.ts +++ b/src/product/entities/product.entity.ts @@ -28,6 +28,11 @@ export class Product extends BaseModel { }) basePrice: number; + @Column({ + default: 'prepaid', + }) + type: string; + @ManyToOne( () => { return ProductSubCategories; diff --git a/src/product/product.service.ts b/src/product/product.service.ts index 559e25c..1afb8a0 100644 --- a/src/product/product.service.ts +++ b/src/product/product.service.ts @@ -124,6 +124,7 @@ export class ProductService { status: it[5] == 'active' ? 'ACTIVE' : 'NOT ACTIVE', sub_categories: subCategories, supplier: supplierData, + type: it[7] == 'postpaid' ? 'postpaid' : 'prepaid', }); return await this.productHistoryPrice.insert({ @@ -355,12 +356,13 @@ export class ProductService { }; } - async findOne(code: string) { + async findOne(code: string, type: string) { try { return await this.productRepository.findOneOrFail({ relations: ['supplier'], where: { code: code, + type: type, }, }); } catch (e) { @@ -436,7 +438,7 @@ export class ProductService { code: string, updatePriceProductDto: UpdatePriceProductDto, ) { - const product = await this.findOne(code); + const product = await this.findOne(code, updatePriceProductDto.productType); await this.productHistoryPrice.insert({ product: product, diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index fa0946e..22e53d7 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -87,6 +87,21 @@ export class TransactionController { }; } + @Post('order-bill-prod') + async orderTransactioBillnProd( + @Body() orderTransactionDto: OrderTransactionDto, + @Request() req, + ) { + return { + data: await this.transactionService.orderTransactionBillProd( + orderTransactionDto, + req.user, + ), + statusCode: HttpStatus.CREATED, + message: 'success', + }; + } + @Post('check-bill') async checkBill( @Body() orderTransactionDto: OrderTransactionDto, diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 056c23b..74226d2 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -312,6 +312,7 @@ export class TransactionService { //GET PRODUCT const product = await this.productService.findOne( orderTransactionDto.productCode, + 'prepaid' ); const product_price = await this.productHistoryPriceService.findOne( @@ -446,6 +447,7 @@ export class TransactionService { //GET PRODUCT AND PRICE const product = await this.productService.findOne( orderTransactionDto.productCode, + 'prepaid', ); const supplier = await this.supplierService.findByCode( @@ -620,6 +622,174 @@ export class TransactionService { }; } + async orderTransactionBillProd( + orderTransactionDto: OrderTransactionDto, + currentUser: any, + ) { + let status; + const amount = 0; + //GET USER DATA + const userData = await this.userService.findByUsername( + currentUser.username, + ); + + //GET PRODUCT AND PRICE + const product = await this.productService.findOne( + orderTransactionDto.productCode, + 'postpaid', + ); + + const supplier = await this.supplierService.findByCode( + product.supplier.code, + ); + + let product_price = await this.productHistoryPriceService.findOne( + product.id, + userData.partner?.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`, + ); + + 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, + ); + } + + //HIT API SUPPLIER + const trxId = Array(6) + .fill(null) + .map(() => { + return Math.round(Math.random() * 16).toString(16); + }) + .join(''); + + let hitSupplier = await doTransaction( + orderTransactionDto.productCode, + orderTransactionDto.destination, + trxId, + supplier, + ); + + if (supplier.code != 'IRS') { + const parsingResponse = hitSupplier.split(' '); + hitSupplier = { + success: hitSupplier.include('diproses'), + harga: parseInt( + parsingResponse[parsingResponse.length - 2].replaceAll('.', ''), + ), + msg: hitSupplier, + }; + } + // const hitSupplier = { + // harga: 2000, + // success: true, + // msg: 'Berhasil', + // }; + + this.logger.log({ + responseAPISupplier: hitSupplier, + }); + + let costInventory = product_price.price; + + if (hitSupplier.harga != product_price.price) { + product_price.endDate = new Date(); + costInventory = hitSupplier.harga; + } + + 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: costInventory, + }, + { + coa_id: coaCostOfSales.id, + debit: costInventory, + }, + { + coa_id: coaAccount.id, + debit: product_price.mark_up_price + costInventory, + }, + { + // eslint-disable-next-line camelcase + coa_id: coaSales.id, + credit: product_price.mark_up_price + costInventory, + }, + ], + }); + }); + } 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: status, + }; + } + async checkBill(orderTransactionDto: OrderTransactionDto, currentUser: any) { //GET USER DATA const userData = await this.userService.findByUsername( @@ -629,6 +799,7 @@ export class TransactionService { //GET PRODUCT AND PRICE const product = await this.productService.findOne( orderTransactionDto.productCode, + 'postpaid', ); const supplier = await this.supplierService.findByCode(