All files / source/outboundOrders industry.js

100% Statements 17/17
100% Branches 17/17
100% Functions 2/2
100% Lines 17/17

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                                  4x 4x   4x 4x 2x                                                         2x     2x 1x                       2x     2x   4x                                                         4x             4x                                         4x 2x               2x                 2x            
import moment from 'moment-timezone';
 
import { sequelize } from '../../../config/database';
import OutboundOrderDao from '../../database/dao/outboundOrder';
import OutboundOrderProductDao from '../../database/dao/outboundOrderProduct';
import OutboundOrderProductInformation from '../outboundOrderProducts/information';
import OutboundOrderAllocate from './allocate';
import OutboundOrderInformation from './information';
import OutboundOrderUpdate from './update';
import OutboundOrderRange from './range';
import APIError from '../../helpers/error';
import { t } from '../../helpers/i18n';
 
async function createIndustryOutboundOrderPickingConfirm(
  industryOutboundOrder,
  outboundOrderId = null
) {
  let outboundOrder = null;
  let lineNumber = null;
 
  await sequelize.transaction(async (transaction) => {
    if (!outboundOrderId) {
      outboundOrder = await OutboundOrderDao.createOutboundOrder(
        {
          status: 1,
          branchCode: industryOutboundOrder.branchCode,
          customerCode: industryOutboundOrder.customerCode,
          orderNumber: industryOutboundOrder.orderNumber,
          orderType: industryOutboundOrder.orderType,
          depositCode: industryOutboundOrder.depositCode,
          pickingType: 2,
          urgent: false,
          completed: true,
          deliveryDate: moment().format(),
          customerOrderCode: industryOutboundOrder.orderNumber,
          erpOrderCode: industryOutboundOrder.orderNumber,
          orderDate: moment().format(),
          totalValue: industryOutboundOrder.totalValue,
          note: '',
          pickStartUser: industryOutboundOrder.userId,
          pickStartAt: moment().format(),
          checkStartUser: industryOutboundOrder.userId,
          checkStartAt: moment().format(),
          createdUser: industryOutboundOrder.userId,
          createdAt: moment().format(),
          updatedUser: industryOutboundOrder.userId,
          updatedAt: moment().format(),
        },
        { transaction }
      );
    } else {
      outboundOrder = await OutboundOrderInformation.getOutboundOrder({
        id: outboundOrderId,
      });
      if (industryOutboundOrder.totalValue > 0) {
        await OutboundOrderUpdate.updateOutboundOrder(
          outboundOrderId,
          {
            totalValue:
              parseFloat(outboundOrder.totalValue) +
              industryOutboundOrder.totalValue,
            updatedUser: industryOutboundOrder.userId,
          },
          { transaction }
        );
      }
      const lastLineNumber =
        await OutboundOrderProductInformation.getLastLineNumber(
          outboundOrderId
        );
      lineNumber = lastLineNumber + 1;
    }
    const products = {
      outboundOrderId: outboundOrderId || outboundOrder.id,
      branchCode: industryOutboundOrder.branchCode,
      serialGroupId: industryOutboundOrder.product.serialGroupId,
      productCode: industryOutboundOrder.product.productCode,
      lineNumber: lineNumber || 1,
      customerProductCode: industryOutboundOrder.product.productCode,
      quantity: industryOutboundOrder.product.quantity,
      allocatedQuantity: 0,
      picked: true,
      checked: true,
      minimumQuantity: 0,
      mandatoryQuantity: 0,
      value: industryOutboundOrder.product.value,
      status: 1,
      note: industryOutboundOrder.product.note,
      ediInfo: '',
      pickedQuantity: industryOutboundOrder.product.quantity,
      pickedAt: moment().format(),
      pickedUser: industryOutboundOrder.userId,
      checkedQuantity: industryOutboundOrder.product.quantity,
      checkedAt: moment().format(),
      checkedUser: industryOutboundOrder.userId,
      createdUser: industryOutboundOrder.userId,
      createdAt: moment().format(),
      updatedUser: industryOutboundOrder.userId,
      updatedAt: moment().format(),
    };
 
    const newProduct = await OutboundOrderProductDao.createOutboundOrderProduct(
      products,
      {
        transaction,
      }
    );
 
    const allocateProduct = await OutboundOrderAllocate.allocateProduct(
      outboundOrder,
      newProduct,
      null,
      industryOutboundOrder.userId,
      transaction,
      1,
      true,
      [
        {
          expirationDate: industryOutboundOrder.expirationDate
            ? moment(industryOutboundOrder.expirationDate).format()
            : [null, moment.utc('1000-01-01 00:00:00').toDate()],
        },
        ...(industryOutboundOrder.lotNumber
          ? [{ lotNumber: industryOutboundOrder.lotNumber }]
          : []),
      ],
      industryOutboundOrder.product.serialGroupId
    );
 
    if (allocateProduct.length === 0) {
      throw new APIError(
        '',
        t(
          'BEE2723' /* ROLLBACK: Não foi possível realizar a alocação do endereço. */
        )
      );
    }
 
    await OutboundOrderRange.createOutboundRanges(
      outboundOrderId || outboundOrder.id,
      allocateProduct,
      industryOutboundOrder.updatedUser,
      transaction,
      industryOutboundOrder.note,
      true
    );
  });
  return outboundOrder;
}
 
export default {
  createIndustryOutboundOrderPickingConfirm,
};