All files / source/orderTypeConfig create.js

100% Statements 119/119
100% Branches 87/87
100% Functions 3/3
100% Lines 119/119

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 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299                                2x 2x                                 1x     1x 1x         21x 21x 21x   21x 1x 1x 1x   20x       20x 1x 1x 1x                 21x 19x 1x 1x 1x 18x 1x 1x 1x       21x 17x 1x 1x 1x   16x       16x 1x 1x 1x         21x 15x 1x 1x 1x 14x 10x     10x 1x 1x 1x                 4x     4x 1x 1x 1x               21x 12x 1x 1x 1x   11x       11x 1x 1x 1x         21x 10x 1x 1x 1x 9x         1x 1x 1x             21x 8x 1x 1x 1x       21x 7x 1x 1x 1x       21x 6x 1x 1x 1x   5x         5x 1x 1x 1x         21x 4x 1x 1x 1x 3x 1x 1x 1x       21x 2x                         2x 1x 1x 1x       21x               22x   22x 21x 21x 21x   21x   21x 20x           1x                                   1x   1x               22x                
import moment from 'moment-timezone';
 
import OrderTypeConfigDao from '../../database/dao/orderTypeConfig';
 
import BranchInformation from '../branches/information';
import CarrierInformation from '../carriers/information';
import CustomerInformation from '../customers/information';
import DepositInformation from '../deposits/information';
import SupplierInformation from '../suppliers/information';
import OrderTypeInformation from '../typeOrders/information';
import OrderTypeConfigInformation from './information';
 
import APIError from '../../helpers/error';
import { t } from '../../helpers/i18n';
 
async function createOrderTypeConfig(orderTypeConfig) {
  try {
    const newOrderTypeConfig = await OrderTypeConfigDao.createOrderTypeConfig({
      movementType: orderTypeConfig.movementType,
      branchCode: orderTypeConfig.branchCode,
      depositCode: orderTypeConfig.depositCode,
      customerSupplierCode: orderTypeConfig.customerSupplierCode,
      carrierCode: orderTypeConfig.carrierCode,
      deliveryType: orderTypeConfig.deliveryType,
      stockGroupCode: orderTypeConfig.stockGroupCode,
      cfop: orderTypeConfig.cfop,
      orderType: orderTypeConfig.orderTypeCode,
      status: orderTypeConfig.status || 1,
      note: orderTypeConfig.note,
      createdUser: orderTypeConfig.createdUser,
      createdAt: moment().format(),
      updatedUser: orderTypeConfig.updatedUser,
      updatedAt: moment().format(),
    });
    return newOrderTypeConfig;
  } catch (error) {
    // eslint-disable-next-line no-console
    console.log(error);
    throw new APIError('', error);
  }
}
 
