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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | 23x 23x 23x 23x 1x 1x 1x 23x 1x 1x 1x 23x 1x 1x 1x 23x 1x 1x 1x 23x 1x 1x 1x 22x 22x 1x 1x 1x 23x 1x 1x 1x 22x 22x 1x 1x 1x 23x 1x 1x 1x 22x 22x 1x 1x 1x 23x 1x 1x 1x 22x 1x 1x 1x 23x 10x 1x 1x 1x 9x 1x 1x 1x 23x 1x 1x 1x 23x 10x 6x 6x 3x 2x 2x 2x 2x 1x 1x 2x 1x 1x 4x 4x 3x 3x 1x 1x 1x 23x 23x 23x 23x 23x 23x 17x 6x 6x 6x 6x 3x 93x 3x 1x 3x 3x 6x 2x 1x 1x 1x 1x 1x 23x | /* eslint-disable no-restricted-syntax */ import { Op } from 'sequelize'; import { t } from '../../helpers/i18n'; import UserInformation from '../users/information'; import BranchInformation from '../branches/information'; import StockInformation from '../stock/information'; import StockUpdate from '../stock/update'; import StockGroupInformation from '../stockGroup/information'; import UnitMeasureInformation from '../unitsMeasure/information'; import ProductCreate from './create'; import ProductInformation from './information'; import ProductUpdate from './update'; import MaterialFamilyInformation from '../materialsFamily/information'; import inboundOrderProducts from '../inboundOrderProducts/information'; import { models } from '../../../config/database'; async function validProduct(product, userApi) { let isValid = true; let code = ''; let message = ''; if (!product.productCode) { isValid = false; code = 9000; message = t('BEE1171' /* Código deve ser informado ! */); } if (!product.name) { isValid = false; code = 9002; message = t('BEE1344' /* Descrição deve ser informada */); } if (!product.fullName) { isValid = false; code = 9003; message = t('BEE1349' /* Descrição completa deve ser informada */); } if (product.status != null && product.status !== 1 && product.status !== 0) { isValid = false; code = 9021; message = t('BEE3145' /* Status inválido */); } if (!product.unitMeasure) { isValid = false; code = 9004; message = t('BEE1303' /* Unidade de medida deve ser informada */); } else { const existUnit = await UnitMeasureInformation.getUnitMeasure({ code: product.unitMeasure, }); if (!existUnit) { isValid = false; code = 9005; message = t( 'BEE1211', { 0: product.unitMeasure } /* Unidade de medida %{0} inválida ! */ ); } } if (!product.materialFamily) { isValid = false; code = 9006; message = t('BEE3149' /* Família Material deve ser informada! */); } else { const existMaterialFamily = await MaterialFamilyInformation.getMaterialFamily( { code: product.materialFamily, }, { attributes: ['id'], include: [], } ); if (!existMaterialFamily) { isValid = false; code = 9011; message = `${t('BEE4165' /* Erro de integração */)}: ${t( 'BEE4166', { 0: product.materialFamily, } /* A Família Material '%{0}' não está cadastrada no WMS BeeStock. Por favor, cadastre a Família Material e reprocesse a integração. */ )}`; } } if (!product.stockGroup) { isValid = false; code = 9013; message = t( 'BEE3740', { 0: t('BEE4009' /* Grupo de Estoque */) } /* %{0} deve ser informado */ ); } else { const existStockGroup = await StockGroupInformation.getStockGroup({ code: product.stockGroup, }); if (!existStockGroup) { isValid = false; code = 9001; message = t( 'BEE3672', { 0: t('BEE4009' /* Grupo de Estoque */) } /* %{0} inválido */ ); } } if (!product.productControlType) { isValid = false; code = 9007; message = t('BEE1350' /* Tipo de controle do produto deve ser informado */); } else if (product.productControlType > 5) { isValid = false; code = 9008; message = t( 'BEE1351', { 0: product.productControlType, } /* Tipo de controle do produto %{0} inválido */ ); } if (userApi.branchUser.company.stockControlType === 0) { if (!product.stockControlType) { isValid = false; code = 9009; message = t( 'BEE1352' /* Tipo de controle de estoque deve ser informado */ ); } else if (product.stockControlType > 4) { isValid = false; code = 9010; message = t( 'BEE1353', { 0: product.stockControlType, } /* Tipo de controle de estoque %{0} inválido */ ); } } if (product.origin && product.origin > 2) { isValid = false; code = 9012; message = t( 'BEE1355', { 0: product.origin } /* Origem do produto %{0} inválida */ ); } if (userApi.branchUser.company.stockControlType === 0) { if (product.stockControlType !== 3) { const existProduct = await ProductInformation.getProduct({ productCode: product.productCode, }); if (existProduct) { if (existProduct.stockControlType === 3) { const branches = await BranchInformation.getAllBranches({}); for (const branch of branches) { const existStock = await StockInformation.getStockBalancesValid({ productCode: product.productCode, [Op.and]: [ { branchCode: branch.code }, { quantity: { [Op.ne]: 0 } }, { depositCode: { [Op.ne]: branch.preReceiptDeposit } }, { lotNumber: { [Op.ne]: '' } }, ], }); if (existStock && existStock.length > 0) { isValid = false; break; } } if (!isValid) { code = 9018; message = t( 'BEE2922', { 0: product.productCode, } /* Produto %{0} possui saldo com LOTE informado */ ); } } } } else { const existProduct = await ProductInformation.getProduct({ productCode: product.productCode, }); if (existProduct && existProduct.stockControlType !== 3) { const productsPendingStorage = await inboundOrderProducts.getInboundOrderProductsDocAndInfo({ productCode: product.productCode, status: [1, 2, 3], quantityCheck: { [Op.gt]: 0, }, }); if (productsPendingStorage && productsPendingStorage.length > 0) { isValid = false; code = 9019; message = t( 'BEE4287', { 0: product.productCode, } /* Produto %{0} pendente de armazenamento */ ); } } } } return { isValid, code, message, }; } async function importProducts(products, userId) { const listProductsErrors = []; const userApi = await UserInformation.getUser({ id: userId }); for (const product of products) { const valid = await validProduct(product, userApi); if (!valid.isValid) { listProductsErrors.push({ code: product.productCode, error: valid.code, message: valid.message, }); } else { const volume = product ? parseFloat(product.height) * parseFloat(product.width) * parseFloat(product.length) : null; const newProduct = { productCode: product.productCode, name: product.name, fullName: product.fullName, fullDescription: product.fullDescription, complementCode: product.complementCode, complementInfo: product.complementInfo, unitMeasure: product.unitMeasure, materialFamily: product.materialFamily, stockGroup: product.stockGroup, productControlType: product.productControlType, stockControlType: product.stockControlType, volume: volume > 9999999 ? null : volume, height: product.height || null, width: product.width || null, length: product.length || null, grossWeight: product.grossWeight || null, netWeight: product.netWeight || null, origin: product.origin || null, taxClassification: product.taxClassification || null, conversionFactor: product.conversionFactor || null, multipleSale: product.multipleSale || null, businessUnit: product.businessUnit || null, level1: product.level1 || null, level2: product.level2 || null, level3: product.level3 || null, level4: product.level4 || null, level5: product.level5 || null, note: product.note, status: product.status, createdUser: userId, updatedUser: userId, }; const existProduct = await ProductInformation.getProduct( { productCode: product.productCode, }, { attributes: ['id', 'stockControlType'], include: [ { model: models.UnitMeasure, as: 'unit', attributes: ['id'], }, { model: models.MaterialFamily, as: 'family', attributes: ['id'], }, ], } ); if (existProduct) { Object.keys(newProduct).forEach( (key) => newProduct[key] == null && delete newProduct[key] ); if (userApi.branchUser.company.stockControlType === 1) { delete newProduct.stockControlType; } await ProductUpdate.updateProduct( existProduct.id, newProduct, {}, false ); } else { await ProductCreate.createProduct(newProduct); } if (existProduct && userApi.branchUser.company.stockControlType === 0) { if ( existProduct.stockControlType !== 3 && product.stockControlType === 3 ) { const lotNumber = 'PADRAO'; const branches = await BranchInformation.getAllBranches({}); for (const branch of branches) { const { preReceiptDeposit, code } = branch; await StockUpdate.updateLotNumberStockBalance( { productCode: product.productCode, [Op.and]: [ { branchCode: code }, { depositCode: { [Op.ne]: preReceiptDeposit } }, { lotNumber: { [Op.eq]: '' } }, ], }, { lotNumber, updatedUser: userId } ); } } } } } return { listProductsErrors }; } export default { importProducts, }; |