fix: add saldo member
This commit is contained in:
parent
8f5b3588d2
commit
e7db0073c5
|
@ -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(
|
||||
id: string,
|
||||
relatedId: string,
|
||||
|
|
|
@ -3,7 +3,12 @@ import { DistributeTransactionDto } from './dto/distribute-transaction.dto';
|
|||
import { OrderTransactionDto } from './dto/order-transaction.dto';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
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 { TransactionJournal } from './entities/transaction-journal.entity';
|
||||
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 { ProductHistoryPriceService } from '../product/history-price/history-price.service';
|
||||
import { CommissionService } from '../configurable/commission.service';
|
||||
import { catchError } from 'rxjs';
|
||||
|
||||
interface JournalEntry {
|
||||
coa_id: string;
|
||||
|
@ -134,65 +140,70 @@ export class TransactionService {
|
|||
);
|
||||
}
|
||||
|
||||
//GET Supplier
|
||||
const supplier = await this.supplierService.findByCode(
|
||||
distributeTransactionDto.supplier,
|
||||
);
|
||||
try {
|
||||
//GET Supplier
|
||||
const supplier = await this.supplierService.findByCode(
|
||||
distributeTransactionDto.supplier,
|
||||
);
|
||||
|
||||
// GET COA
|
||||
const coaAR = await this.coaService.findByUser(
|
||||
distributeTransactionDto.destination,
|
||||
coaType.ACCOUNT_RECEIVABLE,
|
||||
);
|
||||
const coaWallet = await this.coaService.findByUser(
|
||||
distributeTransactionDto.destination,
|
||||
coaType.WALLET,
|
||||
);
|
||||
const coaBudget = await this.coaService.findByName(
|
||||
`${coaType[coaType.BUDGET]}-${supplier.code}`,
|
||||
);
|
||||
// GET COA
|
||||
const coaAR = await this.coaService.findByTwoUser(
|
||||
distributeTransactionDto.destination,
|
||||
currentUser.userId,
|
||||
coaType.ACCOUNT_RECEIVABLE,
|
||||
);
|
||||
const coaWallet = await this.coaService.findByUser(
|
||||
distributeTransactionDto.destination,
|
||||
coaType.WALLET,
|
||||
);
|
||||
const coaBudget = await this.coaService.findByName(
|
||||
`${coaType[coaType.BUDGET]}-${supplier.code}`,
|
||||
);
|
||||
|
||||
const coaContraBudget = await this.coaService.findByName(
|
||||
`${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`,
|
||||
);
|
||||
const coaContraBudget = await this.coaService.findByName(
|
||||
`${coaType[coaType.CONTRA_BUDGET]}-${supplier.code}`,
|
||||
);
|
||||
|
||||
await this.connection.transaction(async (manager) => {
|
||||
//INSERT TRANSACTION
|
||||
const transactionData = new Transactions();
|
||||
await this.connection.transaction(async (manager) => {
|
||||
//INSERT TRANSACTION
|
||||
const transactionData = new Transactions();
|
||||
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = distributeTransactionDto.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.DISTRIBUTION;
|
||||
transactionData.id = uuid.v4();
|
||||
transactionData.amount = distributeTransactionDto.amount;
|
||||
transactionData.user = userData.id;
|
||||
transactionData.status = statusTransaction.SUCCESS;
|
||||
transactionData.type = typeTransaction.DISTRIBUTION;
|
||||
|
||||
await manager.insert(Transactions, transactionData);
|
||||
await manager.insert(Transactions, transactionData);
|
||||
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaAR.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaWallet.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaBudget.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaContraBudget.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
await this.accountingTransaction({
|
||||
createTransaction: false,
|
||||
transactionalEntityManager: manager,
|
||||
transaction: transactionData,
|
||||
amount: transactionData.amount,
|
||||
journals: [
|
||||
{
|
||||
coa_id: coaAR.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaWallet.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaBudget.id,
|
||||
credit: transactionData.amount,
|
||||
},
|
||||
{
|
||||
coa_id: coaContraBudget.id,
|
||||
debit: transactionData.amount,
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
throw e;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user