3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * This class generates form components for processing a pledge payment.
37 class CRM_Pledge_Form_Payment
extends CRM_Core_Form
{
40 * The id of the pledge payment that we are proceessing.
47 * Explicitly declare the entity api name.
49 public function getDefaultEntity() {
50 return 'PledgePayment';
54 * Explicitly declare the form context.
56 public function getDefaultContext() {
61 * Set variables up before form is built.
63 public function preProcess() {
64 // check for edit permission
65 if (!CRM_Core_Permission
::check('edit pledges')) {
66 CRM_Core_Error
::fatal(ts('You do not have permission to access this page.'));
69 $this->_id
= CRM_Utils_Request
::retrieve('ppId', 'Positive', $this);
71 CRM_Utils_System
::setTitle(ts('Edit Scheduled Pledge Payment'));
75 * Set default values for the form.
76 * the default values are retrieved from the database.
78 public function setDefaultValues() {
81 $params['id'] = $this->_id
;
82 CRM_Pledge_BAO_PledgePayment
::retrieve($params, $defaults);
83 if (isset($defaults['contribution_id'])) {
84 $this->assign('pledgePayment', TRUE);
86 $status = CRM_Core_PseudoConstant
::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
87 $this->assign('status', $status);
89 $defaults['option_type'] = 1;
94 * Build the form object.
96 public function buildQuickForm() {
98 $this->addField('scheduled_date', [], TRUE, FALSE);
100 $this->addMoney('scheduled_amount',
101 ts('Scheduled Amount'), TRUE,
102 ['readonly' => TRUE],
110 '1' => ts('Adjust Pledge Payment Schedule?'),
111 '2' => ts('Adjust Total Pledge Amount?'),
113 $element = $this->addRadio('option_type',
122 'name' => ts('Save'),
123 'spacing' => ' ',
128 'name' => ts('Cancel'),
134 * Process the form submission.
136 public function postProcess() {
137 $formValues = $this->controller
->exportValues($this->_name
);
140 'scheduled_date' => $formValues['scheduled_date'],
141 'currency' => $formValues['currency'],
144 if (CRM_Utils_Date
::overdue($params['scheduled_date'])) {
145 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
148 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
151 $pledgeId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
153 CRM_Pledge_BAO_PledgePayment
::add($params);
154 $adjustTotalAmount = (CRM_Utils_Array
::value('option_type', $formValues) == 2);
156 $pledgeScheduledAmount = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
162 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId, 2);
163 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
164 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
166 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
167 $adjustTotalAmount = TRUE;
169 // update pledge status
170 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId,
172 $params['status_id'],
174 $formValues['scheduled_amount'],
178 $statusMsg = ts('Pledge Payment Schedule has been updated.');
179 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');