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,16 +194,58 @@ 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,46 +258,43 @@ export class TransactionService {
} }
try { try {
const orderIRS = await irsService.createTransaction( await this.connection.transaction(async (manager) => {
orderTransactionDto.productCode, let transactionData = new Transactions();
orderTransactionDto.destination, transactionData.id = uuid.v4();
); transactionData.amount = product.price,
transactionData.user = userData.id,
if (orderIRS.success) { transactionData.status = statusTransaction.SUCCESS,
await this.connection.transaction(async (manager) => { transactionData.type = typeTransaction.DISTRIBUTION,
//INSERT TRANSACTION
const transactionSaved = await manager.insert(Transactions, { await manager.insert(Transactions, transactionData);
amount: product.price,
user: 'id_user', await this.accountingTransaction({
status: statusTransaction.SUCCESS, createTransaction: false,
type: typeTransaction.ORDER, transactionalEntityManager:manager,
}); transaction: transactionData,
amount: transactionData.amount,
//INSERT TRANSACTION JOURNAL journals: [{
const journalSender = await manager.insert(TransactionJournal, { coa_id: coaInventory.id,
amount: product.price, credit: product.basePrice
transaction: transactionSaved.identifiers[0], }, {
coa: coaAccount, coa_id: coaCostOfSales.id,
type: balanceType.CREDIT, debit: product.basePrice
}); }, {
coa_id: coaAccount.id,
//UPDATE AMOUNT COA debit: product.price
coaAccount.amount = coaAccount.amount - product.price; }, {
await manager.save(coaAccount); 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;
} }
return true; return true;
} }