impl modulo orde node 22
This commit is contained in:
@@ -1,19 +1,19 @@
|
||||
// src/modules/orders/infrastructure/repositories/orders.repository.ts
|
||||
import { Injectable, HttpException, HttpStatus } from "@nestjs/common";
|
||||
import { DataSource } from "typeorm";
|
||||
import { InjectDataSource } from "@nestjs/typeorm";
|
||||
import { FindOrdersDto } from "../dto/find-orders.dto";
|
||||
import { OrderItemDto } from "../dto/OrderItemDto";
|
||||
import { CutItemDto } from '../dto/CutItemDto';
|
||||
|
||||
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { DataSource } from 'typeorm';
|
||||
import { InjectDataSource } from '@nestjs/typeorm';
|
||||
import { createOracleConfig } from '../../core/configs/typeorm.oracle.config';
|
||||
import { FindOrdersDto } from '../dto/find-orders.dto';
|
||||
|
||||
@Injectable()
|
||||
export class OrdersRepository {
|
||||
constructor(
|
||||
@InjectDataSource('oracle') private readonly dataSource: DataSource
|
||||
@InjectDataSource("oracle") private readonly dataSource: DataSource
|
||||
) {}
|
||||
|
||||
async findOrders(query: FindOrdersDto): Promise<any[]> {
|
||||
await this.dataSource.initialize();
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
@@ -143,11 +143,13 @@ export class OrdersRepository {
|
||||
|
||||
const conditions: string[] = [];
|
||||
|
||||
if (query.storeId) {
|
||||
if (query.codfilial) {
|
||||
conditions.push(`AND PCPEDC.CODFILIAL = :storeId`);
|
||||
}
|
||||
if (query.storeStockId) {
|
||||
conditions.push(`AND EXISTS(SELECT 1 FROM PCPEDI WHERE PCPEDI.NUMPED = PCPEDC.NUMPED AND PCPEDI.CODFILIALRETIRA = :storeStockId)`);
|
||||
if (query.filialretira) {
|
||||
conditions.push(
|
||||
`AND EXISTS(SELECT 1 FROM PCPEDI WHERE PCPEDI.NUMPED = PCPEDC.NUMPED AND PCPEDI.CODFILIALRETIRA = :storeStockId)`
|
||||
);
|
||||
}
|
||||
if (query.sellerId) {
|
||||
conditions.push(`AND PCPEDC.CODUSUR = :sellerId`);
|
||||
@@ -159,42 +161,65 @@ export class OrdersRepository {
|
||||
conditions.push(`AND PCPEDC.CODCOB = :billingId`);
|
||||
}
|
||||
if (query.orderId) {
|
||||
conditions.push(`AND (PCPEDC.NUMPED = :orderId OR PCPEDC.NUMPEDENTFUT = :orderId)`);
|
||||
conditions.push(
|
||||
`AND (PCPEDC.NUMPED = :orderId OR PCPEDC.NUMPEDENTFUT = :orderId)`
|
||||
);
|
||||
}
|
||||
if (query.invoiceId) {
|
||||
conditions.push(`AND PCPEDC.NUMNOTA = :invoiceId`);
|
||||
}
|
||||
if (query.productId) {
|
||||
conditions.push(`AND EXISTS(SELECT 1 FROM PCPEDI WHERE PCPEDI.NUMPED = PCPEDC.NUMPED AND PCPEDI.CODPROD = :productId)`);
|
||||
conditions.push(
|
||||
`AND EXISTS(SELECT 1 FROM PCPEDI WHERE PCPEDI.NUMPED = PCPEDC.NUMPED AND PCPEDI.CODPROD = :productId)`
|
||||
);
|
||||
}
|
||||
if (query.createDateIni) {
|
||||
conditions.push(`AND PCPEDC.DATA >= TO_DATE(:createDateIni, 'YYYY-MM-DD')`);
|
||||
conditions.push(
|
||||
`AND PCPEDC.DATA >= TO_DATE(:createDateIni, 'YYYY-MM-DD')`
|
||||
);
|
||||
}
|
||||
if (query.createDateEnd) {
|
||||
conditions.push(`AND PCPEDC.DATA <= TO_DATE(:createDateEnd, 'YYYY-MM-DD')`);
|
||||
conditions.push(
|
||||
`AND PCPEDC.DATA <= TO_DATE(:createDateEnd, 'YYYY-MM-DD')`
|
||||
);
|
||||
}
|
||||
if (query.invoiceDateIni) {
|
||||
conditions.push(`AND PCPEDC.DTFAT >= TO_DATE(:invoiceDateIni, 'YYYY-MM-DD')`);
|
||||
conditions.push(
|
||||
`AND PCPEDC.DTFAT >= TO_DATE(:invoiceDateIni, 'YYYY-MM-DD')`
|
||||
);
|
||||
}
|
||||
if (query.invoiceDateEnd) {
|
||||
conditions.push(`AND PCPEDC.DTFAT <= TO_DATE(:invoiceDateEnd, 'YYYY-MM-DD')`);
|
||||
conditions.push(
|
||||
`AND PCPEDC.DTFAT <= TO_DATE(:invoiceDateEnd, 'YYYY-MM-DD')`
|
||||
);
|
||||
}
|
||||
if (query.shippimentId) {
|
||||
conditions.push(`AND PCPEDC.NUMCAR = :shippimentId`);
|
||||
}
|
||||
if (query.deliveryType) {
|
||||
const types = query.deliveryType.split(',').map(t => `'${t}'`).join(',');
|
||||
conditions.push(`AND EXISTS(SELECT 1 FROM PCPEDI WHERE PCPEDI.NUMPED = PCPEDC.NUMPED AND PCPEDI.TIPOENTREGA IN (${types}))`);
|
||||
const types = query.deliveryType
|
||||
.split(",")
|
||||
.map((t) => `'${t}'`)
|
||||
.join(",");
|
||||
conditions.push(
|
||||
`AND EXISTS(SELECT 1 FROM PCPEDI WHERE PCPEDI.NUMPED = PCPEDC.NUMPED AND PCPEDI.TIPOENTREGA IN (${types}))`
|
||||
);
|
||||
}
|
||||
if (query.status) {
|
||||
const statusList = query.status.split(',').map(s => `'${s}'`).join(',');
|
||||
const statusList = query.status
|
||||
.split(",")
|
||||
.map((s) => `'${s}'`)
|
||||
.join(",");
|
||||
conditions.push(`AND PCPEDC.POSICAO IN (${statusList})`);
|
||||
}
|
||||
if (query.type) {
|
||||
const types = query.type.split(',').map(t => `'${t}'`).join(',');
|
||||
const types = query.type
|
||||
.split(",")
|
||||
.map((t) => `'${t}'`)
|
||||
.join(",");
|
||||
conditions.push(`AND PCPEDC.CONDVENDA IN (${types})`);
|
||||
}
|
||||
if (query.onlyPendentingTransfer === 'S') {
|
||||
if (query.onlyPendentingTransfer === "S") {
|
||||
conditions.push(`
|
||||
AND NOT EXISTS(SELECT 1 FROM PCNFENT, PCFILIAL
|
||||
WHERE PCFILIAL.CODIGO = PCPEDC.CODFILIAL
|
||||
@@ -202,8 +227,8 @@ export class OrdersRepository {
|
||||
AND PCNFENT.NUMNOTA = PCPEDC.NUMNOTA)`);
|
||||
}
|
||||
|
||||
sql += '\n' + conditions.join('\n');
|
||||
sql += '\nAND ROWNUM < 5000';
|
||||
sql += "\n" + conditions.join("\n");
|
||||
sql += "\nAND ROWNUM < 5000";
|
||||
|
||||
const orders = await queryRunner.manager.query(sql);
|
||||
return orders;
|
||||
@@ -212,4 +237,132 @@ export class OrdersRepository {
|
||||
await this.dataSource.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async findInvoice(chavenfe: string): Promise<any> {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
try {
|
||||
const sql = `
|
||||
SELECT
|
||||
pcnfsaid.codfilial AS "storeId",
|
||||
pcnfsaid.dtsaida AS "invoiceDate",
|
||||
pcnfsaid.numped AS "orderId",
|
||||
pcnfsaid.numnota AS "invoiceId",
|
||||
pcnfsaid.numtransvenda AS "transactionId",
|
||||
pcnfsaid.codcli AS "customerId",
|
||||
pcclient.cliente AS "customer",
|
||||
pcnfsaid.codusur AS "sellerId",
|
||||
pcusuari.nome AS "sellerName",
|
||||
(SELECT SUM(pcmov.qt) FROM pcmov WHERE pcmov.numtransvenda = pcnfsaid.numtransvenda) AS "itensQt",
|
||||
NULL AS "itens"
|
||||
FROM pcnfsaid
|
||||
JOIN pcclient ON pcnfsaid.codcli = pcclient.codcli
|
||||
JOIN pcusuari ON pcnfsaid.codusur = pcusuari.codusur
|
||||
WHERE pcnfsaid.chavenfe = '${chavenfe}'
|
||||
`;
|
||||
|
||||
const invoice = await queryRunner.manager.query(sql);
|
||||
if (!invoice || invoice.length === 0) {
|
||||
throw new HttpException('Nota fiscal não foi localizada no sistema', HttpStatus.BAD_REQUEST);
|
||||
}
|
||||
|
||||
const sqlItem = `
|
||||
SELECT
|
||||
pcmov.codprod AS "productId",
|
||||
pcprodut.descricao AS "productName",
|
||||
pcprodut.embalagem AS "package",
|
||||
pcmov.qt AS "qt",
|
||||
pcprodut.codauxiliar AS "ean",
|
||||
pcprodut.multiplo AS "multiple",
|
||||
pcprodut.tipoproduto AS "productType",
|
||||
REPLACE(
|
||||
CASE
|
||||
WHEN INSTR(pcprodut.URLIMAGEM, ';') > 0
|
||||
THEN SUBSTR(pcprodut.URLIMAGEM,1,INSTR(pcprodut.URLIMAGEM, ';') - 1)
|
||||
WHEN pcprodut.URLIMAGEM IS NOT NULL
|
||||
THEN pcprodut.URLIMAGEM
|
||||
ELSE NULL
|
||||
END,
|
||||
'167.249.211.178:8001',
|
||||
'10.1.1.191'
|
||||
) AS "image"
|
||||
FROM pcmov
|
||||
JOIN pcprodut ON pcmov.codprod = pcprodut.codprod
|
||||
WHERE pcmov.numtransvenda = ${invoice[0].transactionId}
|
||||
`;
|
||||
|
||||
const itens = await queryRunner.manager.query(sqlItem);
|
||||
invoice[0].itens = itens;
|
||||
|
||||
return invoice[0];
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
|
||||
async getItens(orderId: string): Promise<OrderItemDto[]> {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
try {
|
||||
const sql = `SELECT PCPEDI.CODPROD as "productId"
|
||||
, PCPRODUT.DESCRICAO as "description"
|
||||
, PCPRODUT.EMBALAGEM as "pacth"
|
||||
, NVL(PCPEDI.COMPLEMENTO,
|
||||
( SELECT TV7I.COMPLEMENTO FROM PCPEDC, PCPEDC TV7, PCPEDITEMP TV7I
|
||||
WHERE TV7.NUMPED = PCPEDC.NUMPEDENTFUT
|
||||
AND PCPEDC.NUMPED = PCPEDI.NUMPED
|
||||
AND TV7.NUMPEDRCA = TV7I.NUMPEDRCA
|
||||
AND TV7I.CODPROD = PCPEDI.codprod
|
||||
AND TV7I.NUMSEQ = PCPEDI.NUMSEQ ) ) as "color"
|
||||
, PCPEDI.CODFILIALRETIRA as "stockId"
|
||||
, PCPEDI.QT as "quantity"
|
||||
, PCPEDI.PVENDA as "salePrice"
|
||||
, CASE WHEN PCPEDI.TIPOENTREGA = 'RI' THEN 'RETIRA IMEDIATA'
|
||||
WHEN PCPEDI.TIPOENTREGA = 'RP' THEN 'RETIRA POSTERIOR'
|
||||
WHEN PCPEDI.TIPOENTREGA = 'EN' THEN 'ENTREGA'
|
||||
WHEN PCPEDI.TIPOENTREGA = 'EF' THEN 'ENTREGA FUTURA'
|
||||
END as "deliveryType"
|
||||
, ( PCPEDI.QT *
|
||||
PCPEDI.PVENDA ) as "total"
|
||||
, ( PCPEDI.QT * PCPRODUT.PESOBRUTO ) as "weigth"
|
||||
, PCDEPTO.DESCRICAO as "department"
|
||||
, PCMARCA.MARCA as "brand"
|
||||
FROM PCPEDI, PCPRODUT, PCDEPTO, PCMARCA
|
||||
WHERE PCPEDI.CODPROD = PCPRODUT.CODPROD
|
||||
AND PCPRODUT.CODEPTO = PCDEPTO.CODEPTO
|
||||
AND PCPRODUT.CODMARCA = PCMARCA.CODMARCA
|
||||
AND PCPEDI.NUMPED = ${orderId}`;
|
||||
|
||||
const itens = await queryRunner.manager.query(sql);
|
||||
return itens;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
async getCutItens(orderId: string): Promise<CutItemDto[]> {
|
||||
const queryRunner = this.dataSource.createQueryRunner();
|
||||
await queryRunner.connect();
|
||||
|
||||
try {
|
||||
const sql = `
|
||||
SELECT
|
||||
PCCORTEI.CODPROD as "productId",
|
||||
PCPRODUT.DESCRICAO as "description",
|
||||
PCPRODUT.EMBALAGEM as "pacth",
|
||||
PCCORTEI.CODFILIAL as "stockId",
|
||||
(PCCORTEI.QTSEPARADA + PCCORTEI.QTCORTADA) as "saleQuantity",
|
||||
PCCORTEI.QTCORTADA as "cutQuantity",
|
||||
PCCORTEI.QTSEPARADA as "separedQuantity"
|
||||
FROM PCCORTEI
|
||||
JOIN PCPRODUT ON PCCORTEI.CODPROD = PCPRODUT.CODPROD
|
||||
WHERE PCCORTEI.NUMPED = ${orderId}
|
||||
`;
|
||||
|
||||
const itens = await queryRunner.manager.query(sql);
|
||||
return itens;
|
||||
} finally {
|
||||
await queryRunner.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user