add unfinish order transaction

This commit is contained in:
Ilham Dwi Pratama S 2021-12-09 17:02:05 +07:00
parent 78a2cadb5e
commit 847f6f45d6
4 changed files with 91 additions and 48 deletions

View File

@ -29,6 +29,11 @@ export class Product extends BaseModel{
@Column() @Column()
price: number; price: number;
@Column({
nullable:true
})
basePrice: number;
@ManyToOne( @ManyToOne(
() => ProductSubCategories, () => ProductSubCategories,
(subCategories) => subCategories.category, (subCategories) => subCategories.category,

View File

@ -3,7 +3,4 @@ import { IsNotEmpty, IsUUID } from 'class-validator';
export class OrderTransactionDto { export class OrderTransactionDto {
@IsNotEmpty() @IsNotEmpty()
productCode: string; productCode: string;
@IsNotEmpty()
destination: string;
} }

View File

@ -57,7 +57,10 @@ export class TransactionController {
@Post('order') @Post('order')
orderTransaction(@Body() orderTransactionDto: OrderTransactionDto) { orderTransaction(
return this.transactionService.orderTransaction(orderTransactionDto); @Body() orderTransactionDto: OrderTransactionDto,
@Request() req
) {
return this.transactionService.orderTransaction(orderTransactionDto,req.user);
} }
} }

View File

@ -55,7 +55,6 @@ export class TransactionService {
const coaInventory = await this.coaService.findByName( const coaInventory = await this.coaService.findByName(
coaType[coaType.INVENTORY]+'-'+addSaldoSupplier.supplier, coaType[coaType.INVENTORY]+'-'+addSaldoSupplier.supplier,
); );
console.log(coaInventory);
//GET USER //GET USER
const userData = await this.userService.findByUsername(currentUser.username); const userData = await this.userService.findByUsername(currentUser.username);
@ -195,17 +194,59 @@ export class TransactionService {
return true; return true;
} }
async orderTransaction(orderTransactionDto: OrderTransactionDto) { async orderTransaction(orderTransactionDto: OrderTransactionDto,currentUser:any) {
const coaAccount = await this.coaService.findByUser(
'id_user',
coaType.WALLET,
);
//GET PRODUCT //GET PRODUCT
const product = await this.productService.findOne( const product = await this.productService.findOne(
orderTransactionDto.productCode, 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) { if (coaAccount.amount <= product.price) {
throw new HttpException( throw new HttpException(
{ {
@ -217,42 +258,39 @@ export class TransactionService {
} }
try { try {
const orderIRS = await irsService.createTransaction(
orderTransactionDto.productCode,
orderTransactionDto.destination,
);
if (orderIRS.success) {
await this.connection.transaction(async (manager) => { await this.connection.transaction(async (manager) => {
//INSERT TRANSACTION let transactionData = new Transactions();
const transactionSaved = await manager.insert(Transactions, { transactionData.id = uuid.v4();
amount: product.price, transactionData.amount = product.price,
user: 'id_user', transactionData.user = userData.id,
status: statusTransaction.SUCCESS, transactionData.status = statusTransaction.SUCCESS,
type: typeTransaction.ORDER, transactionData.type = typeTransaction.DISTRIBUTION,
});
//INSERT TRANSACTION JOURNAL await manager.insert(Transactions, transactionData);
const journalSender = await manager.insert(TransactionJournal, {
amount: product.price,
transaction: transactionSaved.identifiers[0],
coa: coaAccount,
type: balanceType.CREDIT,
});
//UPDATE AMOUNT COA await this.accountingTransaction({
coaAccount.amount = coaAccount.amount - product.price; createTransaction: false,
await manager.save(coaAccount); 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) { } catch (e) {
throw e; throw e;
} }