Untitled

mail@pastecode.io avatarunknown
plain_text
2 months ago
17 kB
1
Indexable
Never
var Petronas = Petronas || {};
Petronas.CRM = Petronas.CRM || {};
Petronas.CRM.DRO = Petronas.CRM.DRO || {};
Petronas.CRM.DRO.Form = {
    _privateMembers: {
        initialize: function (executionContext) {
            formContext = executionContext.getFormContext();
            OilForm = formContext.getAttribute("ipro_oildistance");
            GasForm = formContext.getAttribute("ipro_gasdistance");
            ARPRYear = formContext.getAttribute("ipro_arprstudyyear").getValue();
            ARPRYearAttr = formContext.getAttribute("ipro_arprstudyyear");
            oilFacilityCtrl = formContext.getControl("ipro_oilnearestfacility");
            gasFacilityCtrl = formContext.getControl("ipro_gasnearestfacility");
            droDetailSubGridCtrl = formContext.getControl('Subgrid_droDetail');
            collectorsSubGridCtrl = formContext.getControl('GridCollectors');
            processorsSubGridCtrl = formContext.getControl('GridProcessors');
            assignedHubSubGridCtrl = formContext.getControl('GridAssignedHub');
            stoiipMMBOESubGridCtrl = formContext.getControl('STOIIPMMBOE');
            gasCR2CBCFSubGridCtrl = formContext.getControl('GASCR2CBCF');
            giipMMBOESubGridCtrl = formContext.getControl('GIIPMMBOE');
            oilCR2CMMBOESubGridCtrl = formContext.getControl('OILCR2CMMBOE');
            giipBCFSubGridCtrl = formContext.getControl('GIIPBCF');

            let entityId = formContext.data.entity.getId();
            oilFacilityFetchFilter =
                `<filter type='and'><condition attribute='ipro_dro' operator='eq' value='${entityId}' /><condition attribute='ipro_arprstudyyear' operator='eq' value='${ARPRYear}' /> <condition attribute='ipro_fluidtype' operator='eq' value='0' /></filter>`

            gasFacilityFetchFilter =
                `<filter type='and'><condition attribute='ipro_dro' operator='eq' value='${entityId}' /><condition attribute='ipro_arprstudyyear' operator='eq' value='${ARPRYear}' /> <condition attribute='ipro_fluidtype' operator='eq' value='1' /></filter>`

        },
    },
    onLoad: function onLoad(executionContext) {
        Petronas.CRM.DRO.Form._privateMembers.initialize(executionContext);

        oilFacilityCtrl.addPreSearch(() => {
            oilFacilityCtrl.addCustomFilter(oilFacilityFetchFilter);
        });
        gasFacilityCtrl.addPreSearch(() => {
            gasFacilityCtrl.addCustomFilter(gasFacilityFetchFilter);
        });
        let FormType = formContext.ui.getFormType();
        if (FormType == 2) {
            var tabObj = formContext.ui.tabs.get("tab_NearestFacility");
            tabObj.setVisible(true);
        }
        else {
            var tabObj = formContext.ui.tabs.get("tab_NearestFacility");
            tabObj.setVisible(false);
        }
        let currentTab = Xrm.Page.ui.tabs.get().find(it => it.getDisplayState() == 'expanded')?.name;
if(!currentTab){
            return;
        }
        Petronas.CRM.DRO.Form.OnTabChange(executionContext, currentTab)
    },
    SetOilDistance_onChange: function SetOilDistance_onChange(executionContext) {
        var Oil = formContext.getAttribute("ipro_oilnearestfacility").getValue();
        if (Oil == null) {
            OilForm.setValue(null);
        }
        else {
            var OilId = Oil[0].id;
            if (OilId != null) {
                Xrm.WebApi.retrieveRecord("ipro_nearestfacility", OilId, "?$select=ipro_distance").then(
                    function success(result) {
                        OilForm.setValue(result.ipro_distance);
                    },
                    function (error) {
                        console.log(error);
                    });
            }
        }
    },

    SetGasDistance_onChange: function SetGasDistance_onChange(executionContext) {
        var Gas = formContext.getAttribute("ipro_gasnearestfacility").getValue();
        if (Gas == null) {
            GasForm.setValue(null);
        }
        else {
            var GasId = Gas[0].id;
            if (GasId != null) {
                Xrm.WebApi.retrieveRecord("ipro_nearestfacility", GasId, "?$select=ipro_distance").then(
                    function success(result) {

                        GasForm.setValue(result.ipro_distance);
                    },
                    function (error) {
                        console.log(error);
                    });
            }
        }
    },
    DroProbabilities: function DroProbabilities(executionContext) {

        let config = [

            { "RefName": "ipro_probabilitytype", "GridName": "STOIIPMMBOE" },
            { "RefName": "ipro_probabilitytype", "GridName": "OILCR2CMMBOE" },
            { "RefName": "ipro_probabilitytype", "GridName": "GIIPMMBOE" },
            { "RefName": "ipro_probabilitytype", "GridName": "GIIPBCF" },
            { "RefName": "ipro_probabilitytype", "GridName": "GASCR2CBCF" },

        ];
        const probabilityTypes = { STOIIPMMBOE: 0, GIIPBCF: 1, OILCR2CMMBOE: 2,GASCR2CBCF:3,GIIPMMBOE:4 }
        let anyRecord = config.find(it => it.GridName == executionContext.name);
        if (!anyRecord)
            return;
        var entityFormOptions = {};
        entityFormOptions["entityName"] = "ipro_droprobability";
        var formParameters = {};
        Dro = [{
            name: executionContext.formContext.getAttribute("ipro_name").getValue(),
            id: executionContext.formContext.data.entity.getId(),
            entityType: executionContext.formContext.data.entity.getEntityName()
        }];
        if (anyRecord.GridName == "STOIIPMMBOE") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, probabilityTypes.STOIIPMMBOE, Dro);
        }
        else if (anyRecord.GridName == "GIIPBCF") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, probabilityTypes.GIIPBCF, Dro);
        }
        else if (anyRecord.GridName == "OILCR2CMMBOE") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, probabilityTypes.OILCR2CMMBOE, Dro);
        }
        else if (anyRecord.GridName == "GASCR2CBCF") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, probabilityTypes.GASCR2CBCF, Dro);
        }
        else if (anyRecord.GridName == "GIIPMMBOE") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, probabilityTypes.GIIPMMBOE, Dro);
        }
      
    },
    NearestFacility: function NearestFacility(executionContext) {
        let config = [
            { "RefName": "ipro_hubtypes", "GridName": "GridAssignedHub" },
            { "RefName": "ipro_hubtypes", "GridName": "GridProcessors" },
            { "RefName": "ipro_hubtypes", "GridName": "GridCollectors" },
        ];

        const hubTypes = { AssignedHub: 0, Processors: 1, Collectors: 2 }
        let anyRecord = config.find(it => it.GridName == executionContext.name);
        if (!anyRecord)
            return;
        var entityFormOptions = {};
        entityFormOptions["entityName"] = "ipro_nearestfacility";
        var formParameters = {};
        Dro = [{
            name: executionContext.formContext.getAttribute("ipro_name").getValue(),
            id: executionContext.formContext.data.entity.getId(),
            entityType: executionContext.formContext.data.entity.getEntityName()
        }];
        if (anyRecord.GridName == "GridAssignedHub") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, hubTypes.AssignedHub, Dro);
        }
        else if (anyRecord.GridName == "GridProcessors") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, hubTypes.Processors, Dro);
        }
        else if (anyRecord.GridName == "GridCollectors") {
            Petronas.CRM.DRO.Form.NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, anyRecord.RefName, hubTypes.Collectors, Dro);
        }
    },
    NearestFacilityOpenForm: function NearestFacilityOpenForm(executionContext, entityFormOptions, formParameters, RefName, setValue, DroLookUp) {
        formParameters[RefName] = setValue;
        formParameters["ipro_dro"] = DroLookUp;
        formParameters["ipro_fieldname"] = executionContext.formContext.getAttribute("ipro_name").getValue();
        // Open the form.
        Xrm.Navigation.openForm(entityFormOptions, formParameters).then(
            function (success) {
                console.log(success);
            },
            function (error) {
                console.log(error);
            });
    },
    OnChangeOfARPRYear: function OnChangeOfARPRYear(executionContext) {
        let formContext = executionContext.getFormContext();
        let id = formContext.data.entity.getId().replace("{", "").replace("}", "");
        if (!id)
            return;
        let arprYearValue = ARPRYearAttr.getValue();
        if (arprYearValue == null || arprYearValue.length !== 4)
            return;
        Xrm.WebApi.retrieveMultipleRecords("ipro_dro", "?$filter=_ipro_parentdro_value eq '" + id + "' and ipro_arprstudyyear eq '" + arprYearValue + "'").then(
            function success(relatedDro) {
                console.log(relatedDro);
                let recordByYear = relatedDro.entities?.[0];

                let arr = ["ipro_fieldstatus", "ipro_operatershipstatus", "ipro_region", "ipro_discoveryyear", "ipro_blockname", "ipro_noofwellsea", "ipro_noofwellsproducing",
                    "ipro_droclass", "ipro_oildrocount", "ipro_nagdrocount", "ipro_dropriotizationnag", "ipro_dropriotizationoil", "ipro_waterdepthmeter", "ipro_highco2observed",
                "ipro_cr2coilmmstb","ipro_oilprice","ipro_oilcost","ipro_utcforoil","ipro_grossvalueforoil","ipro_cr2cgasbscf","ipro_gasprice","ipro_gascost","ipro_utcforgas","ipro_grossvalueforgas",
                "ipro_category","ipro_startingdate","ipro_finisheddate","ipro_company","ipro_plan","ipro_year","ipro_clusterfield","ipro_remark","ipro_promotiondate","ipro_resultstatus","ipro_promotionstatus"
                ,"ipro_gpsalegasbscf","ipro_npsaleoilmmstb","ipro_gpnonsalegasbscf","ipro_npnonsaleoilmmstb","ipro_gasdistance","ipro_oildistance","ipro_fluidtype","ipro_discoveryyear"];
                arr.forEach(it => {
                    formContext.getAttribute(it).setValue(recordByYear?.[it]);
                });
                let lookUpArr = ["ipro_psc","ipro_oilnearestfacility","ipro_gasnearestfacility"];
                lookUpArr.forEach(it => {
                   if(it == "ipro_psc"){
                        var id = recordByYear?.["_ipro_psc_value"];
                        if(id == null){
                            formContext.getAttribute(it).setValue(null);  
                        }
                        else {
                        var lookupValue = new Array();
                        lookupValue[0] = new Object();
                        lookupValue[0].id = recordByYear?.["_ipro_psc_value"];
                        lookupValue[0].name = recordByYear?.["_ipro_psc_value@OData.Community.Display.V1.FormattedValue"];
                        lookupValue[0].entityType = recordByYear?.["_ipro_psc_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                        formContext.getAttribute(it).setValue(lookupValue);
                        }
                    }
                else if(it == "ipro_oilnearestfacility"){
                    var id = recordByYear?.["_ipro_oilnearestfacility_value"];
                    if(id == null){
                        formContext.getAttribute(it).setValue(null);
                        formContext.getAttribute("ipro_oildistance").setValue(null);  
                    }
                    else {
                    var lookupValue = new Array();
                    lookupValue[0] = new Object();
                    lookupValue[0].id = recordByYear?.["_ipro_oilnearestfacility_value"];
                    lookupValue[0].name = recordByYear?.["_ipro_oilnearestfacility_value@OData.Community.Display.V1.FormattedValue"];
                    lookupValue[0].entityType = recordByYear?.["_ipro_oilnearestfacility_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                    formContext.getAttribute(it).setValue(lookupValue);
                    formContext.getAttribute("ipro_oildistance").setValue(recordByYear?.["ipro_oildistance"]);
                    }
                }
                else if(it == "ipro_gasnearestfacility"){
                    var id = recordByYear?.["_ipro_gasnearestfacility_value"];
                    if(id == null){
                        formContext.getAttribute(it).setValue(null);
                        formContext.getAttribute("ipro_gasdistance").setValue(null);  
                    }
                    else {
                    var lookupValue = new Array();
                    lookupValue[0] = new Object();
                    lookupValue[0].id = recordByYear?.["_ipro_gasnearestfacility_value"];
                    lookupValue[0].name = recordByYear?.["_ipro_gasnearestfacility_value@OData.Community.Display.V1.FormattedValue"];
                    lookupValue[0].entityType = recordByYear?.["_ipro_gasnearestfacility_value@Microsoft.Dynamics.CRM.lookuplogicalname"];
                    formContext.getAttribute(it).setValue(lookupValue);
                    formContext.getAttribute("ipro_gasdistance").setValue(recordByYear?.["ipro_gasdistance"]);
                    }
                }
                   
                });


            },
            function (error) {
                console.log(error.message);
                // handle error conditions
            }
        );

    },
    OnTabChange: function (executionContext,tabName) {
        let formContext = executionContext.getFormContext();
        let valueOfYear = ARPRYearAttr.getValue();
        let currentTabName = "";
        if (tabName) {
            currentTabName = tabName;
        }else
          currentTabName = executionContext.getEventSource().name;
        if (valueOfYear) {
            if (currentTabName == 'tab_DRODetail') {

                let filterForDroGrid = '<filter type="and"><condition attribute="statecode" operator="eq" value="0" /><condition attribute="ipro_arprstudyyear" operator="eq" value="' + valueOfYear + '" /></filter>';
                droDetailSubGridCtrl.setFilterXml(filterForDroGrid);
                droDetailSubGridCtrl.refresh();
            }
            else if (currentTabName == 'tab_ARPR') {

                let filterFordroProbabilityGrid = '<filter type="and"><condition attribute="statecode" operator="eq" value="0" /><condition attribute="ipro_arprstudyyear" operator="eq" value="' + valueOfYear + '" /></filter>';
                giipBCFSubGridCtrl.setFilterXml(filterFordroProbabilityGrid);
                giipBCFSubGridCtrl.refresh();
                oilCR2CMMBOESubGridCtrl.setFilterXml(filterFordroProbabilityGrid);
                oilCR2CMMBOESubGridCtrl.refresh();
                giipMMBOESubGridCtrl.setFilterXml(filterFordroProbabilityGrid);
                giipMMBOESubGridCtrl.refresh();
                gasCR2CBCFSubGridCtrl.setFilterXml(filterFordroProbabilityGrid);
                gasCR2CBCFSubGridCtrl.refresh();
                stoiipMMBOESubGridCtrl .setFilterXml(filterFordroProbabilityGrid);
                stoiipMMBOESubGridCtrl .refresh();

            }
            else if (currentTabName == 'tab_NearestFacility') {

                let filterForNearestFacilityGrid = '<filter type="and"><condition attribute="statecode" operator="eq" value="0" /><condition attribute="ipro_arprstudyyear" operator="eq" value="' + valueOfYear + '" /></filter>';
                processorsSubGridCtrl.setFilterXml(filterForNearestFacilityGrid);
                processorsSubGridCtrl.refresh();
                collectorsSubGridCtrl.setFilterXml(filterForNearestFacilityGrid);
                collectorsSubGridCtrl.refresh();
                assignedHubSubGridCtrl.setFilterXml(filterForNearestFacilityGrid);
                assignedHubSubGridCtrl.refresh();
            }
            else {
                return;
            }
        }
    }


}