async function validConfigs(configs) {
  let isValid = true;
  let code = '';
  let message = '';
 
  if (!configs.branchCode) {
    isValid = false;
    code = 9001;
    message = t('BEE1285' /* Código da filial deve ser informado */);
  } else {
    const branch = await BranchInformation.getBranch({
      code: configs.branchCode,
    });
 
    if (!branch) {
      isValid = false;
      code = 9002;
      message = t(
        'BEE1347',
        {
          0: configs.branchCode,
        } /* Filial código %{0} não encontrado */
      );
    }
  }
 
  if (isValid) {
    if (!configs.movementType) {
      isValid = false;
      code = 9003;
      message = t('BEE4095' /* O tipo de movimentação deve ser informado. */);
    } else if (configs.movementType !== 1 && configs.movementType !== 2) {
      isValid = false;
      code = 9004;
      message = t('BEE4096' /* O tipo de movimentação informado é inválido! */);
    }
  }
 
  if (isValid) {
    if (!configs.depositCode) {
      isValid = false;
      code = 9005;
      message = t('BEE193' /* Informe o depósito (obrigatório) ! */);
    } else {
      const deposit = await DepositInformation.getDeposit({
        code: configs.depositCode,
      });
 
      if (!deposit) {
        isValid = false;
        code = 9006;
        message = t('BEE4097' /* Depósito não encontrado! */);
      }
    }
  }
 
  if (isValid) {
    if (!configs.customerSupplierCode) {
      isValid = false;
      code = 9007;
      message = t('BEE4098' /* Cliente/Fornecedor deve ser informado! */);
    } else if (configs.movementType === 1) {
      const supplier = await SupplierInformation.getSupplier({
        code: configs.customerSupplierCode,
      });
      if (!supplier && configs.customerSupplierCode !== '*') {
        isValid = false;
        code = 9008;
        message = t(
          'BEE1250',
          {
            0: configs.customerSupplierCode,
          } /* Fornecedor %{0} não localizado */
        );
      }
      // Saída (Cliente)
    } else {
      const customer = await CustomerInformation.getCustomer({
        code: configs.customerSupplierCode,
      });
      if (!customer && configs.customerSupplierCode !== '*') {
        isValid = false;
        code = 9009;
        message = t(
          'BEE1190',
          { 0: configs.customerSupplierCode } /* Cliente %{0} não localizado */
        );
      }
    }
  }
 
  if (isValid) {
    if (!configs.carrierCode) {
      isValid = false;
      code = 9010;
      message = t('BEE1317' /* Código da transportadora deve ser informado */);
    } else {
      const carrier = await CarrierInformation.getCarrier({
        code: configs.carrierCode,
      });
 
      if (!carrier && configs.carrierCode !== '*') {
        isValid = false;
        code = 9011;
        message = t('BEE1938' /* Transportadora não localizada */);
      }
    }
  }
 
  if (isValid) {
    if (!configs.deliveryType && configs.deliveryType !== 0) {
      isValid = false;
      code = 9012;
      message = t('BEE1340' /* Tipo de entrega deve ser informado */);
    } else if (
      configs.deliveryType !== 0 &&
      configs.deliveryType !== 1 &&
      configs.deliveryType !== 2
    ) {
      isValid = false;
      code = 9013;
      message = t(
        'BEE1341',
        { 0: configs.deliveryType } /* Tipo de entrega %{0} inválido */
      );
    }
  }
 
  if (isValid) {
    if (!configs.stockGroupCode) {
      isValid = false;
      code = 9014;
      message = t('BEE4100' /* O grupo de estoque deve ser informado! */);
    }
  }
 
  if (isValid) {
    if (!configs.cfop) {
      isValid = false;
      code = 9015;
      message = t('BEE4101' /* A operação fiscal deve ser informada! */);
    }
  }
 
  if (isValid) {
    if (!configs.orderType) {
      isValid = false;
      code = 9016;
      message = t('BEE4102' /* O tipo de documento deve ser informado! */);
    } else {
      const orderType = await OrderTypeInformation.getTypeOrder({
        branchCode: configs.branchCode,
        type: configs.orderType,
      });
 
      if (!orderType) {
        isValid = false;
        code = 9017;
        message = t('BEE4103' /* Tipo de documento não encontrado! */);
      }
    }
  }
 
  if (isValid) {
    if (!configs.status && configs.status !== 0) {
      isValid = false;
      code = 9018;
      message = t('BEE1198' /* Status deve ser informado! */);
    } else if (configs.status !== 0 && configs.status !== 1) {
      isValid = false;
      code = 9019;
      message = t('BEE4104' /* Status informado inválido. */);
    }
  }
 
  if (isValid) {
    const orderTypeConfig = await OrderTypeConfigInformation.getOrderTypeConfig(
      {
        movementType: configs.movementType,
        branchCode: configs.branchCode,
        depositCode: configs.depositCode,
        customerSupplierCode: configs.customerSupplierCode,
        carrierCode: configs.carrierCode,
        deliveryType: configs.deliveryType,
        stockGroupCode: configs.stockGroupCode,
        cfop: configs.cfop,
      }
    );
 
    if (orderTypeConfig) {
      isValid = false;
      code = 9020;
      message = t('BEE4107' /* Regra já cadastrada! */);
    }
  }
 
  return {
    isValid,
    code,
    message,
  };
}
 
async function createBulkImportConfigs(bulkImportConfigs, user) {
  const bulkImport = bulkImportConfigs || [];
 
  for (const orderTypeConfig of bulkImport) {
    orderTypeConfig.deliveryType = parseInt(orderTypeConfig.deliveryType, 10);
    orderTypeConfig.status = parseInt(orderTypeConfig.status, 10);
    orderTypeConfig.movementType = parseInt(orderTypeConfig.movementType, 10);
 
    const valid = await validConfigs(orderTypeConfig);
 
    if (!valid.isValid) {
      orderTypeConfig.error = {
        line: orderTypeConfig.line,
        error: valid.code,
        message: valid.message,
      };
    } else {
      const newOrderTypeConfig = {
        movementType: orderTypeConfig.movementType,
        branchCode: orderTypeConfig.branchCode,
        depositCode: orderTypeConfig.depositCode,
        customerSupplierCode: orderTypeConfig.customerSupplierCode,
        carrierCode: orderTypeConfig.carrierCode,
        deliveryType: orderTypeConfig.deliveryType,
        stockGroupCode: orderTypeConfig.stockGroupCode,
        cfop: orderTypeConfig.cfop,
        orderType: orderTypeConfig.orderType,
        status: orderTypeConfig.status || 1,
        note: orderTypeConfig.note,
        createdUser: user,
        createdAt: moment().format(),
        updatedUser: user,
        updatedAt: moment().format(),
      };
 
      await OrderTypeConfigDao.createOrderTypeConfig(newOrderTypeConfig);
 
      orderTypeConfig.error = {
        line: orderTypeConfig.line,
        error: '',
        message: t('BEE4147' /* Regra criada com sucesso! */),
      };
    }
  }
 
  return bulkImport;
}
 
export default {
  createOrderTypeConfig,
  createBulkImportConfigs,
  validConfigs,
};