I have code written by someone and I have been told to fix dates and amounts issues which I have been debugging for a week with no luck. We can connect using a desktop-sharing app for further explanation and debugging. Txs.
function processStartDateChanged(strFrequency, dtNewStartDate = false, numfullDuration = false)
{
try
{ console.log("processStartDateChanged", "strFrequency", strFrequency, "dtNewStartDate", dtNewStartDate, "numfullDuration", numfullDuration);
let dtAppStartDate = dtNewStartDate ? dtNewStartDate : globalAppStartDate;
let dtAppEndDate = globalAppEndDate;
let duration = 0;
let newPayments = [];
let paymentFrequency = 0;
switch (strFrequency)
{
case "Quarterly":
paymentFrequency = 3;
break;
case "Monthly":
paymentFrequency = 1;
break;
case "Biannual":
paymentFrequency = 6;
break;
case "Yearly":
paymentFrequency = 12;
break;
case "Single":
default:
paymentFrequency = 0;
break;
}
// duration = globalNumOriginalDuration;
duration = numfullDuration ? globalNumOriginalDuration : 0;
let dtFirstPaymentDate;
if (dtNewStartDate)
{
dtFirstPaymentDate = new Date(dtAppStartDate); console.log("a - setDate", dtFirstPaymentDate);
dtFirstPaymentDate.setDate(dtFirstPaymentDate.getDate() - 3); console.log("b - setDate", dtFirstPaymentDate);
dtFirstPaymentDate.setMonth(dtFirstPaymentDate.getMonth() + paymentFrequency + 1); console.log("c - setDate", dtFirstPaymentDate);
dtFirstPaymentDate.setDate(1); console.log("e - setDate", dtFirstPaymentDate);
}
else
{
dtFirstPaymentDate = getPaymentDate(dtAppStartDate, strFrequency)
}
console.log("a - dtFirstPaymentDate", dtFirstPaymentDate, "a - dtAppStartDate", dtAppStartDate);
if (strFrequency == "Single")
{
newPayment = {};
newPayment["recordid"] = "new";
// numPaymentPeriod of the Single payments should be the same as the payment period of the Withheld
newPayment["cf_numPaymentPeriod"] = Math.ceil(numCurrentDuration / 12); // year 1 or year 2...
newPayment["cf_txtFrequency"] = strFrequency;
newPayment["cf_txtType"] = "Contract";
newPayment["cf_numRequestedAmount"] = 0;
newPayment["sf_Status Caption"] = "New";
newPayment["sf_Status ID"] = globalPaymentDraftStatusID;
newPayment["cf_dtePaymentDate"] = formatDate(dtFirstPaymentDate, "YYYY-MM-DD");
newPayment["cf_dteActualPaymentDate"] = formatDate(dtFirstPaymentDate, "YYYY-MM-DD");
newPayment["period"] = 0;
newPayment["newperiod"] = numCurrentDuration;
// new1
newPayment["cf_lkupContractingOrganisationL2"] = globalContractOrganisationID;
newPayment["cf_txtPayee"] = globalContractOrganisation;
newPayment["cf_cmbFundingStream"] = globalFundingStream;
//
newPayments.push(newPayment);
}
else
{
let dtCurrentPaymentDate = new Date(dtFirstPaymentDate);
let count = 0;
// we need to find the length of the first period
let periodLength = monthDiff(dtAppStartDate, dtFirstPaymentDate) + 1;
while (duration <= numCurrentDuration)
{
if(duration === numCurrentDuration) {
periodLength = paymentFrequency;
}
else {
if (numCurrentDuration - duration < paymentFrequency) {
periodLength = numCurrentDuration - duration;
}
}
newPayment = {};
newPayment["recordid"] = "new" + (count == 0 ? duration + 1 : duration);
if (count == 0)
{
duration += paymentFrequency + 1;
}
else {
duration += periodLength;
}
// we want to calculate the period number by the first month of the payment
newPayment["cf_numPaymentPeriod"] = Math.ceil((duration - periodLength + 1) / 12); // year 1 or year 2...
newPayment["cf_txtFrequency"] = strFrequency;
newPayment["cf_txtType"] = "Contract";
newPayment["cf_numRequestedAmount"] = 0;
newPayment["sf_Status Caption"] = "New";
newPayment["sf_Status ID"] = globalPaymentDraftStatusID;
newPayment["cf_dtePaymentDate"] = formatDate(dtCurrentPaymentDate, "YYYY-MM-DD");
newPayment["cf_dteActualPaymentDate"] = formatDate(dtCurrentPaymentDate, "YYYY-MM-DD");
// new1
newPayment["cf_lkupContractingOrganisationL2"] = globalContractOrganisationID;
newPayment["cf_txtPayee"] = globalContractOrganisation;
newPayment["cf_cmbFundingStream"] = globalFundingStream;
//
newPayment["period"] = 0;
newPayment["newperiod"] = periodLength;
newPayments.push(newPayment);
if (count == 0)
{
// change period length, might be different only at the first step, cause the project might start from the middle of the payment period
periodLength = paymentFrequency;
}
count++;
if (strFrequency == "Monthly" || strFrequency == "Yearly")
{
// calculate the date of the next payment - first day of the month
dtCurrentPaymentDate = new Date(dtCurrentPaymentDate.getFullYear(), dtCurrentPaymentDate.getMonth() + paymentFrequency, 1);
}
else
{
// calculate the date of the next payment - last day of the month
dtCurrentPaymentDate = new Date(dtCurrentPaymentDate.getFullYear(), dtCurrentPaymentDate.getMonth() + paymentFrequency + 1, 0);
}
console.log("dtCurrentPaymentDate", dtCurrentPaymentDate);
}
}
// add first withheld
newPayment = {};
newPayment["recordid"] = "new_withheld1";
newPayment["cf_txtType"] = "Withheld 1";
// numPaymentPeriod of Withheld payments should be the same as the payment period of the last payment
newPayment["cf_numPaymentPeriod"] = Math.ceil(numCurrentDuration / 12);
newPayment["cf_numRequestedAmount"] = 0;
newPayment["sf_Status Caption"] = "New";
newPayment["sf_Status ID"] = globalPaymentDraftStatusID;
let dtNewPaymentDate = new Date(dtAppEndDate.getFullYear(), dtAppEndDate.getUTCMonth() + 7, 1);
newPayment["cf_dtePaymentDate"] = formatDate(dtNewPaymentDate, "YYYY-MM-DD");
newPayment["cf_dteActualPaymentDate"] = formatDate(dtNewPaymentDate, "YYYY-MM-DD");
// new1
newPayment["cf_lkupContractingOrganisationL2"] = globalContractOrganisationID;
newPayment["cf_txtPayee"] = globalContractOrganisation;
newPayment["cf_cmbFundingStream"] = globalFundingStream;
//
newPayments.push(newPayment);
if (!globalIsGlobalHealth) // 2 withheld payments
{
// add second withheld
newPayment = {};
newPayment["recordid"] = "new_withheld2";
newPayment["cf_txtType"] = "Withheld 2";
newPayment["cf_numRequestedAmount"] = 0;
// numPaymentPeriod of Withheld payments should be the same as the payment period of the last payment
newPayment["cf_numPaymentPeriod"] = Math.ceil(numCurrentDuration / 12);
newPayment["sf_Status Caption"] = "New";
newPayment["sf_Status ID"] = globalPaymentDraftStatusID;
dtNewPaymentDate = new Date(dtAppEndDate.getFullYear(), dtAppEndDate.getUTCMonth() + 25, 1);
newPayment["cf_dtePaymentDate"] = formatDate(dtNewPaymentDate, "YYYY-MM-DD");
newPayment["cf_dteActualPaymentDate"] = formatDate(dtNewPaymentDate, "YYYY-MM-DD");
// new1
newPayment["cf_lkupContractingOrganisationL2"] = globalContractOrganisationID;
newPayment["cf_txtPayee"] = globalContractOrganisation;
newPayment["cf_cmbFundingStream"] = globalFundingStream;
//
newPayments.push(newPayment);
}
// console.log("newPayments", newPayments);
return newPayments;
}
catch (ex)
{
alert("Error in processStartDateChanged(): " + ex.toString());
disableAll();
return false;
}
}