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 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | 4x 4x 4x 4x 1x 3x 2x 2x 1x 5x 5x 5x 5x 5x 1x 4x 4x 3x 3x 3x 1x 3x 2x 6x 6x 6x 6x 1x 5x 1x 4x 4x 3x 3x 2x 3x 2x 2x 2x 2x 3x 1x 1x 1x 2x 1x 1x 1x 7x 7x 7x 1x 1x 7x 7x 7x 1x 6x 6x 6x 6x 6x 6x 1x 5x 3x 2x 2x 2x 2x 1x 1x 1x 1x 3x 3x 5x 5x 5x 4x 3x 3x 3x 3x 3x 1x 2x 1x 4x 4x 1x 3x 3x 1x 2x 1x 5x 5x 5x 5x 5x 5x 5x 1x 4x 1x 3x 1x 2x 1x 3x 3x 3x 1x 2x 1x 3x 3x 3x 1x 2x 2x 2x 1x 1x 3x 3x 3x 1x 2x 1x 4x 4x 1x 3x 2x 4x 4x 4x 3x 3x 1x 6x 6x 6x 6x 6x 5x 5x 5x 5x 4x 1x 3x 2x 1x 2x 2x 1x 2x 2x 2x 1x 2x 2x 2x 2x 1x 1x | import moment from 'moment-timezone'; import { Op, Sequelize } from 'sequelize'; import APIError from '../helpers/error'; import { t } from '../helpers/i18n'; import AddressTransferWaveInformation from '../source/addressTransferWave/information'; import AddressTransferWaveProductsInformation from '../source/addressTransferWaveProducts/information'; import BranchInformation from '../source/branches/information'; import ProductBranchInformation from '../source/productBranches/information'; import ProductInformation from '../source/products/information'; import StockBalanceInformation from '../source/stock/information'; import StorageAddressInformation from '../source/storageAddresses/information'; import UserInformation from '../source/users/information'; import BaptismsInformation from '../source/baptisms/information'; /* import BaptismInformation from '../source/baptisms/information'; */ import InventoryInformation from '../source/inventory/information'; async function validBaptismLabel(req) { const baptismLabel = req.body.baptismLabel || req.query.baptismLabel || req.body.baptism || req.query.baptism; const branchCode = req.body.branchCode || req.query.branchCode || req.body.branch || req.query.branch; const { serialControlDeposit } = await BranchInformation.getBranch({ code: branchCode, }); if (!baptismLabel) { throw new APIError( 'INVALID_BAPTISM_LABEL', t('BEE1201' /* Etiqueta de batismo deve ser informada */) ); } else if (!serialControlDeposit) { const existUZ = await BaptismsInformation.getBaptism( { branchCode, code: baptismLabel, type: 3, // Transferência }, { attributes: ['id'] } ); if (!existUZ) { throw new APIError( '', t( 'BEE3756', { 0: baptismLabel, 1: t('BEE34' /* Transferência */), } /* Etiqueta UZ %{0} do tipo %{1} não gerada. Por favor, verifique! */ ) ); } } } async function validPickupAddress(req) { const pickupAddressId = req.body.pickupAddressId || req.query.pickupAddressId; const pickupAddress = req.body.pickupAddress || req.query.pickupAddress; const { userId } = req; const user = await UserInformation.getUser({ id: userId }); if (!(pickupAddressId || pickupAddress)) { throw new APIError('INVALID_PICKUP_ADDRESS'); } else { const existAddress = await StorageAddressInformation.getStorageAddress({ branchCode: user.mainBranch, ...(pickupAddressId ? { id: pickupAddressId } : {}), ...(pickupAddress ? { code: pickupAddress } : {}), }); if (!existAddress) throw new APIError('NOT_EXIST_PICKUP_ADDRESS'); req.body.pickupAddressId = existAddress.id; req.pickupAddressCode = existAddress.code; if (req.body && !req.body.pickupAddress) req.body.pickupAddress = existAddress.code; if (req.query && !req.query.pickupAddress) req.query.pickupAddress = existAddress.code; } } async function validStorageAddress(req) { const storageAddressId = req.body.storageAddressId || req.query.storageAddressId; const storageAddress = req.body.storageAddress || req.query.storageAddress; const pickupAddress = req.body.pickupAddress || req.query.pickupAddress; if (!(storageAddressId || storageAddress)) { throw new APIError('INVALID_STORAGE_ADDRESS'); } else if (storageAddress === pickupAddress) { throw new APIError( '', t('BEE4289' /* Não é possível realizar uma transferência para o mesmo endereço de origem. */) ); } else { const existAddress = await StorageAddressInformation.getStorageAddress({ branchCode: req.userMainBranch, ...(storageAddressId ? { id: storageAddressId } : {}), ...(storageAddress ? { code: storageAddress } : {}), }); if (!existAddress) throw new APIError('NOT_EXIST_STORES_ADDRESS'); req.storageAddressCode = existAddress.code; if (req.body && !req.body.storageAddress) req.body.storageAddress = existAddress.code; if (req.query && !req.query.storageAddress) req.query.storageAddress = existAddress.code; } } async function getMainBranch(req) { const { userId } = req; const user = await UserInformation.getUser({ id: userId, }); return user.mainBranch; } async function validBranch(req) { if ( req.body && Object.keys(req.body).length && !(req.body.branch || req.body.branchCode) ) { const mainBranch = await getMainBranch(req); req.body.branch = mainBranch; req.body.branchCode = mainBranch; } else if ( req.query && Object.keys(req.query).length && !(req.query.branch || req.query.branchCode) ) { const mainBranch = await getMainBranch(req); req.query.branch = mainBranch; req.query.branchCode = mainBranch; } } async function validBalanceAvailable(req) { const { branchCode, depositCode, depositCodeOut, productCode, lotNumber, pickupAddressId, pickupAddress, quantity, stockBalanceId, addressTransferWaveProductsId, } = req.body; let { expirationDate } = req.body; if (expirationDate === 'Invalid date') { expirationDate = null; req.body.expirationDate = null; } const { product } = req; const pickupAddressValue = await StorageAddressInformation.getStorageAddress({ branchCode, ...(pickupAddressId ? { id: pickupAddressId } : { code: pickupAddress }), }); if (product.controlExpirationDate && !expirationDate) { throw new APIError('', t('BEE2006' /* Data de validade não informada! */)); } const expirationDte = product.controlExpirationDate ? { expirationDate: { [Op.gte]: moment.utc(expirationDate).startOf('day').format(), [Op.lte]: moment.utc(expirationDate).endOf('day').format(), }, } : { expirationDate: [null, moment.utc('1000-01-01 00:00:00').toDate()], }; const IdStockBalance = stockBalanceId ? { id: stockBalanceId } : {}; const balanceAvailable = await StockBalanceInformation.getBalanceAvailable({ branchCode, depositCode: depositCodeOut || depositCode, addressCode: pickupAddressValue.code, productCode, lotNumber: lotNumber || '', quantity: { [Op.gt]: 0 }, ...expirationDte, ...IdStockBalance, }); let notConfirmProductWaveTransfer = false; let transferQuantity = 0; if (!balanceAvailable) { throw new APIError('', t('BEE1794' /* Saldo indisponível */)); } else if (!depositCodeOut) { if ( !balanceAvailable || parseFloat(balanceAvailable.quantity) < parseFloat(quantity) ) { const balance = balanceAvailable.quantity ? balanceAvailable.quantity : 0; const message = t('BEE1209', { 0: balance } /* Saldo disponível: %{0} */); throw new APIError('', message); } } else if (addressTransferWaveProductsId) { const dataAddressTransferWaveProduct = await AddressTransferWaveProductsInformation.getAddressTransferWaveProducts( { id: addressTransferWaveProductsId, } ); req.dataAddressTransferWaveProduct = dataAddressTransferWaveProduct; notConfirmProductWaveTransfer = dataAddressTransferWaveProduct.addressTransferWave.branch .notConfirmProductWaveTransfer; transferQuantity = dataAddressTransferWaveProduct.quantity; } req.stockBalanceId = balanceAvailable.id; req.newReserved = (notConfirmProductWaveTransfer && depositCodeOut ? -Number(transferQuantity) : 0) || depositCodeOut ? -Number(quantity) : Number(quantity); } async function validAllBalanceAvailable(req) { const { branchCode, depositCode, depositCodeOut, addressTransferWaveProductsId, } = req.body; const { dataAddressTransferWaveProduct } = req; if (addressTransferWaveProductsId) { if ( dataAddressTransferWaveProduct.addressTransferWave.branch .notConfirmProductWaveTransfer ) { const dataAddressTransferWaveProducts = await AddressTransferWaveProductsInformation.getAllAddressTransferWaveProducts( { idTransferDoc: dataAddressTransferWaveProduct.idTransferDoc, } ); for (const addressTransferWaveProduct of dataAddressTransferWaveProducts) { const expirationDte = addressTransferWaveProduct.product .controlExpirationDate ? { expirationDate: { [Op.gte]: moment .utc(addressTransferWaveProduct.expirationDate) .startOf('day') .format(), [Op.lte]: moment .utc(addressTransferWaveProduct.expirationDate) .endOf('day') .format(), }, } : { expirationDate: [ null, moment.utc('1000-01-01 00:00:00').toDate(), ], }; const balanceAvailable = await StockBalanceInformation.getBalanceAvailable({ branchCode, depositCode: depositCodeOut || depositCode, addressCode: addressTransferWaveProduct.pickupAddress, productCode: addressTransferWaveProduct.productCode, lotNumber: addressTransferWaveProduct.lotNumber || '', quantity: { [Op.gt]: 0 }, ...expirationDte, }); if (!balanceAvailable) { throw new APIError( '', `${addressTransferWaveProduct.productCode}: ${t( 'BEE1794' /* Saldo indisponível */ )}` ); } else if (!balanceAvailable || !balanceAvailable.reserved) { throw new APIError( '', t('BEE3328' /* Não há quantidade reservada para este item */) ); } } } } } async function validBranchInInventory(req) { const branchCode = req.body.branchCode || req.query.branchCode; if (!branchCode) { throw new APIError('', t('BEE3014' /* A filial deve ser informada */)); } else { const existBranch = await BranchInformation.getBranch({ code: branchCode, }); if (!existBranch) { throw new APIError( '', t('BEE1170', { 0: branchCode } /* Filial %{0} não localizada */) ); } if (existBranch.branchInInventory) { throw new APIError( '', t('BEE3015' /* Filial em processo de Inventário */) ); } } } async function validStatus(req) { const branch = req.query.branch || req.body.branch || req.query.branchCode || req.body.branchCode const baptism = req.query.baptism || req.body.baptism const isDetail = req.query.isDetail || req.body.isDetail || false const validateStatusStarted = Boolean( req.query.validateStatusStarted === 'true' || req.body.validateStatusStarted === 'true' ); const isMobileApp = Boolean(req.query.isMobileApp === 'true'); const addressTransferWave = await AddressTransferWaveInformation.getAddressTransferWave({ branchCode: branch, baptismLabel: baptism.trim(), }); if (!isDetail && addressTransferWave && addressTransferWave.status === 4) { throw new APIError( '', t( 'BEE2263', { 0: baptism, } /* O batismo %{0} esta associado a um documento que foi cancelado */ ) ); } else if ( validateStatusStarted && addressTransferWave && addressTransferWave.status === 4 ) { throw new APIError( '', t( 'BEE2263', { 0: baptism, } /* O batismo %{0} esta associado a um documento que foi cancelado */ ) ); } else if ( validateStatusStarted && addressTransferWave && addressTransferWave.status === 3 ) { throw new APIError('', t('BEE2299' /* Este documento já foi concluído */)); } else if ( !isMobileApp && validateStatusStarted && addressTransferWave && addressTransferWave.status === 2 ) { throw new APIError('', t('BEE2298' /* já foi iniciado a transferência */)); } } async function validStatusTransfer(req) { const { addressTransferWaveProductsId } = req.body; const addressTransferWaveProduct = await AddressTransferWaveProductsInformation.getAddressTransferWaveProducts( { id: addressTransferWaveProductsId, } ); if ( addressTransferWaveProduct.addressTransferWave && addressTransferWaveProduct.addressTransferWave.status === 4 ) { throw new APIError( '', t( 'BEE2263', { 0: addressTransferWaveProduct.addressTransferWave.baptismLabel, } /* O batismo %{0} esta associado a um documento que foi cancelado */ ) ); } else if ( addressTransferWaveProduct.addressTransferWave && addressTransferWaveProduct.addressTransferWave.status === 3 ) { throw new APIError('', t('BEE2299' /* Este documento já foi concluído */)); } } async function validAddressTransferWaveId(req) { const addressTransferWaveId = req.query.addressTransferWaveId || req.body.addressTransferWaveId; let AddressTransferWave = {}; if (!addressTransferWaveId) { throw new APIError('', t('BEE1915' /* Documento não localizada */)); } else { const addressTransferWave = await AddressTransferWaveInformation.getAddressTransferWave({ id: addressTransferWaveId, }); AddressTransferWave = addressTransferWave; if (!addressTransferWave) { throw new APIError( '', t('BEE1902' /* Não foi possível localizar o documento. */) ); } } req.addressTransferWave = AddressTransferWave; } async function validLotNumberAndExpirationDate(req) { const { productCode, lotNumber, expirationDate } = req.body; const ProductData = await ProductInformation.getProduct({ productCode }); if (ProductData && ProductData.stockControlType === 3 && !lotNumber) { throw new APIError('', t('BEE996' /* Informe o lote */)); } else if ( ProductData && ProductData.controlExpirationDate && !expirationDate ) { throw new APIError('', t('BEE2292' /* Informe a data de validade */)); } } async function validQuantity(req) { const { quantity } = req.body; if (quantity !== 0 && !quantity) { throw new APIError( '', t('BEE1039' /* É necessário informar a quantidade */) ); } else if (quantity <= 0) { throw new APIError( '', t('BEE2293' /* A quantidade tem que ser maior que 0 */) ); } } async function validateProductAlreadyAdd(req) { const { idTransferDoc, productCode, ean, lotNumber, expirationDate, serialGroupId, serialLabelId, } = req.body; const { pickupAddressCode } = req; if (idTransferDoc) { const addressTransferWaveProduct = await AddressTransferWaveProductsInformation.getAddressTransferWaveProducts( { idTransferDoc, productCode, lotNumber: lotNumber || '', expirationDate: expirationDate || null, pickupAddress: pickupAddressCode, [Op.and]: [ (serialGroupId ? { serialGroupId } : serialLabelId && { serialLabelId }) || { ean }, ], } ); if (addressTransferWaveProduct) throw new APIError( '', t('BEE2291' /* Produto já está vinculado nesse documento */) ); } } async function validSharedAddress(req) { const { branchCode, lotNumber, expirationDate } = req.body; const addressCode = req.body.storageAddress || req.body.addressCode; const productCode = req.body.productCode || req.productEan.productCode; const stockBalance = await StockBalanceInformation.getStockBalance({ addressCode, branchCode, productCode, quantity: { [Op.gt]: 0 }, expirationDate: expirationDate ? { [Op.gte]: moment.utc(expirationDate).startOf('day').format(), [Op.lte]: moment.utc(expirationDate).endOf('day').format(), } : [null, moment.utc('1000-01-01 00:00:00').toDate()], lotNumber: lotNumber || '', }); if (stockBalance) { const product = await ProductInformation.getProduct({ productCode }); const address = await StorageAddressInformation.getStorageAddress({ [Op.and]: [ { code: addressCode }, { branchCode }, Sequelize.where(Sequelize.col('addressType.str_status'), { [Op.in]: [6, 7], }), ], }); const productBranch = await ProductBranchInformation.getProductBranch({ branchCode, productCode, }); if ( (product.controlExpirationDate || product.stockControlType === 3) && !address ) { if ( product.controlExpirationDate && !moment(expirationDate).isSame(stockBalance.expirationDate, 'day') ) { throw new APIError( '', t( 'BEE2986' /* Data de validade diferente da armazenada no endereço */ ) ); } if ( lotNumber && product.stockControlType === 3 && stockBalance.lotNumber !== lotNumber ) { if ( !productBranch.storeProductsWithDifferentLotsAtSameAddress || !stockBalance.address.shareAddressOfProductsWithDifferentLots ) { throw new APIError( '', t('BEE2987' /* Lote diferente do endereço de armazenamento! */) ); } } } } } async function validDepositCode(req) { const { depositCode } = req.body; if (!depositCode) { throw new APIError( 'INVALID_DEPOSIT_CODE', t('BEE193' /* Informe o depósito (obrigatório) ! */) ); } } async function validProductInventorySheet(req) { const { pickupAddressId, productCode, branchCode } = req.body; const inventorySheets = await InventoryInformation.getAllInventorySheet({ branchCode, productCode, addressId: pickupAddressId, status: [1, 2, 3, 4], // PENDENTE, EM CONTAGEM, CONTAGEM FINALIZADA, APROVADO }); if (inventorySheets && inventorySheets.length > 0) { throw new APIError( '', t( 'BEE2538' /* Existe uma ficha de inventário com esse produto/endereço pendente */ ) ); } } async function validStorageAddressDelete(req) { const { addressTransferWaveProductId } = req.body; const addressTransferWaveProduct = await AddressTransferWaveProductsInformation.getAddressTransferWaveProducts( { id: addressTransferWaveProductId, } ); const stockBalance = await StockBalanceInformation.getStockBalance({ branchCode: addressTransferWaveProduct.addressTransferWave.branchCode, depositCode: addressTransferWaveProduct.addressTransferWave.depositCode, addressCode: addressTransferWaveProduct.pickupAddress, productCode: addressTransferWaveProduct.productCode, lotNumber: addressTransferWaveProduct.lotNumber, expirationDate: addressTransferWaveProduct.expirationDate ? addressTransferWaveProduct.expirationDate : [null, moment.utc('1000-01-01 00:00:00').toDate()], reserved: { [Op.gte]: addressTransferWaveProduct.quantity }, }); if (!stockBalance) { throw new APIError( '', t('BEE3332' /* Não é possível excluir sem saldo reservado. */) ); } req.stockBalanceId = stockBalance.id; } export default { validBaptismLabel, validPickupAddress, validBalanceAvailable, validAllBalanceAvailable, validStorageAddress, validBranch, validBranchInInventory, validStatus, validAddressTransferWaveId, validateProductAlreadyAdd, validLotNumberAndExpirationDate, validQuantity, validSharedAddress, validDepositCode, validProductInventorySheet, validStorageAddressDelete, validStatusTransfer, }; |