All files / source/reports retail.js

100% Statements 9/9
56.52% Branches 13/23
100% Functions 2/2
100% Lines 9/9

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                        1x 1x   1x                                                                                                                                                     1x 10x     1x 2x 2x   2x                                                                
import Sequelize, { Op } from 'sequelize';
import stockBalanceDao from '../../database/dao/stockBalance';
import { models, sequelize } from '../../../config/database';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
 
export default async function generateData(
  columns,
  filters,
  headers = [],
  rows = [],
  opts = {}
) {
  const where = mountWhereBasedFilter(filters);
  where[Op.and] = { ...where[Op.and], lotNumber: { [Op.ne]: '' } };
 
  const dados = await stockBalanceDao.findStockBalances(where, {
    attributes: [
      'branchCode',
      'productCode',
      'quantity',
      'lotNumber',
      [
        Sequelize.fn(
          'SUM',
          Sequelize.col(
            `${models.StockBalance.name}.${models.StockBalance.rawAttributes.quantity.field}`
          )
        ),
        'sumQuantity',
      ],
    ],
    group: ['branchCode', 'lotNumber'],
    include: [
      {
        require: false,
        model: models.Deposit,
        as: 'deposit',
        attributes: ['code'],
        where: { resale: 1 },
      },
      {
        required: false,
        model: models.Product,
        as: 'product',
        attributes: ['name', 'unitMeasure', 'grossWeight', 'minimalLeftOver'],
        include: [
          {
            required: false,
            model: models.ProductBranch,
            as: 'productBranch',
            attributes: ['averageCost'],
            where: {
              branchCode: {
                [Op.eq]: sequelize.col('StockBalance.str_branch_code'),
              },
            },
          },
        ],
      },
      {
        separate: true,
        require: false,
        model: models.StockMovement,
        as: 'movements',
        attributes: ['updatedAt', 'supplierCode', 'branchCode'],
        where: {
          type: 1,
          operation: 'NFE',
        },
        order: ['updatedAt', 'desc'],
        include: [
          {
            require: false,
            model: models.Deposit,
            as: 'deposit',
            attributes: ['code'],
            where: { resale: 1 },
          },
          {
            required: false,
            model: models.Supplier,
            as: 'supplier',
            attributes: ['businessName'],
          },
        ],
      },
    ],
    ...opts,
  });
 
  columns.forEach((it) => {
    headers.push(it.label);
  });
 
  for (const index in dados) {
    Eif (Object.hasOwnProperty.call(dados, index)) {
      const element = dados[index];
 
      rows.push([
        element.branchCode,
        element.productCode,
        element.product.name,
        element.product.unitMeasure,
        element.dataValues.sumQuantity
          ? parseFloat(element.dataValues.sumQuantity)
          : 0,
        element.dataValues.sumQuantity
          ? parseFloat(element.dataValues.sumQuantity)
          : 0,
        element.product.productBranch[0].averageCost
          ? parseFloat(element.product.productBranch[0].averageCost)
          : 0,
        element.product.grossWeight
          ? parseFloat(element.product.grossWeight)
          : 0,
        element.lotNumber,
        element.movements.length &&
        element.movements[0].supplier &&
        element.movements[0].supplier.name
          ? element.movements[0].supplier.name
          : '',
        element.movements.length &&
        element.movements[0].supplier &&
        element.movements[0].supplier.businessName
          ? element.movements[0].supplier.businessName
          : '',
      ]);
    }
  }
}