All files / source/reports inboundOrderPreEntry.js

100% Statements 39/39
65.38% Branches 17/26
100% Functions 7/7
100% Lines 35/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 111 112 113                          1x 7x 1x 1x 1x 1x 1x   1x                                                                           1x 7x   1x 7x     2x 1x 2x 2x 2x   2x 2x 3x 3x 3x 3x             2x 2x   2x 2x 14x   14x   14x       4x         2x         5x 5x 5x      
import moment from 'moment-timezone';
import { Op } from 'sequelize';
import inboundOrderDao from '../../database/dao/inboundOrder';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import { models, sequelize } from '../../../config/database';
 
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  const where = mountWhereBasedFilter(filters);
  let headersValue = columns.map((it) => it.value);
  remove(headersValue, 'branchName');
  remove(headersValue, 'businessName');
  remove(headersValue, 'name');
  remove(headersValue, 'orderTypeDescription');
  remove(headersValue, 'totalNoteValue');
 
  const dados = await inboundOrderDao.findInboundOrders(where, {
    attributes: headersValue,
    include: [
      {
        required: false,
        model: models.Branch,
        as: 'branch',
        attributes: ['name'],
      },
      {
        required: false,
        model: models.Supplier,
        as: 'supplier',
        attributes: ['name', 'businessName'],
      },
      {
        required: false,
        model: models.InboundOrderProduct,
        as: 'products',
        attributes: ['productCode', 'quantity'],
        include: [
          {
            required: false,
            model: models.ProductBranch,
            as: 'productBranch',
            attributes: ['branchCode', 'averageCost'],
            where: {
              branchCode: {
                [Op.eq]: sequelize.col('InboundOrders.str_branch_code'),
              },
            },
          },
        ],
      },
    ],
    ...opts,
  });
 
  headersValue = null;
  headersValue = columns.map((it) => it.value);
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  const inboundOrdersList = dados.length ? dados.map((o) => o.toJSON()) : [];
  inboundOrdersList.forEach((it) => {
    it.branchName = it.branch.name;
    it.businessName = (it.supplier && it.supplier.businessName) || '';
    it.name = (it.supplier && it.supplier.name) || '';
 
    let sumAverageCost = 0;
    for (const product of it.products) {
      Eif (product.productBranch && product.productBranch.length > 0) {
        const productBranch = product.productBranch[0];
        Eif (productBranch.averageCost) {
          sumAverageCost +=
            parseFloat(product.quantity) *
            parseFloat(productBranch.averageCost);
        }
      }
    }
 
    it.totalNoteValue = sumAverageCost;
    sumAverageCost = 0;
 
    const row = [];
    for (let index = 0; index < headersValue.length; index++) {
      const column = headersValue[index];
 
      row.push(it[column]);
 
      if (
        (column === 'createdAt' || column === 'deliveryDate') &&
        row[index] !== null
      ) {
        row[index] = moment(row[index]).isValid()
          ? moment(row[index]).format('L')
          : '';
      }
    }
    rows.push(row);
  });
}
 
function remove(headersValue, value) {
  const index = headersValue.indexOf(value);
  Eif (index != -1) {
    headersValue.splice(index, 1);
  }
}