All files / source/reports stockExpiredProducts.js

100% Statements 59/59
77.46% Branches 55/71
100% Functions 9/9
100% Lines 55/55

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                  10x 27x 7x 7x 7x           20x 20x 16x         10x   10x 10x 10x       10x       10x       10x   10x 10x 10x 10x   10x       10x         10x 10x 26x   26x   26x               6x     26x 5x   26x           5x       10x                   10x 10x   10x 1x     10x   10x 10x         27x 10x 10x 10x   10x                                                                                                                                   10x 27x   10x 27x     10x 10x 10x 11x       11x         11x               10x          
import moment from 'moment-timezone';
import { Op } from 'sequelize';
 
import { models, sequelize } from '../../../config/database';
import stockBalanceDao from '../../database/dao/stockBalance';
import { mountWhereBasedFilter } from '../../helpers/reports/sequelize';
import NumberFormatter from '../../helpers/number/format';
 
function addInformation(headersValue) {
  headersValue.forEach((element) => {
    if (element === 'balance') {
      headersValue.push('quantity');
      headersValue.push('allocated');
      headersValue.push('reserved');
    }
  });
}
 
function remove(headersValue, value) {
  const index = headersValue.indexOf(value);
  if (index !== -1) {
    headersValue.splice(index, 1);
  }
}
 
function printInformation(element, rows, columnsValuesToFind) {
  const tableData = element;
 
  tableData.createdUser = element.userCreated.login;
  tableData.updatedUser = element.userUpdated.login;
  tableData.branchCode =
    element.branch && element.branch.name
      ? `${element.branchCode} - ${element.branch.name}`
      : '';
  tableData.warehouseCode =
    element.warehouse && element.warehouse.name
      ? `${element.warehouseCode} - ${element.warehouse.name}`
      : '';
  tableData.deposelementCode =
    element.deposelement && element.deposelement.name
      ? `${element.deposelementCode} - ${element.deposelement.name}`
      : '';
  tableData.name =
    element.product && element.product.name ? element.product.name : '';
  tableData.quantelementy = element.quantelementy || 0;
  tableData.allocated = element.allocated || 0;
  tableData.reserved = element.reserved || 0;
  tableData.balance =
    element.quantelementy - element.allocated - element.reserved;
  tableData.lastEntranceUser =
    element.userLastEntrance && element.userLastEntrance.login
      ? element.userLastEntrance.login
      : '';
  tableData.lastCountUser =
    element.userLastCount && element.userLastCount.login
      ? element.userLastCount.login
      : '';
 
  const row = [];
  for (let index = 0; index < columnsValuesToFind.length; index += 1) {
    const column = columnsValuesToFind[index];
 
    row.push(tableData[column]);
 
    if (
      (column === 'createdAt' ||
        column === 'updatedAt' ||
        column === 'lastEntranceAt' ||
        column === 'lastCountAt' ||
        column === 'manufacturingDate') &&
      row[index]
    ) {
      row[index] = moment(row[index]).format('L LTS');
    }
 
    if (column === 'expirationDate') {
      row[index] = moment(row[index]).format('L');
    }
    if (
      (column === 'balance' ||
        column === 'quantity' ||
        column === 'allocated') &&
      row[index] !== null
    ) {
      row[index] = NumberFormatter.format(row[index], 4);
    }
  }
 
  rows.push(row);
}
 
export default async function generateData(
  columns,
  filters = null,
  headers = [],
  rows = [],
  opts = {}
) {
  const queryFilters = filters;
  const { isCloseToExpiration = null } = filters;
 
  if (queryFilters && queryFilters.isCloseToExpiration) {
    delete queryFilters.isCloseToExpiration;
  }
 
  const where = mountWhereBasedFilter(queryFilters);
 
  Eif (!where[Op.and]) where[Op.and] = {};
  where[Op.and].expirationDate = {
    [Op.ne]: moment.utc('1000-01-01 00:00:00').toDate(),
    [Op.not]: null,
  };
 
  let columnsValuesToFind = columns.map((column) => column.value);
  addInformation(columnsValuesToFind);
  remove(columnsValuesToFind, 'name');
  remove(columnsValuesToFind, 'balance');
 
  const data = await stockBalanceDao.findStockBalances(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.Warehouse,
        as: 'warehouse',
        attributes: ['name'],
      },
      {
        required: false,
        model: models.Deposit,
        as: 'deposit',
        attributes: ['name'],
      },
      {
        required: false,
        model: models.StorageAddress,
        as: 'address',
        attributes: ['name', 'curve'],
        where: {
          branchCode: {
            [Op.eq]: sequelize.col('StockBalance.str_branch_code'),
          },
        },
      },
      {
        required: false,
        model: models.Product,
        as: 'product',
        attributes: ['name', 'outboundPreExpiration'],
      },
      {
        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);
  });
 
  const todayDate = moment().startOf('day').toDate();
  Eif (data) {
    data.forEach((element) => {
      const expirationDate = moment(element.expirationDate)
        .startOf('day')
        .toDate();
 
      const diffDate = parseInt(
        moment(expirationDate).diff(todayDate, 'days'),
        10
      );
 
      if (
        diffDate <= 0 ||
        (element.product &&
          element.product.outboundPreExpiration &&
          isCloseToExpiration &&
          isCloseToExpiration.value &&
          diffDate <= parseInt(element.product.outboundPreExpiration, 10))
      ) {
        printInformation(element, rows, columnsValuesToFind);
      }
    });
  }
}