DistributionAnalyzerSearch = function( searchOptions ) { let searchOptionDefaults = { applySearch : function( searchParams ) { // default does nothing, caller must override }, getSearchParams : function() { return null; }, partnerMode : false }; this.filterPreferences = {}; // scrum 46030 this.searchOptions = $.extend( true, {}, searchOptionDefaults, searchOptions ) this.dxForm = null this.dxPopup = null; /* this.purchasingMethods = [ {"purchasingmethod":"DIRECT","desc":"Direct"}, {"purchasingmethod":"INDIRECT","desc":"Indirect"} ]; this.customerTypes = [ {"directshipoperator":"OPERATOR","desc":"Direct Ship Operators"}, {"directshipoperator":"DISTRIBUTOR","desc":"Distributors"} ]; */ } DistributionAnalyzerSearch.prototype.constructor = DistributionAnalyzerSearch; // changed from OpportunitySearch to DistributionAnalyzerSearch by MWG on 8/29/2022 @3:33pm DistributionAnalyzerSearch.prototype.createForm = function() { let instance = this; instance.salesPeriods = []; let salesPeriodDataSource = Fse.Data.newDataSource( { object : "CRM.dateRanges", paginate : false, keyField : "rangeKey", objectParams : { rangeTypes : "M", calendarTypes : "F", includeThis : false, includeLast : false, includeNext : false, includeFiscalPeriods : "LYM,TYM,NYM" }}) salesPeriodDataSource.load().done( function( items ) { instance.salesPeriods = items; }); let showMFR = Fse.Portal.appConfiguration.STP.ownerType !== "MFR"; let showBudgetCategory = Fse.Portal.appConfiguration.STP.ownerType === "MFR"; // let showRedistributor = Fse.Portal.appConfiguration.STP.ownerType === "MFR"; // let showPurchasingMethod = Fse.Portal.appConfiguration.STP.ownerType === "MFR"; let purchasingMonthsDataSource = Fse.Data.newDataSource( { paginate : false, object : "OPA.purchasingMonths", keyField : "purchasingMonthRaw" } ); purchasingMonthsDataSource.sort( [ { selector : "purchasingMonthRaw", desc : true } ]); // start group 1 items let group1Items = [ // start group 1 items { template : function() { return $("
").append( "Timeframe" ).css( { "text-align": "center", "font-size" : "larger" } ) } }, { dataField : "lastPurchasingMonth", editorType : "dxSelectBox", isRequired : true, editorOptions : { dataSource : purchasingMonthsDataSource, valueExpr: "purchasingMonth", displayExpr : "purchasingMonthDisplay", searchEnabled : false, showClearButton: false, deferRendering : false } }, { dataField : "windowMonths", editorType : "dxSelectBox", editorOptions : { items : [ { fieldValue : 12, displayValue : "12" }, { fieldValue : 11, displayValue : "11" }, { fieldValue : 10, displayValue : "10" }, { fieldValue : 9, displayValue : "9" }, { fieldValue : 8, displayValue : "8" }, { fieldValue : 7, displayValue : "7" }, { fieldValue : 6, displayValue : "6" }, { fieldValue : 5, displayValue : "5" }, { fieldValue : 4, displayValue : "4" }, { fieldValue : 3, displayValue : "3" }, { fieldValue : 2, displayValue : "2" }, { fieldValue : 1, displayValue : "1" } ], valueExpr : "fieldValue", displayExpr : "displayValue" } } ]; // end group 1 items // start group 2 items let group2Items = [ // start group 2 items { template : function() { return $("
").append( "Territory" ).css( { "text-align": "center", "font-size" : "larger" } ) } }, { dataField : "territoryPath", label : { location : "left", text : "Territory" }, editorType : "dxDropDownBox", cssClass : instance.activeFilterPreferences.territoryPath ? 'fx-active-preference' : null, // scrum 46030 editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "TER.salesTerritories", keyField : "territoryPath" } ), searchExpr : "territoryPath", searchMode : "contains", displayExpr : "territoryPath", multipleSelectedDisplay : "Multiple Territories Selected", keyExpr : "territoryPath", title : "Select Territories" }) }, //sales rep { dataField : "accountOwner_fspro_userId", label : { location : "left", text : "Sales Rep" }, editorType : "dxDropDownBox", cssClass : instance.activeFilterPreferences.accountOwner_fspro_userId ? 'fx-active-preference' : null, // scrum 46030 editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "CRM.salesRepList", keyField : "valueId" } ), searchExpr : "valueName", searchMode : "contains", displayExpr : "valueName", multipleSelectedDisplay : "Multiple Sales Reps Selected", keyExpr : "valueId", title : "Select Sales Reps" }) } ]; let showStaffType = Fse.Portal.appConfiguration.STP.ownerType === "MFR"; if( showStaffType ) { group2Items.push( { dataField: "accountOwner_ownerType", label : { location : "left", text : "Staff Type" }, editorType : "dxSelectBox", cssClass : instance.activeFilterPreferences.accountOwner_ownerType ? 'fx-active-preference' : null, editorOptions : { showClearButton : true, placeholder : "Select Staff Type", dataSource : { store : { type : "array", key : "value", data : [ { value : "MFR", text : "Manufacturer Staff Only" }, { value : "BRO", text : "Broker Staff Only" } ]}}, valueExpr : "value", displayExpr : "text" } } ) } group2Items.push( { isRequired : false, dataField : "cdr_dstPath", label : { location : "left", text : "Distributor" }, editorType : "dxDropDownBox", editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "CDR.distributors2", keyField : "cdr_dstPath" } ), searchExpr : "cdr_dstPath", searchMode : "contains", displayExpr : "cdr_dstPath", multipleSelectedDisplay : "Multiple Distributors Selected", keyExpr : "cdr_dstPath", title : "Select Distributors" }) } ); /* if( showRedistributor ) { group2Items.push( { dataField : "redist_cdr_recordId", label : { location : "left", text : "Redistributor" }, editorType : "dxDropDownBox", editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "CDR.redistributorAccounts", keyField : "dstId" } ), searchExpr : "cdr_dstname", searchMode : "contains", displayExpr : "cdr_dstname", multipleSelectedDisplay : "Multiple Redistributors Selected", keyExpr : "dstId", title : "Select Redistributors" }) } ) } */ group2Items.push ( { dataField : "buyingGroupId", label : { location : "left", text : "Buying Group" }, editorType : "dxDropDownBox", editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "CDR.buyingGroups", keyField : "uniqueId" } ), searchExpr : "bgName", searchMode : "contains", displayExpr : "bgName", multipleSelectedDisplay : "Multiple Buying Groups Selected", keyExpr : "uniqueId", title : "Select Buying Group" }) } ); group2Items.push ( { dataField : "classificationId", label : { location : "left", text : "Classification" }, editorType : "dxDropDownBox", editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "CDR.classifications", keyField : "classificationId" } ), searchExpr : "classificationName", searchMode : "contains", displayExpr : "classificationName", multipleSelectedDisplay : "Multiple Classifications Selected", keyExpr : "classificationId", title : "Select Classification" }) } ); // end group 2 items // start group 3 items let group3Items = [ { template : function() { return $("
").append( "Product" ).css( { "text-align": "center", "font-size" : "larger" } ) } }, ]; if( showMFR ) { group3Items.push( { dataField : "mfr_id", label : { location : "left", text : "Manufacturer" }, editorType : "dxDropDownBox", cssClass : instance.activeFilterPreferences.mfr_id ? 'fx-active-preference' : null, // scrum 46030 editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "PRD.manufacturers", keyField : "mfr_id" } ), searchExpr : "mfr_name", searchMode : "contains", displayExpr : "mfr_name", multipleSelectedDisplay : "Multiple Manufacturers Selected", keyExpr : "mfr_id", title : "Select Manufacturer" }) } ); } //product hierarchy path group3Items.push( { dataField : "productHierarchyPath", label : { location : "left", text : "Product" }, editorType : "dxDropDownBox", cssClass : instance.activeFilterPreferences.productHierarchyPath ? 'fx-active-preference' : null, // scrum 46030 editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "PRD.productHierarchyPaths", key : "productHierarchyPath", objectParams : { "crmActive" : "Y" } } ), searchExpr : "productHierarchyPath", searchMode : "contains", displayExpr : "productHierarchyPath", multipleSelectedDisplay : "Multiple Product Hierarchy Paths Selected", keyExpr : "productHierarchyPath", title : "Select Product Hierarchy Path", searchTemplate : function( applyFilters ) { return Fse.UI.productHierarchyPathMultiSelectSearchTemplate( applyFilters ) } }) }, { dataField : "productSetTags", label : { location : "left", text : "Product Set" }, editorType : "dxDropDownBox", cssClass : instance.activeFilterPreferences.productSetTags ? 'fx-active-preference' : null, editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "PRD.productSets", keyField : "productSetTag" } ), searchExpr : "productSetName", searchMode : "contains", displayExpr : "productSetName", multipleSelectedDisplay : "Multiple Product Sets Selected", keyExpr : "productSetTag", title : "Select Product Set" }) } ); if( showBudgetCategory ) { group3Items.push( //budget categories { dataField : "budgetCategoryId", label : { location : "left", text : "Budget Category" }, editorType : "dxDropDownBox", cssClass : instance.activeFilterPreferences.budgetCategoryId ? 'fx-active-preference' : null, // scrum 46030 editorOptions : { placeholder : "Select Budget Category", dataSource : Fse.Data.newDataSource( { object : "PRD.budgetCategories", keyField : "budgetCategoryId" } ), valueExpr : "budgetCategoryId", displayExpr : "budgetCategory", contentTemplate : function( e ) { const v = e.component.option( "value" ); const $list = $("
").dxList( { dataSource : e.component.getDataSource(), displayExpr : "budgetCategory", selectionMode: "multiple", showSelectionControls : true, selectedItemKeys : v, onSelectionChanged : function( sce ) { const keys = sce.component.option( "selectedItemKeys" ); e.component.option( "value", keys ); } }) list = $list.dxList('instance'); e.component.on('valueChanged', (args) => { const { value } = args; list.option( "selectedItemKeys", value ); }); return $list; } } } ) /* group3Items.push( { dataField : "budgetCategoryMgr_fspro_userId", label : { location : "left", text : "Budget Category Mgr." }, editorType : "dxDropDownBox", editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "CRM.salesRepList", keyField : "valueId", objectParams : { staffOnly : true } } ), searchExpr : "valueName", searchMode : "contains", displayExpr : "valueName", multipleSelectedDisplay : "Multiple Managers Selected", keyExpr : "valueId", title : "Select Managers" }) } ); */ } group3Items.push( //brand { dataField : "brandId", label : { location : "left", text : "Brand" }, editorType : "dxDropDownBox", editorOptions : Fse.UI.multiSelectDropDownBoxEditorOptions( { dataSource : Fse.Data.newDataSource( { object : "PRD.brandsList", keyField : "brandId" } ), searchExpr : "brand", searchMode : "contains", displayExpr : "brand", multipleSelectedDisplay : "Multiple Brands Selected", keyExpr : "brandId", title : "Select Brand" }) } ); /* group3Items.push( //focus items { dataField : "focusProduct", label : { location : "left", text : "Focus Items" }, editorType : "dxSwitch", cssClass : instance.activeFilterPreferences.focusProduct ? 'fx-active-preference' : null, // scrum 46030 editorOptions: { value: false } } ); group3Items.push( // Excluded Discontinued { dataField : "excludeDiscontinued", label : { location : "left", text : "Exclude Discontinued" }, editorType : "dxSwitch", editorOptions: { value: false } } ); group3Items.push( // Exclude Distributor Private Label { dataField : "excludeDistributorPrivateLabel", label : { location : "left", text : "Excl. Dist. Private Label" }, editorType : "dxSwitch", editorOptions: { value: false } } ); group3Items.push( // Exclude Distributor Private Label { dataField : "excludeOperatorPrivateLabel", label : { location : "left", text : "Excl. Oper. Private Label" }, editorType : "dxSwitch", editorOptions: { value: false } } ); */ // end group 3 items let formItems = []; formItems.push( { name: "group1", itemType : "group", items : group1Items } ) if( ! instance.searchOptions.partnerMode ) { formItems.push( { name: "group2", itemType : "group", colCount : 1, items : group2Items } ) } formItems.push( { name: "group3", itemType : "group", colCount : 1, items : group3Items } ) instance.dxForm = $("
").dxForm( { validationGroup : "distributionAnalyzerSearch", colCount : formItems.length, items : formItems // end items , onFieldDataChanged : function( e ) { if( e.dataField == "customRange" ) { let dxForm = e.component; if( e.value ) { dxForm.getEditor( "salesPeriodStart" ).option( "disabled", false ); dxForm.getEditor( "salesPeriodEnd" ).option( "disabled", false ); dxForm.getEditor( "timeframe" ).option( "disabled", true ); } else { dxForm.getEditor( "salesPeriodStart" ).option( "disabled", true ); dxForm.getEditor( "salesPeriodEnd" ).option( "disabled", true ); dxForm.getEditor( "timeframe" ).option( "disabled", false ); } } if( e.dataField === "mfr_id" ) { let fields = [] fields.push( e.component.getEditor( "productHierarchyPath" ) ); fields.push( e.component.getEditor( "brandId" ) ); let fieldOptions = { value : null }; if( e.value && e.value != "") { fieldOptions.disabled = true; } else { fieldOptions.disabled = false; } fields.forEach( function( f ) { if( f ) { f.option( fieldOptions ); } }) } } }).dxForm("instance"); instance.dxValidator = $("
").dxValidator( { adapter : { getValue : function() { return instance.dxForm; }, applyValidationResults : function( validationResult ) { if( validationResult.isValid ) { return; } Fse.UI.toast( validationResult.brokenRule.message, "warning" ); } }, validationGroup : "distributionAnalyzerSearch", validationRules : [ { type : "custom", reevaluate : true, message: "You must select a distributor or a territory", validationCallback : function( options ) { let fd = instance.dxForm.option( "formData" ); if( ( fd.territoryPath && fd.territoryPath.length ) || instance.activeFilterPreferences.territoryPath ) { return true; } else if ( fd.cdr_dstPath && fd.cdr_dstPath.length ) { return true; } else if ( fd.classificationId && fd.classificationId.length ) { return true; } else { return false; } } } ] }).dxValidator( "instance" ); } DistributionAnalyzerSearch.prototype.createPopup = function() { let instance = this; instance.createForm(); // scrum 46030 let bottomToolbarItems = [ { toolbar : "bottom", location : "after", widget : "dxButton", options : { text : "search", type : "default", onClick : function( e ) { let validationResult = instance.dxForm.validate(); if( ! validationResult.isValid ) { return; } let applySearch = instance.searchOptions.applySearch; let searchParams = instance.dxForm.option( "formData" ); if( searchParams.timeframe ) { searchParams.timeframe = instance.dxForm.getEditor( "timeframe" ).option( "selectedItem" ); } instance.dxPopup.hide(); applySearch( searchParams ); } } } ]; // scrum 46030 if( Object.keys( instance.activeFilterPreferences ).length ) { bottomToolbarItems.push( { toolbar : "bottom", location : "before", template : function() { let annotation = $("
").append( $("").addClass( "dx-field-item-label-text" ).append( "*filter preference active")).addClass( "fx-active-preference" ).css( { "cursor" : "pointer" } ); return annotation; } }) } instance.dxPopup = $("
").dxPopup( { title : "Distribution Analyzer Filters", contentTemplate : function() { return $("
") .append( instance.dxForm.element() ) .append( instance.dxValidator.element() ) }, toolbarItems : bottomToolbarItems, height : "auto", // on hidden added to cleanup ui objects - scrum 46030 width : instance.searchOptions.partnerMode ? "60vw" : null, onHidden : function( e ) { // clean up e.component.element().remove(); instance.dxForm.dispose(); instance.dxForm = null; instance.dxPopup.dispose(); instance.dxPopup = null; } }).appendTo( "body" ).dxPopup("instance"); } // scrum 46030 DistributionAnalyzerSearch.prototype.show = function( searchParams, filterPreferences ) { this.processFilterPreferences( filterPreferences, searchParams ); this.createPopup(); if( searchParams ) { let searchParams2 = $.extend( true, {}, searchParams ); if( searchParams2.timeframe ) { searchParams2.timeframe = searchParams2.timeframe.rangeKey ? searchParams2.timeframe.rangeKey : searchParams2.timeframe; } this.dxForm.updateData( searchParams2 ); } this.dxPopup.show(); } // scrum 46030 DistributionAnalyzerSearch.prototype.getPreferenceableFilters = function() { // when the global filters are expressed with different variable names, they need to translated to this widgets variable names const preferenceableFilters = { // widgetVariableName : "globalFilterVariableName" territoryPath : "territoryPath", accountOwner_fspro_userId : "salesRepId", focusProduct : "focusProduct", budgetCategoryId : "budgetCategoryId", productHierarchyPath : "productHierarchyPath", mfr_id : "mfr_id", accountOwner_ownerType : "staffType", productSetTags : "productSetTags" } return preferenceableFilters; } // scrum 46030 DistributionAnalyzerSearch.prototype.processFilterPreferences = function( filterPreferencesIn, searchParamsIn ) { let filterPreferences = {} if( filterPreferencesIn ) { filterPreferences = $.extend( true, {}, filterPreferencesIn ); } let searchParams = {}; if( searchParamsIn ) { searchParams = $.extend( true, {}, searchParamsIn ); } this.activeFilterPreferences = {}; // when the global filters are expressed with different variable names, they need to translated to this widgets variable names let preferenceableFilters = this.getPreferenceableFilters(); for( let p in preferenceableFilters ) { let gp = p; // global filter param is the same name as widget param // apply translation - global filter param is not the same name as widget param if( preferenceableFilters[p] ) { gp = preferenceableFilters[p]; } // if the global filter preferences have the propery and it has a value, update the objectparams if( filterPreferences.hasOwnProperty( gp ) && filterPreferences[gp]) { if( searchParams[p] == null ) { this.activeFilterPreferences[p] = filterPreferences[gp]; } } } }