All files / source/reports stockStockValuedBalance.js

100% Statements 36/36
78% Branches 39/50
100% Functions 6/6
100% Lines 33/33

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                30x 30x 18x                     6x 33x 6x 6x 6x 6x 6x   6x                                                                                                                                                                         6x 33x   6x 33x     6x 5x         5x       5x   5x     5x 5x               5x   5x               5x 5x 21x   21x   21x 2x   21x       4x     5x      
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';
 
function remove(headersValue, value) {
  const index = headersValue.indexOf(value);
  if (index !== -1) {
    headersValue.splice(index, 1);
  }
}
 
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  const where = mountWhereBasedFilter(filters);
  let columnsValuesToFind = columns.map((column) => column.value);
  remove(columnsValuesToFind, 'name');
  remove(columnsValuesToFind, 'balance');
  remove(columnsValuesToFind, 'curve');
  remove(columnsValuesToFind, 'unitMeasure');
  remove(columnsValuesToFind, 'materialFamily');
 
  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.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'],
          include: [
            {
              attributes: ['code', 'name'],
              required: false,
              model: models.MaterialFamily,
              as: 'family',
            },
            {
              attributes: ['averageCost'],
              required: false,
              model: models.ProductBranch,
              as: 'productBranch',
              where: {
                branchCode: {
                  [Op.eq]: sequelize.col('StockBalance.str_branch_code'),
                },
              },
            },
          ],
        },
        {
          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.branchCode =
      it.branchCode && it.branch && it.branch.name
        ? `${it.branchCode} - ${it.branch.name}`
        : '';
 
    it.depositCode =
      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.balance =
      it.product &&
      it.product.productBranch &&
      it.product.productBranch.length > 0 &&
      it.product.productBranch[0].averageCost
        ? it.quantity * it.product.productBranch[0].averageCost
        : 0;
 
    it.curve = it.address && it.address.curve ? it.address.curve : '';
 
    it.materialFamily =
      it.product &&
      it.product.family &&
      it.product.family.code &&
      it.product.family.name
        ? `${it.product.family.code} - ${it.product.family.name}`
        : '';
 
    const row = [];
    for (let index = 0; index < columnsValuesToFind.length; index++) {
      const column = columnsValuesToFind[index];
 
      row.push(it[column]);
 
      if (column === 'expirationDate' && row[index]) {
        row[index] = moment(row[index]).format('l LTS');
      }
      if (
        (column === 'quantity' || column === 'balance') &&
        row[index] !== null
      ) {
        row[index] = NumberFormatter.format(row[index], 4);
      }
    }
    rows.push(row);
  });
}