SOURCE

// JavaScript source code
// JavaScript source code
var f1 = ["new_product", "new_duration", "new_city", "new_date"];
var f2 = ["new_product", "new_duration", "new_city", "new_date", "new_contract"];
var f3 = ["new_noofmodule", "new_company", "new_contact"];
var f4 = ["new_totalpersontime", "new_company", "new_contact"];
var f5 = ["new_estimatepd", "new_company", "new_contact"];
var f6 = ["new_date", "new_duration"];

var f34 = ["new_totalpersontime"];
var f35 = ["new_estimatepd"];
var f21 = ["new_contract"];
var f51 = ["new_product", "new_noofapply", "new_city", "new_date"];
var f61 = ["new_city", "new_product"];
var f43 = ["new_noofmodule"];
var f1010 = ["new_product"];
//加载订单
function Onload() {
    //选项卡的显隐性
    SetTabVisible();
    //计算net revenue
    //CalculateNetRevenue();
    //设置合同查找视图
    SetContractView();
    //设置产品查找条件
    OnloadSetProductFilter();
    //锁定订单类型字段
    if (Xrm.Page.ui.getFormType() != 1) {
        Xrm.Page.ui.controls.get("header_new_ordertype").setDisabled(true);
    }

}

//设置显隐性
function SetTabVisible() {
    var oType = Xrm.Page.getAttribute("new_ordertype").getValue();
    if (oType == 1) //1、公开课 2、内训 3、在线 4、人才测评 5、咨询 6、国际项目 7、复合式培训 8、虚拟课堂-公开课 9、虚拟课堂-内训 10、培训管理-服务 11、接力课程
    {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f1);
        SetLevel(2, f3);
        SetLevel(2, f34);
        SetLevel(2, f35);
        debugger;
        SetLevel(2, f21);
    }
    else if (oType == 8) //1、公开课 2、内训 3、在线 4、人才测评 5、咨询 6、国际项目 7、复合式培训 8、虚拟课堂-公开课 9、虚拟课堂-内训 10、培训管理-服务 11、接力课程
    {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f1);
        SetLevel(2, f3);
        SetLevel(2, f34);
        SetLevel(2, f35);
        debugger;
        SetLevel(2, ["new_city"]);
    }
    else if (oType == 2|| oType == 7) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f2);
        SetLevel(1, f21);
        SetLevel(2, f3);
        SetLevel(2, f34);
        SetLevel(2, f35);
    }
    else if (oType == 9) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f2);
        SetLevel(1, f21);
        SetLevel(2, f3);
        SetLevel(2, f34);
        SetLevel(2, f35);
        SetLevel(2, ["new_city"]);
    }
    else if (oType == 3 || oType == 11) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f3);
        SetLevel(2, f1);
        SetLevel(2, f34);
        SetLevel(2, f35);
        SetLevel(2, f21);

    }
    else if (oType == 4) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f4);
        SetLevel(2, f1);
        SetLevel(2, f43);
        SetLevel(2, f35);
        SetLevel(2, f21);

    }
    else if (oType == 5) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f5);
        SetLevel(2, f43);
        SetLevel(2, f34);
        SetLevel(2, f51);
        SetLevel(2, f21);
        Xrm.Page.getAttribute("new_duration").setRequiredLevel("none");

    }
    else if (oType == 6) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(true);
        //设置必填字段
        SetLevel(1, f6);
        SetLevel(2, f3);
        SetLevel(2, f34);
        SetLevel(2, f35);
        SetLevel(2, f61);
        SetLevel(2, f21);
    }
    else if (oType == 10) {
        Xrm.Page.ui.tabs.get("tab_pd").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_inhouse").setVisible(true);
        Xrm.Page.ui.tabs.get("tab_elearning").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_evaluationorder").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_consult").setVisible(false);
        Xrm.Page.ui.tabs.get("tab_international").setVisible(false);
        //设置必填字段
        SetLevel(1, f2);
        SetLevel(1, f21);
        SetLevel(2, f3);
        SetLevel(2, f34);
        SetLevel(2, f35);
        //设置必填字段
        SetLevel(2, f1010);
    }
    //else if (oType == 7) {
    //    debugger;
    //    Xrm.Page.getAttribute("new_contract").setRequiredLevel("none");
    //}
}

