All files / source/inventoriesErp import.js

100% Statements 73/73
100% Branches 40/40
100% Functions 2/2
100% Lines 73/73

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                          16x 16x 16x   16x 1x 1x 1x   15x       15x 1x 1x 1x             16x 1x 1x 1x   15x       15x 1x 1x 1x             16x 1x 1x 1x   15x       15x 1x 1x 1x             16x 1x 1x 1x   15x       15x 1x 1x 1x             16x 1x 1x 1x 15x       1x 1x 1x           16x 1x 1x 1x   15x       15x 1x 1x 1x             16x           1x 1x 1x               16x           1x 1x 1x               16x               16x   16x 16x   16x 14x           2x                                       2x       16x            
/* eslint-disable no-restricted-syntax */
import moment from 'moment-timezone';
import validator from 'validator';
 
import { t } from '../../helpers/i18n';
import BranchInformation from '../branches/information';
import DepositInformation from '../deposits/information';
import ProductInformation from '../products/information';
import UnitMeasureInformation from '../unitsMeasure/information';
import WarehouseInformation from '../warehouses/information';
import InventoryErpCreate from './create';
 
async function validInventoryErp(inventoryErp) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!inventoryErp.productCode) {
    isValid = false;
    code = 9000;
    message = t('BEE1288' /* Código do produto deve ser informado */);
  } else {
    const exitProduct = await ProductInformation.getProduct({
      productCode: inventoryErp.productCode,
    });
 
    if (!exitProduct) {
      isValid = false;
      code = 9001;
      message = t(
        'BEE1233',
        { 0: inventoryErp.productCode } /* Produto %{0} não localizado */
      );
    }
  }
 
  if (!inventoryErp.branchCode) {
    isValid = false;
    code = 9002;
    message = t('BEE1285' /* Código da filial deve ser informado */);
  } else {
    const existBranch = await BranchInformation.getBranch({
      code: inventoryErp.branchCode,
    });
 
    if (!existBranch) {
      isValid = false;
      code = 9003;
      message = t(
        'BEE1170',
        { 0: inventoryErp.branchCode } /* Filial %{0} não localizada */
      );
    }
  }
 
  if (!inventoryErp.warehouseCode) {
    isValid = false;
    code = 9004;
    message = t('BEE1329' /* Armazém deve ser informado ! */);
  } else {
    const exitWarehouse = await WarehouseInformation.getWarehouse({
      code: inventoryErp.warehouseCode,
    });
 
    if (!exitWarehouse) {
      isValid = false;
      code = 9005;
      message = t(
        'BEE1236',
        { 0: inventoryErp.warehouseCode } /* Armazém %{0} não localizada */
      );
    }
  }
 
  if (!inventoryErp.depositCode) {
    isValid = false;
    code = 9006;
    message = t('BEE1330' /* Depósito deve ser informado ! */);
  } else {
    const exitDeposit = await DepositInformation.getDeposit({
      code: inventoryErp.depositCode,
    });
 
    if (!exitDeposit) {
      isValid = false;
      code = 9007;
      message = t(
        'BEE1193',
        { 0: inventoryErp.depositCode } /* Deposito %{0} não localizada */
      );
    }
  }
 
  if (!inventoryErp.quantity) {
    isValid = false;
    code = 9008;
    message = t('BEE1331' /* Quantidade deve ser informada */);
  } else if (
    typeof inventoryErp.quantity !== 'number' ||
    inventoryErp.quantity <= 0
  ) {
    isValid = false;
    code = 9009;
    message = t(
      'BEE1146',
      { 0: inventoryErp.quantity } /* Quantidade %{0} inválida ! */
    );
  }
 
  if (!inventoryErp.unitMeasure) {
    isValid = false;
    code = 9010;
    message = t('BEE1303' /* Unidade de medida deve ser informada */);
  } else {
    const existUnit = await UnitMeasureInformation.getUnitMeasure({
      code: inventoryErp.unitMeasure,
    });
 
    if (!existUnit) {
      isValid = false;
      code = 9011;
      message = t(
        'BEE1211',
        { 0: inventoryErp.unitMeasure } /* Unidade de medida %{0} inválida ! */
      );
    }
  }
 
  if (
    inventoryErp.fifoDate &&
    !validator.toDate(
      moment(inventoryErp.fifoDate, 'DD/MM/YYYY', true).format()
    )
  ) {
    isValid = false;
    code = 9012;
    message = t(
      'BEE1332',
      {
        0: inventoryErp.fifoDate,
      } /* Data FIFO %{0} com problema no formato! 'DD/MM/YYYY' */
    );
  }
 
  if (
    inventoryErp.expirationDate &&
    !validator.toDate(
      moment(inventoryErp.expirationDate, 'DD/MM/YYYY', true).format()
    )
  ) {
    isValid = false;
    code = 9013;
    message = t(
      'BEE1333',
      {
        0: inventoryErp.expirationDate,
      } /* Data de validade %{0} com problema no formato! 'DD/MM/YYYY'` */
    );
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function importInventoriesErp(inventoriesErp, userId) {
  const listInventoriesErpErrors = [];
 
  for (const inventoryErp of inventoriesErp) {
    const valid = await validInventoryErp(inventoryErp);
 
    if (!valid.isValid) {
      listInventoriesErpErrors.push({
        code: inventoryErp.productCode,
        error: valid.code,
        message: valid.message,
      });
    } else {
      const newInventoryErp = {
        productCode: inventoryErp.productCode,
        branchCode: inventoryErp.branchCode,
        warehouseCode: inventoryErp.warehouseCode,
        depositCode: inventoryErp.depositCode,
        quantity: inventoryErp.quantity,
        unitMeasure: inventoryErp.unitMeasure,
        storageAddress: inventoryErp.storageAddress,
        lotNumber: inventoryErp.lotNumber,
        fifoDate: inventoryErp.fifoDate
          ? moment(inventoryErp.fifoDate, 'DD/MM/YYYY', true).format()
          : new Date(moment().format('MM/DD/YYYY')),
        expirationDate: inventoryErp.expirationDate
          ? moment(inventoryErp.expirationDate, 'DD/MM/YYYY', true).format()
          : new Date(moment('12/31/9999').format('MM/DD/YYYY')),
        note: inventoryErp.note,
        createdUser: userId,
        updatedUser: userId,
      };
 
      await InventoryErpCreate.createInventoryErp(newInventoryErp);
    }
  }
 
  return { listInventoriesErpErrors };
}
 
export default {
  importInventoriesErp,
};