Fixing dates and amounts in JavaScript

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.

Hey @asifakhtar, it would be better if you could post the relevant code so we can discuss it here.

1 Like

Txs raiphm for the reply.
I have hardcoded all the variables here.
relevant code: in multiple posts because of the character limit:

var globalPayments = [{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2023","sf_Status Caption":"Draft","sf_Level Two ID":"23377769","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377769","cf_cmbFundingStream":"51","cf_numPaymentPeriod":1,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2023-12-01","cf_dtePaymentDate":"2023-02-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":12,"newperiod":12},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2023","sf_Status Caption":"Draft","sf_Level Two ID":"23377770","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377770","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-01-01","cf_dtePaymentDate":"2023-03-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2023","sf_Status Caption":"Draft","sf_Level Two ID":"23377801","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377801","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-02-01","cf_dtePaymentDate":"2023-04-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2023","sf_Status Caption":"Draft","sf_Level Two ID":"23377803","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377803","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-03-01","cf_dtePaymentDate":"2023-05-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377805","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377805","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-04-01","cf_dtePaymentDate":"2023-06-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377807","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377807","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-05-01","cf_dtePaymentDate":"2023-07-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377776","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377776","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-06-01","cf_dtePaymentDate":"2023-08-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377773","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377773","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-07-01","cf_dtePaymentDate":"2023-09-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377777","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377777","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-08-01","cf_dtePaymentDate":"2023-10-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377809","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377809","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-09-01","cf_dtePaymentDate":"2023-11-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377808","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377808","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-10-01","cf_dtePaymentDate":"2023-12-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"56300.59","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377806","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377806","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"56300.59","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-11-01","cf_dtePaymentDate":"2024-01-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377804","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377804","cf_cmbFundingStream":"51","cf_numPaymentPeriod":2,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2024-12-01","cf_dtePaymentDate":"2024-02-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377802","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377802","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-01-01","cf_dtePaymentDate":"2024-03-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377800","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377800","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-02-01","cf_dtePaymentDate":"2024-04-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2024","sf_Status Caption":"Draft","sf_Level Two ID":"23377798","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377798","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-03-01","cf_dtePaymentDate":"2024-05-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377796","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377796","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-04-01","cf_dtePaymentDate":"2024-06-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377794","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377794","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-05-01","cf_dtePaymentDate":"2024-07-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377793","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377793","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-06-01","cf_dtePaymentDate":"2024-08-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377790","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377790","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-07-01","cf_dtePaymentDate":"2024-09-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377788","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377788","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-08-01","cf_dtePaymentDate":"2024-10-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377786","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377786","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-09-01","cf_dtePaymentDate":"2024-11-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377780","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377780","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-10-01","cf_dtePaymentDate":"2024-12-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"63577.79","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377782","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377782","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"63577.79","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-11-01","cf_dtePaymentDate":"2025-01-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377771","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377771","cf_cmbFundingStream":"51","cf_numPaymentPeriod":3,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2025-12-01","cf_dtePaymentDate":"2025-02-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377799","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377799","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-01-01","cf_dtePaymentDate":"2025-03-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377797","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377797","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-02-01","cf_dtePaymentDate":"2025-04-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2025","sf_Status Caption":"Draft","sf_Level Two ID":"23377772","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377772","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-03-01","cf_dtePaymentDate":"2025-05-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377774","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377774","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-04-01","cf_dtePaymentDate":"2025-06-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377795","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377795","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-05-01","cf_dtePaymentDate":"2025-07-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377792","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377792","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-06-01","cf_dtePaymentDate":"2025-08-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377775","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377775","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-07-01","cf_dtePaymentDate":"2025-09-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377783","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377783","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-08-01","cf_dtePaymentDate":"2025-10-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377779","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377779","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-09-01","cf_dtePaymentDate":"2025-11-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377791","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377791","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-10-01","cf_dtePaymentDate":"2025-12-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64615.87","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377789","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377789","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64615.87","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-11-01","cf_dtePaymentDate":"2026-01-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64149.38","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377787","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377787","cf_cmbFundingStream":"51","cf_numPaymentPeriod":4,"cf_numRequestedAmountRounded":"64149.38","cf_txtType":"Contract","cf_dteActualPaymentDate":"2026-12-01","cf_dtePaymentDate":"2026-02-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64149.38","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377785","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377785","cf_cmbFundingStream":"51","cf_numPaymentPeriod":5,"cf_numRequestedAmountRounded":"64149.38","cf_txtType":"Contract","cf_dteActualPaymentDate":"2027-01-01","cf_dtePaymentDate":"2026-03-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"sf_Parent ID":"198737470","cf_txtPayee":"Imperial College London","sf_Status ID":"75938","cf_numRequestedAmount":"64149.38","sf_Type ID":"76089","cf_numPaymentWithheld":"2","cf_txtPaymentFiscalYear":"2026","sf_Status Caption":"Draft","sf_Level Two ID":"23377781","cf_numPercentageWithheld":"0.1","cf_cmbManuallyUpdated":"","cf_lkupContractingOrganisationL2":"1749016","recordid":"23377781","cf_cmbFundingStream":"51","cf_numPaymentPeriod":5,"cf_numRequestedAmountRounded":"64149.38","cf_txtType":"Contract","cf_dteActualPaymentDate":"2027-02-01","cf_dtePaymentDate":"2026-04-01","cf_txtFrequency":"Monthly","cf_txtProfileMethod":"Budget","period":1,"newperiod":1},{"recordid":"new40","cf_numPaymentPeriod":5,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-05-01","cf_dteActualPaymentDate":"2026-05-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":-9,"newperiod":-9},{"recordid":"new41","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-06-01","cf_dteActualPaymentDate":"2026-06-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new42","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-07-01","cf_dteActualPaymentDate":"2026-07-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new43","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-08-01","cf_dteActualPaymentDate":"2026-08-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new44","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-09-01","cf_dteActualPaymentDate":"2026-09-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new45","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-10-01","cf_dteActualPaymentDate":"2026-10-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new46","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-11-01","cf_dteActualPaymentDate":"2026-11-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new47","cf_numPaymentPeriod":4,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2026-12-01","cf_dteActualPaymentDate":"2026-12-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new48","cf_numPaymentPeriod":5,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2027-01-01","cf_dteActualPaymentDate":"2027-01-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new49","cf_numPaymentPeriod":5,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2027-02-01","cf_dteActualPaymentDate":"2027-02-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new50","cf_numPaymentPeriod":5,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2027-03-01","cf_dteActualPaymentDate":"2027-03-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":1},{"recordid":"new51","cf_numPaymentPeriod":5,"cf_txtFrequency":"Monthly","cf_txtType":"Contract","cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2027-04-01","cf_dteActualPaymentDate":"2027-04-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","period":1,"newperiod":0},{"recordid":"new_withheld1","cf_txtType":"Withheld 1","cf_numPaymentPeriod":6,"cf_numRequestedAmount":0,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2027-10-01","cf_dteActualPaymentDate":"2027-10-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","newperiod":1,"period":1},{"recordid":"new_withheld2","cf_txtType":"Withheld 2","cf_numRequestedAmount":0,"cf_numPaymentPeriod":6,"sf_Status Caption":"New","sf_Status ID":"75938","cf_dtePaymentDate":"2029-04-01","cf_dteActualPaymentDate":"2029-04-01","cf_lkupContractingOrganisationL2":"1749016","cf_txtPayee":"Imperial College London","cf_cmbFundingStream":"17","newperiod":1,"period":1}]
const globalEventID = "23337888";
const globalOwnerID = "88635481";
const globalOpportunityID = "198737470";
const globalOpportunityTypeID = "28701";
const globalIsGlobalHealth = globalOpportunityTypeID == "28704" ? true : false;
const globalIsBudgetAmendment = "1" == "1" ? true : false;
const globalIsCTU = globalOpportunityTypeID == "28705" || globalOpportunityTypeID == "28710" ? true : false;
const globalPaymentPendingStatusID = "76222";
const globalPaymentPaidStatusID = "76119";
const globalPaymentDraftStatusID = "75938";
const globalTentativePaymentTypeID = "76333";
const globalTentativeDisbursementTypeID = "76334";
const globalPaymentTypeID = "76089";
const globalDisbursementTypeID = "76120";
const globalContractOrganisationID = "" != "" ? "" : "1749016";
const globalRefundTypeID = "76276";
const globalTentativeOwnerID = "" != "" ? "" : "88635481";
const globalFundingStream = "17";
const globalContractOrganisation = "" != "" ? "" : "Imperial College London";
const arrJointFunding = "".split("|;|").filter(function(value) {
    return value != undefined && value != "";
});
const globalIsJointFunding = arrJointFunding.length == 5 ? true : false; //should only ever be one
const globalNumTotalCost = Number("2673754.6") + ("ÂŁ128,000.00" == "" ? 0 : Number("128000.00"));
const globalNumOriginalDuration = Number("39");
var numCurrentDuration = globalNumOriginalDuration + Number("12");
var globalAppStartDate = "" != "" ? new Date(" 00:00:00.000") : new Date("2023-01-01 00:00:00.000");
var globalPrevAppStartDate = "2023-01-01" != "" ? new Date("2023-01-01 00:00:00.000") : new Date("2023-01-01 00:00:00.000");
var globalAppOrgEndDate = new Date("2026-03-31 00:00:00.000");
var globalAppEndDate = new Date(globalAppStartDate);
var strAppStartDate = "" != "" ?
    '' :
    '01 January 2023';

