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