All files / source/reports stockStockBalance.js

100% Statements 47/47
75% Branches 45/60
100% Functions 8/8
100% Lines 44/44

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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182                                3x 9x 3x 3x 3x 3x 3x   3x                                                                                                                                               3x 9x   3x 9x     3x 3x 3x 3x   3x       3x       3x   3x     3x 3x 3x 3x   3x       3x   3x   3x 3x 9x   9x   9x                 3x     9x           2x     3x         12x 12x 6x         3x 9x 2x 2x 2x        
import { Op } from 'sequelize';
 
import moment from 'moment-timezone';
import stockBalanceDao from '../../database/dao/stockBalance';
import { models, sequelize } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
// Funcao criada para popular o array headers e rows enviados por parametro
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  const where = mountWhereBasedFilter(filters);
  let columnsValuesToFind = columns.map((column) => column.value);
  addInformation(columnsValuesToFind);
  remove(columnsValuesToFind, 'name');
  remove(columnsValuesToFind, 'balance');
  remove(columnsValuesToFind, 'curve');
  remove(columnsValuesToFind, 'unitMeasure');
 
  const dados = await stockBalanceDao.findStockBalances(
    {
      quantity: { [Op.gt]: 0 },
      ...where,
    },
    {
      attributes: columnsValuesToFind,
      include: [
        {
          required: true,
          model: models.User,
          as: 'userCreated',
          attributes: ['login'],
        },
        {
          required: true,
          model: models.User,
          as: 'userUpdated',
          attributes: ['login'],
        },
        {
          required: false,
          model: models.Branch,
          as: 'branch',
          attributes: ['name'],
        },
        {
          required: false,
          model: models.Warehouse,
          as: 'warehouse',
          attributes: ['name'],
        },
        {
          required: false,
          model: models.Deposit,
          as: 'deposit',
          attributes: ['name'],
        },
        {
          required: false,
          model: models.StorageAddress,
          as: 'address',
          where: {
            branchCode: {
              [Op.eq]: sequelize.col('StockBalance.str_branch_code'),
            },
          },
          attributes: ['name', 'curve'],
        },
        {
          required: false,
          model: models.Product,
          as: 'product',
          attributes: ['name', 'unitMeasure'],
        },
        {
          required: true,
          model: models.User,
          as: 'userLastEntrance',
          attributes: ['login'],
        },
        {
          required: false,
          model: models.User,
          as: 'userLastCount',
          attributes: ['login'],
        },
      ],
      ...opts,
    }
  );
 
  columnsValuesToFind = null;
  columnsValuesToFind = columns.map((it) => it.value);
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  dados.forEach((it) => {
    it.createdUser = it.userCreated.login;
    it.updatedUser = it.userUpdated.login;
    it.branchCode =
      it.branch && it.branch.name ? `${it.branchCode} - ${it.branch.name}` : '';
    it.warehouseCode =
      it.warehouse && it.warehouse.name
        ? `${it.warehouseCode} - ${it.warehouse.name}`
        : '';
    it.depositCode =
      it.deposit && it.deposit.name
        ? `${it.depositCode} - ${it.deposit.name}`
        : '';
    it.name = it.product && it.product.name ? it.product.name : '';
 
    it.unitMeasure =
      it.product && it.product.unitMeasure ? it.product.unitMeasure : '';
 
    it.quantity = it.quantity ? it.quantity : 0;
    it.allocated = it.allocated ? it.allocated : 0;
    it.reserved = it.reserved ? it.reserved : 0;
    it.balance = it.quantity - it.allocated - it.reserved;
 
    it.lastEntranceUser =
      it.userLastEntrance && it.userLastEntrance.login
        ? it.userLastEntrance.login
        : '';
    it.lastCountUser =
      it.userLastCount && it.userLastCount.login ? it.userLastCount.login : '';
    it.curve = it.address && it.address.curve ? it.address.curve : '';
 
    const row = [];
    for (let index = 0; index < columnsValuesToFind.length; index++) {
      const column = columnsValuesToFind[index];
 
      row.push(it[column]);
 
      if (
        (column === 'createdAt' ||
          column === 'updatedAt' ||
          column === 'lastEntranceAt' ||
          column === 'lastCountAt' ||
          column === 'expirationDate' ||
          column === 'manufacturingDate') &&
        row[index]
      ) {
        row[index] = moment(row[index]).format('l LTS');
      }
 
      if (
        (column === 'balance' ||
          column === 'quantity' ||
          column === 'allocated') &&
        row[index] !== null
      ) {
        row[index] = NumberFormatter.format(row[index], 4);
      }
    }
    rows.push(row);
  });
}
 
function remove(headersValue, value) {
  const index = headersValue.indexOf(value);
  if (index !== -1) {
    headersValue.splice(index, 1);
  }
}
 
function addInformation(headersValue) {
  headersValue.forEach((element) => {
    if (element === 'balance') {
      headersValue.push('quantity');
      headersValue.push('allocated');
      headersValue.push('reserved');
    }
  });
}