//确认开课
function ClassOpen() {

    Xrm.Page.getAttribute("new_status").setValue(2);
    Xrm.Page.getAttribute("new_status").setSubmitMode("always");
    Xrm.Page.data.entity.save();

}
//取消开课
function ClassCancle() {
    Xrm.Page.getAttribute("header_new_status").setValue(3);
    Xrm.Page.getAttribute("header_new_status").setSubmitMode("always");
    Xrm.Page.data.entity.save();
}
//计算net revenue
function CalculateNetRevenue() {
    debugger;
    var netRevenue;
    var hotelFee = (Xrm.Page.getAttribute("new_hotelexpense").getValue() == null) ? 0 : Xrm.Page.getAttribute("new_hotelexpense").getValue();
    var ticketFee = (Xrm.Page.getAttribute("new_planeticketexpense").getValue() == null) ? 0 : Xrm.Page.getAttribute("new_planeticketexpense").getValue();
    var otherFee = (Xrm.Page.getAttribute("new_otherexpense").getValue() == null) ? 0 : Xrm.Page.getAttribute("new_otherexpense").getValue();
    var printFee = (Xrm.Page.getAttribute("new_printmaterialexpense").getValue() == null) ? 0 : Xrm.Page.getAttribute("new_printmaterialexpense").getValue();
    var thirdpartyFee = (Xrm.Page.getAttribute("new_thirdpartyexpense").getValue() == null) ? 0 : Xrm.Page.getAttribute("new_thirdpartyexpense").getValue();
    var revenue = (Xrm.Page.getAttribute("new_revenue").getValue() == null) ? 0 : Xrm.Page.getAttribute("new_revenue").getValue();
    var isOrNot = Xrm.Page.getAttribute("new_includetravelexpense").getValue();
    if (isOrNot) {
        netRevenue = revenue - hotelFee - ticketFee - otherFee - printFee - thirdpartyFee;
    }
    else {
        netRevenue = revenue;
    }
    Xrm.Page.getAttribute("new_netrevenue").setValue(netRevenue);
    Xrm.Page.getAttribute("new_netrevenue").setSubmitMode("always");

}
//设置字段required level 1-必填 2-none
function SetLevel(c, f) {
    if (c == 1) {
        for (var i = 0; i < f.length; i++) {
            Xrm.Page.getAttribute(f[i]).setRequiredLevel("required");
        }
    }
    if (c == 2) {
        for (var i = 0; i < f.length; i++) {
            Xrm.Page.getAttribute(f[i]).setRequiredLevel("none");
        }
    }
}

