Merge pull request #4556 from jitendrapurohit/CRM-15586
[civicrm-core.git] / CRM / Pledge / Form / Payment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment'));
65 }
66
67 /**
68 * This function sets the default values for the form.
69 * the default values are retrieved from the database
70 *
71 * @access public
72 *
73 * @return void
74 */
75 function setDefaultValues() {
76 $defaults = array();
77 if ($this->_id) {
78 $params['id'] = $this->_id;
79 CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults);
80 list($defaults['scheduled_date']) = CRM_Utils_Date::setDateDefaults($defaults['scheduled_date']);
81 if (isset($defaults['contribution_id'])) {
82 $this->assign('pledgePayment', TRUE);
83 }
84 $status = CRM_Contribute_PseudoConstant::contributionStatus($defaults['status_id']);
85 $this->assign('status', $status);
86 }
87 $defaults['option_type'] = 1;
88 return $defaults;
89 }
90
91 /**
92 * Function to build the form
93 *
94 * @return void
95 * @access public
96 */
97 public function buildQuickForm() {
98 //add various dates
99 $this->addDate('scheduled_date', ts('Scheduled Date'), TRUE);
100
101 $this->addMoney('scheduled_amount',
102 ts('Scheduled Amount'), TRUE,
103 array(
104 'READONLY' => TRUE,
105 'style' => "background-color:#EBECE4",
106 ),
107 TRUE,
108 'currency',
109 NULL, TRUE
110 );
111
112 $optionTypes = array('1' => ts('Adjust Pledge Payment Schedule?'),
113 '2' => ts('Adjust Total Pledge Amount?'),
114 );
115 $element = $this->addRadio('option_type',
116 NULL,
117 $optionTypes,
118 array(
119 ), '<br/>'
120 );
121
122 $this->addButtons(array(
123 array(
124 'type' => 'next',
125 'name' => ts('Save'),
126 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
127 'js' => array('onclick' => "return verify( );"),
128 'isDefault' => TRUE,
129 ),
130 array(
131 'type' => 'cancel',
132 'name' => ts('Cancel'),
133 ),
134 )
135 );
136 }
137
138 /**
139 * Function to process the form
140 *
141 * @access public
142 *
143 * @return void
144 */
145 public function postProcess() {
146 //get the submitted form values.
147 $formValues = $this->controller->exportValues($this->_name);
148 $params = array();
149 $formValues['scheduled_date'] = CRM_Utils_Date::processDate($formValues['scheduled_date']);
150 $params['scheduled_date'] = CRM_Utils_Date::format($formValues['scheduled_date']);
151 $params['currency'] = CRM_Utils_Array::value('currency', $formValues);
152 $now = date('Ymd');
153 $contributionStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
154
155 if (CRM_Utils_Date::overdue(CRM_Utils_Date::customFormat($params['scheduled_date'], '%Y%m%d'), $now)) {
156 $params['status_id'] = array_search('Overdue', $contributionStatus);
157 }
158 else {
159 $params['status_id'] = array_search('Pending', $contributionStatus);
160 }
161
162 $params['id'] = $this->_id;
163 $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
164
165 CRM_Pledge_BAO_PledgePayment::add($params);
166 $adjustTotalAmount = FALSE;
167 if (CRM_Utils_Array::value('option_type', $formValues) == 2) {
168 $adjustTotalAmount = TRUE;
169 }
170
171
172 $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
173 $params['id'],
174 'scheduled_amount',
175 'id'
176 );
177
178 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2);
179 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
180 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
181 }
182 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
183 $adjustTotalAmount = TRUE;
184 }
185 //update pledge status
186 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
187 array($params['id']),
188 $params['status_id'],
189 NULL,
190 $formValues['scheduled_amount'],
191 $adjustTotalAmount
192 );
193
194 $statusMsg = ts('Pledge Payment Schedule has been updated.');
195 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
196 }
197 }
198