All files / middlewares outboundProductAllocationValidations.js

100% Statements 45/45
100% Branches 43/43
100% Functions 5/5
100% Lines 44/44

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                    4x       4x             4x 1x           3x           3x           3x 3x       7x 7x   7x 1x     6x 6x 1x     5x 1x     4x 1x     3x       3x 1x           2x   2x 1x                         1x       6x 6x 6x     6x           6x             6x   6x       6x 6x 6x 1x                     3x 3x   3x   3x 3x     3x         1x                              
import APIError from '../helpers/error';
import { t } from '../helpers/i18n';
 
import OutboundProductAllocationsInformation from '../source/outboundOrderProductAllocations/information';
import StockInformation from '../source/stock/information';
import ProductBranchInformation from '../source/productBranches/information';
import BranchInformation from '../source/branches/information';
 
async function validOutboundProductAllocation(req) {
  const outboundProductAllocationId =
    req.body.outboundProductAllocationId ||
    req.query.outboundProductAllocationId;
 
  const productAllocation =
    await OutboundProductAllocationsInformation.getOutboundOrderProductAllocation(
      {
        id: outboundProductAllocationId,
      },
      { userBranches: req.userBranches }
    );
 
  if (!productAllocation) {
    throw new APIError(
      '',
      t('BEE3089' /* Alocação do Produto não localizada! */)
    );
  }
 
  req.product =
    productAllocation.outboundProduct &&
    productAllocation.outboundProduct.product
      ? productAllocation.outboundProduct.product
      : {};
 
  req.typeOrders =
    productAllocation.outboundOrder &&
    productAllocation.outboundOrder.typeOrders
      ? productAllocation.outboundOrder.typeOrders
      : {};
 
  req.quantity = productAllocation.quantity;
  req.productAllocation = productAllocation;
}
 
async function validBalancesToChange(req) {
  const { balances } = req.body;
  const deposit = [];
 
  if (!balances || !balances.length) {
    throw new APIError('', t('BEE3090' /* Nenhum saldo informado! */));
  }
 
  for (const balance of balances) {
    if (!balance.stockBalanceId) {
      throw new APIError('', t('BEE3182' /* Saldo de Estoque inválido! */));
    }
 
    if (!balance.quantity) {
      throw new APIError('', t('BEE1476' /* Quantidade Inválida! */));
    }
 
    if (balance.quantity <= 0) {
      throw new APIError('', t('BEE1476' /* Quantidade Inválida! */));
    }
 
    const stockBalance = await StockInformation.getStockBalance({
      id: balance.stockBalanceId,
    });
 
    if (!stockBalance) {
      throw new APIError(
        '',
        t('BEE3183' /* Saldo de estoque não localizado */)
      );
    }
 
    deposit.push(stockBalance.deposit.fractionalLabel);
 
    if (stockBalance.quantity < balance.quantity) {
      throw new APIError(
        '',
        t(
          'BEE3037',
          {
            0: stockBalance.quantity,
            1: balance.quantity,
          } /* Saldo Estoque não possui quantidade suficiente (%{0}) para atender a alocação solicitada (%{1})! */
        )
      );
    }
  }
 
  req.fractionalLabels = deposit;
}
 
async function validDepositFractionalLabel(req) {
  const { productCode } = req.product;
  const { prefix } = req.typeOrders;
  const { quantity, userMainBranch } = req;
 
  const productNotPrefix =
    await ProductBranchInformation.getProductBranchAndFactor({
      branchCode: userMainBranch,
      productCode: prefix ? productCode.replace(prefix, '') : productCode,
    });
 
  const factor =
    productNotPrefix &&
    productNotPrefix.productEans &&
    productNotPrefix.productEans.length &&
    productNotPrefix.productEans[0].factor
      ? productNotPrefix.productEans[0].factor
      : 1;
 
  const isMultiple = Number(quantity) % factor === 0;
 
  const { serialControlDeposit } = await BranchInformation.getBranch({
    code: userMainBranch,
  });
 
  for (let i = 0; i < req.fractionalLabels.length; i++) {
    const fractionalLabel = req.fractionalLabels[i];
    if (!isMultiple && serialControlDeposit && !fractionalLabel) {
      throw new APIError(
        '',
        t(
          'BEE3326' /* Produtos com quantidade fracionada não podem ser alocados para depósito não fracionado. */
        )
      );
    }
  }
}
 
async function validQuantity(req) {
  const { balances } = req.body;
  const { productAllocation } = req;
 
  let quantityToChange = 0;
 
  balances.forEach((balance) => {
    quantityToChange += parseFloat(balance.quantity);
  });
 
  if (
    quantityToChange >
    parseFloat(productAllocation.quantity) -
      parseFloat(productAllocation.pickedQuantity || 0)
  ) {
    throw new APIError(
      '',
      t(
        'BEE3539' /* A quantidade de troca de alocação ultrapassa a quantidade pendente para separação! */
      )
    );
  }
}
 
export default {
  validOutboundProductAllocation,
  validBalancesToChange,
  validDepositFractionalLabel,
  validQuantity,
};