All files / source/reports productCurveDivergences.js

100% Statements 16/16
70.27% Branches 26/37
100% Functions 1/1
100% Lines 15/15

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                          1x 1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x                                                                                                                     1x 1x 1x                                      
import { Op } from 'sequelize';
import StockBalanceDao from '../../database/dao/stockBalance';
import I18n from '../../helpers/i18n';
import { models, sequelize } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
export default async function generateData(
  filters,
  headers = [],
  rows = [],
  opts = {},
  appLanguage = 'ptBr'
) {
  const where = mountWhereBasedFilter(filters);
  where[Op.and] = { ...where[Op.and], quantity: { [Op.gt]: 0 } };
 
  headers.push(I18n.t('BEE1994' /* Código Filial */, undefined, appLanguage));
  headers.push(I18n.t('BEE1997' /* Nome Filial */, undefined, appLanguage));
  headers.push(I18n.t('BEE1995' /* Código Depósito */, undefined, appLanguage));
  headers.push(I18n.t('BEE1996' /* Nome Depósito */, undefined, appLanguage));
  headers.push(I18n.t('BEE1999' /* Código Produto */, undefined, appLanguage));
  headers.push(I18n.t('BEE1998' /* Nome Produto */, undefined, appLanguage));
  headers.push(I18n.t('BEE432' /* Endereço */, undefined, appLanguage));
  headers.push(I18n.t('BEE1966' /* Curva Endereço */, undefined, appLanguage));
  headers.push(I18n.t('BEE1965' /* Curva Produto */, undefined, appLanguage));
 
  const dados = await StockBalanceDao.findStockBalances(where, {
    attributes: [
      'branchCode',
      'warehouseCode',
      'depositCode',
      'productCode',
      'addressCode',
    ],
    include: [
      {
        required: true,
        model: models.StorageAddress,
        as: 'address',
        where: {
          branchCode: {
            [Op.eq]: sequelize.col('StockBalance.str_branch_code'),
          },
        },
        attributes: ['curve'],
      },
      {
        required: true,
        model: models.Product,
        as: 'product',
        attributes: ['name', 'fullName'],
        include: [
          {
            required: true,
            model: models.ProductBranch,
            as: 'productBranch',
            attributes: ['curve'],
            where: {
              curve: { [Op.ne]: sequelize.col('address.str_curve') },
            },
          },
        ],
      },
      {
        required: true,
        model: models.Warehouse,
        as: 'warehouse',
        attributes: ['name', 'code'],
      },
      {
        required: true,
        model: models.Deposit,
        as: 'deposit',
        attributes: ['name', 'code'],
      },
      {
        required: true,
        model: models.Branch,
        as: 'branch',
        attributes: ['name', 'code'],
      },
    ],
    ...opts,
  });
 
  for (let index = 0; index < dados.length; index++) {
    const element = dados[index];
    rows.push([
      element.branch && element.branch.code ? element.branch.code : '',
      element.branch && element.branch.name ? element.branch.name : '',
      // (element.warehouse && element.warehouse.code && element.warehouse.name) ? `${element.warehouse.code} - ${element.warehouse.name}` : '',
      element.deposit && element.deposit.code ? element.deposit.code : '',
      element.deposit && element.deposit.name ? element.deposit.name : '',
      element.productCode ? element.productCode : '',
      element.product && element.product.name ? element.product.name : '',
      // (element.product && element.product.fullName) ? element.product.fullName : '',
      element.addressCode ? element.addressCode : '',
      element.address && element.address.curve ? element.address.curve : '',
      element.product &&
      element.product.productBranch &&
      element.product.productBranch.curve
        ? element.product.productBranch.curve
        : '',
    ]);
  }
}