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