All files / crons/jobs getStockBalanceErp.js

0% Statements 0/33
0% Branches 0/19
0% Functions 0/3
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82                                                                                                                                                                   
require('babel-register');
require('babel-polyfill');
require('moment');
const moment = require('moment-timezone');
const { parentPort } = require('worker_threads');
const databaseConnect = require('../../../config/database');
 
const { default: Totvs } = require('../../services/totvs');
 
// ####################################################################
// JOB EXECUTADO PARA BUSCAR O SALDO DO ESTOQUE ERP DE TODAS AS FILIAS
// ####################################################################
// Funcao de envio de mensagem utilizada para logging
function sendMessage(message = '') {
  if (parentPort) parentPort.postMessage(message);
}
 
const jobExec = async () => {
  sendMessage('Database connecting...');
  const models = await databaseConnect.default();
 
  const branches = await models.Branch.findAll({});
 
  if (branches && branches.length > 0) {
    for (const branch of branches) {
      const balances = await Totvs.getStockBalanceErp(branch.code);
 
      if (balances && balances.length > 0) {
        const newBalances = [];
 
        for (let index = 0; index < balances.length; index++) {
          const balance = balances[index];
 
          const newInventoryErp = {
            productCode: balance.productCode,
            branchCode: balance.branchCode,
            warehouseCode: balance.warehouseCode,
            depositCode: balance.depositCode,
            quantity: balance.quantity,
            unitMeasure: balance.unitMeasure,
            storageAddress: balance.storageAddress,
            lotNumber: balance.lotNumber,
            fifoDate: balance.fifoDate
              ? moment(balance.fifoDate, 'YYYY-MM-DD', true).format()
              : moment('9999-12-31', 'YYYY-MM-DD').format(),
            expirationDate: balance.expirationDate
              ? moment(balance.expirationDate, 'YYYY-MM-DD', true).format()
              : moment('9999-12-31', 'YYYY-MM-DD').format(),
            note: balance.note,
            createdAt: moment().format(),
            createdUser: 1,
            updatedAt: moment().format(),
            updatedUser: 1,
          };
 
          newBalances.push(newInventoryErp);
        }
 
        if (newBalances.length > 0) {
          await models.InventoryErp.destroy({
            where: {
              branchCode: branch.code,
            },
          });
 
          await models.InventoryErp.bulkCreate(newBalances);
        }
      }
    }
  }
  sendMessage('Finished!');
};
(async () => {
  sendMessage('jobExec');
  // funcao que executa o job
  await jobExec();
 
  // processo necessario para informar a conclusao do job
  if (parentPort) sendMessage('done');
  else process.exit(0);
})();