All files / source/reports allocationAddress.js

100% Statements 36/36
88.13% Branches 52/59
100% Functions 5/5
100% Lines 34/34

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                            3x 3x 51x   3x                                                                                                                     3x 51x     3x 3x     3x 3x 3x 3x     3x 3x 3x 3x 3x 3x       3x 3x 3x 3x 3x 3x 3x 3x         3x 51x 51x 51x           2x   51x 3x     3x        
import moment from 'moment-timezone';
import OutboundOrderProductAllocationDao from '../../database/dao/outboundOrderProductAllocation';
import StorageAddressTypes from '../../database/enumerators/storageAddressTypes';
import { models } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  const where = mountWhereBasedFilter(filters.whereStockAllocation);
  const whereAddress = mountWhereBasedFilter(filters.whereAddress);
  const columnsValues = columns.map((it) => it.value);
  const dados =
    await OutboundOrderProductAllocationDao.findOutboundOrderProductAllocations(
      where,
      {
        attributes: ['id', 'branchCode', 'stockBalanceId', 'quantity'],
        include: [
          {
            required: false,
            model: models.StockBalance,
            as: 'balance',
            attributes: [
              'id',
              'addressCode',
              'lotNumber',
              'expirationDate',
              'allocated',
            ],
            include: [
              {
                required: true,
                model: models.StorageAddress,
                as: 'address',
                attributes: [
                  'id',
                  'rangeCode',
                  'sector',
                  'street',
                  'level',
                  'column',
                  'drawer',
                  'type',
                  'curve',
                ],
                where: whereAddress,
              },
            ],
          },
          {
            required: false,
            model: models.Branch,
            as: 'branch',
            attributes: ['code', 'name'],
          },
          {
            required: true,
            model: models.Product,
            as: 'product',
            attributes: ['productCode', 'name', 'stockControlType'],
          },
          {
            required: false,
            model: models.OutboundOrder,
            as: 'outboundOrder',
            attributes: ['id', 'orderNumber'],
          },
        ],
        ...opts,
      }
    );
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  const allocationAddressList = dados.length
    ? dados.map((o) => o.toJSON())
    : [];
 
  allocationAddressList.forEach((it) => {
    Eif (it.balance) {
      const row = [];
      it.branchCode = it.branch
        ? `${it.branch.code} - ${it.branch.name}`
        : it.branchCode;
      it.orderNumber = (it.outboundOrder && it.outboundOrder.orderNumber) || '';
      it.productCode = (it.product && it.product.productCode) || '';
      it.expirationDate = it.balance.expirationDate || '';
      it.quantity = it.balance.allocated || 0;
      it.addressCode = it.balance.addressCode || '';
      it.lotNumber =
        it.product.stockControlType === 3 && it.balance.lotNumber
          ? it.balance.lotNumber
          : ''; // DOC.OBS: apresentar Lote apenas para os produtos com este tipo de controle
      it.range = (it.balance.address && it.balance.address.rangeCode) || '';
      it.sector = (it.balance.address && it.balance.address.sector) || '';
      it.street = (it.balance.address && it.balance.address.street) || '';
      it.column = (it.balance.address && it.balance.address.column) || '';
      it.level = (it.balance.address && it.balance.address.level) || '';
      it.drawer = (it.balance.address && it.balance.address.drawer) || '';
      it.curve = (it.balance.address && it.balance.address.curve) || '';
      it.type =
        it.balance.address && it.balance.address.type
          ? StorageAddressTypes.getStatusValues(it.balance.address.type)
          : 0;
 
      for (let index = 0; index < columnsValues.length; index++) {
        const column = columnsValues[index];
        row.push(it[column]);
        if (
          column === 'expirationDate' &&
          row[index] &&
          it.product.stockControlType === 1
        ) {
          // DOC.OBS: quando o produto for do tipo Serial(tipo 1), no campo Data Vencimento deverá ser preenchido. O campo Lote ficará “em branco”
          row[index] = moment(row[index]).format('l LTS');
        }
        if (column === 'quantity' && row[index] !== null) {
          row[index] = NumberFormatter.format(row[index], 4);
        }
      }
      rows.push(row);
    }
  });
}