import { Injectable } from '@nestjs/common'; import { Esventregasporcliente } from 'src/domain/entity/views/esventregasporcliente.entity'; import { Connection, getConnection } from 'typeorm'; import { ResultModel } from '../../domain/models/result.model'; import { Esvretiralojascliente } from '../../domain/entity/views/esventregaslojascliente.entity'; import { EsvRetiraLojas } from '../../domain/entity/views/esvretiralojas.entity'; import { connectionOptions } from 'src/configs/typeorm.config'; @Injectable() export class ShippingService { constructor(private connection: Connection) { } async GetShippingsByCustomer(): Promise { const connection = getConnection(); const queryRunner = connection.createQueryRunner(); await queryRunner.connect(); try { /*const user = await this.GetUser(queryRunner); if (user == null) { return new ResultModel(false, 'Não foi possível localizar entregas para este usuário', null, 'Não foi localizado motorista para este usuário.'); }*/ const deliveries = await queryRunner.manager .getRepository(Esventregasporcliente) .createQueryBuilder('ESVENTREGASPORCLIENTE') .select(['cliente as "nomeCliente"' , 'cgcent as "cnpj_cpf"' , 'email as "email"' , 'endereco as "endereco"' , 'numero as "numero"' , 'bairro as "bairro"' , 'complemento as "complemento"' , 'cidade as "cidade"' , 'estado as "uf"' , 'cep as "cep"' , 'qtpedidos as "quantidadePedidos"' , 'qtlojas as "quantidaeLojas"' , 'entrega_concluida as "entregaConcluida"' , 'telefone as "telefoneCliente"' , 'celular as "celularCliente"' , 'codcli as "codigoCliente"']) .where("email = 'eduardoestevao.gyn@gmail.com'") .getRawMany(); return deliveries; } catch (error) { console.log(error); return new ResultModel(false, 'Não foi possível consultar as entregas por cliente.', null, error); } finally { await queryRunner.release(); } } async GetCollectByShop(): Promise { const connection = getConnection(); const queryRunner = connection.createQueryRunner(); await queryRunner.connect(); try { /*const user = await this.GetUser(queryRunner); if (user == null) { return new ResultModel(false, 'Não foi possível localizar entregas para este usuário', null, 'Não foi localizado motorista para este usuário.'); }*/ const deliveries = await queryRunner.manager .getRepository(EsvRetiraLojas) .createQueryBuilder('ESVRETIRALOJAS') .select(['codigo as "codigoLoja"' , 'razaosocial as "razaoSocial"' , 'quantidadepedidos as "quantidadePedidos"' ]) .where("email = 'eduardoestevao.gyn@gmail.com'") .getRawMany(); return deliveries; } catch (error) { console.log(error); return new ResultModel(false, 'Não foi possível consultar as coletas por loja.', null, error); } finally { await queryRunner.release(); } } async GetCollectByCustomer(id: string): Promise { const connection = getConnection(); const queryRunner = connection.createQueryRunner(); await queryRunner.connect(); try { /*const user = await this.GetUser(queryRunner); if (user == null) { return new ResultModel(false, 'Não foi possível localizar entregas para este usuário', null, 'Não foi localizado motorista para este usuário.'); }*/ const deliveries = await queryRunner.manager .getRepository(Esvretiralojascliente) .createQueryBuilder('ESVRETIRALOJASCLIENTE') .select([ 'codfilial as "codigoFilial"' ,'numped as "numeroPedido"' ,'numnota as "numeroNota"' ,'dtfat as "dataFaturamento"' ,'datapedido as "dataPedido"' ,'codcli as "codigoCliente"' ,'cliente as "nomeCliente"' ,'codfilialretira as "codigoLoja"' ,'razaosocial as "nomeLoja"' ,'qtitens as "quantidadeItens"' ,'quantidade as "quantidade"' ]) .where("email = 'eduardoestevao.gyn@gmail.com'") .andWhere("codfilial = :codfilial", {codfilial: id}) .getRawMany(); return deliveries; } catch (error) { console.log(error); return new ResultModel(false, 'Não foi possível consultar as coletas por cliente.', null, error); } finally { await queryRunner.release(); } } async getDeliverySchedule() { const connection = new Connection(connectionOptions); await connection.connect(); const queryRunner = connection.createQueryRunner(); await queryRunner.connect(); try { const sql = `SELECT PCDIASUTEIS.DATA as "dateDelivery", NVL (PCDIASUTEIS.DIAROTA, 'N') as "delivery", CASE WHEN NVL (PCDIASUTEIS.DIAROTA, 'N') = 'N' THEN 0 ELSE (PARAMFILIAL.OBTERCOMONUMBER ('CAPACIDADE_LOGISTICA', 12)) END as "deliverySize", ROUND ( (NVL (VENDAS.TOTPESO, 0) / 1000), 3) as "saleWeigth", CASE WHEN NVL (PCDIASUTEIS.DIAROTA, 'N') = 'N' THEN 0 ELSE ROUND ( GREATEST ( ( ( PARAMFILIAL.OBTERCOMONUMBER ('CAPACIDADE_LOGISTICA', 12) * 1000) - NVL (VENDAS.TOTPESO, 0)) / 1000, 0), 3) END as "avaliableDelivery" FROM PCDIASUTEIS, ( SELECT PCPEDC.DTENTREGA, SUM (PCPEDC.TOTPESO) TOTPESO FROM PCPEDC WHERE PCPEDC.POSICAO IN ('L', 'M') AND PCPEDC.CONDVENDA = 8 AND PCPEDC.CODFILIAL IN (12, 13, 4, 6) AND EXISTS (SELECT TV7.NUMPED FROM PCPEDC TV7 WHERE TV7.NUMPED = PCPEDC.NUMPEDENTFUT AND TV7.POSICAO = 'F') AND PCPEDC.DTENTREGA >= TRUNC (SYSDATE) + 3 GROUP BY PCPEDC.DTENTREGA) VENDAS WHERE PCDIASUTEIS.CODFILIAL = 12 AND PCDIASUTEIS.DATA BETWEEN TRUNC (SYSDATE) + 3 AND TRUNC(SYSDATE) + 20 --AND NVL(PCDIASUTEIS.DIAROTA,'N') = 'S' AND PCDIASUTEIS.DATA = VENDAS.DTENTREGA(+) ORDER BY PCDIASUTEIS.DATA `; const data = await queryRunner.query(sql); const sqlDeliveryDate = `SELECT TRUNC(SYSDATE) + esf_calcular_prazo_entrega_programada(TRUNC(SYSDATE), '12', 129, '', 0, 500) as "date" FROM DUAL`; const dataDeliveryDate = await queryRunner.query(sqlDeliveryDate); const dataComplete = { dateDelivery: dataDeliveryDate[0].date, deliveries: [...data] }; return dataComplete; } finally { await queryRunner.release(); } } }