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()
price: number;
@Column({
nullable:true
})
basePrice: number;
@ManyToOne(
() => ProductSubCategories,
(subCategories) => subCategories.category,

View File

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

View File

@ -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);
}
}

View File

@ -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;
}