fix: distribute saldo partner from admin

This commit is contained in:
ilham 2021-12-12 01:01:37 +07:00
parent ed5ca6556d
commit 78470a0e3f
4 changed files with 45 additions and 10 deletions

View File

@ -6,4 +6,7 @@ export class DistributeTransactionDto {
@IsNotEmpty()
destination: string;
@IsNotEmpty()
supplier: string;
}

View File

@ -19,15 +19,15 @@ export class TransactionJournal extends BaseModel {
@Column()
amount: number;
@OneToOne(
@ManyToOne(
() => {
return Transactions;
},
(trans) => {
return trans.id;
(transaction) => {
return transaction.id;
},
)
transaction: Transactions;
transaction_head: Transactions;
@ManyToOne(
() => {

View File

@ -116,6 +116,25 @@ export class TransactionService {
distributeTransactionDto: DistributeTransactionDto,
currentUser: any,
) {
//GET USER
const userData = await this.userService.findByUsername(
currentUser.username,
);
if (userData.roles.name != 'Admin') {
throw new HttpException(
{
statusCode: HttpStatus.NOT_ACCEPTABLE,
error: 'Roles Not Admin',
},
HttpStatus.NOT_ACCEPTABLE,
);
}
//GET Supplier
const supplier = await this.supplierService.findByCode(
distributeTransactionDto.supplier,
);
// GET COA
const coaAR = await this.coaService.findByUser(
distributeTransactionDto.destination,
@ -125,10 +144,12 @@ export class TransactionService {
distributeTransactionDto.destination,
coaType.WALLET,
);
const coaBudget = await this.coaService.findByName(
`${coaType[coaType.BUDGET]}-${supplier.code}`,
);
//GET USER
const userData = await this.userService.findByUsername(
currentUser.username,
const coaContraBudget = await this.coaService.findByName(
`${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`,
);
await this.connection.transaction(async (manager) => {
@ -157,6 +178,14 @@ export class TransactionService {
coa_id: coaWallet.id,
credit: transactionData.amount,
},
{
coa_id: coaBudget.id,
credit: transactionData.amount,
},
{
coa_id: coaContraBudget.id,
debit: transactionData.amount,
},
],
});
});
@ -412,9 +441,9 @@ export class TransactionService {
? balanceType.DEBIT
: balanceType.CREDIT;
journalEntry.amount = journal.debit ? journal.debit : journal.credit;
journalEntry.transaction = transaction;
journalEntry.transaction_head = transaction;
return this.transactionJournalRepository.save(journalEntry);
return createJournalDto.transactionalEntityManager.save(journalEntry);
}),
);

View File

@ -157,7 +157,10 @@ export class UsersService {
async findByUsername(username: string) {
try {
return await this.usersRepository.findOneOrFail({
username: username,
where: {
username: username,
},
relations: ['roles'],
});
} catch (e) {
if (e instanceof EntityNotFoundError) {