228 lines
7.9 KiB
TypeScript
228 lines
7.9 KiB
TypeScript
|
|
/*
|
||
|
|
https://docs.nestjs.com/providers#services
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { Estprotocoloentrega } from 'src/domain/entity/tables/estprotocolo.entity';
|
||
|
|
import { DeliveryOrderModel } from 'src/domain/models/delivery-order.model';
|
||
|
|
import { ImageOrderModel } from 'src/domain/models/image-order.model';
|
||
|
|
import { PaymentModel } from 'src/domain/models/payment.model';
|
||
|
|
import { ResultModel } from 'src/domain/models/result.model';
|
||
|
|
import { getConnection } from 'typeorm';
|
||
|
|
import { Estimagemnota } from '../../domain/entity/tables/estimagemnota.entity';
|
||
|
|
import { Estpagamento } from '../../domain/entity/tables/estpagamento.entity';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class DeliveryOrderService {
|
||
|
|
|
||
|
|
async getDataDelivery(shipmentId: number, customerId: number): Promise<any> {
|
||
|
|
const connection = getConnection();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
try {
|
||
|
|
const result = await queryRunner.manager
|
||
|
|
.getRepository(Estprotocoloentrega)
|
||
|
|
.createQueryBuilder('estprotocoloentrega')
|
||
|
|
.where('numcar = :numcar and codcli = :codcli', { numcar: shipmentId, codcli: customerId})
|
||
|
|
.getOne();
|
||
|
|
|
||
|
|
return result; //new ResultModel(true, 'Registro localizado com sucesso!', result, {});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.log(error);
|
||
|
|
return new ResultModel(false,
|
||
|
|
'Erro ao atualizar dados de entrega.',
|
||
|
|
null,
|
||
|
|
error);
|
||
|
|
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
async createOrReplaceDeliveryOrder(data: DeliveryOrderModel): Promise<any> {
|
||
|
|
const connection = getConnection();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
await queryRunner.startTransaction();
|
||
|
|
console.log(data);
|
||
|
|
try {
|
||
|
|
const updateDelivery = await queryRunner.manager
|
||
|
|
.getRepository(Estprotocoloentrega)
|
||
|
|
.createQueryBuilder('estprotocoloentrega')
|
||
|
|
.where('numcar = :numcar and codcli = :codcli',
|
||
|
|
{ numcar: data.numeroCarregamento, codcli: data.codigoCliente})
|
||
|
|
.getOne();
|
||
|
|
if ( updateDelivery ) {
|
||
|
|
await queryRunner.manager
|
||
|
|
.createQueryBuilder()
|
||
|
|
.update(Estprotocoloentrega)
|
||
|
|
.set({
|
||
|
|
dataEntrega: data.dataEntrega,
|
||
|
|
cpfRecebedor: data.cpfRecebedor,
|
||
|
|
nomeRecebedor: data.nomeRecebedor,
|
||
|
|
urlImagemProtocolo: data.urlImagemProtocolo,
|
||
|
|
latitude: data.latitude,
|
||
|
|
longitude: data.longitude,
|
||
|
|
})
|
||
|
|
.where('numcar = :numcar and codcli = :codcli', { numcar: data.numeroCarregamento, codcli: data.codigoCliente})
|
||
|
|
.execute()
|
||
|
|
} else {
|
||
|
|
await queryRunner.manager
|
||
|
|
.createQueryBuilder()
|
||
|
|
.insert()
|
||
|
|
.into(Estprotocoloentrega)
|
||
|
|
.values(data)
|
||
|
|
.execute()
|
||
|
|
}
|
||
|
|
|
||
|
|
await queryRunner.commitTransaction();
|
||
|
|
return new ResultModel(true, 'Registro atualizado com sucesso!', data, {});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
await queryRunner.rollbackTransaction();
|
||
|
|
console.log(error);
|
||
|
|
return new ResultModel(false,
|
||
|
|
'Erro ao atualizar dados de entrega.',
|
||
|
|
null,
|
||
|
|
error);
|
||
|
|
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
async getImageOrder(shipmentId: number, orderId: number): Promise<any> {
|
||
|
|
const connection = getConnection();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
try {
|
||
|
|
const result = await queryRunner.manager
|
||
|
|
.getRepository(Estimagemnota)
|
||
|
|
.createQueryBuilder('estimagemnota')
|
||
|
|
.select(["numnota as \"numeroNota\"", "numped as \"numeroPedido\", numcar as \"numeroCarregamento\""])
|
||
|
|
.addSelect(["tipo as \"tipo\", data as \"data\", url as \"url\", latitude as \"latitude\", longitude as \"longitude\" "])
|
||
|
|
.where('numcar = :numcar and numped = :numped ', { numcar: shipmentId, numped: orderId})
|
||
|
|
.getRawMany();
|
||
|
|
|
||
|
|
return result;
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
console.log(error);
|
||
|
|
return new ResultModel(false,
|
||
|
|
'Erro ao atualizar imagens da nota fiscal.',
|
||
|
|
null,
|
||
|
|
error);
|
||
|
|
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async createImageOrder(dataImage: ImageOrderModel): Promise<any> {
|
||
|
|
console.log(dataImage);
|
||
|
|
const connection = getConnection();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
await queryRunner.startTransaction();
|
||
|
|
try {
|
||
|
|
//const dateDelivery = '2022-04-14T17:52:00';
|
||
|
|
//dataImage.data = dateDelivery;
|
||
|
|
//console.log(dataImage);
|
||
|
|
await queryRunner.manager
|
||
|
|
.createQueryBuilder()
|
||
|
|
.insert()
|
||
|
|
.into(Estimagemnota)
|
||
|
|
.values(dataImage)
|
||
|
|
.execute()
|
||
|
|
|
||
|
|
await queryRunner.commitTransaction();
|
||
|
|
return new ResultModel(true, 'Imagens incluídas com sucesso!', dataImage, {});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
await queryRunner.rollbackTransaction();
|
||
|
|
console.log(error);
|
||
|
|
return new ResultModel(false,
|
||
|
|
'Erro ao incluir imagens da nota fiscal.',
|
||
|
|
null,
|
||
|
|
error);
|
||
|
|
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
async createPayment(payment: PaymentModel): Promise<any> {
|
||
|
|
console.log(payment);
|
||
|
|
const connection = getConnection();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
await queryRunner.startTransaction();
|
||
|
|
try {
|
||
|
|
let cobranca = 'PAGV';
|
||
|
|
switch (payment.formaPagto){
|
||
|
|
case 'credit_card_parcelado':
|
||
|
|
cobranca = 'PAGP';
|
||
|
|
break;
|
||
|
|
case 'debit_card':
|
||
|
|
cobranca = 'PAGD';
|
||
|
|
break;
|
||
|
|
case 'credit_card':
|
||
|
|
cobranca = 'PAGV';
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
cobranca = 'DH';
|
||
|
|
break;
|
||
|
|
};
|
||
|
|
|
||
|
|
await queryRunner.manager
|
||
|
|
.createQueryBuilder()
|
||
|
|
.insert()
|
||
|
|
.into(Estpagamento)
|
||
|
|
.values({
|
||
|
|
orderId: payment.orderId,
|
||
|
|
dataPagamento: payment.dataPagamento,
|
||
|
|
codigoAutorizacao: payment.idTransacao,
|
||
|
|
codigoResposta: '00',
|
||
|
|
dataRequisicao: payment.dataPagamento,
|
||
|
|
dataServidor: payment.dataPagamento,
|
||
|
|
estAcquirer: 'pagseguro',
|
||
|
|
idTransacao: payment.codigoAutorizacao,
|
||
|
|
nsu: payment.nsu,
|
||
|
|
parcelas: payment.parcelas,
|
||
|
|
valor: payment.valor / 100,
|
||
|
|
nomeBandeira: 'pagseguro',
|
||
|
|
formaPagto: payment.formaPagto,
|
||
|
|
codigoFuncionario: null,
|
||
|
|
cobranca: cobranca
|
||
|
|
})
|
||
|
|
.execute()
|
||
|
|
|
||
|
|
await queryRunner.commitTransaction();
|
||
|
|
return new ResultModel(true, 'Pagamento incluído com sucesso!', {}, {});
|
||
|
|
|
||
|
|
} catch (error) {
|
||
|
|
await queryRunner.rollbackTransaction();
|
||
|
|
console.log(error);
|
||
|
|
return new ResultModel(false,
|
||
|
|
'Erro ao pagamento para carrgamento / cliente.',
|
||
|
|
null,
|
||
|
|
error);
|
||
|
|
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
}
|