Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
fee14197 | 4 | | CiviCRM version 5 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
6a488035 TO |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
6a488035 TO |
27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
6b83d5bd | 31 | * @copyright CiviCRM LLC (c) 2004-2019 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
cc28438b | 35 | * This class generates form components for processing a pledge payment. |
6a488035 TO |
36 | */ |
37 | class CRM_Pledge_Form_Payment extends CRM_Core_Form { | |
38 | ||
39 | /** | |
fe482240 | 40 | * The id of the pledge payment that we are proceessing. |
6a488035 TO |
41 | * |
42 | * @var int | |
6a488035 TO |
43 | */ |
44 | public $_id; | |
45 | ||
267fb11a SL |
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 | ||
6a488035 | 60 | /** |
fe482240 | 61 | * Set variables up before form is built. |
6a488035 TO |
62 | */ |
63 | public function preProcess() { | |
64 | // check for edit permission | |
65 | if (!CRM_Core_Permission::check('edit pledges')) { | |
0499b0ad | 66 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
67 | } |
68 | ||
69 | $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this); | |
e2046b33 CW |
70 | |
71 | CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment')); | |
6a488035 TO |
72 | } |
73 | ||
74 | /** | |
c490a46a | 75 | * Set default values for the form. |
cc28438b | 76 | * the default values are retrieved from the database. |
6a488035 | 77 | */ |
00be9182 | 78 | public function setDefaultValues() { |
6a488035 TO |
79 | $defaults = array(); |
80 | if ($this->_id) { | |
81 | $params['id'] = $this->_id; | |
82 | CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
6a488035 TO |
83 | if (isset($defaults['contribution_id'])) { |
84 | $this->assign('pledgePayment', TRUE); | |
85 | } | |
ab6ba136 | 86 | $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']); |
6a488035 TO |
87 | $this->assign('status', $status); |
88 | } | |
89 | $defaults['option_type'] = 1; | |
90 | return $defaults; | |
91 | } | |
92 | ||
93 | /** | |
fe482240 | 94 | * Build the form object. |
6a488035 TO |
95 | */ |
96 | public function buildQuickForm() { | |
cc28438b | 97 | // add various dates |
267fb11a | 98 | $this->addField('scheduled_date', [], TRUE, FALSE); |
6a488035 TO |
99 | |
100 | $this->addMoney('scheduled_amount', | |
101 | ts('Scheduled Amount'), TRUE, | |
b467b98d | 102 | array('readonly' => TRUE), |
6a488035 TO |
103 | TRUE, |
104 | 'currency', | |
b467b98d CW |
105 | NULL, |
106 | TRUE | |
6a488035 TO |
107 | ); |
108 | ||
098201d8 | 109 | $optionTypes = array( |
353ffa53 | 110 | '1' => ts('Adjust Pledge Payment Schedule?'), |
6a488035 TO |
111 | '2' => ts('Adjust Total Pledge Amount?'), |
112 | ); | |
113 | $element = $this->addRadio('option_type', | |
114 | NULL, | |
115 | $optionTypes, | |
098201d8 | 116 | array(), '<br/>' |
6a488035 TO |
117 | ); |
118 | ||
119 | $this->addButtons(array( | |
120 | array( | |
121 | 'type' => 'next', | |
122 | 'name' => ts('Save'), | |
123 | 'spacing' => ' ', | |
6a488035 TO |
124 | 'isDefault' => TRUE, |
125 | ), | |
126 | array( | |
127 | 'type' => 'cancel', | |
128 | 'name' => ts('Cancel'), | |
129 | ), | |
130 | ) | |
131 | ); | |
132 | } | |
133 | ||
134 | /** | |
fe482240 | 135 | * Process the form submission. |
6a488035 TO |
136 | */ |
137 | public function postProcess() { | |
6a488035 | 138 | $formValues = $this->controller->exportValues($this->_name); |
267fb11a SL |
139 | $params = [ |
140 | 'id' => $this->_id, | |
141 | 'scheduled_date' => $formValues['scheduled_date'], | |
142 | 'currency' => $formValues['currency'], | |
143 | ]; | |
6a488035 | 144 | |
267fb11a | 145 | if (CRM_Utils_Date::overdue($params['scheduled_date'])) { |
ab6ba136 | 146 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue'); |
6a488035 TO |
147 | } |
148 | else { | |
ab6ba136 | 149 | $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending'); |
6a488035 TO |
150 | } |
151 | ||
6a488035 TO |
152 | $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id'); |
153 | ||
154 | CRM_Pledge_BAO_PledgePayment::add($params); | |
267fb11a | 155 | $adjustTotalAmount = (CRM_Utils_Array::value('option_type', $formValues) == 2); |
6a488035 | 156 | |
6a488035 TO |
157 | $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
158 | $params['id'], | |
159 | 'scheduled_amount', | |
160 | 'id' | |
161 | ); | |
162 | ||
163 | $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2); | |
164 | if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) { | |
165 | $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); | |
166 | } | |
167 | if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) { | |
168 | $adjustTotalAmount = TRUE; | |
169 | } | |
cc28438b | 170 | // update pledge status |
6a488035 TO |
171 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, |
172 | array($params['id']), | |
173 | $params['status_id'], | |
174 | NULL, | |
175 | $formValues['scheduled_amount'], | |
176 | $adjustTotalAmount | |
177 | ); | |
178 | ||
179 | $statusMsg = ts('Pledge Payment Schedule has been updated.'); | |
180 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
181 | } | |
96025800 | 182 | |
6a488035 | 183 | } |