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 | 3x 3x 2x 1x 1x | import moment from 'moment-timezone'; import { t } from '../../helpers/i18n'; import APIError from '../../helpers/error'; import InboundOrderDao from '../../database/dao/inboundOrder'; import TypeOrderInformation from '../typeOrders/information'; import InboundOrderInformation from './information'; import InboundOrdersImport from './import'; async function createInboundOrder(inboundOrder) { try { const newInboundOrder = await InboundOrderDao.createInboundOrder({ branchCode: inboundOrder.branchCode, supplierCode: inboundOrder.supplierCode, orderNumber: inboundOrder.orderNumber, orderType: inboundOrder.orderType, serie: inboundOrder.serie, specie: inboundOrder.specie, accessKey: inboundOrder.accessKey, carrierCode: inboundOrder.carrierCode, licensePlate: inboundOrder.licensePlate, expectedVolumes: inboundOrder.expectedVolumes, status: inboundOrder.status, cfop: inboundOrder.cfop, deliveryDate: inboundOrder.deliveryDate, postalCode: inboundOrder.postalCode, ibge: inboundOrder.ibge, street: inboundOrder.street, number: inboundOrder.number, complement: inboundOrder.complement, district: inboundOrder.district, city: inboundOrder.city, state: inboundOrder.state, country: inboundOrder.country, note: inboundOrder.note, canceled: inboundOrder.canceled, createdUser: inboundOrder.createdUser, createdAt: moment().format(), updatedUser: inboundOrder.updatedUser, updatedAt: moment().format(), }); return newInboundOrder; } catch (error) { console.log(error); throw new APIError('', error); } } /** * Can import multiple inbound orders for csv file * @param {*} bulkImportOrders * @param {*} userId * @returns */ async function createBulkImportInboundOrder(bulkImportOrders, userId) { const bulkImport = bulkImportOrders || []; const groupProductsByOrderNumber = (orders) => { return orders.reduce((acc, cur) => { const { orderNumber } = cur; if (!orderNumber) return acc; if (!acc[orderNumber]) { acc[orderNumber] = { products: [] }; } acc[orderNumber].products.push({ lineNumber: cur.lineNumber, productCode: cur.productCode, supplierProductCode: cur.supplierProductCode || cur.productCode, quantity: parseFloat(cur.quantity), deliveryDate: cur.productDeliveryDate, unitMeasure: cur.unitMeasure, purchaseRequest: cur.purchaseRequest, purchaseOrder: cur.purchaseOrder, installment: cur.installment, crossDockingOrder: cur.crossDockingOrder, crossDockingQuantity: cur.crossDockingQuantity, batchNumber: cur.batchNumber, expirationDate: cur.expirationDate, note: cur.productNote || '', }); return acc; }, {}); }; const validateOrder = async (order, processedOrders) => { const validOrder = await this.validOrderNumber(order); if (!validOrder.isValid) { return { line: order.line, error: validOrder.code, message: validOrder.message, }; } const validOrderTypeResult = await this.validOrderType(order); if (!validOrderTypeResult.isValid) { return { line: order.line, error: validOrderTypeResult.code, message: validOrderTypeResult.message, }; } const result = await InboundOrdersImport.importInboundOrders( [order], userId ); if (result.listInboundOrdersErrors.length > 0) { const [error] = result.listInboundOrdersErrors; return { line: order.line, error: error.code, message: error.message, }; } processedOrders.add(order.orderNumber); return { line: order.line, error: '', message: t('BEE3928', { 0: order.orderNumber }), // Documento de entrada %{0} criado com sucesso }; }; const productsByOrder = groupProductsByOrderNumber(bulkImport); const filteredBulkImport = bulkImport.filter((order) => { if (order.orderNumber) { order.canceled = !!( order.canceled && order.canceled.toLowerCase() === 'sim' ); order.products = productsByOrder[order.orderNumber] ? productsByOrder[order.orderNumber].products : []; } return Object.keys(order).length === 39; }); const orderNumberProcessed = new Set(); for (const newInboundOrder of filteredBulkImport) { if (!orderNumberProcessed.has(newInboundOrder.orderNumber)) { newInboundOrder.error = await validateOrder( newInboundOrder, orderNumberProcessed ); } else { newInboundOrder.error = { line: newInboundOrder.line, error: '', message: t('BEE3928', { 0: newInboundOrder.orderNumber }), // Documento de entrada %{0} criado com sucesso }; } } return filteredBulkImport; } async function validOrderNumber(inboundOrder) { const { branchCode, supplierCode, orderNumber, orderType } = inboundOrder; const existOrder = await InboundOrderInformation.getInboundOrderValidation({ branchCode, supplierCode, orderNumber, orderType, }); if (existOrder) { return { isValid: false, code: 9017, message: t('BEE3926', { 0: orderNumber }), // Documento já cadastrado }; } return { isValid: true, code: '', message: '', }; } async function validOrderType(inboundOrder) { const typeOrder = await TypeOrderInformation.getTypeOrder({ branchCode: inboundOrder.branchCode, type: inboundOrder.orderType, }); if (typeOrder && !typeOrder.inboundManualRequest) { return { isValid: false, code: 9019, message: t('BEE3915'), // A filial não possui o tipo de documento de requisição manual no recebimento }; } return { isValid: true, code: '', message: '', }; } export default { createInboundOrder, validOrderNumber, validOrderType, createBulkImportInboundOrder, }; |