Ian province abbreviation patch - issue 724
[civicrm-core.git] / CRM / Pledge / Form / Payment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
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 */
47 public $_id;
48
49 /**
50 * Set variables up before form is built.
51 *
52 * @return void
53 */
54 public function preProcess() {
55 // check for edit permission
56 if (!CRM_Core_Permission::check('edit pledges')) {
57 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
58 }
59
60 $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this);
61
62 CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment'));
63 }
64
65 /**
66 * Set default values for the form.
67 * the default values are retrieved from the database
68 *
69 *
70 * @return void
71 */
72 public function setDefaultValues() {
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 /**
89 * Build the form object.
90 *
91 * @return void
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('readonly' => TRUE),
100 TRUE,
101 'currency',
102 NULL,
103 TRUE
104 );
105
106 $optionTypes = array(
107 '1' => ts('Adjust Pledge Payment Schedule?'),
108 '2' => ts('Adjust Total Pledge Amount?'),
109 );
110 $element = $this->addRadio('option_type',
111 NULL,
112 $optionTypes,
113 array(), '<br/>'
114 );
115
116 $this->addButtons(array(
117 array(
118 'type' => 'next',
119 'name' => ts('Save'),
120 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
121 'isDefault' => TRUE,
122 ),
123 array(
124 'type' => 'cancel',
125 'name' => ts('Cancel'),
126 ),
127 )
128 );
129 }
130
131 /**
132 * Process the form submission.
133 *
134 *
135 * @return void
136 */
137 public function postProcess() {
138 //get the submitted form values.
139 $formValues = $this->controller->exportValues($this->_name);
140 $params = array();
141 $formValues['scheduled_date'] = CRM_Utils_Date::processDate($formValues['scheduled_date']);
142 $params['scheduled_date'] = CRM_Utils_Date::format($formValues['scheduled_date']);
143 $params['currency'] = CRM_Utils_Array::value('currency', $formValues);
144 $now = date('Ymd');
145 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
146
147 if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) {
148 $params['status_id'] = array_search('Overdue', $contributionStatus);
149 }
150 else {
151 $params['status_id'] = array_search('Pending', $contributionStatus);
152 }
153
154 $params['id'] = $this->_id;
155 $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
156
157 CRM_Pledge_BAO_PledgePayment::add($params);
158 $adjustTotalAmount = FALSE;
159 if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
160 $adjustTotalAmount = TRUE;
161 }
162
163 $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
164 $params['id'],
165 'scheduled_amount',
166 'id'
167 );
168
169 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2);
170 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
171 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
172 }
173 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
174 $adjustTotalAmount = TRUE;
175 }
176 //update pledge status
177 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
178 array($params['id']),
179 $params['status_id'],
180 NULL,
181 $formValues['scheduled_amount'],
182 $adjustTotalAmount
183 );
184
185 $statusMsg = ts('Pledge Payment Schedule has been updated.');
186 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
187 }
188
189 }