All files / source/reports productsSeparationDate.js

97.29% Statements 36/37
78.12% Branches 25/32
100% Functions 8/8
96.96% Lines 32/33

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                              5x 5x   5x 5x 5x     5x     5x               5x                                     7x 5x   5x 7x   3x   6x     7x     3x           5x     5x   2x   2x           2x             2x       2x         7x     5x 2x 1x     5x   5x 5x 5x                 7x              
import moment from 'moment-timezone';
import { Op } from 'sequelize';
 
import { t } from '../../helpers/i18n';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import outboundRangeProductDao from '../../database/dao/outboundRangeProduct';
import { models } from '../../../config/database';
 
export default async function generateData(
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const where = mountWhereBasedFilter(filters);
  where[Op.and] = { ...where[Op.and], picked: { [Op.eq]: 1 } };
 
  headers.push({ header: t('BEE145' /* Filial */, undefined, appLanguage) });
  headers.push({ header: t('BEE1854' /* Setor */, undefined, appLanguage) });
  headers.push({
    header: t('BEE378' /* Código do Produto */, undefined, appLanguage),
  });
  headers.push({
    header: t('BEE1092' /* Separador */, undefined, appLanguage),
  });
  headers.push({
    header: t(
      'BEE2290' /* Total de Produtos Separados */,
      undefined,
      appLanguage
    ),
  });
 
  const dados = await outboundRangeProductDao.findOutboundRangeProducts(where, {
    attributes: ['productCode', 'pickedQuantity', 'pickedAt', 'pickedUser'],
    include: [
      {
        require: false,
        model: models.User,
        as: 'pickedUserObj',
        attributes: ['login'],
      },
      {
        require: false,
        model: models.OutboundRange,
        as: 'outboundRange',
        attributes: ['branchCode', 'rangeCode'],
      },
    ],
    ...opts,
  });
 
  const pickedProducts = dados.map((o) => o.toJSON());
  const separationData = [];
 
  const separationDates = pickedProducts.reduce((accumulator, element) => {
    if (
      element.pickedQuantity &&
      !accumulator.find((elem) => elem == moment(element.pickedAt).format('L'))
    ) {
      accumulator.push(moment(element.pickedAt).format('L'));
    }
 
    if (
      !separationData.find(
        (elem) =>
          elem.pickedUser == element.pickedUser &&
          elem.productCode == element.productCode &&
          elem.outboundRange.rangeCode == element.outboundRange.rangeCode &&
          elem.outboundRange.branchCode == element.outboundRange.branchCode
      )
    ) {
      element[moment(element.pickedAt).format('L')] = parseFloat(
        element.pickedQuantity
      );
      separationData.push(element);
    } else {
      const index = separationData.findIndex(
        (elem2) =>
          elem2.pickedUser == element.pickedUser &&
          elem2.productCode == element.productCode &&
          elem2.outboundRange.rangeCode == element.outboundRange.rangeCode &&
          elem2.outboundRange.branchCode == element.outboundRange.branchCode
      );
 
      Iif (moment(element.pickedAt).format('L') in separationData[index]) {
        separationData[index][moment(element.pickedAt).format('L')] =
          parseFloat(element.pickedQuantity) +
          parseFloat(
            separationData[index][moment(element.pickedAt).format('L')]
          );
      } else {
        separationData[index][moment(element.pickedAt).format('L')] =
          parseFloat(element.pickedQuantity);
      }
 
      separationData[index].pickedQuantity =
        parseFloat(separationData[index].pickedQuantity) +
        parseFloat(element.pickedQuantity);
    }
 
    return accumulator;
  }, []);
 
  separationDates.sort((a, b) => {
    if (moment(a, 'l').isBefore(moment(b, 'l').format())) return -1;
    Eif (moment(a, 'l').isAfter(moment(b, 'l').format())) return 1;
  });
 
  headers.push(...separationDates);
 
  for (let index = 0; index < separationData.length; index++) {
    const element = separationData[index];
    rows.push([
      element.outboundRange.branchCode,
      element.outboundRange.rangeCode,
      element.productCode,
      element.pickedUserObj && element.pickedUserObj.login
        ? element.pickedUserObj.login
        : '',
      element.pickedQuantity ? parseFloat(element.pickedQuantity) : 0,
      ...separationDates.map((elem) =>
        separationData[index][elem]
          ? parseFloat(separationData[index][elem])
          : 0
      ),
    ]);
  }
}