Merge pull request #14383 from civicrm/5.14
[civicrm-core.git] / CRM / Pledge / Form / Payment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
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. |
13 | |
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. |
18 | |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * This class generates form components for processing a pledge payment.
36 */
37 class CRM_Pledge_Form_Payment extends CRM_Core_Form {
38
39 /**
40 * The id of the pledge payment that we are proceessing.
41 *
42 * @var int
43 */
44 public $_id;
45
46 /**
47 * Explicitly declare the entity api name.
48 */
49 public function getDefaultEntity() {
50 return 'PledgePayment';
51 }
52
53 /**
54 * Explicitly declare the form context.
55 */
56 public function getDefaultContext() {
57 return 'create';
58 }
59
60 /**
61 * Set variables up before form is built.
62 */
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.'));
67 }
68
69 $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this);
70
71 CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment'));
72 }
73
74 /**
75 * Set default values for the form.
76 * the default values are retrieved from the database.
77 */
78 public function setDefaultValues() {
79 $defaults = [];
80 if ($this->_id) {
81 $params['id'] = $this->_id;
82 CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults);
83 if (isset($defaults['contribution_id'])) {
84 $this->assign('pledgePayment', TRUE);
85 }
86 $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
87 $this->assign('status', $status);
88 }
89 $defaults['option_type'] = 1;
90 return $defaults;
91 }
92
93 /**
94 * Build the form object.
95 */
96 public function buildQuickForm() {
97 // add various dates
98 $this->addField('scheduled_date', [], TRUE, FALSE);
99
100 $this->addMoney('scheduled_amount',
101 ts('Scheduled Amount'), TRUE,
102 ['readonly' => TRUE],
103 TRUE,
104 'currency',
105 NULL,
106 TRUE
107 );
108
109 $optionTypes = [
110 '1' => ts('Adjust Pledge Payment Schedule?'),
111 '2' => ts('Adjust Total Pledge Amount?'),
112 ];
113 $element = $this->addRadio('option_type',
114 NULL,
115 $optionTypes,
116 [], '<br/>'
117 );
118
119 $this->addButtons([
120 [
121 'type' => 'next',
122 'name' => ts('Save'),
123 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
124 'isDefault' => TRUE,
125 ],
126 [
127 'type' => 'cancel',
128 'name' => ts('Cancel'),
129 ],
130 ]);
131 }
132
133 /**
134 * Process the form submission.
135 */
136 public function postProcess() {
137 $formValues = $this->controller->exportValues($this->_name);
138 $params = [
139 'id' => $this->_id,
140 'scheduled_date' => $formValues['scheduled_date'],
141 'currency' => $formValues['currency'],
142 ];
143
144 if (CRM_Utils_Date::overdue($params['scheduled_date'])) {
145 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
146 }
147 else {
148 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
149 }
150
151 $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
152
153 CRM_Pledge_BAO_PledgePayment::add($params);
154 $adjustTotalAmount = (CRM_Utils_Array::value('option_type', $formValues) == 2);
155
156 $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
157 $params['id'],
158 'scheduled_amount',
159 'id'
160 );
161
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);
165 }
166 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
167 $adjustTotalAmount = TRUE;
168 }
169 // update pledge status
170 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
171 [$params['id']],
172 $params['status_id'],
173 NULL,
174 $formValues['scheduled_amount'],
175 $adjustTotalAmount
176 );
177
178 $statusMsg = ts('Pledge Payment Schedule has been updated.');
179 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
180 }
181
182 }