All files / source/stock allocation.js

100% Statements 11/11
100% Branches 8/8
100% Functions 1/1
100% Lines 10/10

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            5x 5x 4x 1x   3x                                   3x   3x   2x   2x           2x              
import APIError from '../../helpers/error';
import { t } from '../../helpers/i18n';
import StockUpdate from './update';
import AllocationCreate from '../outboundOrderProductAllocations/create';
 
async function createOutboundAllocation(product, stockBalance) {
  try {
    if (!product) throw new APIError('', t('BEE3140' /* Produto inválido! */));
    if (!stockBalance)
      throw new APIError('', t('BEE3182' /* Saldo de Estoque inválido! */));
 
    const allocation = {
      outboundOrderId: product.outboundOrderId,
      outboundOrderProductId: product.outboundOrderProductId,
      branchCode: stockBalance.branchCode,
      productCode: stockBalance.productCode,
      stockBalanceId: stockBalance.id,
      quantity: product.quantity,
      status: 1,
      baptismLabel: '',
      pickedQuantity: 0,
      pickedAt: null,
      pickedUser: null,
      note: '',
      createdUser: product.createdUser,
      updatedUser: product.updatedUser,
    };
 
    const newAllocation =
      await AllocationCreate.createOutboundOrderProductAllocation(allocation);
 
    if (newAllocation) {
      const allocated =
        parseFloat(stockBalance.allocated) - parseFloat(product.quantity || 0);
 
      await StockUpdate.updateStockBalance(stockBalance.id, {
        allocated,
        updatedUser: product.updatedUser,
      });
    }
  } catch (error) {
    throw new APIError(error);
  }
}
 
export default {
  createOutboundAllocation,
};