compucorp / uk.co.compucorp.membershipextras

Membership Extras for CiviCRM
Other
5 stars 8 forks source link

CIWEMB-264: Allow adding membership line item when there are no pending instalments left in the payment plan #470

Closed omarabuhussein closed 1 year ago

omarabuhussein commented 1 year ago

Overview

Making the manage instalments screen more flexible, where the user can now add a membership line item, even when all the instalments within the payment plan are completed (paid).

Before

Currently, the ability to add new membership line item (or even a normal line item) is not supported in Membershipextras when all the the instalments (contribution) within the payment plan are completed (paid), and an error toast message is shown when the user tries to do so.

(the error toast message appears in the background currently, but that is a separate and known UI problem)

2023-05-31 17_12_27-vvsdds vsdsdv _ compuclient22sspv3

After

Instead of showing the error toast message mentioned in the "before" section above, we now present two options to the user, the first is to create the membership without charging the user for it in the current period:

1111111

With this option, the following happen:

1- New Membership with the selected type and dates will be created 2- New membership subscription line item will be created on the payment plan, which the user can see inside the manage instalments screen, the amount of that line item is 0, and will automatically renew with the rest of the payment plan (unless the user ticks the auto renew checkbox off when adding the line item). though for the autorenewal to pick the price of the membership correctly at autorenewal , the Membershipextras "Use latest price when auto renew membership" setting should be enabled:

2023-05-31 17_26_41-Payment Plan Settings _ compuclient22sspv3

The second option is to create one off payment:

ezgif-5-ff50eb25c6

With this option, the user will be present with additional fields to control certain values on the payment to be created:

image

Given the contribution is linked to a membership, CiviCRM will by default uses "Memberships - Receipt (on-line)" mailing template

image

When the user submit the form, the following will happen:

1- New Membership with the selected type and dates will be created 2- New membership subscription line item will be created on the payment plan, which the user can see inside the manage instalments screen, the amount of that line item is Amount exc Tax and tax amount for the line item will be (Amount inc tax - Amount exc tax), and it will automatically renew with the rest of the payment plan (unless the user ticks the auto renew checkbox off when adding the line item). 3- New pending Contribution, the contribution total amount will = ((Amount inc tax), and the 'receive_date' will = the Scheduled Charge Date, and it will be linked to the membership (through the MembershipPayment entity) and to the payment plan (through the contribution_recur_id field on the contribution).

Technical Details

The work in this PR adjusts the code inside js/CurrentPeriodLineItemHandler.js, so instead of showing a validation error toast message when adding a line item to a completed payment plan, it now uses CRM.loadForm to either load the form at the following path:

civicrm/recurring-contribution/add-noinstalments-membership-lineitem

in case of adding membership line item

or the one at this path:

civicrm/recurring-contribution/add-noinstalments-donation-lineitem

in case of adding non membership line item.

The rest of the PR implements the form that the user sees when visiting the first path (the one related to adding membership line item), where the 2nd path will be implemented in another PR.

The form itself is a standard CiviCRM quickform, where the template file of the form contains supporting JS code that is mostly related to updating the "Amount inc tax" field whenever the "Amount exc tax" or the financial type change, and for that calculation new API end point was introduced which is ContributionRecurLineItem.calculatetaxamount, that takes the amount and the financial type, and calculated the total amount including tax using such information.

When it comes to the form itself, it operates in two modes, one is when paying without payment PAYMENT_TYPE_NO_PAYMENT and the other is when making a one off payment PAYMENT_TYPE_ONE_OFF_PAYMENT, both modes are very similar where the main differences are:

1- In "No Payment" mode the subscription line item amount will be 0 where in "One off payment" mode it will contain the submitted value. 2- In "No Payment" no contribution will be created, where in "One off Payment" a contribution will be created, that contribution will also be linked to the membership through the MembershipPayment entity, and to the recurring contribution through the contribution contribution_recur_id field. 3- In "On off payment" an email will be sent if the user select "send confirmation email", this was implemented using the core Contribution.sendconfirmation API.

Here is a diagram that shows the entities (in green) that will get created after submitting the form with "No Payment" option:

2023-05-31 19_52_19-_ Unsaved document - Pencil

And this with "One off payment option":

2023-05-31 19_51_20-_ Unsaved document - Pencil