What is the formula that considers periodic (week/fortnightly/monthly) repayments

I am writing some code that can take in variables such as loan amount,payment freq (weekly, fortnightly, monthly),length of mortgage in years,interest amount to forecast how much I would pay at the the end of every year and to calculate my weekly/fortnightly/monthly repayments, total interest paid and the total paid to the bank. I've managed to get this to work fairly precisely (I'm off by a cent a year).

Question:

I want to integrate extra repayments to my code to see how much the mortgage will be shortened by and create a list output something like this:

Q What is the formula that considers periodic (week/fortnightly/monthly) repayments