All files / source/reports emptyAddresses.js

79.24% Statements 42/53
58.73% Branches 37/63
100% Functions 4/4
81.25% Lines 39/48

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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167                                1x 1x   1x   1x   1x   9x 1x                                                                                                                           1x 1x                             1x     1x   1x 9x     1x 1x 1x 1x     1x     1x     1x       1x 1x 1x 1x   1x   1x     1x 1x 1x 1x 1x   1x 1x 9x 9x 9x 2x   9x                     1x      
import { literal, Op } from 'sequelize';
import moment from 'moment-timezone';
import { t } from '../../helpers/i18n';
import StorageAddressDao from '../../database/dao/storageAddress';
import { models, sequelize } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const columnSide = filters.columnSide ? filters.columnSide.value : 'both';
  const filtersCustom = filters;
 
  Iif (filtersCustom.columnSide) delete filtersCustom.columnSide;
 
  Eif (filtersCustom.size.value[0] === null) delete filtersCustom.size;
 
  const where = mountWhereBasedFilter(filtersCustom);
 
  const columnsValues = columns.map((it) => it.value);
  let dados = await StorageAddressDao.findStorageAddresses(where, {
    attributes: [
      ...columnsValues,
      [sequelize.fn('SUM', sequelize.col('`balances`.`flo_quantity`')), 'soma'],
    ],
    order: ['code'],
    include: [
      {
        model: models.StockBalance,
        as: 'balances',
        required: false,
        attributes: ['id', 'branchCode', 'addressCode', 'quantity'],
        where: {
          branchCode: {
            [Op.eq]: sequelize.col('StorageAddresses.str_branch_code'),
          },
        },
      },
      {
        required: false,
        model: models.StorageAddressSize,
        as: 'addressSize',
        attributes: ['id', 'code', 'name'],
      },
      {
        required: false,
        model: models.StorageAddressType,
        as: 'addressType',
        attributes: ['id', 'name'],
      },
      {
        required: false,
        model: models.Branch,
        as: 'branch',
        attributes: ['code', 'name'],
      },
      {
        required: false,
        model: models.Range,
        as: 'range',
        attributes: ['code', 'name', 'branchCode'],
      },
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
    ],
    group: ['code', 'branchCode', ...columnsValues],
    having: {
      [Op.or]: [literal('`soma` IS NULL'), literal('`soma` = 0')],
    },
    ...opts,
  });
 
  const filteredData = dados.filter((item) => {
    Iif (item.column) {
      const value = parseInt(
        item.column.replace(item.column.substr(0, 1), ''),
        10
      );
 
      if (columnSide === 'even' && value % 2 !== 0) {
        return false;
      }
 
      if (columnSide === 'odd' && value % 2 === 0) {
        return false;
      }
    }
 
    return true;
  });
 
  dados = filteredData;
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  for (const address of dados) {
    address.createdUser = address.userCreated ? address.userCreated.login : '';
    address.updatedUser = address.userUpdated.login;
    address.blocked = address.blocked
      ? t('BEE172' /* Sim */, undefined, appLanguage)
      : t('BEE173' /* Não */, undefined, appLanguage);
    address.shareAddress = address.shareAddress
      ? t('BEE172' /* Sim */, undefined, appLanguage)
      : t('BEE173' /* Não */, undefined, appLanguage);
    address.branchCode = address.branch
      ? `${address.branch.code} - ${address.branch.name}`
      : address.branchCode;
    address.rangeCode = address.range
      ? `${address.range.code} - ${address.range.name}`
      : address.rangeCode;
 
    Iif (address.type === 0) address.type = '';
    Eif (address.type === 1)
      address.type = `${address.addressType.id} - ${address.addressType.name}`;
    Iif (address.type === 2)
      address.type = `${address.addressType.id} - ${address.addressType.name}`;
    Iif (address.type === 3)
      address.type = `${address.addressType.id} - ${address.addressType.name}`;
    Iif (address.type === 4)
      address.type = `${address.addressType.id} - ${address.addressType.name}`;
 
    address.height = address.height || 0;
    address.width = address.width || 0;
    address.length = address.length || 0;
    address.capacityVolume = address.capacityVolume || 0;
    address.capacityWeight = address.capacityWeight || 0;
 
    const row = [];
    for (let index = 0; index < columnsValues.length; index++) {
      const column = columnsValues[index];
      row.push(address[column]);
      if ((column === 'createdAt' || column === 'updatedAt') && row[index]) {
        row[index] = moment(row[index]).format('l LTS');
      }
      Iif (
        (column === 'height' ||
          column === 'width' ||
          column === 'length' ||
          column === 'capacityVolume' ||
          column === 'capacityWeight') &&
        row[index] !== null
      ) {
        row[index] = NumberFormatter.format(row[index], 3);
      }
    }
    rows.push(row);
  }
}