All files / source/reports productBranch.js

92.3% Statements 36/39
66.66% Branches 20/30
100% Functions 7/7
91.42% Lines 32/35

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                                    1x 9x 1x 1x 1x   1x                                                 9x 1x 9x 9x 1x   9x     1x 1x 1x   1x 1x         1x     1x     1x 1x 9x   14x 9x 2x   9x 1x     1x       1x         1x   1x         1x              
/* eslint-disable no-param-reassign */
import moment from 'moment-timezone';
import { t } from '../../helpers/i18n';
import NumberFormatter from '../../helpers/number/format';
import ProductProductBranchDao from '../../database/dao/productBranch';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import { models } from '../../../config/database';
 
// Funcao criada para popular o array headers e rows enviados por parametro
async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  typeDoc = 'xlsx',
  opts = {},
  appLanguage = 'ptBr'
) {
  const where = mountWhereBasedFilter(filters);
  const headersValue = columns.map((it) => it.value);
  const index = headersValue.indexOf('product.name');
  Eif (index !== -1) {
    headersValue.splice(index, 1);
  }
  const dados = await ProductProductBranchDao.findProductBranches(where, {
    attributes: headersValue,
    include: [
      {
        required: false,
        model: models.Product,
        as: 'product',
        attributes: ['name'],
      },
      {
        required: false,
        model: models.User,
        as: 'userCreated',
        attributes: ['login'],
      },
      {
        required: false,
        model: models.User,
        as: 'userUpdated',
        attributes: ['login'],
      },
    ],
    ...opts,
  });
 
  const headersValueRes = columns.map((it) => it.value);
  columns.forEach((it) => {
    const header = { header: it.label };
    if (it.value === 'averageCost') {
      header.numFmt = '0.00';
    }
    headers.push(header);
  });
 
  dados.forEach((it) => {
    it.createdUser = it.userCreated.dataValues.login;
    it.updatedUser = it.userUpdated.dataValues.login;
 
    if (it.receivesExpired === true || it.receivesExpired === 1) {
      it.receivesExpired = t('BEE172' /* Sim */, undefined, appLanguage);
    } else E{
      it.receivesExpired = t('BEE173' /* Não */, undefined, appLanguage);
    }
 
    Iif (it.pickingExpired === true || it.pickingExpired === 1) {
      it.pickingExpired = t('BEE172' /* Sim */, undefined, appLanguage);
    } else {
      it.pickingExpired = t('BEE173' /* Não */, undefined, appLanguage);
    }
 
    const row = [];
    for (let idx = 0; idx < headersValueRes.length; idx += 1) {
      const column = headersValueRes[idx];
 
      row.push(column.split('.').reduce((o, i) => o[i] || '', it));
      if (column === 'createdAt' || column === 'updatedAt') {
        row[idx] = moment(row[idx]).format('l LTS');
      }
      if (column === 'averageCost') {
        Iif (typeDoc === 'pdf') {
          row[idx] = NumberFormatter.format(row[idx], 3);
        } else {
          row[idx] = parseFloat(row[idx]);
        }
      }
    }
    rows.push(row);
  });
}
 
async function getCountProductBranch(filters, filterPermission, opts = {}) {
  const where = mountWhereBasedFilter(filters);
 
  const dados = await ProductProductBranchDao.countProductBranches(
    filterPermission ? { ...where, ...filterPermission } : where,
    opts
  );
 
  return dados;
}
 
export default {
  generateData,
  getCountProductBranch,
};