168 lines
6.0 KiB
TypeScript
168 lines
6.0 KiB
TypeScript
|
|
/*
|
||
|
|
https://docs.nestjs.com/providers#services
|
||
|
|
*/
|
||
|
|
|
||
|
|
import { Injectable } from '@nestjs/common';
|
||
|
|
import { connectionOptions } from 'src/configs/typeorm.config';
|
||
|
|
import { Pcclient } from 'src/domain/entity/tables/pcclient.entity';
|
||
|
|
import { Store } from 'src/domain/entity/tables/pcfilial.entity';
|
||
|
|
import { Pctabtrib } from 'src/domain/entity/tables/pctabtrib.entity';
|
||
|
|
import { Connection } from 'typeorm';
|
||
|
|
|
||
|
|
@Injectable()
|
||
|
|
export class SharedService {
|
||
|
|
|
||
|
|
async generateIdCustomer() {
|
||
|
|
console.log("Gerando idcustomer");
|
||
|
|
const connection = new Connection(connectionOptions);
|
||
|
|
await connection.connect();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
|
||
|
|
// lets now open a new transaction:
|
||
|
|
await queryRunner.startTransaction();
|
||
|
|
|
||
|
|
try {
|
||
|
|
|
||
|
|
let sql = 'SELECT PROXNUMCLI as "proxnumcli" FROM PCCONSUM WHERE 1 = 1 FOR UPDATE';
|
||
|
|
const param = await queryRunner.query(sql);
|
||
|
|
// const param = await queryRunner.manager
|
||
|
|
// .getRepository(Pcconsum)
|
||
|
|
// .createQueryBuilder('pcconsum')
|
||
|
|
// .setLock("dirty_read")
|
||
|
|
// .getOne();
|
||
|
|
|
||
|
|
const idCustomer = param[0].proxnumcli;
|
||
|
|
console.log(idCustomer);
|
||
|
|
|
||
|
|
sql = "UPDATE PCCONSUM SET PROXNUMCLI = NVL(PROXNUMCLI,0) + 1 WHERE 1 = 1"
|
||
|
|
await queryRunner.query(sql, []);
|
||
|
|
// commit transaction now:
|
||
|
|
await queryRunner.commitTransaction();
|
||
|
|
return idCustomer;
|
||
|
|
|
||
|
|
} catch (err) {
|
||
|
|
// since we have errors let's rollback changes we made
|
||
|
|
await queryRunner.rollbackTransaction();
|
||
|
|
return -1;
|
||
|
|
|
||
|
|
} finally {
|
||
|
|
// you need to release query runner which is manually created:
|
||
|
|
await queryRunner.release();
|
||
|
|
await connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async generateIdPreOrder(idSeller: number) {
|
||
|
|
const connection = new Connection(connectionOptions);
|
||
|
|
await connection.connect();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
|
||
|
|
// lets now open a new transaction:
|
||
|
|
await queryRunner.startTransaction();
|
||
|
|
|
||
|
|
try {
|
||
|
|
|
||
|
|
let sql = `SELECT PROXNUMPED as "proxnumped" FROM PCUSUARI WHERE CODUSUR = :1 FOR UPDATE`;
|
||
|
|
const seller = await queryRunner.query(sql, [idSeller] );
|
||
|
|
|
||
|
|
// const seller = await queryRunner.manager
|
||
|
|
// .getRepository(Pcusuari)
|
||
|
|
// .createQueryBuilder('pcusuari')
|
||
|
|
// .where('pcusuari.codusur = :idSeller', {idSeller: idSeller})
|
||
|
|
// .setLock("dirty_read")
|
||
|
|
// .getOne();
|
||
|
|
|
||
|
|
const idPreOrder = seller[0].proxnumped;
|
||
|
|
// await getConnection()
|
||
|
|
// .createQueryBuilder()
|
||
|
|
// .update(Pcusuari)
|
||
|
|
// .set({ proxnumped: idPreOrder + 1 })
|
||
|
|
// .where('pcusuari.codusur = :idSeller', {idSeller: idSeller})
|
||
|
|
// .execute();
|
||
|
|
// commit transaction now:
|
||
|
|
sql = `UPDATE PCUSUARI SET PROXNUMPED = NVL(PROXNUMPED,0) + 1 WHERE CODUSUR = :1`;
|
||
|
|
await queryRunner.query(sql, [idSeller] );
|
||
|
|
await queryRunner.commitTransaction();
|
||
|
|
return idPreOrder;
|
||
|
|
} catch (err) {
|
||
|
|
// since we have errors let's rollback changes we made
|
||
|
|
await queryRunner.rollbackTransaction();
|
||
|
|
console.log(err);
|
||
|
|
throw err;
|
||
|
|
} finally {
|
||
|
|
// you need to release query runner which is manually created:
|
||
|
|
await queryRunner.release();
|
||
|
|
await connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async getCustomer(idCustomer: number) {
|
||
|
|
const connection = new Connection(connectionOptions);
|
||
|
|
await connection.connect();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
try {
|
||
|
|
const customer = await queryRunner.manager
|
||
|
|
.getRepository(Pcclient)
|
||
|
|
.createQueryBuilder('pcclient')
|
||
|
|
.where("\"pcclient\".codcli = :idCustomer", { idCustomer })
|
||
|
|
.getOne();
|
||
|
|
return customer;
|
||
|
|
} catch (error) {
|
||
|
|
throw error;
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
await connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async getTaxId(productId: number, stockId: string, state: string) {
|
||
|
|
const connection = new Connection(connectionOptions);
|
||
|
|
await connection.connect();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
try {
|
||
|
|
const tax = await queryRunner.manager
|
||
|
|
.getRepository(Pctabtrib)
|
||
|
|
.createQueryBuilder('pctabtrib')
|
||
|
|
.where("pctabtrib.codprod = :codprod and pctabtrib.codfilialnf = :codfilialnf and ufdestino = :ufdestino",
|
||
|
|
{ codprod: productId, codfilialnf: stockId, ufdestino: state })
|
||
|
|
.getOne();
|
||
|
|
return tax != null ? tax.codst : 0;
|
||
|
|
} catch (error) {
|
||
|
|
throw error;
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
await connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
async getStores(storeId: string) {
|
||
|
|
const connection = new Connection(connectionOptions);
|
||
|
|
await connection.connect();
|
||
|
|
const queryRunner = connection.createQueryRunner();
|
||
|
|
await queryRunner.connect();
|
||
|
|
try {
|
||
|
|
let where = "1=1";
|
||
|
|
if (storeId != null){
|
||
|
|
where = `ID = ${storeId}`;
|
||
|
|
}
|
||
|
|
const stores = await queryRunner.manager
|
||
|
|
.getRepository(Store)
|
||
|
|
.createQueryBuilder('pcfilial')
|
||
|
|
.select(['storeId', 'name', 'shortName'])
|
||
|
|
.where(where)
|
||
|
|
.getMany();
|
||
|
|
return stores;
|
||
|
|
} catch (error) {
|
||
|
|
throw error;
|
||
|
|
} finally {
|
||
|
|
await queryRunner.release();
|
||
|
|
await connection.close();
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
}
|