Merge pull request #17387 from eileenmcnaughton/ct
[civicrm-core.git] / CRM / Pledge / Form / Payment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
bc77d7c0
TO
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 |
6a488035 9 +--------------------------------------------------------------------+
d25dd0ee 10 */
6a488035
TO
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
6a488035
TO
16 */
17
18/**
cc28438b 19 * This class generates form components for processing a pledge payment.
6a488035
TO
20 */
21class CRM_Pledge_Form_Payment extends CRM_Core_Form {
22
23 /**
fe482240 24 * The id of the pledge payment that we are proceessing.
6a488035
TO
25 *
26 * @var int
6a488035
TO
27 */
28 public $_id;
29
267fb11a
SL
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
6a488035 44 /**
fe482240 45 * Set variables up before form is built.
beb414cc 46 *
47 * @throws \CRM_Core_Exception
6a488035
TO
48 */
49 public function preProcess() {
50 // check for edit permission
51 if (!CRM_Core_Permission::check('edit pledges')) {
beb414cc 52 CRM_Core_Error::statusBounce(ts('You do not have permission to access this page.'));
6a488035
TO
53 }
54
55 $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this);
e2046b33
CW
56
57 CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment'));
6a488035
TO
58 }
59
60 /**
c490a46a 61 * Set default values for the form.
cc28438b 62 * the default values are retrieved from the database.
6a488035 63 */
00be9182 64 public function setDefaultValues() {
be2fb01f 65 $defaults = [];
6a488035
TO
66 if ($this->_id) {
67 $params['id'] = $this->_id;
68 CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults);
6a488035
TO
69 if (isset($defaults['contribution_id'])) {
70 $this->assign('pledgePayment', TRUE);
71 }
ab6ba136 72 $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
6a488035
TO
73 $this->assign('status', $status);
74 }
75 $defaults['option_type'] = 1;
76 return $defaults;
77 }
78
79 /**
fe482240 80 * Build the form object.
6a488035
TO
81 */
82 public function buildQuickForm() {
cc28438b 83 // add various dates
267fb11a 84 $this->addField('scheduled_date', [], TRUE, FALSE);
6a488035
TO
85
86 $this->addMoney('scheduled_amount',
87 ts('Scheduled Amount'), TRUE,
be2fb01f 88 ['readonly' => TRUE],
6a488035
TO
89 TRUE,
90 'currency',
b467b98d
CW
91 NULL,
92 TRUE
6a488035
TO
93 );
94
be2fb01f 95 $optionTypes = [
353ffa53 96 '1' => ts('Adjust Pledge Payment Schedule?'),
6a488035 97 '2' => ts('Adjust Total Pledge Amount?'),
be2fb01f 98 ];
6a488035
TO
99 $element = $this->addRadio('option_type',
100 NULL,
101 $optionTypes,
be2fb01f 102 [], '<br/>'
6a488035
TO
103 );
104
be2fb01f
CW
105 $this->addButtons([
106 [
6a488035
TO
107 'type' => 'next',
108 'name' => ts('Save'),
109 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
6a488035 110 'isDefault' => TRUE,
be2fb01f
CW
111 ],
112 [
6a488035
TO
113 'type' => 'cancel',
114 'name' => ts('Cancel'),
be2fb01f 115 ],
c86d4e7c 116 ]);
6a488035
TO
117 }
118
119 /**
fe482240 120 * Process the form submission.
6a488035
TO
121 */
122 public function postProcess() {
6a488035 123 $formValues = $this->controller->exportValues($this->_name);
267fb11a
SL
124 $params = [
125 'id' => $this->_id,
126 'scheduled_date' => $formValues['scheduled_date'],
127 'currency' => $formValues['currency'],
128 ];
6a488035 129
267fb11a 130 if (CRM_Utils_Date::overdue($params['scheduled_date'])) {
ab6ba136 131 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
6a488035
TO
132 }
133 else {
ab6ba136 134 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
6a488035
TO
135 }
136
6a488035
TO
137 $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
138
139 CRM_Pledge_BAO_PledgePayment::add($params);
267fb11a 140 $adjustTotalAmount = (CRM_Utils_Array::value('option_type', $formValues) == 2);
6a488035 141
6a488035
TO
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 }
cc28438b 155 // update pledge status
6a488035 156 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
be2fb01f 157 [$params['id']],
6a488035
TO
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 }
96025800 167
6a488035 168}