3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * This class generates form components for processing a pledge payment.
21 class CRM_Pledge_Form_Payment
extends CRM_Core_Form
{
24 * The id of the pledge payment that we are proceessing.
31 * Explicitly declare the entity api name.
33 public function getDefaultEntity() {
34 return 'PledgePayment';
38 * Explicitly declare the form context.
40 public function getDefaultContext() {
45 * Set variables up before form is built.
47 * @throws \CRM_Core_Exception
49 public function preProcess() {
50 // check for edit permission
51 if (!CRM_Core_Permission
::check('edit pledges')) {
52 CRM_Core_Error
::statusBounce(ts('You do not have permission to access this page.'));
55 $this->_id
= CRM_Utils_Request
::retrieve('ppId', 'Positive', $this);
57 CRM_Utils_System
::setTitle(ts('Edit Scheduled Pledge Payment'));
61 * Set default values for the form.
62 * the default values are retrieved from the database.
64 public function setDefaultValues() {
67 $params['id'] = $this->_id
;
68 CRM_Pledge_BAO_PledgePayment
::retrieve($params, $defaults);
69 if (isset($defaults['contribution_id'])) {
70 $this->assign('pledgePayment', TRUE);
72 $status = CRM_Core_PseudoConstant
::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
73 $this->assign('status', $status);
75 $defaults['option_type'] = 1;
80 * Build the form object.
82 public function buildQuickForm() {
84 $this->addField('scheduled_date', [], TRUE, FALSE);
86 $this->addMoney('scheduled_amount',
87 ts('Scheduled Amount'), TRUE,
96 '1' => ts('Adjust Pledge Payment Schedule?'),
97 '2' => ts('Adjust Total Pledge Amount?'),
99 $element = $this->addRadio('option_type',
108 'name' => ts('Save'),
109 'spacing' => ' ',
114 'name' => ts('Cancel'),
120 * Process the form submission.
122 public function postProcess() {
123 $formValues = $this->controller
->exportValues($this->_name
);
126 'scheduled_date' => $formValues['scheduled_date'],
127 'currency' => $formValues['currency'],
130 if (CRM_Utils_Date
::overdue($params['scheduled_date'])) {
131 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
134 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
137 $pledgeId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
139 CRM_Pledge_BAO_PledgePayment
::add($params);
140 $adjustTotalAmount = (CRM_Utils_Array
::value('option_type', $formValues) == 2);
142 $pledgeScheduledAmount = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
148 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId, 2);
149 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
150 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
152 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
153 $adjustTotalAmount = TRUE;
155 // update pledge status
156 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId,
158 $params['status_id'],
160 $formValues['scheduled_amount'],
164 $statusMsg = ts('Pledge Payment Schedule has been updated.');
165 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');