function ArrayToMap(inputArray, key1) { var result = {} inputArray.forEach(function (value) { result[value[key1]] = value }) return result } function ArrayToMapMultipleValues(inputArray, key1) { var result = {} inputArray.forEach(function (value) { if (!result[value[key1]]) result[value[key1]] = [value] else result[value[key1]].push(value) }) return result } function amazonEUCustomImportPostMapHook(options) { $$.logExecution('DEBUG', 'amazonEUCustomImportPostMapHook | options.postMapData', JSON.stringify(options.postMapData)); if (!(options.postMapData && options.postMapData.length > 0)) { return []; } var customLines = ['Celigo_GiftWrap_Substitute', 'Celigo_Shipping_Substitute', 'Celigo_ShippingDisc_Substitute', 'Celigo_Tax_Substitute'] options.postMapData.forEach(function (postMapDataJson) { if (postMapDataJson && postMapDataJson.nlobjFieldIds.internalid && postMapDataJson.exportedDoc.orderItem[0].rightTaxCode) { var r = new $R({ nlobjRecordType: postMapDataJson.nlobjFieldIds.recordType, id: postMapDataJson.nlobjFieldIds.internalid }); var items = r.get('item'); var preSubmitOrderTaxData = postMapDataJson.nlobjFieldIds.custbody_celigo_pre_order_submit_data if (preSubmitOrderTaxData) { preSubmitOrderTaxData = JSON.parse(preSubmitOrderTaxData) while (typeof (preSubmitOrderTaxData) === "string") { preSubmitOrderTaxData = JSON.parse(preSubmitOrderTaxData) } } var rightTaxCode = postMapDataJson.exportedDoc.orderItem[0].rightTaxCode if (postMapDataJson.nlobjSublistIds && postMapDataJson.nlobjSublistIds.item && postMapDataJson.nlobjSublistIds.item.lines && items) { var updatedItems = ArrayToMap(postMapDataJson.nlobjSublistIds.item.lines, 'item'); var updatedItemsSKU = ArrayToMap(postMapDataJson.nlobjSublistIds.item.lines, 'custcol_celigo_etail_original_sku') } var itemsSKU = ArrayToMapMultipleValues(items, 'custcol_celigo_etail_original_sku') var itemsMapInOrder = ArrayToMapMultipleValues(items, 'item') var itemsMapWithETailLine = ArrayToMap(items, 'custcol_celigo_etail_order_line_id') var finalItems = [] if (updatedItems || itemsSKU) { $$.logExecution('DEBUG', 'amazonEUCustomImportPostMapHook | updatedItemsSKU', JSON.stringify(updatedItemsSKU)); $$.logExecution('DEBUG', 'amazonEUCustomImportPostMapHook | itemsMapWithETailLine', JSON.stringify(itemsMapWithETailLine)); var lines = postMapDataJson.nlobjSublistIds.item.lines rightTaxCode = (lines && lines[0] && lines[0]['taxcode']) ? lines[0]['taxcode'] : rightTaxCode for (var eTailLineId in itemsMapWithETailLine) { $$.logExecution('DEBUG', 'amazonEUCustomImportPostMapHook | eTailLineId', eTailLineId); var lineItem = itemsMapWithETailLine[eTailLineId] var originalSKU = lineItem['custcol_celigo_etail_original_sku'] if (updatedItemsSKU[originalSKU] || customLines.indexOf(eTailLineId) > -1) { var currentLine = updatedItemsSKU[originalSKU] ? JSON.parse(JSON.stringify(updatedItemsSKU[originalSKU])) : {} currentLine['line'] = lineItem['line'] currentLine['taxcode'] = currentLine['taxcode'] || rightTaxCode delete currentLine['item'] if (eTailLineId === 'Celigo_Tax_Substitute' && postMapDataJson.exportedDoc.orderItem[0]["Tax Collection Responsibility"]==="Marketplace" && preSubmitOrderTaxData && rightTaxCode) currentLine['amount'] = 0 finalItems.push(currentLine) var discountLine = itemsMapWithETailLine['Celigo_Discount_Line_' + eTailLineId] if (discountLine) { currentLine = { line: discountLine['line'], taxcode: rightTaxCode } finalItems.push(currentLine) } if (preSubmitOrderTaxData && preSubmitOrderTaxData.items && preSubmitOrderTaxData.items[eTailLineId]) { preSubmitOrderTaxData.items[eTailLineId].taxcode = rightTaxCode if (preSubmitOrderTaxData.items['Celigo_Discount_Line_' + eTailLineId]) preSubmitOrderTaxData.items['Celigo_Discount_Line_' + eTailLineId].taxcode = rightTaxCode } } } postMapDataJson.nlobjSublistIds.item.lines = finalItems } if (postMapDataJson.exportedDoc.orderItem[0]["Tax Collection Responsibility"] === "Marketplace") { if (preSubmitOrderTaxData && rightTaxCode) { for (var orderLineId in preSubmitOrderTaxData.items) { preSubmitOrderTaxData.items[orderLineId].taxAmount = 0 preSubmitOrderTaxData.items[orderLineId].taxrate1 = "0.0%" preSubmitOrderTaxData.items[orderLineId].taxcode = rightTaxCode } if (preSubmitOrderTaxData.shippingtaxcode) { preSubmitOrderTaxData.shippingtaxcode = rightTaxCode preSubmitOrderTaxData.shippingtax1rate = "0.0%" } } } // body level shipping code if (preSubmitOrderTaxData && preSubmitOrderTaxData.shippingtaxcode) { postMapDataJson.nlobjFieldIds.shippingtaxcode = rightTaxCode preSubmitOrderTaxData.shippingtaxcode = rightTaxCode } postMapDataJson.nlobjFieldIds.custbody_celigo_pre_order_submit_data = JSON.stringify(preSubmitOrderTaxData) } else postMapDataJson.nlobjSublistIds.item.lines = [] }) $$.logExecution('DEBUG', 'amazonEUCustomImportPostMapHook | postMapResponseData', JSON.stringify(options.postMapData)); return options.postMapData.map(function (d) { return { data: d } }) } function amazonEUCustomImportPostSubmitHook(options) { $$.logExecution('DEBUG', 'amazonEUCustomImportPostSubmitHook | options.responseData', JSON.stringify(options.responseData)); if (!(options.responseData && options.responseData.length > 0)) { return options.responseData; } options.postMapData.forEach(function (postMapDataJson) { if (postMapDataJson.nlobjFieldIds.internalid) { var r = new $R({ nlobjRecordType: postMapDataJson.nlobjFieldIds.recordType, id: postMapDataJson.nlobjFieldIds.internalid }); var items = r.get('item'); var itemsMapWithETailLine = ArrayToMap(items, 'custcol_celigo_etail_order_line_id') var updatedItems if (postMapDataJson && postMapDataJson.nlobjSublistIds && postMapDataJson.nlobjSublistIds.item && postMapDataJson.nlobjSublistIds.item.lines) updatedItems = ArrayToMap(postMapDataJson.nlobjSublistIds.item.lines, 'line'); var preSubmitOrderTaxData = postMapDataJson.nlobjFieldIds.custbody_celigo_pre_order_submit_data if (updatedItems && preSubmitOrderTaxData) { preSubmitOrderTaxData = JSON.parse(preSubmitOrderTaxData) var finalItems = [] items.forEach(function (lineItem) { if (updatedItems[lineItem['line']] && updatedItems[lineItem['line']]['taxcode'] && preSubmitOrderTaxData.items[lineItem['custcol_celigo_etail_order_line_id']]) { lineItem['taxrate1'] = preSubmitOrderTaxData.items[lineItem['custcol_celigo_etail_order_line_id']].taxrate1 finalItems.push({ 'line': lineItem['line'], 'taxrate1': (itemsMapWithETailLine['Celigo_Tax_Substitute']) ? '0.0%' : lineItem['taxrate1'] }) } }) // body level shipping code if (preSubmitOrderTaxData.shippingtax1rate) { r.set('shippingtax1rate', preSubmitOrderTaxData.shippingtax1rate) } r.set({ item: finalItems }) r.save() } } }) return options.responseData }