UNPKG

wholesale-vuex

Version:
477 lines (446 loc) 18 kB
import { snakeCase } from 'lodash' import humps from 'lodash-humps' import createHumps from 'lodash-humps/lib/createHumps' import { getField, updateField } from 'vuex-map-fields' import { checkExistOrNot, returnOriginalSkuQtyBySku } from './common.service.js' import wholesaleOrderItemService from './wholesale_order_item.service' const { doPostOne, doPatchOne, doFetchOne, doDeleteSelected, doExportPurchaseOrderItemsInExcel } = wholesaleOrderItemService const snakes = createHumps(snakeCase) const { v4 } = require('uuid') export default { namespaced: true, state: { one: null, oneVariantsObj: {}, inProgress: true, variantsListInProgress: true, variantsList: [], originalVariants: [], masterItemOne:null, masterItemOneIcpData:null, orderItemIcpData:null }, getters: { getField, }, mutations: { updateField, setOne(state, one) { state.one = one; if (state.one.variants.length > 0 && state.one.format === 'sku') { state.oneVariantsObj = Object.assign({}, state.one.variants[0]) let resultColor = state.oneVariantsObj.color let newColor = resultColor && resultColor.length && typeof resultColor == "object" ? resultColor[0] : resultColor && typeof resultColor == "string" ? resultColor : null; state.oneVariantsObj.color = newColor let resultSize = state.oneVariantsObj.size let newSize = resultSize && resultSize.length && typeof resultSize == "object" ? resultSize[0] : resultSize && typeof resultSize == "string" ? resultSize : null; state.oneVariantsObj.size = newSize let resultCategory = state.oneVariantsObj.category let newCategory = resultCategory && resultCategory.length && Array.isArray(resultCategory) ? resultCategory : typeof resultCategory == "object" ? resultCategory[0] : resultCategory && typeof resultCategory == "string" ? resultCategory : null; state.oneVariantsObj.category = newCategory } if (state.one.variants.length > 0 && state.one.format === "variant_size_color") { state.oneVariantsObj = Object.assign({}, state.one.variants[0]) /* size */ // let resultSize = state.one.variants.map(a =>a.size // ); // let newListSize=(resultSize.toString()).split(","); // state.oneVariantsObj.size=[...new Set(newListSize)]; state.one.variants.map((a) => { if (!a.qtyPacked){ a.qtyPacked=a.qty } }); let resultSize = [] state.one.variants.filter((a) => { // if (a.size) // will aslo add empty value in array resultSize.push(a.size+'') }); let newListSize = resultSize //&& resultSize.length ? (resultSize.toString()).split(",") : null; state.oneVariantsObj.size = newListSize && newListSize.length ? [...new Set(newListSize)] : null; /* color */ let resultColor = [] state.one.variants.filter((a) => { // if (a.color) // will aslo add empty value in array resultColor.push(a.color) }); let newListColor = resultColor && resultColor.length ? (resultColor.toString()).split(",") : null; state.oneVariantsObj.color = newListColor && newListColor.length ? [...new Set(newListColor)] : null; /* Category */ let resultCategory = state.one.variants.map(a => a.category ); let newListCategory = (resultCategory.toString()).split(","); state.oneVariantsObj.category = [...new Set(newListCategory)]; state.variantsList = state.one.variants; state.originalVariants = state.one.variants; } }, setItemMasterOne(state, orderItem) { if(orderItem && orderItem.shipmentCancelDateOld){ orderItem.shipmentCancelDate=orderItem.shipmentCancelDateOld delete orderItem.shipmentCancelDateOld } if(orderItem && orderItem.shipmentStartDateOld){ orderItem.shipmentStartDate=orderItem.shipmentStartDateOld delete orderItem.shipmentStartDateOld } state.masterItemOne = JSON.stringify(orderItem); }, inProgress(state, yesOrNo) { state.inProgress = yesOrNo }, resetFormFields(state) { state.one.name = '' state.one.brandProductId = '' state.one.quantities = '' state.one.importData = '' state.one.unitPrice = null }, setOneVariantsObj(state, oneItem) { state.oneVariantsObj = oneItem; }, resetOrderItemIcpData(state) { state.orderItemIcpData = null; }, variantsListInProgress(state, yesOrNo) { state.variantsListInProgress = yesOrNo; }, setNewVariantsList(state) { state.variantsList = []; state.originalVariants = [] }, setItemVariantsDamagedUrls(state,item){ if(state.variantsList.length > 0){ for(let i=0; i<state.variantsList.length;i++){ if(JSON.stringify(state.variantsList[i]) === JSON.stringify(item)){ state.variantsList[i].damagedUrls= item.damagedUrls } } } }, setItemVariantsList(state) { // state.variantsList = [] if(state.variantsList.length <= 0){ state.oneVariantsObj.category.filter((categoryItem) => { if (state.oneVariantsObj.color && state.oneVariantsObj.color.length > 0) { // if variants has color and size state.oneVariantsObj.color.filter((colorItem) => { if (state.oneVariantsObj.size && state.oneVariantsObj.size.length > 0) { state.oneVariantsObj.size.filter((sizeItem) => { state.variantsList.push( { variantId:v4(), sku: state.one.variantsRefCode + '-' + categoryItem + '-' + sizeItem + '-' + colorItem, category: categoryItem ? categoryItem : null, size: sizeItem ? sizeItem : null, color: colorItem ? colorItem : null, qty: 0, //qty: state.oneVariantsObj.qty, retailPrice: state.oneVariantsObj.retailPrice, wholesalePrice: state.oneVariantsObj.wholesalePrice, eanCode: null,// state.oneVariantsObj.eanCode, qtyPacked:0 } ) }) } else { // if variants has color and no size state.variantsList.push( { variantId:v4(), sku: state.one.variantsRefCode + '-' + categoryItem + '-' + colorItem, category: categoryItem, size: null, color: colorItem ? colorItem : null, qty: 0, //qty: state.oneVariantsObj.qty, retailPrice: state.oneVariantsObj.retailPrice, wholesalePrice: state.oneVariantsObj.wholesalePrice, eanCode: null, //state.oneVariantsObj.eanCode qtyPacked:0 } ) } }) } else if (state.oneVariantsObj.size && state.oneVariantsObj.size.length > 0) { // if variants has size and no color state.oneVariantsObj.size.filter((sizeItem) => { state.variantsList.push( { variantId:v4(), sku: state.one.variantsRefCode + '-' + categoryItem + '-' + sizeItem, category: categoryItem, size: sizeItem, color: null, qty: 0, //qty: state.oneVariantsObj.qty, retailPrice: state.oneVariantsObj.retailPrice, wholesalePrice: state.oneVariantsObj.wholesalePrice, eanCode: null, //state.oneVariantsObj.eanCode qtyPacked:0 } ) }) } else { // if variants has no size and no color , only has category state.variantsList.push( { variantId:v4(), sku: state.one.variantsRefCode + '-' + categoryItem, category: categoryItem, size: null, color: null, qty: 0, //qty: state.oneVariantsObj.qty, retailPrice: state.oneVariantsObj.retailPrice, wholesalePrice: state.oneVariantsObj.wholesalePrice, eanCode: null,//state.oneVariantsObj.eanCode qtyPacked:0 } ) } }) } else { let variantList = state.variantsList.length > 0 ? state.variantsList : state.originalVariants let newColors = checkExistOrNot({oneVariantsObj: state.oneVariantsObj.color,variantsList: variantList, type: 'color'}) let newCategory = checkExistOrNot({oneVariantsObj: state.oneVariantsObj.category,variantsList: variantList, type: 'category'}) let newSize = checkExistOrNot({oneVariantsObj: state.oneVariantsObj.size,variantsList: variantList, type: 'size'}) let newVariantItems = [] state.originalVariants.filter((item)=>{ let flag = false if(state.oneVariantsObj.category.includes(item.category)){ if(!state.oneVariantsObj.size.includes(item.size)){ flag = true } if(!state.oneVariantsObj.color.includes(item.color)){ flag = true } if(!flag) { newVariantItems.push(item) } } }) let totalMixedCategories = [...new Set([...newCategory, ...state.oneVariantsObj.category])] let totalMixedColors = [...new Set([...newColors, ...state.oneVariantsObj.color])] let totalMixedSizes = [...new Set([...newSize, ...state.oneVariantsObj.size])] // if(newCategory.length){ for(let i=0; i< totalMixedCategories.length; i++){ let category = totalMixedCategories[i]; // if(newColors.length){ for(let j=0; j< totalMixedColors.length; j++){ let color = totalMixedColors[j]; // if(newSize.length){ for(let k=0; k< totalMixedSizes.length; k++){ let size = totalMixedSizes[k]; let obj = { variantId:v4(), sku: state.one.variantsRefCode + '-' + category + '-' + size + '-' + color, category: category ? category : null, size: size ? size : null, color: color ? color : null, qty: returnOriginalSkuQtyBySku(state.variantsList, 'qty', category , size ,color), retailPrice: state.oneVariantsObj.retailPrice, // returnOriginalSkuQtyBySku(state.variantsList, 'retailPrice', category , size ,color),// wholesalePrice: state.oneVariantsObj.wholesalePrice, // returnOriginalSkuQtyBySku(state.variantsList, 'wholesalePrice', category , size ,color), eanCode: returnOriginalSkuQtyBySku(state.variantsList, 'eanCode', category , size ,color), qtyPacked: returnOriginalSkuQtyBySku(state.variantsList, 'qtyPacked', category , size ,color), } // avoid duplication if(!newVariantItems.filter((item => (item.category == obj.category && item.size == obj.size && item.color == obj.color))).length) newVariantItems.push(obj) else { newVariantItems.filter((item,index) => { if(item.category == obj.category && item.size == obj.size && item.color == obj.color){ let {sku, ...othersProp } = obj newVariantItems[index] = { sku: item.sku, ...othersProp } } }) } } // totalMixedSizes //} } //totalMixedColors //} } // totalMixedCategories //} //newCategory.length state.variantsList = [...new Set([...newVariantItems])] } }, }, actions: { new({ commit }, { appId, wholesaleOrderId, purchaserBusinessEntityId, providerBusinessEntityId, brandId,startDate,endDate }) { commit('inProgress', false) commit('setOne', { 'appId': appId, 'wholesaleOrderId': wholesaleOrderId, 'brandId': brandId, 'format': null, 'lineNumber': 0, 'quantity': 0, 'unitPrice': 0, 'name': null, 'description': null, 'variantsRefCode': null, 'variants': [], 'purchaserBusinessEntityId': purchaserBusinessEntityId, 'providerBusinessEntityId': providerBusinessEntityId, 'shipmentStartDate':startDate, 'shipmentCancelDate':endDate, 'hsCode':null }) }, newItemVariant({ commit }) { commit('setOneVariantsObj', { sku: null, category: null, size: null, color: null, qty: null, retailPrice: null, wholesalePrice: null, eanCode: null, qtyPacked:null }) }, fetchOne: async ({ commit,state }, { wholesaleOrderItemId, appId }) => { commit('inProgress', true) try { const orderItem = await doFetchOne({ wholesaleOrderItemId, appId }) if (orderItem) { if(orderItem.icp_data){ state.orderItemIcpData=orderItem.icp_data state.masterItemOneIcpData=JSON.stringify(orderItem.icp_data) } commit('setOne', humps(orderItem)) commit('setItemMasterOne', humps(orderItem)) return humps(orderItem) } } // eslint-disable-next-line no-useless-catch catch (err) { console.log(err) return {error:true} //throw err } finally { commit('inProgress', false) } }, save: async ({ commit, state }, { itemsFormat }) => { commit('inProgress', true) try { if (undefined === state.one.id) { state.one.format = itemsFormat; if (state.one.format === 'sku') { state.one.variants = []; state.one.variants.push(state.oneVariantsObj); } else { state.one.variants = []; state.one.variants = (state.variantsList); } await doPostOne(snakes(state.one)) commit('inProgress', false) return ({ new: true }) } else { if (state.one.format === 'sku') { state.one.variants = []; state.one.variants.push(state.oneVariantsObj); } else { state.one.variants = []; state.one.variants = (state.variantsList); } await doPatchOne(snakes(state.one)) commit('inProgress', false) return ({ update: true }) } } // eslint-disable-next-line no-useless-catch catch (err) { throw err } finally { commit('inProgress', false) } }, deleteSelected: async ({ commit }, { wholesaleOrderItemId }) => { commit('inProgress', true); try { await doDeleteSelected({ wholesaleOrderItemId }); return ({ deleteSelected: true }) } catch (err) { return ({ deleteSelected: false }) } finally { commit('inProgress', false); } }, newItemVariantsList: async ({ commit }) => { commit('variantsListInProgress', true); try { commit('setItemVariantsList'); commit('variantsListInProgress', false); } catch (err) { commit('variantsListInProgress', false); } }, newVariantsList: async ({ commit }) => { commit('variantsListInProgress', true); try { commit('setNewVariantsList'); commit('variantsListInProgress', false); } catch (err) { commit('variantsListInProgress', false); } }, setOrderItem: async ({ commit,state }, {one}) => { commit('inProgress', true); try { if(one.icp_data){ state.orderItemIcpData=one.icp_data state.masterItemOneIcpData=JSON.stringify(one.icp_data) } commit('setOne', one) commit('setItemMasterOne',one) commit('inProgress', false); } catch (err) { commit('inProgress', false); } finally { commit('inProgress', false); } }, exportOrderItemsInExcel: async ({ commit }, { salesOrderId, businessEntityId, preTemplateId }) => { commit('inProgress', true); try { let data = await doExportPurchaseOrderItemsInExcel({ salesOrderId, businessEntityId, preTemplateId }) return ({ blob: data.blob, errorMessage: data.errorMessage }) } catch (err) { throw err } finally { commit('inProgress', false); } }, fetchItem: async ({ commit,state }, { wholesaleOrderItemId, appId }) => { commit('inProgress', true) try { let orderItem = await doFetchOne({ wholesaleOrderItemId, appId }) if (orderItem) { state.orderItemIcpData=orderItem.icp_data state.masterItemOneIcpData=JSON.stringify(orderItem.icp_data) return humps(orderItem) } return null } // eslint-disable-next-line no-useless-catch catch (err) { console.log(err) return {error:true} //throw err } finally { commit('inProgress', false) } }, }, }