//带出Product 的相关信息
function TakeDuration() {

    var orderType = Xrm.Page.getAttribute("new_ordertype").getValue();
    var productRe = Xrm.Page.getAttribute("new_product").getValue();
    var productEntity = CRK.Retrieve("Product", productRe[0].id, new Array("new_Duration", "new_FieldEN", "new_Price1", "Name", "new_Date", "new_teacher1", "new_City", "new_InhouseCourseName", "new_cityinternational"));
    Xrm.Page.getAttribute("new_duration").setValue(productEntity.new_Duration);
    Xrm.Page.getAttribute("new_price").setValue(productEntity.new_Price1.Value);
    if (productEntity.new_Date != null) {
        Xrm.Page.getAttribute("new_date").setValue(new Date(new Date(parseInt(productEntity.new_Date.substring(6, 19))).getTime() - 8 * 60 * 60 * 1000));
    }
    if (productEntity.new_InhouseCourseName != null) {
        Xrm.Page.getAttribute("new_inhousecoursename").setValue(productEntity.new_InhouseCourseName);
        Xrm.Page.getAttribute("new_inhousecoursename").setSubmitMode("always");
    }
    if (productEntity.new_FieldEN != null) {
        Xrm.Page.getAttribute("new_field1").setValue(productEntity.new_FieldEN);
        Xrm.Page.getAttribute("new_field1").setSubmitMode("always");
    }
    if (productEntity.new_teacher1.Id != null) {
        var terLookup = new Array(1);
        terLookup[0] = new Object();
        terLookup[0].id = productEntity.new_teacher1.Id;
        terLookup[0].name = productEntity.new_teacher1.Name;
        terLookup[0].entityType = productEntity.new_teacher1.LogicalName;
        Xrm.Page.getAttribute("new_trainer").setValue(terLookup);
    }
    if (orderType == 6) {
        if (productEntity.new_cityinternational != null) {
            Xrm.Page.getAttribute("new_cityinternational").setValue(productEntity.new_cityinternational);
            Xrm.Page.getAttribute("new_cityinternational").setSubmitMode("always");
        }
    }
    else {
        if (productEntity.new_City.Id != null) {
            var cityLookup = new Array(1);
            cityLookup[0] = new Object();
            cityLookup[0].id = productEntity.new_City.Id;
            cityLookup[0].name = productEntity.new_City.Name;
            cityLookup[0].entityType = productEntity.new_City.LogicalName;
            Xrm.Page.getAttribute("new_city").setValue(cityLookup);
        }
    }
    Xrm.Page.getAttribute("new_duration").setSubmitMode("always");
    Xrm.Page.getAttribute("new_city").setSubmitMode("always");
    Xrm.Page.getAttribute("new_trainer").setSubmitMode("always");
    Xrm.Page.getAttribute("new_price").setSubmitMode("always");
    Xrm.Page.getAttribute("new_date").setSubmitMode("always");
}
//带出company的信息
function TakeInfoOfCompany() {
    var companyRe = Xrm.Page.getAttribute("new_company").getValue()
    if (companyRe != null) {
        var companyEntity = CRK.Retrieve("Account", companyRe[0].id, new Array("PrimaryContactId"));
        if (companyEntity.PrimaryContactId.Id != null) {
            var conLookup = new Array(1);
            conLookup[0] = new Object();
            conLookup[0].id = companyEntity.PrimaryContactId.Id;
            conLookup[0].name = companyEntity.PrimaryContactId.Name;
            conLookup[0].entityType = companyEntity.PrimaryContactId.LogicalName;
            Xrm.Page.getAttribute("new_contact").setValue(conLookup);
            var contactEntity = CRK.Retrieve("Contact", companyEntity.PrimaryContactId.Id, new Array("new_TelOffice", "EMailAddress1"));
            Xrm.Page.getAttribute("new_contactemail").setValue(contactEntity.EMailAddress1);
            Xrm.Page.getAttribute("new_contacttel").setValue(contactEntity.new_TelOffice);
            Xrm.Page.getAttribute("new_contacttel").setSubmitMode("always");
            Xrm.Page.getAttribute("new_contactemail").setSubmitMode("always");
            Xrm.Page.getAttribute("new_contact").setSubmitMode("always");
        }
    }
}

