All files / source/reports movementOutboundOrderList.js

75.94% Statements 60/79
56% Branches 56/100
100% Functions 7/7
75% Lines 57/76

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264                  3x 3x 1x                       1x 1x 1x 1x 1x   1x 1x 1x 2x     1x   1x   20x   1x 1x 1x   1x                                                                                                                                                                                             1x 20x 1x 20x   1x 1x   1x       1x         1x         1x         1x         1x         1x         1x         1x 1x         1x 1x       1x 1x 20x   20x 20x                   6x 14x 2x     12x 1x     11x 1x     10x 1x     9x 1x 8x             8x                           8x             1x      
import moment from 'moment-timezone';
import { t } from '../../helpers/i18n';
import outboundOrder from '../../database/dao/outboundOrder';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import OutboundOrderStatus from '../../database/enumerators/outboundOrder';
import formatDuration from '../../helpers/date/formatDuration';
 
function remove(headersValue, value) {
  const index = headersValue.indexOf(value);
  if (index !== -1) {
    headersValue.splice(index, 1);
  }
}
// Funcao criada para popular o array headers e rows enviados por parametro
export default async function generateData(
  columns,
  filtersParam,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  let whereProd = {};
  const filters = filtersParam;
  Eif (filters.productCode) {
    whereProd = mountWhereBasedFilter({ productCode: filters.productCode });
    delete filters.productCode;
  }
  Eif (filters.status) {
    const statusEnu = [];
    filters.status.forEach((sts) => {
      statusEnu.push(OutboundOrderStatus[sts]);
    });
 
    filters.status = { type: 'in', value: statusEnu };
  }
  const where = mountWhereBasedFilter(filters);
 
  let headersValue = columns.map((it) => it.value);
 
  remove(headersValue, 'quantityOfProducts');
  remove(headersValue, 'totalHours');
  remove(headersValue, 'pendingSeparationTime');
 
  const dados = await outboundOrder.findOutboundOrders(where, {
    attributes: [
      ...headersValue,
      'pickStartAt',
      'allocationAt',
      'createdAt',
      'finishedAt',
    ],
    include: [
      {
        required: false,
        model: models.Branch,
        as: 'branch',
        attributes: ['code', 'name'],
      },
      {
        required: false,
        model: models.Carrier,
        as: 'carrier',
        attributes: ['code', 'name'],
      },
      {
        separate: true,
        required: false,
        model: models.OutboundOrderProduct,
        as: 'products',
        attributes: ['productCode', 'customerProductCode', 'orderValue'],
        include: [
          {
            required: true,
            model: models.Product,
            as: 'product',
            attributes: ['id', 'name'],
            where: whereProd,
          },
        ],
      },
      {
        required: false,
        model: models.User,
        as: 'allocationUserObj',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'attributionUserObj',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'pickStartUserObj',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'pickEndUserObj',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'checkStartUserObj',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'checkEndUserObj',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.Priority,
        as: 'priority',
        attributes: ['name'],
      },
    ],
    ...opts,
  });
 
  headersValue = [];
  headersValue = columns.map((it) => it.value);
  columns.forEach((it) => {
    headers.push({ header: it.label, numFmt: it.numFmt });
  });
  dados.forEach((item) => {
    const it = { ...item.dataValues };
 
    Iif (!Number.isNaN(parseFloat(it.totalValue))) {
      it.totalValue = parseFloat(it.totalValue && it.totalValue);
    }
 
    Iif (!Number.isNaN(parseFloat(it.orderValue))) {
      it.orderValue = parseFloat(it.orderValue);
    }
 
    // Usuário Realizou Alocação
    Iif (it.allocationUserObj && it.allocationUserObj.login) {
      it.allocationUser = it.allocationUserObj.login;
    }
 
    // Usuário que Atribuiu o documento
    Iif (it.attributionUserObj && it.attributionUserObj.login) {
      it.attributionUser = it.attributionUserObj.login;
    }
 
    // Usuário Inicio Separação
    Iif (it.pickStartUserObj && it.pickStartUserObj.login) {
      it.pickStartUser = it.pickStartUserObj.login;
    }
 
    // Usuário Final Separação
    Iif (it.pickEndUserObj && it.pickEndUserObj.login) {
      it.pickEndUser = it.pickEndUserObj.login;
    }
 
    // Usuário Inicio Conferência
    Iif (it.checkStartUserObj && it.checkStartUserObj.login) {
      it.checkStartUser = it.checkStartUserObj.login;
    }
 
    // Usuário Final Conferência
    Iif (it.checkEndUserObj && it.checkEndUserObj.login) {
      it.checkEndUser = it.checkEndUserObj.login;
    }
 
    // usuário de criação
    it.createdUser = '';
    Iif (it.userCreated && it.userCreated.login) {
      it.createdUser = it.userCreated.login;
    }
 
    // usuário de atualização
    it.updatedUser = '';
    Iif (it.userUpdated && it.userUpdated.login) {
      it.updatedUser = it.userUpdated.login;
    }
 
    const row = [];
    for (let index = 0; index < headersValue.length; index++) {
      const column = headersValue[index];
 
      row.push(it[column]);
      if (
        column === 'createdAt' ||
        column === 'updatedAt' ||
        column === 'allocationAt' ||
        column === 'attributionAt' ||
        column === 'pickStartAt' ||
        column === 'pickEndAt' ||
        column === 'checkStartAt' ||
        column === 'checkEndAt'
      ) {
        row[index] = row[index] ? moment(row[index]).format('L LTS') : '';
      } else if (column === 'partialOrder' || column === 'urgent') {
        row[index] = row[index]
          ? t('BEE172' /* Sim */, undefined, appLanguage)
          : t('BEE173' /* Não */, undefined, appLanguage);
      } else if (column === 'status') {
        row[index] = it.status
          ? OutboundOrderStatus.getStatusLabel(it.status, appLanguage)
          : '';
      } else if (column === 'branchCode') {
        row[index] = it.branch
          ? `${it.branch.code} - ${it.branch.name}`
          : row[index];
      } else if (column === 'carrierCode') {
        row[index] = it.carrier
          ? `${it.carrier.code} - ${it.carrier.name}`
          : row[index];
      } else if (column === 'quantityOfProducts') {
        row[index] = it.products && it.products.length ? it.products.length : 0;
      } else Iif (column === 'totalHours') {
        const startDate = moment(it.createdAt);
        const finishedDate = it.finished ? moment(it.finished) : moment();
        row[index] = formatDuration.msToMonthsDaysHoursMinutesAndSeconds(
          finishedDate.diff(startDate),
          false
        );
      } else Iif (column === 'pendingSeparationTime') {
        if (it.allocationAt) {
          const allocationDate = moment(it.allocationAt);
          const pickStartDate = it.pickStartAt
            ? moment(it.pickStartAt)
            : moment();
 
          row[index] = formatDuration.msToMonthsDaysHoursMinutesAndSeconds(
            pickStartDate.diff(allocationDate),
            false
          );
        } else {
          row[index] = '';
        }
      } else Iif (column === 'priorityCode') {
        row[index] =
          it.priorityCode && it.priority.name
            ? `${it.priorityCode} - ${it.priority.name}`
            : '';
      }
    }
    rows.push(row);
  });
}