Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
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 | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * This class generates form components for processing a pledge payment | |
38 | * | |
39 | */ | |
40 | class CRM_Pledge_Form_Payment extends CRM_Core_Form { | |
41 | ||
42 | /** | |
100fef9d | 43 | * The id of the pledge payment that we are proceessing |
6a488035 TO |
44 | * |
45 | * @var int | |
6a488035 TO |
46 | */ |
47 | public $_id; | |
48 | ||
49 | /** | |
100fef9d | 50 | * Set variables up before form is built |
6a488035 TO |
51 | * |
52 | * @return void | |
6a488035 TO |
53 | */ |
54 | public function preProcess() { | |
55 | // check for edit permission | |
56 | if (!CRM_Core_Permission::check('edit pledges')) { | |
0499b0ad | 57 | CRM_Core_Error::fatal(ts('You do not have permission to access this page.')); |
6a488035 TO |
58 | } |
59 | ||
60 | $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this); | |
e2046b33 CW |
61 | |
62 | CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment')); | |
6a488035 TO |
63 | } |
64 | ||
65 | /** | |
c490a46a | 66 | * Set default values for the form. |
6a488035 TO |
67 | * the default values are retrieved from the database |
68 | * | |
6a488035 | 69 | * |
355ba699 | 70 | * @return void |
6a488035 | 71 | */ |
00be9182 | 72 | public function setDefaultValues() { |
6a488035 TO |
73 | $defaults = array(); |
74 | if ($this->_id) { | |
75 | $params['id'] = $this->_id; | |
76 | CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
77 | list($defaults['scheduled_date']) = CRM_Utils_Date::setDateDefaults($defaults['scheduled_date']); | |
78 | if (isset($defaults['contribution_id'])) { | |
79 | $this->assign('pledgePayment', TRUE); | |
80 | } | |
81 | $status = CRM_Contribute_PseudoConstant::contributionStatus($defaults['status_id']); | |
82 | $this->assign('status', $status); | |
83 | } | |
84 | $defaults['option_type'] = 1; | |
85 | return $defaults; | |
86 | } | |
87 | ||
88 | /** | |
c490a46a | 89 | * Build the form object |
6a488035 | 90 | * |
355ba699 | 91 | * @return void |
6a488035 TO |
92 | */ |
93 | public function buildQuickForm() { | |
94 | //add various dates | |
95 | $this->addDate('scheduled_date', ts('Scheduled Date'), TRUE); | |
96 | ||
97 | $this->addMoney('scheduled_amount', | |
98 | ts('Scheduled Amount'), TRUE, | |
99 | array( | |
100 | 'READONLY' => TRUE, | |
101 | 'style' => "background-color:#EBECE4", | |
102 | ), | |
103 | TRUE, | |
104 | 'currency', | |
105 | NULL, TRUE | |
106 | ); | |
107 | ||
098201d8 | 108 | $optionTypes = array( |
353ffa53 | 109 | '1' => ts('Adjust Pledge Payment Schedule?'), |
6a488035 TO |
110 | '2' => ts('Adjust Total Pledge Amount?'), |
111 | ); | |
112 | $element = $this->addRadio('option_type', | |
113 | NULL, | |
114 | $optionTypes, | |
098201d8 | 115 | array(), '<br/>' |
6a488035 TO |
116 | ); |
117 | ||
118 | $this->addButtons(array( | |
119 | array( | |
120 | 'type' => 'next', | |
121 | 'name' => ts('Save'), | |
122 | 'spacing' => ' ', | |
123 | 'js' => array('onclick' => "return verify( );"), | |
124 | 'isDefault' => TRUE, | |
125 | ), | |
126 | array( | |
127 | 'type' => 'cancel', | |
128 | 'name' => ts('Cancel'), | |
129 | ), | |
130 | ) | |
131 | ); | |
132 | } | |
133 | ||
134 | /** | |
c490a46a | 135 | * Process the form submission |
6a488035 | 136 | * |
6a488035 | 137 | * |
355ba699 | 138 | * @return void |
6a488035 TO |
139 | */ |
140 | public function postProcess() { | |
141 | //get the submitted form values. | |
142 | $formValues = $this->controller->exportValues($this->_name); | |
143 | $params = array(); | |
144 | $formValues['scheduled_date'] = CRM_Utils_Date::processDate($formValues['scheduled_date']); | |
145 | $params['scheduled_date'] = CRM_Utils_Date::format($formValues['scheduled_date']); | |
146 | $params['currency'] = CRM_Utils_Array::value('currency', $formValues); | |
147 | $now = date('Ymd'); | |
148 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
149 | ||
150 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) { | |
151 | $params['status_id'] = array_search('Overdue', $contributionStatus); | |
152 | } | |
153 | else { | |
154 | $params['status_id'] = array_search('Pending', $contributionStatus); | |
155 | } | |
156 | ||
157 | $params['id'] = $this->_id; | |
158 | $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id'); | |
159 | ||
160 | CRM_Pledge_BAO_PledgePayment::add($params); | |
161 | $adjustTotalAmount = FALSE; | |
162 | if (CRM_Utils_Array::value('option_type', $formValues) == 2) { | |
163 | $adjustTotalAmount = TRUE; | |
164 | } | |
165 | ||
6a488035 TO |
166 | $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
167 | $params['id'], | |
168 | 'scheduled_amount', | |
169 | 'id' | |
170 | ); | |
171 | ||
172 | $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2); | |
173 | if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) { | |
174 | $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); | |
175 | } | |
176 | if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) { | |
177 | $adjustTotalAmount = TRUE; | |
178 | } | |
179 | //update pledge status | |
180 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, | |
181 | array($params['id']), | |
182 | $params['status_id'], | |
183 | NULL, | |
184 | $formValues['scheduled_amount'], | |
185 | $adjustTotalAmount | |
186 | ); | |
187 | ||
188 | $statusMsg = ts('Pledge Payment Schedule has been updated.'); | |
189 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
190 | } | |
96025800 | 191 | |
6a488035 | 192 | } |