223 lines
5.9 KiB
TypeScript
223 lines
5.9 KiB
TypeScript
import {
|
|
forwardRef,
|
|
HttpException,
|
|
HttpStatus,
|
|
Inject,
|
|
Injectable,
|
|
} from '@nestjs/common';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { Connection, EntityNotFoundError, Not, Repository } from 'typeorm';
|
|
import { Supplier } from '../entities/supplier.entity';
|
|
import { InputCoaDto } from '../../transaction/dto/input-coa.dto';
|
|
import { balanceType, coaType } from '../../helper/enum-list';
|
|
import { CreateSupplierDto } from '../dto/create-supplier.dto';
|
|
import { CoaService } from '../../transaction/coa.service';
|
|
import * as uuid from 'uuid';
|
|
import { UpdateSupplierDto } from '../dto/update-supplier.dto';
|
|
import { COA } from '../../transaction/entities/coa.entity';
|
|
|
|
@Injectable()
|
|
export class SupplierService {
|
|
constructor(
|
|
@InjectRepository(Supplier)
|
|
private supplierRepository: Repository<Supplier>,
|
|
@Inject(
|
|
forwardRef(() => {
|
|
return CoaService;
|
|
}),
|
|
)
|
|
private coaService: CoaService,
|
|
private connection: Connection,
|
|
) {}
|
|
|
|
async create(createSupplierDto: CreateSupplierDto) {
|
|
const check = await this.supplierRepository.findOne({
|
|
code: createSupplierDto.code,
|
|
});
|
|
|
|
if (check) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
|
error: 'Supplier Already Exist',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
const supplierData = new Supplier();
|
|
supplierData.id = uuid.v4();
|
|
supplierData.name = createSupplierDto.name;
|
|
supplierData.code = createSupplierDto.code;
|
|
supplierData.status = false;
|
|
|
|
await this.connection.transaction(async (manager) => {
|
|
const result = await manager.insert(Supplier, supplierData);
|
|
|
|
const dataCoaInventory = new InputCoaDto();
|
|
dataCoaInventory.supplier = supplierData;
|
|
dataCoaInventory.balanceType = balanceType.DEBIT;
|
|
dataCoaInventory.type = coaType.INVENTORY;
|
|
dataCoaInventory.coaEntityManager = manager;
|
|
await this.coaService.create(dataCoaInventory);
|
|
|
|
const dataCoaCostOfSales = new InputCoaDto();
|
|
dataCoaCostOfSales.supplier = supplierData;
|
|
dataCoaCostOfSales.balanceType = balanceType.DEBIT;
|
|
dataCoaCostOfSales.type = coaType.COST_OF_SALES;
|
|
dataCoaCostOfSales.coaEntityManager = manager;
|
|
await this.coaService.create(dataCoaCostOfSales);
|
|
|
|
const dataCoaBudget = new InputCoaDto();
|
|
dataCoaBudget.supplier = supplierData;
|
|
dataCoaBudget.balanceType = balanceType.DEBIT;
|
|
dataCoaBudget.type = coaType.BUDGET;
|
|
dataCoaBudget.coaEntityManager = manager;
|
|
await this.coaService.create(dataCoaBudget);
|
|
|
|
const dataCoaContraBudget = new InputCoaDto();
|
|
dataCoaContraBudget.supplier = supplierData;
|
|
dataCoaContraBudget.balanceType = balanceType.CREDIT;
|
|
dataCoaContraBudget.type = coaType.CONTRA_BUDGET;
|
|
dataCoaContraBudget.coaEntityManager = manager;
|
|
await this.coaService.create(dataCoaContraBudget);
|
|
});
|
|
|
|
return supplierData;
|
|
}
|
|
|
|
async update(id: string, updateSupplierDto: UpdateSupplierDto) {
|
|
const check = await this.supplierRepository.findOne({
|
|
code: updateSupplierDto.code,
|
|
id: Not(id),
|
|
});
|
|
|
|
if (check) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_ACCEPTABLE,
|
|
error: 'Supplier Already Exist',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
}
|
|
|
|
const supplierData = new Supplier();
|
|
|
|
supplierData.name = updateSupplierDto.name;
|
|
supplierData.status = true;
|
|
|
|
await this.connection.transaction(async (manager) => {
|
|
await manager.update(Supplier, { id: id }, supplierData);
|
|
});
|
|
|
|
return supplierData;
|
|
}
|
|
|
|
setStatus = async (id: string, type: string) => {
|
|
const supplierData = new Supplier();
|
|
|
|
if (type === 'active') {
|
|
supplierData.status = true;
|
|
} else {
|
|
supplierData.status = false;
|
|
}
|
|
|
|
await this.connection.transaction(async (manager) => {
|
|
await manager.update(Supplier, { id: id }, supplierData);
|
|
});
|
|
|
|
return supplierData;
|
|
};
|
|
|
|
async findAllSupplier(page) {
|
|
const baseQuery = this.supplierRepository
|
|
.createQueryBuilder('supplier')
|
|
.leftJoinAndMapOne(
|
|
'supplier.coa',
|
|
COA,
|
|
'coa',
|
|
`coa.supplier = supplier.id and coa.type = '2'`,
|
|
)
|
|
.leftJoinAndMapOne(
|
|
'supplier.coa_undistribute',
|
|
COA,
|
|
'coa_undistribute',
|
|
`coa_undistribute.supplier = supplier.id and coa_undistribute.type = '9'`,
|
|
)
|
|
.select(['supplier', 'coa.amount', 'coa_undistribute.amount']);
|
|
|
|
const data = await baseQuery
|
|
.skip(page * 10)
|
|
.take(10)
|
|
.getMany();
|
|
|
|
const totalData = await baseQuery.getCount();
|
|
|
|
return {
|
|
data,
|
|
count: totalData,
|
|
};
|
|
}
|
|
|
|
async findByCode(code: string) {
|
|
try {
|
|
return await this.supplierRepository.findOneOrFail({
|
|
code: code,
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Supplier not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
async findByActive() {
|
|
try {
|
|
return await this.supplierRepository.findOneOrFail({
|
|
status: true,
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Supplier Data not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
|
|
async findByActiveAll() {
|
|
try {
|
|
return await this.supplierRepository.find({
|
|
status: true,
|
|
});
|
|
} catch (e) {
|
|
if (e instanceof EntityNotFoundError) {
|
|
throw new HttpException(
|
|
{
|
|
statusCode: HttpStatus.NOT_FOUND,
|
|
error: 'Supplier Data not found',
|
|
},
|
|
HttpStatus.NOT_FOUND,
|
|
);
|
|
} else {
|
|
throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|