143 lines
3.5 KiB
TypeScript
143 lines
3.5 KiB
TypeScript
import { forwardRef, HttpException, HttpStatus, Inject } from '@nestjs/common';
|
|
import { EntityNotFoundError, Repository } from 'typeorm';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { COA } from './entities/coa.entity';
|
|
import { coaType } from '../helper/enum-list';
|
|
import { InputCoaDto } from './dto/input-coa.dto';
|
|
import { UsersService } from 'src/users/users.service';
|
|
|
|
export class CoaService {
|
|
constructor(
|
|
@InjectRepository(COA)
|
|
private coaRepository: Repository<COA>,
|
|
@Inject(
|
|
forwardRef(() => {
|
|
return UsersService;
|
|
}),
|
|
)
|
|
private userService: UsersService,
|
|
) {}
|
|
|
|
async create(inputCoaDto: InputCoaDto) {
|
|
const coaData = new COA();
|
|
let name = '';
|
|
if (inputCoaDto.user) {
|
|
coaData.user = inputCoaDto.user.id;
|
|
name = inputCoaDto.user.username;
|
|
}
|
|
|
|
if (inputCoaDto.supplier) {
|
|
coaData.supplier = inputCoaDto.supplier.id;
|
|
name = inputCoaDto.supplier.code;
|
|
}
|
|
|
|
coaData.name = `${coaType[inputCoaDto.type]}-${name}`;
|
|
coaData.balanceType = inputCoaDto.balanceType;
|
|
coaData.type = inputCoaDto.type;
|
|
coaData.amount = 0;
|
|
|
|
const result = await inputCoaDto.coaEntityManager.insert(COA, coaData);
|
|
|
|
if (
|
|
inputCoaDto.type == coaType.ACCOUNT_RECEIVABLE ||
|
|
inputCoaDto.type == coaType.ACCOUNT_PAYABLE
|
|
) {
|
|
coaData.relatedUser = inputCoaDto.relatedUserId;
|
|
await inputCoaDto.coaEntityManager.save(coaData);
|
|
}
|
|
|
|
return coaData;
|
|
}
|
|
|
|
async findByUser(id: string, typeOfCoa: coaType) {
|
|
console.log('findbyuser', 'msk')
|
|
try {
|
|
return await this.coaRepository.findOneOrFail({
|
|
user: id,
|
|
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 findByTwoUser(from: string, destination: string, typeOfCoa: coaType) {
|
|
try {
|
|
console.log('findbytwouser', 'msk')
|
|
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,
|
|
typeOfCoa: coaType,
|
|
) {
|
|
try {
|
|
return await this.coaRepository.findOneOrFail({
|
|
user: id,
|
|
type: typeOfCoa,
|
|
relatedUser: relatedId,
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Coa Data not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
async findByName(name: string) {
|
|
try {
|
|
console.log('coaname', name);
|
|
return await this.coaRepository.findOneOrFail({ name: name });
|
|
} catch (e) {
|
|
console.log('coaerror', e);
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'COA Data not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|