All files / source/reports outboundProductPendingAllocation.js

91.3% Statements 63/69
79.59% Branches 39/49
100% Functions 13/13
90.9% Lines 60/66

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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                          4x 4x 24x 8x 4x 24x 4x 4x 24x   4x 4x       4x 24x 4x 4x 24x 4x   4x                                                                                                                                                                                                   4x 24x   4x 4x   4x 3x 3x 18x     18x 6x 12x 3x 9x 3x 3x       6x 3x 3x       3x 3x         18x         9x     18x   3x 3x     1x 6x     6x 2x 4x 1x 3x 1x 1x       2x 1x 1x 1x         6x         2x     6x     1x        
import Sequelize, { Op } from 'sequelize';
import outboundOrderProductDao from '../../database/dao/outboundOrderProduct';
import { models, sequelize } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
// 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);
  const headersValueOutboundProduct = columns
    .filter((it) => it.table === 'outboundOrderProduct')
    .map((it) => it.value);
  const headersValueOutbound = columns
    .filter((it) => it.table === 'outboundOrder')
    .map((it) => it.value);
  const headersValueStock = columns
    .filter((it) => it.table === 'stockBalance' && it.value !== 'available')
    .map((it) => {
      Eif (it.value === 'stockQuantity') {
        return 'quantity';
      }
      return it.value;
    });
  const headersValueProduct = columns
    .filter((it) => it.table === 'product')
    .map((it) => (it.value === 'productName' ? 'name' : it.value));
  const headersValueClient = columns
    .filter((it) => it.table === 'customer')
    .map((it) => it.value);
 
  const dados = await outboundOrderProductDao.findOutboundOrderProducts(
    { ...where, status: 1 },
    {
      attributes: [
        'outboundOrderId',
        'lineNumber',
        ...headersValueOutboundProduct,
      ],
      include: [
        {
          required: true,
          model: models.OutboundOrder,
          as: 'outboundOrder',
          attributes: headersValueOutbound,
          include: [
            {
              attributes: ['resaleDeposit'],
              required: true,
              model: models.Branch,
              as: 'branch',
            },
            {
              attributes: headersValueClient,
              required: true,
              model: models.Customer,
              as: 'customer',
            },
            {
              required: false,
              model: models.TypeOrders,
              as: 'typeOrders',
              attributes: [],
              where: {
                branchCode: {
                  [Op.eq]: sequelize.col('outboundOrder.str_branch_code'),
                },
              },
            },
          ],
        },
        {
          separate: true,
          required: false,
          model: models.StockBalance,
          as: 'balances',
          attributes: [
            [
              Sequelize.literal(
                'balances.flo_quantity - (balances.flo_allocated + balances.flo_reserved)'
              ),
              'available',
            ],
            ...(headersValueStock || {}),
          ],
          where: {
            [Op.and]: [
              {
                branchCode: {
                  [Op.eq]: Sequelize.col(
                    'OutboundOrderProducts.str_branch_code'
                  ),
                },
              },
              Sequelize.where(
                Sequelize.col('balances.flo_quantity'),
                '>',
                Sequelize.where(
                  Sequelize.col('balances.flo_allocated'),
                  '+',
                  Sequelize.col('balances.flo_reserved')
                )
              ),
              Sequelize.literal(
                `CASE WHEN \`outboundOrder->typeOrders\`.\`str_allocate_balance_deposits\` IS NOT NULL 
                THEN \`outboundOrder->typeOrders\`.\`str_allocate_balance_deposits\` 
                ELSE 
                  CASE WHEN \`outboundOrder\`.\`str_deposit_code\` IS NOT NULL
                    THEN \`outboundOrder\`.\`str_deposit_code\`
                    ELSE \`outboundOrder->branch\`.\`str_resale_deposit\`
                  END
              END 
              LIKE CONCAT('%', \`balances\`.\`str_deposit_code\`, '%')`
              ),
            ],
          },
        },
        {
          required: true,
          model: models.Product,
          as: 'product',
          attributes: headersValueProduct,
        },
      ],
      order: ['outboundOrderId', 'lineNumber'],
      ...opts,
    }
  );
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
  dados.forEach((it) => {
    let row = [];
 
    if (it.balances.length > 0) {
      for (let idx = 0; idx < it.balances.length; idx++) {
        for (let index = 0; index < columns.length; index++) {
          const column = columns[index].value;
          let value;
 
          if (columns[index].table === 'outboundOrderProduct') {
            value = it[column];
          } else if (columns[index].table === 'outboundOrder') {
            value = it.outboundOrder[columns[index].value];
          } else if (columns[index].table === 'product') {
            if (columns[index].value === 'productName') {
              value = it.product.name;
            } else E{
              value = it.product[columns[index].value];
            }
          } else if (columns[index].table === 'stockBalance') {
            if (columns[index].value === 'stockQuantity') {
              value = it.balances[idx].dataValues.quantity;
            } else E{
              value = it.balances[idx].dataValues[columns[index].value];
            }
          } else if (columns[index].table === 'customer') {
            value = it.outboundOrder.customer[column];
          } else E{
            value = '';
          }
 
          if (
            columns[index].type === 'Number' &&
            value !== '' &&
            value !== null
          ) {
            value = parseFloat(value);
          }
 
          row.push(value);
        }
        rows.push(row);
        row = [];
      }
    } else {
      for (let index = 0; index < columns.length; index++) {
        const column = columns[index].value;
        let value;
 
        if (columns[index].table === 'outboundOrderProduct') {
          value = it[column];
        } else if (columns[index].table === 'outboundOrder') {
          value = it.outboundOrder[columns[index].value];
        } else if (columns[index].table === 'product') {
          if (columns[index].value === 'productName') {
            value = it.product.name;
          } else E{
            value = it.product[columns[index].value];
          }
        } else if (columns[index].table === 'stockBalance') {
          value = '';
        } else if (columns[index].table === 'customer') {
          value = it.outboundOrder.customer[column];
        } else E{
          value = '';
        }
 
        if (
          columns[index].type === 'Number' &&
          value !== '' &&
          value !== null
        ) {
          value = parseFloat(value);
        }
 
        row.push(value);
      }
 
      rows.push(row);
    }
  });
}