All files / source/stock delete.js

100% Statements 14/14
100% Branches 7/7
100% Functions 1/1
100% Lines 14/14

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                                          3x                       3x 3x             3x   2x       2x                   2x             2x               2x 1x                         1x 1x                         1x           2x            
/* eslint-disable default-param-last */
import moment from 'moment';
 
import StockBalanceDao from '../../database/dao/stockBalance';
import StockMovementDao from '../../database/dao/stockMovement';
import APIError from '../../helpers/error';
import { t } from '../../helpers/i18n';
import SerialGroupInformation from '../serialGroup/information';
import SerialGroupUpdate from '../serialGroup/update';
import SerialLabelUpdate from '../serialLabel/update';
 
async function deleteStockMovement(
  depositCode,
  orderNumber,
  productCode,
  productLineNumber,
  // eslint-disable-next-line default-param-last
  type,
  userId,
  transaction = null
) {
  const stockMovements = await StockMovementDao.findStockMovements(
    {
      depositCode,
      orderNumber,
      productCode,
      productLineNumber,
      type,
    },
    { transaction }
  );
 
  // eslint-disable-next-line no-restricted-syntax
  for (const mov of stockMovements) {
    const balance = await StockBalanceDao.findStockBalance(
      {
        id: mov.stockBalanceId,
      },
      { transaction }
    );
 
    if (balance) {
      const newBalanceQuantity =
        mov.type === 1
          ? parseFloat(balance.quantity) - parseFloat(mov.quantity)
          : parseFloat(balance.quantity) + parseFloat(mov.quantity);
 
      await StockBalanceDao.updateStockBalance(
        { id: balance.id },
        {
          quantity: newBalanceQuantity,
          updatedUser: userId,
          updatedAt: moment().format(),
        },
        { transaction }
      );
 
      await StockMovementDao.deleteStockMovement(
        {
          id: mov.id,
        },
        { transaction }
      );
 
      const serialGroup = await SerialGroupInformation.getSerialGroup(
        {
          id: mov.serialGroupId,
        },
        { transaction }
      );
 
      // DESATUALIZAR DADOS SERIAL LABEL / SERIAL GROUP
      if (serialGroup) {
        await SerialGroupUpdate.updateSerialGroup(
          serialGroup.id,
          {
            entranceCheckedAt: null,
            branchCode: serialGroup.originalBranchCode,
            productCode: serialGroup.originalProductCode,
            stockBalanceId: 0,
            depositCode: '',
            updatedUser: userId,
          },
          { transaction }
        );
 
        for (const serial of serialGroup.serialLabels) {
          await SerialLabelUpdate.updateSerialLabel(
            { id: serial.id },
            {
              branchCode: serial.originalBranchCode,
              depositCode: '',
              productCode: serial.originalProductCode,
              updatedUser: userId,
            },
            { transaction }
          );
        }
      }
    } else {
      throw new APIError(
        '',
        t('BEE3183' /* Saldo de estoque não localizado */)
      );
    }
  }
  return true;
}
 
export default {
  deleteStockMovement,
};