When partial payments are made, the interest that has accrued to the time of payment, if any, shall be first paid, and the residue of such partial payment shall be placed to the payment of the principal, except that the parties may agree in writing that such partial payment, or any portion thereof, shall be applied first to the payment of principal, in which case the residue shall be applied to the payment of interest that has accrued to the time of payment.