vendaweb-api/src/delivery/orders/orders.service.ts

128 lines
6.5 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { Pcpedc } from 'src/domain/entity/tables/pcpedc.entity';
import { Pcpedi } from 'src/domain/entity/tables/pcpedi.entity';
import { ResultModel } from 'src/domain/models/result.model';
import { getConnection } from 'typeorm';
import { Estpagamento } from '../../domain/entity/tables/estpagamento.entity';
@Injectable()
export class OrdersService {
async GetOrdersByCustomer(id: number): Promise<any> {
const connection = getConnection();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
try {
const user = { matricula: 154969 }; //await this.GetUser(queryRunner); 2854
if (user == null) {
return new ResultModel(false, 'Não foi possível localizar notas para este usuário',
null,
'Não foi localizado motorista para este usuário.');
}
const orders = await queryRunner.manager
.getRepository(Pcpedc)
.createQueryBuilder('pcpedc')
.innerJoinAndSelect('pcpedc.pcclient', 'pcclient')
.innerJoinAndSelect('pcpedc.pccarreg', 'pccarreg',
'pccarreg.codmotorista = :matricula ', //AND pccarreg.dtfecha is null',
{ matricula: user.matricula })
.leftJoinAndSelect('pcpedc.pcclientendent', 'pcclientendent')
.innerJoinAndSelect("pcpedc.pcplpag", "pcplpag")
.select(['pcpedc.numcar as "numeroCarga"',
'NVL(pcpedc.numnota, pcpedc.numcupom) as "numeroNota"',
'pcpedc.dtfat as "dataEmissao"',
'pcpedc.numped as "numeroPedido"',
'TRUNC(pcpedc.vlatend * 100) as "valorNota"',
'pcpedc.codcli as "codigoCliente"',
'pcclient.cliente as "nomeCliente"',
'NVL(pcclientendent.enderent, pcclient.enderent) as "endereco"',
'NVL(pcclientendent.numeroent, pcclient.numeroent) as "numero"',
'NVL(pcclientendent.bairroent, pcclient.bairroent) as "bairro"',
'NVL(pcclientendent.complementoent, pcclient.complementoent) as "complemento"',
'NVL(pcclientendent.municent, pcclient.municent) as "cidade"',
'NVL(pcclientendent.estent, pcclient.estent) as "estado"',
'NVL(pcclientendent.cepent, pcclient.cepent) as "cep"',
'pcclient.pontorefer as "pontoReferencia"',
'pcpedc.codendentcli as "codigoEnderecoEntrega"',
'pcclientendent.telent as "telefoneEntrega"',
'pcclient.telent as "telefoneCliente"',
'pcclient.telcelent as "celularCliente"',
'pcplpag.descricao as "planoPagamento"',
'NVL(\"pcplpag\".NUMPARCELAS,0) as "numeroParcelas"',
'NVL((select sum(trunc(pccrecli.valor*100)) from pccrecli where pccrecli.numtransvendadesc = \"pcpedc\".numtransvenda),0) as "valorCreditos"',
'NVL((select sum(trunc(pcestcom.vldevolucao*100)) from pcestcom where pcestcom.numtransvenda = \"pcpedc\".numtransvenda),0) as "valorDevolucoes"',
'NVL((select sum(trunc(estpagamento.valor*100)) from estpagamento where estpagamento.numorca = \"pcpedc\".numped),0) as "valorRecebimentos"',
'NVL((select count(1) from estimagemnota where estimagemnota.numped = \"pcpedc\".numped and estimagemnota.tipo = \'P\'),0) as "notaComImagens"',
'NVL((select count(1) from estimagemnota where estimagemnota.numped = \"pcpedc\".numped and estimagemnota.tipo = \'C\'),0) as "notaComProtocolo"',
'NVL((select count(1) from estprotocoloentrega where estprotocoloentrega.numcar = \"pcpedc\".numcar and estprotocoloentrega.codcli = pcpedc.codcli),0) as "notaComDadosRecebedor"',
])
.addSelect(subQuery => {
return subQuery
.select('count(distinct item.codfilialretira)', 'lojas')
.from(Pcpedi, 'item')
.where('item.numped = pcpedc.numped and item.codfilialretira <> pcpedc.codfilial')
}, 'lojas')
.where("pcpedc.posicao = 'F'")
.andWhere("pcpedc.codcli = :codcli", { codcli: id })
.getRawMany();
return orders;
} catch (error) {
console.log(error);
return new ResultModel(false,
'Erro ao consultar notas fiscais.',
null,
error);
} finally {
await queryRunner.release();
}
}
async GetPayments(id: number) {
// const user = await this.GetUser(queryRunner);
const connection = getConnection();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
try {
const payments = await queryRunner.manager
.getRepository(Estpagamento)
.createQueryBuilder('estpagamento')
.select(['\"estpagamento\".numorca as "orderId"',
'TRUNC(\"estpagamento\".valor * 100) as "valor"',
'\"estpagamento\".nsu as "nsu"',
'\"estpagamento\".dtpagamento as "dataPagamento"',
'\"estpagamento\".codautorizacao as "codigoAutorizacao"',
'\"estpagamento\".idtransacao as "idTransacao"',
'\"estpagamento\".parcelas as "parcelas"',
'\"estpagamento\".formapagto as "formaPagto"',
])
.innerJoin('estpagamento.pedido', 'pcpedc')
.innerJoin('pcpedc.pcclient', 'pcclient')
//.innerJoin('pcpedc.pccarreg', 'pccarreg',
// 'pccarreg.codmotorista1 = :matricula ', //AND pccarreg.dtfecha is null',
// { matricula: user.matricula })
.where('\"pcpedc\".codcli = :id', { id })
.getRawMany()
return payments;
} catch (error) {
console.log(error);
return new ResultModel(false,
'Erro ao consultar pagamentos.',
null,
error);
} finally {
await queryRunner.release();
}
}
}