Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
fa938177 | 6 | | Copyright CiviCRM LLC (c) 2004-2016 | |
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 | |
fa938177 | 31 | * @copyright CiviCRM LLC (c) 2004-2016 |
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 | ||
46 | /** | |
fe482240 | 47 | * Set variables up before form is built. |
6a488035 TO |
48 | */ |
49 | public function preProcess() { | |
50 | // check for edit permission | |
51 | if (!CRM_Core_Permission::check('edit pledges')) { | |
0499b0ad | 52 | CRM_Core_Error::fatal(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() { |
6a488035 TO |
65 | $defaults = array(); |
66 | if ($this->_id) { | |
67 | $params['id'] = $this->_id; | |
68 | CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults); | |
69 | list($defaults['scheduled_date']) = CRM_Utils_Date::setDateDefaults($defaults['scheduled_date']); | |
70 | if (isset($defaults['contribution_id'])) { | |
71 | $this->assign('pledgePayment', TRUE); | |
72 | } | |
73 | $status = CRM_Contribute_PseudoConstant::contributionStatus($defaults['status_id']); | |
74 | $this->assign('status', $status); | |
75 | } | |
76 | $defaults['option_type'] = 1; | |
77 | return $defaults; | |
78 | } | |
79 | ||
80 | /** | |
fe482240 | 81 | * Build the form object. |
6a488035 TO |
82 | */ |
83 | public function buildQuickForm() { | |
cc28438b | 84 | // add various dates |
6a488035 TO |
85 | $this->addDate('scheduled_date', ts('Scheduled Date'), TRUE); |
86 | ||
87 | $this->addMoney('scheduled_amount', | |
88 | ts('Scheduled Amount'), TRUE, | |
b467b98d | 89 | array('readonly' => TRUE), |
6a488035 TO |
90 | TRUE, |
91 | 'currency', | |
b467b98d CW |
92 | NULL, |
93 | TRUE | |
6a488035 TO |
94 | ); |
95 | ||
098201d8 | 96 | $optionTypes = array( |
353ffa53 | 97 | '1' => ts('Adjust Pledge Payment Schedule?'), |
6a488035 TO |
98 | '2' => ts('Adjust Total Pledge Amount?'), |
99 | ); | |
100 | $element = $this->addRadio('option_type', | |
101 | NULL, | |
102 | $optionTypes, | |
098201d8 | 103 | array(), '<br/>' |
6a488035 TO |
104 | ); |
105 | ||
106 | $this->addButtons(array( | |
107 | array( | |
108 | 'type' => 'next', | |
109 | 'name' => ts('Save'), | |
110 | 'spacing' => ' ', | |
6a488035 TO |
111 | 'isDefault' => TRUE, |
112 | ), | |
113 | array( | |
114 | 'type' => 'cancel', | |
115 | 'name' => ts('Cancel'), | |
116 | ), | |
117 | ) | |
118 | ); | |
119 | } | |
120 | ||
121 | /** | |
fe482240 | 122 | * Process the form submission. |
6a488035 TO |
123 | */ |
124 | public function postProcess() { | |
cc28438b | 125 | // get the submitted form values. |
6a488035 TO |
126 | $formValues = $this->controller->exportValues($this->_name); |
127 | $params = array(); | |
128 | $formValues['scheduled_date'] = CRM_Utils_Date::processDate($formValues['scheduled_date']); | |
129 | $params['scheduled_date'] = CRM_Utils_Date::format($formValues['scheduled_date']); | |
130 | $params['currency'] = CRM_Utils_Array::value('currency', $formValues); | |
131 | $now = date('Ymd'); | |
132 | $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
133 | ||
134 | if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) { | |
135 | $params['status_id'] = array_search('Overdue', $contributionStatus); | |
136 | } | |
137 | else { | |
138 | $params['status_id'] = array_search('Pending', $contributionStatus); | |
139 | } | |
140 | ||
141 | $params['id'] = $this->_id; | |
142 | $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id'); | |
143 | ||
144 | CRM_Pledge_BAO_PledgePayment::add($params); | |
145 | $adjustTotalAmount = FALSE; | |
146 | if (CRM_Utils_Array::value('option_type', $formValues) == 2) { | |
147 | $adjustTotalAmount = TRUE; | |
148 | } | |
149 | ||
6a488035 TO |
150 | $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', |
151 | $params['id'], | |
152 | 'scheduled_amount', | |
153 | 'id' | |
154 | ); | |
155 | ||
156 | $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2); | |
157 | if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) { | |
158 | $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId); | |
159 | } | |
160 | if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) { | |
161 | $adjustTotalAmount = TRUE; | |
162 | } | |
cc28438b | 163 | // update pledge status |
6a488035 TO |
164 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId, |
165 | array($params['id']), | |
166 | $params['status_id'], | |
167 | NULL, | |
168 | $formValues['scheduled_amount'], | |
169 | $adjustTotalAmount | |
170 | ); | |
171 | ||
172 | $statusMsg = ts('Pledge Payment Schedule has been updated.'); | |
173 | CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success'); | |
174 | } | |
96025800 | 175 | |
6a488035 | 176 | } |