const globalDateOptions = {
    day: '2-digit',
    month: '2-digit',
    year: 'numeric'
};
var globalArrYearlyTotals = [
    750674.55,
    847703.81,
    861544.96,
    261831.28,
    80000
];
var globalFlatYearlyTotals = [];
var globalPaymentsDisburesements = [];
var globalDisbursements = [];
var globalReprofiledDisbursements; // map
var globalPaymentsToDelete = [];
var globalArrRefundPayments = [];

var globalInitialWithheldPercent;
var globalExceptionOccurred = false;
var numAjaxRequests = 0;
var arrJSONVars = [];
var globalNumYears = 0;
var globalArrEventIDs = [];
var objPayments;
var globalRefundAmountTotal = 0;
var globalPaidAmountTotal = 0;
var globalDisbursementAdjustment;
var globalBoolRevisedDate = "" != "" ? true : false;
var globalDteRevisedDate = " 00:00:00.000";
var globalPaidArr = [];
let globalLastYearRowsCount = 0;
function reprofileSchedule() {
    try {
        console.log("reprofileSchedule - begin");

        // recalculate withheld payments
        let grandTotal = Number($tdNewGrandTotal.data("amount"));
        let newWithheldTotal = grandTotal * $optWithheld.val();
        let finalWithhelpPercent = $optWithheld.val();
        //let getRecordswithBadFrequency = [];									  

        // empty the array
        globalPaymentsDisburesements = [];

        if (globalIsGlobalHealth) {
            // this is only one payment for globalIsGlobalHealth
            if (newWithheldTotal < 100000) {
                newWithheldTotal = 100000;
            }
            finalWithhelpPercent = newWithheldTotal / grandTotal;

            // console.log("finalWithhelpPercent 1", finalWithhelpPercent, newWithheldTotal, grandTotal);
        } else {
            // we have two withheld payment for other application types
            // console.log("newWithheldTotal 1", newWithheldTotal, newWithheldTotal / 2);
            newWithheldTotal = newWithheldTotal / 2;
        }

        if ($optProfile.val() == "Flat") {
            console.log("flat");
            let remainedAmount = globalArrYearlyTotals.reduce(function(acc, val) {
                return acc + val;
            }, 0);

            let reprofilingPeriod = 0;

            for (let i = 0; i < globalPayments.length; i++) {
                let record = globalPayments[i];
                let strEventID = record["recordid"];
                let strType = record["cf_txtType"];
                let numPaymentAmount = Number(record["cf_numRequestedAmount"]);
                let strStatusID = record["sf_Status ID"];
                let monthCount = record["newperiod"];

                if (strType.indexOf("Withheld") >= 0 || /*strStatusID == globalPaymentPendingStatusID ||*/ strStatusID == globalPaymentPaidStatusID) {
                    //console.log("strType", strType);
                    let $tdNew = $("#tdNewPayment_" + strEventID);
                    let $td = $("#tdPayment_" + strEventID);
                    if (strType.indexOf("Withheld") >= 0) {
                        // we want to subtract the withheld amount according to the number if withheld payments (1 or 2)
                        remainedAmount -= newWithheldTotal;
                        $tdNew.text(formatAsCurrency((newWithheldTotal), 2, 2));
                        $tdNew.data("amount", newWithheldTotal);
                    } else {
                        // we copy the data for the paid payments
                        remainedAmount -= numPaymentAmount;
                        $tdNew.text($td.text());
                        $tdNew.data("amount", $td.data("amount"));
                    }
                } else {
                    reprofilingPeriod += monthCount;
                }
            }

            if (globalBoolRevisedDate) {
                remainedAmount -= globalPaidAmountTotal + globalRefundAmountTotal;
            }

            // new monthly payment
            let flatPayment = (remainedAmount / reprofilingPeriod).toFixed(2);
            let newTotal = flatPayment * reprofilingPeriod;
            // adjustment will be applied to the first payment
            let centsAdjustments = newTotal - remainedAmount;
            let firstPayment = true;

            for (let i = 0; i < globalPayments.length; i++) {
                let strEventID = globalPayments[i]["recordid"];
                let strType = globalPayments[i]["cf_txtType"];
                let strStatusID = globalPayments[i]["sf_Status ID"];
                let monthCount = globalPayments[i]["newperiod"];
                let disbursements = [];

                if (strType.indexOf("Withheld") == -1 && /*strStatusID != globalPaymentPendingStatusID &&*/ strStatusID != globalPaymentPaidStatusID) {
                    let $td = $("#tdNewPayment_" + strEventID);
                    let paymentMonthCount = globalPayments[i]["newperiod"]; //$td.data("month");
                    let numPaymentAmount = flatPayment * paymentMonthCount - (firstPayment ? centsAdjustments : 0);

                    $td.text(formatAsCurrency(numPaymentAmount, 2, 2));
                    $td.data("amount", numPaymentAmount);

                    console.log("flatPayment", numPaymentAmount, monthCount, paymentMonthCount);

                    globalPayments[i]["cf_numRequestedAmount"] = numPaymentAmount;

                    for (j = 0; j < monthCount; j++) {
                        disbursements.push(firstPayment && j == 0 ? (flatPayment - centsAdjustments) : flatPayment);
                    }
                    firstPayment = false;
                }

                if (strType.indexOf("Withheld") > -1) {
                    let $td = $("#tdNewPayment_" + strEventID);
                    globalPayments[i]["cf_numRequestedAmount"] = newWithheldTotal;
                    disbursements.push(newWithheldTotal);
                }

                globalPaymentsDisburesements.push(disbursements);
            }
        } else // reprofile by yearly budget amount
        {
            console.log("yearly");
            var arrYearlyTotals = [];
            var arrYearlyMonths = [];

            // prepare arrays with total amount and covered month per project year
            globalArrYearlyTotals.forEach(function(value, index, array) {
                arrYearlyTotals.push(value);
            });

            // console.log("arrYearlyTotals", arrYearlyTotals); ///myc

            for (let i = 0; i < globalArrYearlyTotals.length; i++) {
                if (numCurrentDuration > (i + 1) * 12) {
                    arrYearlyMonths.push(12);
                } else {
                    arrYearlyMonths.push(numCurrentDuration - i * 12)
                }
            }

            // console.log("arrYearlyMonths", arrYearlyMonths); ///myc

            // if all payments of one of the years were paid in full then we need to remove the withheld of this year from full withheld
            // to calculate the final withheld percantage correctly
            let withheldFromPaidPayments = 0;
            /*
            const highestFrequency = arr => (arr || []).sort((a,b) => arr.filter(v => v===a).length - arr.filter(v => v===b).length).pop(); 

            const getHighestFrequency = highestFrequency(globalPayments.filter(x => x.cf_txtType === "Contract").map(x => x.newperiod));        
            getRecordswithBadFrequency = globalPayments.filter(x => x.cf_txtType === "Contract" && x.newperiod !== getHighestFrequency) || [];      
            globalPayments = globalPayments.map(x => x.cf_txtType === "Contract" && x.newperiod !== getHighestFrequency ? {...x, "newperiod": getHighestFrequency} : x);
            */

            for (let i = 0; i < globalPayments.length; i++) {
                let record = globalPayments[i];
                let strEventID = record["recordid"];
                let strStatusID = record["sf_Status ID"];
                let monthCount = record["newperiod"];
                let numPaymentPeriod = record["cf_numPaymentPeriod"]
                let numPaymentAmount = Number(record["cf_numRequestedAmount"]);

                // if payment is already paid, then we need to adjust values in arrYearlyMonths and arrYearlyTotals arrays
                if (strStatusID == globalPaymentPaidStatusID) {
                    // console.log("here", monthCount, numPaymentPeriod, numPaymentAmount); ///myc
                    // reduce number of months that should be reduced
                    if (arrYearlyMonths[numPaymentPeriod - 1] < monthCount) {
                        let over = monthCount - arrYearlyMonths[numPaymentPeriod - 1];
                        arrYearlyMonths[numPaymentPeriod - 1] = 0;
                        arrYearlyMonths[numPaymentPeriod] -= over;

                        // we need to look through the disbursements to identify the amount that should be reduce from the next period total
                        for (let j = 0; j < globalDisbursements.length; j++) {
                            let objDisbursement = globalDisbursements[j];

                            // get only the disbursements of the current payment
                            if (objDisbursement["sf_Parent ID"] == strEventID) {
                                // get the disbursement date
                                let dteStartDate = new Date(objDisbursement["sf_Start Date"]);
                                // identify how many months passed from the beginning of the project
                                let numDisbursementMonth = monthDiff(globalAppStartDate, dteStartDate);
                                // if the disbursement falls under "next" year, then remove disbursement's amount from the next year total
                                if (Math.floor((numDisbursementMonth + 1) / 12) == numPaymentPeriod) {
                                    arrYearlyTotals[numPaymentPeriod] -= Number(objDisbursement["sf_Amount"]);
                                } else {
                                    arrYearlyTotals[numPaymentPeriod - 1] -= Number(objDisbursement["sf_Amount"]);
                                }
                            }
                        }

                        withheldFromPaidPayments += arrYearlyTotals[numPaymentPeriod - 1];
                        arrYearlyTotals[numPaymentPeriod - 1] = 0;
                    } else {
                        // console.log("here2", numPaymentPeriod, arrYearlyMonths[numPaymentPeriod - 1], monthCount, arrYearlyTotals[numPaymentPeriod - 1], numPaymentAmount); ///myc
                        arrYearlyMonths[numPaymentPeriod - 1] -= monthCount;
                        // arrYearlyTotals[numPaymentPeriod - 1] -= numPaymentAmount;
                    }
                } else {
                    // we can have Paid payment only at the beginning of the list cause all the payments are ordered by payment date
                    // so one we reach unpaid payment we can break the cycle
                    break;
                }
            }


            // recalculate total amount remained concidering paid payments
            let remainedAmount = arrYearlyTotals.reduce(function(acc, val) {
                return acc + val;
            }, 0);
            //console.log("finalWithhelpPercent", finalWithhelpPercent);
            // console.log("remainedAmount", remainedAmount);

            // recalculate final withheld percent concidering paid payments
            if (globalIsGlobalHealth) {
                // final percent might change if newWithheldTotal = 100000 now
                finalWithhelpPercent = (newWithheldTotal - withheldFromPaidPayments) / remainedAmount;
            } else {
                finalWithhelpPercent = ((newWithheldTotal * 2) - withheldFromPaidPayments) / remainedAmount;
            }

            // console.log("finalWithhelpPercent", newWithheldTotal, withheldFromPaidPayments, remainedAmount);

            let yearMonths = 0;
            let periodIndex = 0;
            let arrYearlyMonthsClone = JSON.parse(JSON.stringify(arrYearlyMonths)); // clone array to add back paid months for calculations below
            let numPaidPaymentTempAdjustment = 0; // Paid Payment Adjustment Amounts to the same period
            let numPaidPaymentAdjustment = 0; // Paid Payment Adjustment Amounts to the same period
            let numPaidPaymentPeriod = -1; // Check Period for Paid Payment Adjustments
            let numPaidPaymentCounter = 0;

            if (globalBoolRevisedDate) {
                arrYearlyTotals[0] -= globalPaidAmountTotal + globalRefundAmountTotal;
            }

            for (let i = 0; i < globalPayments.length; i++) {
                //console.log("periodIndex", periodIndex);
                let record = globalPayments[i];
                console.log("record2", record, arrYearlyMonthsClone);
                let strEventID = record["recordid"];
                let strType = record["cf_txtType"];
                let strStatusID = record["sf_Status ID"];
                let monthCount = record["newperiod"];
                let numPaymentPeriod = record["cf_numPaymentPeriod"]
                let numPaymentAmount = Number(record["cf_numRequestedAmount"]);
                let disbursements = [];

                // console.log("yearMonths", yearMonths, monthCount, yearMonths + monthCount);
                // we are still in the same period
                if (i + 1 < globalPayments.length && (numPaymentPeriod <= globalPayments[i + 1]["cf_numPaymentPeriod"] || numPaymentPeriod >= globalPayments[i + 1]["cf_numPaymentPeriod"])) {
                    //console.log("processPayment");
                    // find td where we are going to add an updated payment
                    let $tdNew = $("#tdNewPayment_" + strEventID);

                    // if payment is already paid, then just copy the old payment over
                    if (strStatusID == globalPaymentPaidStatusID) {
                        let numPaidPaymentPeriodTotal = 0;
                        let $td = $("#tdPayment_" + strEventID);
                        $tdNew.text($td.text());
                        $tdNew.data("amount", $td.data("amount"));

                        // console.log("before1", arrYearlyTotals[periodIndex], arrYearlyMonthsClone[periodIndex], monthCount);
                        arrYearlyMonthsClone[periodIndex] += monthCount;
                        numPaidPaymentCounter++;

                        // console.log("before2", arrYearlyTotals[periodIndex], arrYearlyMonthsClone[periodIndex], monthCount);
                        numPaidPaymentPeriod = numPaymentPeriod;
                        numPaidPaymentPeriodTotal = arrYearlyTotals[periodIndex] * (1 - finalWithhelpPercent) / 12 * monthCount;
                        numPaidPaymentTempAdjustment += (numPaidPaymentPeriodTotal - numPaymentAmount);

                        // console.log("numPaidPaymentPeriodTotal", numPaidPaymentPeriodTotal, numPaymentAmount, numPaidPaymentTempAdjustment);
                        numPaidPaymentAdjustment = numPaidPaymentTempAdjustment / (4 - numPaidPaymentCounter);

                        // console.log("after", numPaidPaymentPeriod, numPaymentPeriod, arrYearlyTotals[periodIndex], arrYearlyMonthsClone[periodIndex], monthCount, numPaidPaymentAdjustment, numPaidPaymentPeriodTotal, numPaymentAmount);
                    } else if (strType.indexOf("Withheld") < 0) {
                        // calculate new payment
                        let paymentTotal = 0;

                        // if yearMonths < 0 it means that there are some months left from the previous pay period
                        if (yearMonths + monthCount > 12) {
                            let prevYearMonth = 12 - yearMonths; // 2
                            let currentYearMonth = monthCount + yearMonths - 12; // 1
                            // get left months' amount from the previous pay period - we divide by 12
                            // having some months left from the previous period meens that that period was a full year
                            // let prevPeriod = arrYearlyTotals[periodIndex] * (1 - finalWithhelpPercent) * prevYearMonth / arrYearlyMonths[periodIndex];
                            // console.log("prevPeriod", prevYearMonth, arrYearlyTotals[periodIndex], arrYearlyMonthsClone[periodIndex]);
                            let prevPeriod = arrYearlyTotals[periodIndex] * (1 - finalWithhelpPercent) / arrYearlyMonthsClone[periodIndex] * prevYearMonth;
                            // add the amount from the current pay period
                            // let currentPeriod = arrYearlyTotals[periodIndex + 1] * (1 - finalWithhelpPercent) * currentYearMonth / arrYearlyMonths[periodIndex + 1];
                            // console.log("currentPeriod", currentYearMonth, arrYearlyTotals[periodIndex + 1], arrYearlyMonthsClone[periodIndex + 1]);
                            let currentPeriod = arrYearlyTotals[periodIndex + 1] * (1 - finalWithhelpPercent) / arrYearlyMonthsClone[periodIndex + 1] * currentYearMonth;

                            // console.log("monthCount1", prevYearMonth, prevPeriod, prevYearMonth, prevPeriod / prevYearMonth);
                            // console.log("monthCount2", prevYearMonth, currentPeriod, currentYearMonth, prevPeriod / prevYearMonth);
                            for (j = 0; j < monthCount; j++) {
                                if (j < prevYearMonth) {
                                    disbursements.push(prevPeriod / prevYearMonth);
                                } else {
                                    disbursements.push(currentPeriod / currentYearMonth);
                                }
                            }
                            paymentTotal = prevPeriod + currentPeriod;

                            console.log("paymentTotal1", paymentTotal, prevPeriod, currentPeriod);
                        } else {
                            // add the amount from the current pay period
                            // paymentTotal += arrYearlyTotals[periodIndex] * (1 - finalWithhelpPercent) * monthCount / arrYearlyMonths[periodIndex];
                            if (numPaymentPeriod === numPaidPaymentPeriod) {
                                paymentTotal += arrYearlyTotals[periodIndex] * (1 - finalWithhelpPercent) / arrYearlyMonthsClone[periodIndex] * monthCount;
                                paymentTotal += numPaidPaymentAdjustment;
                            } else {
                                paymentTotal += arrYearlyTotals[periodIndex] * (1 - finalWithhelpPercent) / arrYearlyMonthsClone[periodIndex] * monthCount;
                            }

                            console.log("paymentTotal2", paymentTotal, arrYearlyTotals[periodIndex], arrYearlyMonthsClone[periodIndex], monthCount);

                            for (j = 0; j < monthCount; j++) {
                                disbursements.push(paymentTotal / monthCount);
                            }
                        }
                        console.log("paymentTotal", paymentTotal, arrYearlyTotals[periodIndex], (1 - finalWithhelpPercent), 12, monthCount);
                        // set up new amount
                        $tdNew.text(formatAsCurrency(paymentTotal.toFixed(2), 2, 2));
                        $tdNew.data("amount", paymentTotal.toFixed(2));
                    }
                    yearMonths += monthCount;
                }

                // last payment of the period, next payment is next period
                if (i + 1 == globalPayments.length || numPaymentPeriod < globalPayments[i + 1]["cf_numPaymentPeriod"]) {
                    // switch to the next period
                    periodIndex++;
                    // we need to reset the months count when we switch the period, so that we know if we still have some months left from the previous period
                    yearMonths -= 12;
                }

                if (strType.indexOf("Withheld") >= 0) {
                    //console.log("newWithheldTotal", newWithheldTotal);
                    let $tdNew = $("#tdNewPayment_" + strEventID);
                    //let $td = $("#tdPayment_" + strEventID);
                    $tdNew.text(formatAsCurrency((newWithheldTotal), 2, 2));
                    $tdNew.data("amount", newWithheldTotal);
                    record["cf_numRequestedAmount"] = newWithheldTotal;
                    disbursements.push(newWithheldTotal);
                }
                globalPaymentsDisburesements.push(disbursements);
            }
        }

        // // we do not need to update new grand total here, I am doing this just to make sure that the totals are matching after reprofiling
        let newGrandTotal;

        if (globalBoolRevisedDate) {
            newGrandTotal = sumTDAttr("td[data-type='newpayment'],td[data-type='newwithheld'],td[data-type='newpaid'],td[data-type='newrefund']", false, "amount", null);
        } else {
            newGrandTotal = sumTDAttr("td[data-type='newpayment'],td[data-type='newwithheld']", false, "amount", null);
        }
        // console.log("grand", grandTotal, newGrandTotal, grandTotal - newGrandTotal);

        globalDisbursementAdjustment = null;
        // grand adjustment
        if (grandTotal != newGrandTotal) {
            let adjustment = grandTotal - newGrandTotal;
            let $td = $("td:not(.nochange)[data-type='newpayment']:last");
            let val = Number($td.data("amount"));
            // console.log("adjustment", adjustment, val);
            let numLastPaymentIndex = globalIsGlobalHealth ? globalPayments.length - 2 : globalPayments.length - 3;

            // adjustments for last disbursement for reprofiling disbursement
            globalDisbursementAdjustment = adjustment;
            val = val + adjustment; // Math.ceil(adjustment*100)/100;

            globalPayments[numLastPaymentIndex]["cf_numRequestedAmount"] = Number(val);

            $td.html(formatAsCurrency(val, 2, 2));
            $td.data("amount", val);

            let numLastDisburesement = globalIsGlobalHealth ? globalPaymentsDisburesements.length - 2 : globalPaymentsDisburesements.length - 3;
            let objDisburesements = globalPaymentsDisburesements[numLastDisburesement];
            let numLastDisburesementsLength = objDisburesements ? objDisburesements.length : 0;

            if (numLastDisburesementsLength && numLastDisburesementsLength > 0) {
                let numDisbAmount = val / numLastDisburesementsLength;

                for (let x = 0; x < numLastDisburesementsLength; x++) {
                    objDisburesements[x] = numDisbAmount
                }
            }
        }

        /*
        globalPayments.forEach((list1) => {
            getRecordswithBadFrequency.forEach((list2) => {
                if (list2.recordid === list1.recordid) {
                    list1.newperiod = list2.newperiod;
                }
            })
        })
        */

        newGrandTotal = grandTotal;
        $tdNewGrandTotal.html(formatAsCurrency(newGrandTotal, 2, 2));
        $tdNewGrandTotal.data("amount", newGrandTotal);
        // $tdNewFinalTotal.html(formatAsCurrency(newGrandTotal - globalRefundAmountTotal, 2, 2));
        $tdNewFinalTotal.html(formatAsCurrency(newGrandTotal, 2, 2));

        if (globalPrevAppStartDate.getTime() !== globalAppStartDate.getTime()) {
            // project start date changed: we will create the full disbursements map only when we the start date changed and we need to recalculate everything
            reprofileDisbursements(finalWithhelpPercent, newWithheldTotal);
        }

        // console.log("reprofileSchedule disb", globalPaymentsDisburesements);
        $btnSave.prop("disabled", false);
    } catch (ex) {
        alert("Error in reprofileSchedule(): " + ex.toString());
        //disableAll();
        return false;
    }
}

There is a Reprofile Payments button on the page. When clicked, it calls reprofileSchedule() and it is supposed to divide yearly totals into months in every year and display amounts. Right now the Updated Amounts for the rows where “Payment Status” is New are wrong.

Hi @asifakhtar,

Can you please format your code using the </> button in the text editor menu. I have done it for you this time.

First thought looking at post#5, is that you have a function which is 405 lines long including commented out blocks of code — No wonder you are struggling!

Time is a consideration, obviously, but in my opinion this code needs refactoring. It needs breaking down into a smaller and where possible generic reusable and testable functions. Code that you can actually fathom out what is going on.

2 Likes

Sure I will format my code next time.

1 Like