Commit | Line | Data |
---|---|---|
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 | */ |
21 | class 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. |
6a488035 TO |
46 | */ |
47 | public function preProcess() { | |
48 | // check for edit permission | |
49 | if (!CRM_Core_Permission::check('edit pledges')) { | |
0499b0ad | 50 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
51 | } |
52 | ||
53 | $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this); | |
e2046b33 CW |
54 | |
55 | CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment')); | |
6a488035 TO |
56 | } |
57 | ||
58 | /** | |
c490a46a | 59 | * Set default values for the form. |
cc28438b | 60 | * the default values are retrieved from the database. |
6a488035 | 61 | */ |
00be9182 | 62 | public function setDefaultValues() { |
be2fb01f | 63 | $defaults = []; |
6a488035 TO |
64 | if ($this->_id) { |
65 | $params['id'] = $this->_id; | |
66 | CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
6a488035 TO |
67 | if (isset($defaults['contribution_id'])) { |
68 | $this->assign('pledgePayment', TRUE); | |
69 | } | |
ab6ba136 | 70 | $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']); |
6a488035 TO |
71 | $this->assign('status', $status); |
72 | } | |
73 | $defaults['option_type'] = 1; | |
74 | return $defaults; | |
75 | } | |
76 | ||
77 | /** | |
fe482240 | 78 | * Build the form object. |
6a488035 TO |
79 | */ |
80 | public function buildQuickForm() { | |
cc28438b | 81 | // add various dates |
267fb11a | 82 | $this->addField('scheduled_date', [], TRUE, FALSE); |
6a488035 TO |
83 | |
84 | $this->addMoney('scheduled_amount', | |
85 | ts('Scheduled Amount'), TRUE, | |
be2fb01f | 86 | ['readonly' => TRUE], |
6a488035 TO |
87 | TRUE, |
88 | 'currency', | |
b467b98d CW |
89 | NULL, |
90 | TRUE | |
6a488035 TO |
91 | ); |
92 | ||
be2fb01f | 93 | $optionTypes = [ |
353ffa53 | 94 | '1' => ts('Adjust Pledge Payment Schedule?'), |
6a488035 | 95 | '2' => ts('Adjust Total Pledge Amount?'), |
be2fb01f | 96 | ]; |
6a488035 TO |
97 | $element = $this->addRadio('option_type', |
98 | NULL, | |
99 | $optionTypes, | |
be2fb01f | 100 | [], '<br/>' |
6a488035 TO |
101 | ); |
102 | ||
be2fb01f CW |
103 | $this->addButtons([ |
104 | [ | |
6a488035 TO |
105 | 'type' => 'next', |
106 | 'name' => ts('Save'), | |
107 | 'spacing' => ' ', | |
6a488035 | 108 | 'isDefault' => TRUE, |
be2fb01f CW |
109 | ], |
110 | [ | |
6a488035 TO |
111 | 'type' => 'cancel', |
112 | 'name' => ts('Cancel'), | |
be2fb01f | 113 | ], |
c86d4e7c | 114 | ]); |
6a488035 TO |
115 | } |
116 | ||
117 | /** | |
fe482240 | 118 | * Process the form submission. |
6a488035 TO |
119 | */ |
120 | public function postProcess() { | |
6a488035 | 121 | $formValues = $this->controller->exportValues($this->_name); |
267fb11a SL |
122 | $params = [ |
123 | 'id' => $this->_id, | |
124 | 'scheduled_date' => $formValues['scheduled_date'], | |
125 | 'currency' => $formValues['currency'], | |
126 | ]; | |
6a488035 | 127 | |
267fb11a | 128 | if (CRM_Utils_Date::overdue($params['scheduled_date'])) { |
ab6ba136 | 129 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue'); |
6a488035 TO |
130 | } |
131 | else { | |
ab6ba136 | 132 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending'); |
6a488035 TO |
133 | } |
134 | ||
6a488035 TO |
135 | $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id'); |
136 | ||
137 | CRM_Pledge_BAO_PledgePayment::add($params); | |
267fb11a | 138 | $adjustTotalAmount = (CRM_Utils_Array::value('option_type', $formValues) == 2); |
6a488035 | 139 | |
6a488035 TO |
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 | } | |
cc28438b | 153 | // update pledge status |
6a488035 | 154 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, |
be2fb01f | 155 | [$params['id']], |
6a488035 TO |
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 | } | |
96025800 | 165 | |
6a488035 | 166 | } |