Merge pull request #15982 from civicrm/5.20
[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 public function preProcess() {
48 // check for edit permission
49 if (!CRM_Core_Permission::check('edit pledges')) {
50 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
51 }
52
53 $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this);
54
55 CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment'));
56 }
57
58 /**
59 * Set default values for the form.
60 * the default values are retrieved from the database.
61 */
62 public function setDefaultValues() {
63 $defaults = [];
64 if ($this->_id) {
65 $params['id'] = $this->_id;
66 CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults);
67 if (isset($defaults['contribution_id'])) {
68 $this->assign('pledgePayment', TRUE);
69 }
70 $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
71 $this->assign('status', $status);
72 }
73 $defaults['option_type'] = 1;
74 return $defaults;
75 }
76
77 /**
78 * Build the form object.
79 */
80 public function buildQuickForm() {
81 // add various dates
82 $this->addField('scheduled_date', [], TRUE, FALSE);
83
84 $this->addMoney('scheduled_amount',
85 ts('Scheduled Amount'), TRUE,
86 ['readonly' => TRUE],
87 TRUE,
88 'currency',
89 NULL,
90 TRUE
91 );
92
93 $optionTypes = [
94 '1' => ts('Adjust Pledge Payment Schedule?'),
95 '2' => ts('Adjust Total Pledge Amount?'),
96 ];
97 $element = $this->addRadio('option_type',
98 NULL,
99 $optionTypes,
100 [], '<br/>'
101 );
102
103 $this->addButtons([
104 [
105 'type' => 'next',
106 'name' => ts('Save'),
107 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
108 'isDefault' => TRUE,
109 ],
110 [
111 'type' => 'cancel',
112 'name' => ts('Cancel'),
113 ],
114 ]);
115 }
116
117 /**
118 * Process the form submission.
119 */
120 public function postProcess() {
121 $formValues = $this->controller->exportValues($this->_name);
122 $params = [
123 'id' => $this->_id,
124 'scheduled_date' => $formValues['scheduled_date'],
125 'currency' => $formValues['currency'],
126 ];
127
128 if (CRM_Utils_Date::overdue($params['scheduled_date'])) {
129 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
130 }
131 else {
132 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
133 }
134
135 $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
136
137 CRM_Pledge_BAO_PledgePayment::add($params);
138 $adjustTotalAmount = (CRM_Utils_Array::value('option_type', $formValues) == 2);
139
140 $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
141 $params['id'],
142 'scheduled_amount',
143 'id'
144 );
145
146 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2);
147 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
148 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
149 }
150 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
151 $adjustTotalAmount = TRUE;
152 }
153 // update pledge status
154 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
155 [$params['id']],
156 $params['status_id'],
157 NULL,
158 $formValues['scheduled_amount'],
159 $adjustTotalAmount
160 );
161
162 $statusMsg = ts('Pledge Payment Schedule has been updated.');
163 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
164 }
165
166 }