All files / source/reports stockProductsWithoutMovement.js

0% Statements 0/21
0% Branches 0/12
0% Functions 0/1
0% Lines 0/21

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                                                                                                                                                                                                                                                                                               
/* eslint-disable no-param-reassign */
import { Op } from 'sequelize';
import { t } from '../../helpers/i18n';
import stockBalanceDao from '../../database/dao/stockBalance';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
export default async function generateData(
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const movementDate = filters ? filters.movementDate : false;
 
  const queryFilters = { ...filters };
 
  const startDate = movementDate.value[0];
  const endDate = movementDate.value[1];
 
  if (queryFilters && queryFilters.movementDate) {
    delete queryFilters.movementDate;
  }
  const where = mountWhereBasedFilter(queryFilters);
 
  headers.push({ header: t('BEE145' /* Filial */, undefined, appLanguage) });
 
  headers.push({ header: t('BEE182' /* Depósito */, undefined, appLanguage) });
 
  headers.push({ header: t('BEE267' /* Endereço */, undefined, appLanguage) });
 
  headers.push({
    header: t('BEE378' /* Código do Produto */, undefined, appLanguage),
  });
  headers.push({
    header: t('BEE758' /* Descrição do Produto */, undefined, appLanguage),
  });
 
  headers.push({
    header: t('BEE1756' /* Unidade de Medida */, undefined, appLanguage),
  });
 
  headers.push({
    header: t('BEE2668' /* Família Material */, undefined, appLanguage),
  });
 
  headers.push({
    header: t('BEE4242' /* Curva ABC */, undefined, appLanguage),
  });
 
  headers.push({
    header: t(
      'BEE4241' /* Quantidade Atual no Estoque */,
      undefined,
      appLanguage
    ),
  });
 
  headers.push({
    header: t('BEE4239' /* Última Movimentação */, undefined, appLanguage),
  });
 
  headers.push({
    header: t(
      'BEE4240' /* Usuário da última movimentação */,
      undefined,
      appLanguage
    ),
  });
 
  const movementedProducts = await stockBalanceDao.findStockBalances(where, {
    attributes: [
      'branchCode',
      'depositCode',
      'addressCode',
      'productCode',
      'lastMovingDate',
      'lastMovingUser',
      'quantity',
    ],
    include: [
      {
        required: false,
        model: models.Branch,
        as: 'branch',
        attributes: ['name'],
      },
      {
        required: false,
        model: models.Deposit,
        as: 'deposit',
        attributes: ['name'],
      },
      {
        required: false,
        model: models.Product,
        as: 'product',
        attributes: ['fullDescription', 'unitMeasure', 'materialFamily'],
        include: [
          {
            required: false,
            model: models.ProductBranch,
            as: 'productBranch',
            attributes: ['curve'],
          },
        ],
      },
      {
        required: false,
        model: models.StockMovement,
        as: 'movements',
        where: {
          movementDate: {
            [Op.between]: [startDate, endDate],
          },
        },
      },
    ],
    ...opts,
    where: {
      ...where,
      '$movements.id$': null,
    },
  });
 
  for (const element of movementedProducts) {
    rows.push([
      `${element.branchCode} - ${element.branch.name}`,
      `${element.depositCode} - ${element.deposit.name}`,
      element.addressCode,
      element.productCode,
      element.product.fullDescription,
      element.product.unitMeasure,
      element.product.materialFamily,
      element.product.productBranch[0].curve,
      element.quantity ? NumberFormatter.format(element.quantity, 4) : 0,
      element.lastMovingDate,
      element.lastMovingUser,
    ]);
  }
}