fix: add saldo member

This commit is contained in:
ilham 2021-12-17 14:53:42 +07:00
parent 8f5b3588d2
commit e7db0073c5
2 changed files with 86 additions and 53 deletions

View File

@ -76,6 +76,28 @@ export class CoaService {
} }
} }
async findByTwoUser(from: string, destination: string, typeOfCoa: coaType) {
try {
return await this.coaRepository.findOneOrFail({
user: from,
relatedUser: destination,
type: typeOfCoa,
});
} catch (e) {
if (e instanceof EntityNotFoundError) {
throw new HttpException(
{
statusCode: HttpStatus.NOT_FOUND,
error: 'COA not found',
},
HttpStatus.NOT_FOUND,
);
} else {
throw e;
}
}
}
async findByUserWithRelated( async findByUserWithRelated(
id: string, id: string,
relatedId: string, relatedId: string,

View File

@ -3,7 +3,12 @@ import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
import { OrderTransactionDto } from './dto/order-transaction.dto'; import { OrderTransactionDto } from './dto/order-transaction.dto';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Transactions } from './entities/transactions.entity'; import { Transactions } from './entities/transactions.entity';
import { Connection, EntityManager, Repository } from 'typeorm'; import {
Connection,
EntityManager,
EntityNotFoundError,
Repository,
} from 'typeorm';
import { COA } from './entities/coa.entity'; import { COA } from './entities/coa.entity';
import { TransactionJournal } from './entities/transaction-journal.entity'; import { TransactionJournal } from './entities/transaction-journal.entity';
import { CoaService } from './coa.service'; import { CoaService } from './coa.service';
@ -23,6 +28,7 @@ import { AddSaldoSupplier } from './dto/add-saldo-supplier.dto';
import { SupplierService } from '../users/supplier/supplier.service'; import { SupplierService } from '../users/supplier/supplier.service';
import { ProductHistoryPriceService } from '../product/history-price/history-price.service'; import { ProductHistoryPriceService } from '../product/history-price/history-price.service';
import { CommissionService } from '../configurable/commission.service'; import { CommissionService } from '../configurable/commission.service';
import { catchError } from 'rxjs';
interface JournalEntry { interface JournalEntry {
coa_id: string; coa_id: string;
@ -134,65 +140,70 @@ export class TransactionService {
); );
} }
//GET Supplier try {
const supplier = await this.supplierService.findByCode( //GET Supplier
distributeTransactionDto.supplier, const supplier = await this.supplierService.findByCode(
); distributeTransactionDto.supplier,
);
// GET COA // GET COA
const coaAR = await this.coaService.findByUser( const coaAR = await this.coaService.findByTwoUser(
distributeTransactionDto.destination, distributeTransactionDto.destination,
coaType.ACCOUNT_RECEIVABLE, currentUser.userId,
); coaType.ACCOUNT_RECEIVABLE,
const coaWallet = await this.coaService.findByUser( );
distributeTransactionDto.destination, const coaWallet = await this.coaService.findByUser(
coaType.WALLET, distributeTransactionDto.destination,
); coaType.WALLET,
const coaBudget = await this.coaService.findByName( );
`${coaType[coaType.BUDGET]}-${supplier.code}`, const coaBudget = await this.coaService.findByName(
); `${coaType[coaType.BUDGET]}-${supplier.code}`,
);
const coaContraBudget = await this.coaService.findByName( const coaContraBudget = await this.coaService.findByName(
`${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`, `${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`,
); );
await this.connection.transaction(async (manager) => { await this.connection.transaction(async (manager) => {
//INSERT TRANSACTION //INSERT TRANSACTION
const transactionData = new Transactions(); const transactionData = new Transactions();
transactionData.id = uuid.v4(); transactionData.id = uuid.v4();
transactionData.amount = distributeTransactionDto.amount; transactionData.amount = distributeTransactionDto.amount;
transactionData.user = userData.id; transactionData.user = userData.id;
transactionData.status = statusTransaction.SUCCESS; transactionData.status = statusTransaction.SUCCESS;
transactionData.type = typeTransaction.DISTRIBUTION; transactionData.type = typeTransaction.DISTRIBUTION;
await manager.insert(Transactions, transactionData); await manager.insert(Transactions, transactionData);
await this.accountingTransaction({ await this.accountingTransaction({
createTransaction: false, createTransaction: false,
transactionalEntityManager: manager, transactionalEntityManager: manager,
transaction: transactionData, transaction: transactionData,
amount: transactionData.amount, amount: transactionData.amount,
journals: [ journals: [
{ {
coa_id: coaAR.id, coa_id: coaAR.id,
debit: transactionData.amount, debit: transactionData.amount,
}, },
{ {
coa_id: coaWallet.id, coa_id: coaWallet.id,
credit: transactionData.amount, credit: transactionData.amount,
}, },
{ {
coa_id: coaBudget.id, coa_id: coaBudget.id,
credit: transactionData.amount, credit: transactionData.amount,
}, },
{ {
coa_id: coaContraBudget.id, coa_id: coaContraBudget.id,
debit: transactionData.amount, debit: transactionData.amount,
}, },
], ],
});
}); });
}); } catch (e) {
throw e;
}
return true; return true;
} }