var DependentOptionSetConfig;
function Form_Onload() {
adjustProbability();
OpportunityStage();
OpportunityFacility_Load();
SetSubStageList();
Xrm.Page.ui.controls.get("dot_loantovalue").setDisabled(true);
onCreditPolicyChange();
}
function SetSubStageList() {
if ((Xrm.Page.data.entity.attributes.get("dot_stage") != null) && (Xrm.Page.data.entity.attributes.get("dot_stage").getValue() != null) &&
Xrm.Page.data.entity.attributes.get("dot_stage").getValue() == 179913000) { //Credit
//MM: 11/6/2014 Added option : 179910000 before 179,913,003 based on CR
//MM: 20/6/2014 Added option: 179913005 before pending saction(179,913,003)
DependentOptionSetConfig =
[{ "parent": "dot_stage", "dependent": "dot_substage",
"options": [
{ "value": "179913000", "label": "Credit", "showOptions": ["179913001", "179913002","179913004","179913005","179913006","179913007","179913003"] },
]
}];
filterDependentField("dot_stage", "dot_substage");
} else {
DependentOptionSetConfig =
[{ "parent": "dot_facilitytype", "dependent": "dot_substage",
"options": [
{ "value": "179910001", "label": "Commercial Finance", "showOptions": ["179914002", "179910002", "179910003", "179910004", "179910005"] },
{ "value": "179910002", "label": "Vehicle Finance", "showOptions": ["179914002", "179910002", "179910003", "179910004", "179910005", "179910006", "179910007"] },
{ "value": "179910003", "label": "Small Business Loans", "showOptions": ["179910002", "179910003", "179910004", "179910005", "179910006"] },
{ "value": "179910004", "label": "Consumer Finance", "showOptions": ["179914002", "179910002", "179910003", "179910004", "179910005", "179910006", "179910007"] },
{ "value": "179910005", "label": "Marine Finance", "showOptions": ["179914002", "179910002", "179910003", "179910004", "179910005", "179910006", "179910007"] },
{ "value": "179910006", "label": "Invoice Finance", "showOptions": ["179910002", "179910003", "179910004", "179910005", "179910006"] },
{ "value": "179910007", "label": "Inventory Finance", "showOptions": ["179910002", "179910003", "179910004", "179910005", "179910006"] },
{ "value": "179910008", "label": "Cheque Discounting", "showOptions": ["179910002", "179910003", "179910004", "179910005", "179910006"] },
{ "value": "179910009", "label": "Finance Lease", "showOptions": ["179910002", "179910003", "179910004", "179910005", "179910006"] },
]
}];
filterDependentField("dot_facilitytype", "dot_substage");
}
}
function adjustProbability() {
var rate = null;
var stage = null;
var substage = null;
var perc = null;
if ((Xrm.Page.data.entity.attributes.get("dot_stage") != null) && (Xrm.Page.data.entity.attributes.get("dot_stage").getValue() != null))
stage = Xrm.Page.data.entity.attributes.get("dot_stage").getValue();
if ((Xrm.Page.data.entity.attributes.get("dot_substage") != null) && (Xrm.Page.data.entity.attributes.get("dot_substage").getValue() != null))
substage = Xrm.Page.data.entity.attributes.get("dot_substage").getValue();
if ((Xrm.Page.data.entity.attributes.get("opportunityratingcode") != null) && (Xrm.Page.data.entity.attributes.get("opportunityratingcode").getValue() != null))
rate = Xrm.Page.data.entity.attributes.get("opportunityratingcode").getValue();
switch (stage) {
case 179911000: // Collecting Documents
switch (rate) {
case 3: // cold
perc = 10;
break;
case 2: // warm
perc = 25;
break;
case 1: // hot
perc = 50;
break;
default:
break;
}
break;
case 179912000: // Preparing Application
switch (rate) {
case 3: // cold
perc = 25;
break;
case 2: // warm
perc = 50;
break;
case 1: // hot
perc = 75;
break;
default:
break;
}
break;
case 179913000: // Credit
switch (rate) {
case 3: // cold
perc = 25;
break;
case 2: // warm
perc = 50;
break;
case 1: // hot
perc = 75;
break;
default:
break;
}
break;
case 179910000: // Sanctioned
switch (rate) {
case 3: // cold
perc = 25;
break;
case 2: // warm
perc = 50;
break;
case 1: // hot
perc = 100;
break;
default:
break;
}
break;
case 179914000: // PSF
switch (rate) {
case 3: // cold
perc = 25;
break;
case 2: // warm
perc = 50;
break;
case 1: // hot
perc = 100;
break;
default:
break;
}
break;
case 179915000: // Disbursed
switch (rate) {
case 3: // cold
perc = 100;
break;
case 2: // warm
perc = 100;
break;
case 1: // hot
perc = 100;
break;
default:
break;
}
break;
default:
break;
}
//set closeprobability field value
Xrm.Page.data.entity.attributes.get("closeprobability").setValue(perc);
Xrm.Page.data.entity.attributes.get("closeprobability").setSubmitMode("always");
}
function OpportunityStage() {
var stage = Xrm.Page.data.entity.attributes.get("dot_stage");
if (stage && (stage.getValue() != null)) {
var dealNo = Xrm.Page.data.entity.attributes.get("dot_dealnumber");
if (dealNo) {
dealNo.setRequiredLevel("none");
}
var disbursementDate = Xrm.Page.data.entity.attributes.get("dot_actualdisbursementdate");
if (disbursementDate) {
disbursementDate.setRequiredLevel("none");
Xrm.Page.ui.controls.get("dot_actualdisbursementdate").setVisible(false);
}
var substageCtrl = Xrm.Page.ui.controls.get("dot_substage");
var substageAttr = Xrm.Page.data.entity.attributes.get("dot_substage");
var sanctionTerms = Xrm.Page.ui.controls.get("dot_sanctionterms");
var declineReasons = Xrm.Page.ui.controls.get("dot_declinereasons");
var declineReason = Xrm.Page.data.entity.attributes.get("dot_declinereason");
if (sanctionTerms) {
sanctionTerms.setVisible(false);
}
if (declineReasons) {
declineReasons.setVisible(false);
}
if (declineReason) {
declineReason.setRequiredLevel("none");
Xrm.Page.ui.controls.get("dot_declinereason").setVisible(false);
}
switch (stage.getValue()) {
case 179915000: //Disbursed
if (dealNo) {
dealNo.setRequiredLevel("required");
//Added by MM ON 11/6/2014 CR
if (sanctionTerms) {
sanctionTerms.setVisible(true);
}
}
if (disbursementDate) {
disbursementDate.setRequiredLevel("required");
// If the attribute exists we can assume the control will also exist
Xrm.Page.ui.controls.get("dot_actualdisbursementdate").setVisible(true);
}
if (substageAttr) {
substageAttr.setValue(null);
}
if (substageCtrl) {
substageCtrl.setVisible(false);
}
break;
case 179913000: // Credit
if (substageCtrl) {
substageCtrl.setVisible(true);
}
if (sanctionTerms) {
sanctionTerms.setVisible(true);
}
break;
case 179914000: //PSF
if (substageCtrl) {
substageCtrl.setVisible(true);
}
//Added by MM ON 11/6/2014 CR
if (sanctionTerms) {
sanctionTerms.setVisible(true);
}
break;
case 179918000: // Facility Repaid/Closed
if (disbursementDate) {
disbursementDate.setRequiredLevel("required");
// If the attribute exists we can assume the control will also exist
Xrm.Page.ui.controls.get("dot_actualdisbursementdate").setVisible(true);
}
break;
// On Sanctioned show the sanctioned comments field
case 179910000: // Sanctioned
if (sanctionTerms) {
sanctionTerms.setVisible(true);
}
break;
// On Declined show the decline reasons field
case 179916000: // GFC Declined
if (declineReasons) {
declineReasons.setVisible(true);
}
if (declineReason) {
declineReason.setRequiredLevel("required");
Xrm.Page.ui.controls.get("dot_declinereason").setVisible(true);
}
break;
case 179917000: // Customer Declined
if (declineReasons) {
declineReasons.setVisible(true);
}
if (declineReason) {
declineReason.setRequiredLevel("required");
Xrm.Page.ui.controls.get("dot_declinereason").setVisible(true);
}
break;
default:
Xrm.Page.data.entity.attributes.get("dot_substage").setValue(null);
Xrm.Page.ui.controls.get("dot_substage").setVisible(false);
break;
}
}
}
function OpportunityFacility_Change() {
if ((Xrm.Page.data.entity.attributes.get("dot_facilitytype") != null) && (Xrm.Page.data.entity.attributes.get("dot_facilitytype").getValue() != null)) {
var newName = Xrm.Page.data.entity.attributes.get("dot_facilitytype").getText();
Xrm.Page.data.entity.attributes.get("name").setValue(newName);
switch (Xrm.Page.data.entity.attributes.get("dot_facilitytype").getValue()) {
case 179910006: //Invoice Finance
case 179910007: //Inventory Finance
Xrm.Page.ui.controls.get("dot_flatrate").setVisible(false);
Xrm.Page.ui.controls.get("dot_perdayrate").setVisible(true);
if ((Xrm.Page.data.entity.attributes.get("dot_effectiverate") != null) && (Xrm.Page.data.entity.attributes.get("dot_effectiverate").getValue() != null)) {
var tempvar1 = Xrm.Page.data.entity.attributes.get("dot_effectiverate").getValue();
Xrm.Page.data.entity.attributes.get("dot_perdayrate").setValue(tempvar1 / 360);
}
break;
default:
Xrm.Page.ui.controls.get("dot_flatrate").setVisible(true);
Xrm.Page.ui.controls.get("dot_perdayrate").setVisible(false);
if ((Xrm.Page.data.entity.attributes.get("dot_effectiverate") != null) && (Xrm.Page.data.entity.attributes.get("dot_effectiverate").getValue() != null)) {
var tempvar2 = Xrm.Page.data.entity.attributes.get("dot_effectiverate").getValue();
//Xrm.Page.data.entity.attributes.get("dot_flatrate").setValue(tempvar2 ?????? );
}
break;
}
}
}
function OpportunityFacility_Load() {
if ((Xrm.Page.data.entity.attributes.get("dot_facilitytype") != null) && (Xrm.Page.data.entity.attributes.get("dot_facilitytype").getValue() != null)) {
var newName = Xrm.Page.data.entity.attributes.get("dot_facilitytype").getText();
Xrm.Page.data.entity.attributes.get("name").setValue(newName);
switch (Xrm.Page.data.entity.attributes.get("dot_facilitytype").getValue()) {
case 179910006: //Invoice Finance
case 179910007: //Inventory Finance
Xrm.Page.ui.controls.get("dot_flatrate").setVisible(false);
Xrm.Page.ui.controls.get("dot_perdayrate").setVisible(true);
break;
default:
Xrm.Page.ui.controls.get("dot_flatrate").setVisible(true);
Xrm.Page.ui.controls.get("dot_perdayrate").setVisible(false);
break;
}
}
}
function CalculateLoanToValue() {
var AR = 0;
if ((Xrm.Page.data.entity.attributes.get("estimatedvalue") != null) && (Xrm.Page.data.entity.attributes.get("estimatedvalue").getValue() != null))
AR = Xrm.Page.data.entity.attributes.get("estimatedvalue").getValue();
var PS = 0;
if ((Xrm.Page.data.entity.attributes.get("dot_proposedsecurity") != null) && (Xrm.Page.data.entity.attributes.get("dot_proposedsecurity").getValue() != null))
PS = Xrm.Page.data.entity.attributes.get("dot_proposedsecurity").getValue();
if (PS == 0) {
Xrm.Page.data.entity.attributes.get("dot_loantovalue").setValue(0);
} else {
Xrm.Page.data.entity.attributes.get("dot_loantovalue").setValue(AR / PS * 100);
}
Xrm.Page.data.entity.attributes.get("dot_loantovalue").setSubmitMode("always");
}
function Calculations(context) {
// debugger;
var facilityType = 0;
if ((Xrm.Page.data.entity.attributes.get("dot_facilitytype") != null) && (Xrm.Page.data.entity.attributes.get("dot_facilitytype").getValue() != null)) {
facilityType = Xrm.Page.data.entity.attributes.get("dot_facilitytype").getValue();
}
if (facilityType == 179910001 // Commercial Finance
|| facilityType == 179910004 // Consumer Finance
|| facilityType == 179910005 // Marine Finance
|| facilityType == 179910009 // Finance Lease
|| facilityType == 179910002 // Vehicle Lease
|| facilityType == 179910003 // Small Business Loans
) {
} else {
// Only clear the fields if this was initiated from the facility type change.
// Otherwise let the user type numbers here freely.
var callerCtrl = null;
if (typeof (context) != 'undefined' && context != null)
callerCtrl = context.getEventSource();
if (callerCtrl != null && callerCtrl.getName() == 'dot_facilitytype') {
var clearFields = ["estimatedvalue",
"dot_effectiverate",
"dot_term",
"dot_monthlyeffective",
"dot_monthlyrepayment",
"dot_totalrepayment",
"dot_interestpaid",
"dot_totalinterest",
"dot_flatrate",
"dot_arrangementfee",
"dot_arrangementfeeperc",
"dot_creditlife",
"dot_includearrangementfee",
"dot_includecreditlife",
"dot_totalloanamount",
"dot_wair"
];
ClearNumericFields(clearFields);
}
// Always return here, do not calculate anything
return;
}
var amountRequired = 0;
if ((Xrm.Page.data.entity.attributes.get("estimatedvalue") != null) && (Xrm.Page.data.entity.attributes.get("estimatedvalue").getValue() != null)) {
amountRequired = Xrm.Page.data.entity.attributes.get("estimatedvalue").getValue();
}
var effectiveRate = 0;
if ((Xrm.Page.data.entity.attributes.get("dot_effectiverate") != null) && (Xrm.Page.data.entity.attributes.get("dot_effectiverate").getValue() != null)) {
effectiveRate = Xrm.Page.data.entity.attributes.get("dot_effectiverate").getValue();
}
var term = 0;
if ((Xrm.Page.data.entity.attributes.get("dot_term") != null) && (Xrm.Page.data.entity.attributes.get("dot_term").getValue() != null)) {
term = Xrm.Page.data.entity.attributes.get("dot_term").getValue();
}
var arrangementFeePerc = 0;
if ((Xrm.Page.data.entity.attributes.get("dot_arrangementfeeperc") != null) && (Xrm.Page.data.entity.attributes.get("dot_arrangementfeeperc").getValue() != null)) {
arrangementFeePerc = Xrm.Page.data.entity.attributes.get("dot_arrangementfeeperc").getValue();
}
var wair = (effectiveRate / 100) * amountRequired;
Xrm.Page.data.entity.attributes.get("dot_wair").setValue(wair);
Xrm.Page.data.entity.attributes.get("dot_wair").setSubmitMode("always");
var arrangementFee = (arrangementFeePerc / 100) * amountRequired;
Xrm.Page.data.entity.attributes.get("dot_arrangementfee").setValue(arrangementFee);
Xrm.Page.data.entity.attributes.get("dot_arrangementfee").setSubmitMode("always");
var creditLife = amountRequired * (0.028 / 100) * term;
Xrm.Page.data.entity.attributes.get("dot_creditlife").setValue(creditLife);
Xrm.Page.data.entity.attributes.get("dot_creditlife").setSubmitMode("always");
var dot_includearrangementfee = false;
if ((Xrm.Page.data.entity.attributes.get("dot_includearrangementfee") != null) && (Xrm.Page.data.entity.attributes.get("dot_includearrangementfee").getValue() != null)) {
dot_includearrangementfee = Xrm.Page.data.entity.attributes.get("dot_includearrangementfee").getValue();
}
var dot_includecreditlife = false;
if ((Xrm.Page.data.entity.attributes.get("dot_includecreditlife") != null) && (Xrm.Page.data.entity.attributes.get("dot_includecreditlife").getValue() != null)) {
dot_includecreditlife = Xrm.Page.data.entity.attributes.get("dot_includecreditlife").getValue();
}
var totalLoanAmount = amountRequired;
if (dot_includearrangementfee == true) {
totalLoanAmount += arrangementFee;
}
if (dot_includecreditlife == true) {
totalLoanAmount += creditLife;
}
Xrm.Page.data.entity.attributes.get("dot_totalloanamount").setValue(totalLoanAmount);
Xrm.Page.data.entity.attributes.get("dot_totalloanamount").setSubmitMode("always");
var monthlyEffective = effectiveRate / 100 / 12;
Xrm.Page.data.entity.attributes.get("dot_monthlyeffective").setValue(monthlyEffective);
Xrm.Page.data.entity.attributes.get("dot_monthlyeffective").setSubmitMode("always");
var monthlyRepayment = 0;
if (monthlyEffective > 0 && term > 0 && amountRequired > 0) {
monthlyRepayment = -PMT(monthlyEffective, term, totalLoanAmount, 0, 0);
}
Xrm.Page.data.entity.attributes.get("dot_monthlyrepayment").setValue(monthlyRepayment);
Xrm.Page.data.entity.attributes.get("dot_monthlyrepayment").setSubmitMode("always");
var totalRepayment = monthlyRepayment * term;
Xrm.Page.data.entity.attributes.get("dot_totalrepayment").setValue(totalRepayment);
Xrm.Page.data.entity.attributes.get("dot_totalrepayment").setSubmitMode("always");
//MM : Changed on 11/6/2014 from var interestPaid = totalRepayment - amountRequired
//to var interestPaid = totalRepayment - totalLoanAmount.getValue() ;;
var totalLoanAmount= Xrm.Page.data.entity.attributes.get("dot_totalloanamount");
//Added on 11/6/2014 for checking if there is such field and its not null
if(!isUndefinedOrNull(totalLoanAmount))
{
//calculate the new interest based on the CR 11/6/2014
var interestPaid = totalRepayment - totalLoanAmount.getValue() ;
Xrm.Page.data.entity.attributes.get("dot_interestpaid").setValue(interestPaid);
Xrm.Page.data.entity.attributes.get("dot_interestpaid").setSubmitMode("always");
//MM : Changed on 11/6/2014 from totalInterest = (interestPaid / amountRequired) * 100;
//to totalInterest = (interestPaid / totalLoanAmount.getValue()) * 100;
var totalInterest = 0;
if (amountRequired != 0) {
totalInterest = (interestPaid / totalLoanAmount.getValue()) * 100;
}
Xrm.Page.data.entity.attributes.get("dot_totalinterest").setValue(totalInterest);
Xrm.Page.data.entity.attributes.get("dot_totalinterest").setSubmitMode("always");
}
var flatRate = 0;
if (term > 0) {
flatRate = totalInterest / (term / 12);
}
Xrm.Page.data.entity.attributes.get("dot_flatrate").setValue(flatRate);
Xrm.Page.data.entity.attributes.get("dot_flatrate").setSubmitMode("always");
} //Calculation
function PMT(rate_per_period, number_of_payments, present_value, future_value, type) {
if (rate_per_period != 0.0) {
// Interest rate exists
var q = Math.pow(1 + rate_per_period, number_of_payments);
return -(rate_per_period * (future_value + (q * present_value))) / ((-1 + q) * (1 + rate_per_period * (type)));
} else if (number_of_payments != 0.0) {
// No interest rate, but number of payments exists
return -(future_value + present_value) / number_of_payments;
}
return 0;
}
function ClearNumericFields(myArray) {
var arrayLength = myArray.length;
for (var i = 0; i < arrayLength; i++) {
Xrm.Page.data.entity.attributes.get(myArray[i]).setValue(0);
Xrm.Page.data.entity.attributes.get(myArray[i]).setSubmitMode("always");
}
}
//MM:11/6/2014: Will check whether the field is undefined/ null
function isUndefinedOrNull(variable)
{
if (typeof variable === "undefined" || variable === null)
{
return true;
}
else
{
return false;
}
}//isUndefinedOrNull
function onCreditPolicyChange()
{
var CreditPolicy=Xrm.Page.data.entity.attributes.get("dot_creditpolicy");
if(!isUndefinedOrNull(CreditPolicy))
{
if(CreditPolicy.getValue()!=null)
{
//True -> show fields
if(CreditPolicy.getValue()==false)
{
hideShowFields("dot_policydeviations,dot_policydeviationsauthorizedby",true);
}
else //hide fields
{
hideShowFields("dot_policydeviations,dot_policydeviationsauthorizedby",false);
}
}
}
}
// MM: 17/6/2014 Added to disable/enable fields
// FieldsArrayToHide will hold a comma separated fields that will hide,
// Operation will be = false if the fields will be hidden , true will be shown
function hideShowFields(FieldsArrayToHide,Operation)
{
var FieldsArray = FieldsArrayToHide != null ? FieldsArrayToHide.split(',') : null; //split parameters
if (FieldsArray != null) {
for (var i = 0; i < FieldsArray.length; i++)
{
var FieldName = Xrm.Page.ui.controls.get(FieldsArray[i]);
if(!isUndefinedOrNull(FieldName))
{
FieldName.setVisible(Operation)
}
}
}
}
------------------------------------------------------
this is the javascript code
Thanks
dilip