All files / source/reports stockAdress.js

96.87% Statements 31/32
71.87% Branches 23/32
100% Functions 5/5
96.66% Lines 29/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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                2x 16x   2x 16x 2x     14x 2x   12x     2x                                                                         2x 16x     2x 5x 5x 5x 5x 5x               5x 3x   5x 2x     5x 5x 40x   40x 40x 10x   40x         5x      
import moment from 'moment-timezone';
import I18n from '../../helpers/i18n';
import stockMovementDao from '../../database/dao/stockMovement';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
// Funcao criada para popular o array headers e rows enviados por parametro
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const where = mountWhereBasedFilter(filters);
  const headersValue = columns.map((it) => it.value);
 
  const attributes = columns.map((it) => {
    if (it.value === 'serialGroupCode') {
      return 'serialGroup.str_code';
    }
 
    if (it.value === 'expirationDate') {
      return 'stockBalance.dte_expiration';
    }
    return it.value;
  });
 
  const dados = await stockMovementDao.findStockMovements(where, {
    attributes,
    include: [
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userMovement',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.SerialGroup,
        as: 'serialGroup',
        attributes: ['code'],
      },
      {
        required: false,
        model: models.StockBalance,
        as: 'stockBalance',
        attributes: ['addressCode', 'expirationDate', 'lotNumber'],
      },
    ],
    ...opts,
  });
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  dados.forEach((it) => {
    it.createdUser = it.userCreated ? it.userCreated.dataValues.login : '';
    it.updatedUser = it.userUpdated ? it.userUpdated.dataValues.login : '';
    it.movementUser = it.userMovement ? it.userMovement.dataValues.login : '';
    it.serialGroupCode = it.serialGroup ? it.serialGroup.dataValues.code : '';
    it.expirationDate =
      it.stockBalance &&
      !moment(it.stockBalance.dataValues.expirationDate).isSame(
        moment.utc('1000-01-01 00:00:00').toDate()
      )
        ? it.stockBalance.dataValues.expirationDate
        : '';
 
    if (it.type == 1) {
      it.type = I18n.t('BEE201' /* Entrada */, undefined, appLanguage);
    }
    if (it.type == 2) {
      it.type = I18n.t('BEE202' /* SaĆ­da */, undefined, appLanguage);
    }
 
    const row = [];
    for (let index = 0; index < headersValue.length; index++) {
      const column = headersValue[index];
 
      row.push(it[column]);
      if (column === 'createdAt' || column === 'updatedAt') {
        row[index] = moment(row[index]).format('l LTS');
      }
      Iif (column === 'quantity' && row[index] !== null) {
        row[index] = NumberFormatter.format(row[index], 4);
      }
    }
 
    rows.push(row);
  });
}