3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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-2017
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 * Set variables up before form is built.
49 public function preProcess() {
50 // check for edit permission
51 if (!CRM_Core_Permission
::check('edit pledges')) {
52 CRM_Core_Error
::fatal(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 list($defaults['scheduled_date']) = CRM_Utils_Date
::setDateDefaults($defaults['scheduled_date']);
70 if (isset($defaults['contribution_id'])) {
71 $this->assign('pledgePayment', TRUE);
73 $status = CRM_Core_PseudoConstant
::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
74 $this->assign('status', $status);
76 $defaults['option_type'] = 1;
81 * Build the form object.
83 public function buildQuickForm() {
85 $this->addDate('scheduled_date', ts('Scheduled Date'), TRUE);
87 $this->addMoney('scheduled_amount',
88 ts('Scheduled Amount'), TRUE,
89 array('readonly' => TRUE),
97 '1' => ts('Adjust Pledge Payment Schedule?'),
98 '2' => ts('Adjust Total Pledge Amount?'),
100 $element = $this->addRadio('option_type',
106 $this->addButtons(array(
109 'name' => ts('Save'),
110 'spacing' => ' ',
115 'name' => ts('Cancel'),
122 * Process the form submission.
124 public function postProcess() {
125 // get the submitted form values.
126 $formValues = $this->controller
->exportValues($this->_name
);
128 $formValues['scheduled_date'] = CRM_Utils_Date
::processDate($formValues['scheduled_date']);
129 $params['scheduled_date'] = CRM_Utils_Date
::format($formValues['scheduled_date']);
130 $params['currency'] = CRM_Utils_Array
::value('currency', $formValues);
133 if (CRM_Utils_Date
::overdue(CRM_Utils_Date
::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) {
134 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
137 $params['status_id'] = CRM_Core_PseudoConstant
::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
140 $params['id'] = $this->_id
;
141 $pledgeId = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
143 CRM_Pledge_BAO_PledgePayment
::add($params);
144 $adjustTotalAmount = FALSE;
145 if (CRM_Utils_Array
::value('option_type', $formValues) == 2) {
146 $adjustTotalAmount = TRUE;
149 $pledgeScheduledAmount = CRM_Core_DAO
::getFieldValue('CRM_Pledge_DAO_PledgePayment',
155 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId, 2);
156 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
157 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment
::getOldestPledgePayment($pledgeId);
159 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
160 $adjustTotalAmount = TRUE;
162 // update pledge status
163 CRM_Pledge_BAO_PledgePayment
::updatePledgePaymentStatus($pledgeId,
164 array($params['id']),
165 $params['status_id'],
167 $formValues['scheduled_amount'],
171 $statusMsg = ts('Pledge Payment Schedule has been updated.');
172 CRM_Core_Session
::setStatus($statusMsg, ts('Saved'), 'success');