diff --git a/src/product/entities/product.entity.ts b/src/product/entities/product.entity.ts index a139fe0..2a3cef8 100644 --- a/src/product/entities/product.entity.ts +++ b/src/product/entities/product.entity.ts @@ -29,6 +29,11 @@ export class Product extends BaseModel{ @Column() price: number; + @Column({ + nullable:true + }) + basePrice: number; + @ManyToOne( () => ProductSubCategories, (subCategories) => subCategories.category, diff --git a/src/transaction/dto/order-transaction.dto.ts b/src/transaction/dto/order-transaction.dto.ts index 2081cba..462682c 100644 --- a/src/transaction/dto/order-transaction.dto.ts +++ b/src/transaction/dto/order-transaction.dto.ts @@ -3,7 +3,4 @@ import { IsNotEmpty, IsUUID } from 'class-validator'; export class OrderTransactionDto { @IsNotEmpty() productCode: string; - - @IsNotEmpty() - destination: string; } diff --git a/src/transaction/transaction.controller.ts b/src/transaction/transaction.controller.ts index 801fcc7..af080c3 100644 --- a/src/transaction/transaction.controller.ts +++ b/src/transaction/transaction.controller.ts @@ -57,7 +57,10 @@ export class TransactionController { @Post('order') - orderTransaction(@Body() orderTransactionDto: OrderTransactionDto) { - return this.transactionService.orderTransaction(orderTransactionDto); + orderTransaction( + @Body() orderTransactionDto: OrderTransactionDto, + @Request() req + ) { + return this.transactionService.orderTransaction(orderTransactionDto,req.user); } } diff --git a/src/transaction/transaction.service.ts b/src/transaction/transaction.service.ts index 0313d0f..e5b211b 100644 --- a/src/transaction/transaction.service.ts +++ b/src/transaction/transaction.service.ts @@ -55,7 +55,6 @@ export class TransactionService { const coaInventory = await this.coaService.findByName( coaType[coaType.INVENTORY]+'-'+addSaldoSupplier.supplier, ); - console.log(coaInventory); //GET USER const userData = await this.userService.findByUsername(currentUser.username); @@ -195,16 +194,58 @@ export class TransactionService { return true; } - async orderTransaction(orderTransactionDto: OrderTransactionDto) { - const coaAccount = await this.coaService.findByUser( - 'id_user', - coaType.WALLET, - ); - + async orderTransaction(orderTransactionDto: OrderTransactionDto,currentUser:any) { //GET PRODUCT const product = await this.productService.findOne( orderTransactionDto.productCode, ); + + //GET USER + const userData = await this.userService.findByUsername(currentUser.username); + let supervisorData = []; + if(userData.superior != null){ + supervisorData.push(await this.userService.findByUsername(currentUser.username)); + if(supervisorData[0].superior != null){ + supervisorData.push(await this.userService.findByUsername(currentUser.username)); + if(supervisorData[0].superior != null){ + supervisorData.push(await this.userService.findByUsername(currentUser.username)); + } + } + } + + //GET COA + const coaAccount = await this.coaService.findByUser( + userData.id, + coaType.WALLET, + ); + + const coaInventory = await this.coaService.findByName( + coaType[coaType.INVENTORY]+'-IRS', + ); + + const coaCostOfSales = await this.coaService.findByName( + coaType[coaType.COST_OF_SALES]+'-SYSTEM', + ); + + const coaSales = await this.coaService.findByName( + coaType[coaType.SALES]+'-SYSTEM', + ); + + const coaExpense = await this.coaService.findByName( + coaType[coaType.EXPENSE]+'-SYSTEM', + ); + + supervisorData = supervisorData.map(async it =>{ + const coaAccount = await this.coaService.findByUser( + it.id, + coaType.WALLET, + ); + + return { + coa_id: coaAccount.id, + credit: 0 + } + }) if (coaAccount.amount <= product.price) { throw new HttpException( @@ -217,46 +258,43 @@ export class TransactionService { } try { - const orderIRS = await irsService.createTransaction( - orderTransactionDto.productCode, - orderTransactionDto.destination, - ); - - if (orderIRS.success) { - await this.connection.transaction(async (manager) => { - //INSERT TRANSACTION - const transactionSaved = await manager.insert(Transactions, { - amount: product.price, - user: 'id_user', - status: statusTransaction.SUCCESS, - type: typeTransaction.ORDER, - }); - - //INSERT TRANSACTION JOURNAL - const journalSender = await manager.insert(TransactionJournal, { - amount: product.price, - transaction: transactionSaved.identifiers[0], - coa: coaAccount, - type: balanceType.CREDIT, - }); - - //UPDATE AMOUNT COA - coaAccount.amount = coaAccount.amount - product.price; - await manager.save(coaAccount); + await this.connection.transaction(async (manager) => { + let transactionData = new Transactions(); + transactionData.id = uuid.v4(); + transactionData.amount = product.price, + transactionData.user = userData.id, + transactionData.status = statusTransaction.SUCCESS, + transactionData.type = typeTransaction.DISTRIBUTION, + + await manager.insert(Transactions, transactionData); + + await this.accountingTransaction({ + createTransaction: false, + transactionalEntityManager:manager, + transaction: transactionData, + amount: transactionData.amount, + journals: [{ + coa_id: coaInventory.id, + credit: product.basePrice + }, { + coa_id: coaCostOfSales.id, + debit: product.basePrice + }, { + coa_id: coaAccount.id, + debit: product.price + }, { + coa_id: coaSales.id, + credit: product.price + },{ + coa_id: coaExpense.id, + credit: 0 + }].concat(supervisorData) }); - } else { - throw new HttpException( - { - statusCode: HttpStatus.INTERNAL_SERVER_ERROR, - error: `Transaction Failed because ${orderIRS.msg}`, - }, - HttpStatus.INTERNAL_SERVER_ERROR, - ); - } + }); } catch (e) { throw e; } - + return true; }