All files / source/reports qtyProductsSeparatedPerDateAndUser.js

100% Statements 27/27
78.57% Branches 22/28
100% Functions 6/6
100% Lines 26/26

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                          3x   3x     3x 3x 3x 3x       3x                                                                 3x 3x 3x   3x 6x 6x 5x 5x 3x   3x       6x 4x     6x             6x         3x   3x   3x 3x             8x          
import moment from 'moment-timezone';
import { t } from '../../helpers/i18n';
import outboundRangeProductDao from '../../database/dao/outboundRangeProduct';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
export default async function generateData(
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const where = mountWhereBasedFilter(filters);
 
  headers.push({
    header: t('BEE1994' /* Código Filial */, undefined, appLanguage),
  });
  headers.push({ header: t('BEE145' /* Filial */, undefined, appLanguage) });
  headers.push({ header: t('BEE1854' /* Setor */, undefined, appLanguage) });
  headers.push({ header: t('BEE1' /* Usuário */, undefined, appLanguage) });
  headers.push({
    header: t('BEE1134' /* Quantidade Separada */, undefined, appLanguage),
  });
 
  const dados = await outboundRangeProductDao.findOutboundRangeProducts(where, {
    attributes: ['pickedUser', 'pickedQuantity', 'pickedAt'],
    include: [
      {
        required: false,
        model: models.OutboundOrder,
        as: 'outboundOrder',
        attributes: ['branchCode'],
        include: [
          {
            required: false,
            model: models.Branch,
            as: 'branch',
            attributes: ['name'],
          },
        ],
      },
      {
        required: false,
        model: models.OutboundRange,
        as: 'outboundRange',
        attributes: ['rangeCode'],
      },
      {
        required: false,
        model: models.User,
        as: 'pickedUserObj',
        attributes: ['login'],
      },
    ],
    ...opts,
  });
 
  const organizedData = [];
  const separationsUsers = {};
  const separationDates = [];
 
  dados.forEach((element) => {
    const formattedDate = moment(element.pickedAt).format('l');
    if (element.pickedQuantity && !separationDates.includes(formattedDate)) {
      separationDates.push(formattedDate);
      if (
        !organizedData.some((elem) => elem.pickedUser === element.pickedUser)
      ) {
        organizedData.push(element);
      }
    }
 
    if (!separationsUsers[element.pickedUser]) {
      separationsUsers[element.pickedUser] = { pickedQuantity: 0 };
    }
 
    separationsUsers[element.pickedUser].pickedQuantity =
      separationsUsers[element.pickedUser] &&
      separationsUsers[element.pickedUser].pickedQuantity
        ? parseFloat(separationsUsers[element.pickedUser].pickedQuantity) +
          (parseFloat(element.pickedQuantity) || 0)
        : parseFloat(element.pickedQuantity) || 0;
 
    separationsUsers[element.pickedUser][formattedDate] =
      (separationsUsers[element.pickedUser][formattedDate] || 0) +
      parseFloat(element.pickedQuantity || 0);
  });
 
  separationDates.sort((a, b) => moment(a, 'l').diff(moment(b, 'l')));
 
  headers.push(...separationDates);
 
  organizedData.forEach((element) => {
    rows.push([
      element.outboundOrder.branchCode,
      element.outboundOrder.branch.name,
      element.outboundRange.rangeCode,
      element.pickedUserObj.login || '',
      separationsUsers[element.pickedUser].pickedQuantity,
      ...separationDates.map(
        (date) => separationsUsers[element.pickedUser][date] || 0
      ),
    ]);
  });
}