//设置合同查找视图
//set contract custom view
function SetContractView() {
    var value;
    var filter;
    var number = Xrm.Page.getAttribute("new_ordertype").getValue();
    var company = Xrm.Page.getAttribute("new_company").getValue();
    // 指定视图的GUID(任意)
    var viewId = "{6FCA2F82-58D0-48f6-B241-2173AF5609FC}";
    // 查找实体的逻辑名称
    var entityName = "new_contract";
    // 指定视图的名称
    //if (number == 1) {
    //    var viewDisplayName = "公开课合同关联视图";
    //    value = "<value>1</value>" + "<value>3</value>" + "<value>4</value>" + "<value>9</value>";
    //}
    if (number == 2) {
        var viewDisplayName = "内训合同关联视图";
        value = "<value>2</value>" + "<value>4</value>" + "<value>9</value>";
    }
    if (number == 3) {
        var viewDisplayName = "在线合同关联视图";
        value = "<value>4</value>" + "<value>5</value>" + "<value>9</value>";
    }
    if (number == 4) {
        var viewDisplayName = "人才测评合同关联视图";
        value = "<value>6</value>" + "<value>9</value>";
    }
    if (number == 5) {
        var viewDisplayName = "咨询合同关联视图";
        value = "<value>7</value>" + "<value>9</value>";
    }
    if (number == 6) {
        var viewDisplayName = "国际项目合同关联视图";
        value = "<value>9</value>" + "<value>8</value>";
    }

    if (company != null) {
        filter = "<condition attribute='new_company' operator='eq' uitype='account' value='" + company[0].id + "' />";
    }
    // 视图的fetchXml查询
    var fetchXml = ["<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>",
        "<entity name='new_contract'>",
        "<attribute name='new_contractid' />",
        "<attribute name='new_name' />",
        "<attribute name='createdon' />",
        "<attribute name='new_company' />",
        "<attribute name='new_status' />",
        "<attribute name='new_grossamount' />",
        "<attribute name='new_contracttype' />",
        "<order attribute='new_name' descending='false' />",
        "<filter type='and'>",
        "<condition attribute='new_contracttype' operator='in'>",
        value,
        "</condition>",
        filter,
        " </filter>",
        "</entity>",
        "</fetch>",].join("");


    // 指定视图布局XML
    var layoutXml = ["<grid name='resultset' object='10013' jump='name' select='1' icon='1' preview='1'>",
        "<row name='result' id='new_contractid'>",
        "<cell name='new_name' width='200'/>",
        "<cell name='new_company' width='180'/>",
        "<cell name='new_contracttype' width='180'/>",
        "<cell name='new_grossamount' width='180'/>",
        "<cell name='new_status' width='100'/>",
        "</row>",
        "</grid>"].join("");
    // 是否设置为默认视图
    var isDefault = true;
    switch (number) {
        //case 1:
        //    Xrm.Page.getControl("new_contractno").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault);
        case 2:
            Xrm.Page.getControl("new_contract").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault);
        case 3:
            Xrm.Page.getControl("new_contract1").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault);
        case 4:
            Xrm.Page.getControl("new_contract2").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault);
        case 5:
            Xrm.Page.getControl("new_contract3").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault);
        case 6:
            Xrm.Page.getControl("new_contract4").addCustomView(viewId, entityName, viewDisplayName, fetchXml, layoutXml, isDefault);
    }
    Xrm.Page.data.entity.save();
}
//根据订单类型设置产品的查询
function addProductFilter() {
    var number = Xrm.Page.getAttribute("new_ordertype").getValue();
    if (number == 1) {
        var customerProductFilter = "<filter type='and'><condition attribute='new_category' operator='eq' value='" + number + "' /></filter>";
        Xrm.Page.getControl("new_product").addCustomFilter(customerProductFilter, "product");
    }
    if (number == 2) {
        var customerProductFilter = "<filter type='and'><condition attribute='new_category' operator='eq' value='" + number + "' /></filter>";
        Xrm.Page.getControl("new_product1").addCustomFilter(customerProductFilter, "product");
    }
    if (number == 6) {
        var customerProductFilter = "<filter type='and'><condition attribute='new_category' operator='eq' value='7' /></filter>";
        Xrm.Page.getControl("new_product2").addCustomFilter(customerProductFilter, "product");
    }

}
//加载时执行
function OnloadSetProductFilter() {

    // if (Xrm.Page.ui.getFormType() != 1) {
    var number = Xrm.Page.getAttribute("new_ordertype").getValue();
    if (number == 1) {
        Xrm.Page.getControl("new_product").addPreSearch(addProductFilter);
    }
    else if (number == 2) {
        Xrm.Page.getControl("new_product1").addPreSearch(addProductFilter);
    }
    else if (number == 6) {
        Xrm.Page.getControl("new_product2").addPreSearch(addProductFilter);
    }

    //  }
}
//订单类型变化时执行
function ChangeSetProductFilter() {

    var number = Xrm.Page.getAttribute("new_ordertype").getValue();
    if (number == 1) {
        Xrm.Page.getControl("new_product").addPreSearch(addProductFilter);
    }
    else if (number == 2) {
        Xrm.Page.getControl("new_product1").addPreSearch(addProductFilter);
    }
    else if (number == 6) {
        Xrm.Page.getControl("new_product2").addPreSearch(addProductFilter);
    }
}
console 命令行工具 X clear

                    
>
console