Merge pull request #14894 from eileenmcnaughton/date_fisc
[civicrm-core.git] / CRM / Pledge / Form / Payment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
6b83d5bd 6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
6b83d5bd 31 * @copyright CiviCRM LLC (c) 2004-2019
6a488035
TO
32 */
33
34/**
cc28438b 35 * This class generates form components for processing a pledge payment.
6a488035
TO
36 */
37class 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
267fb11a
SL
46 /**
47 * Explicitly declare the entity api name.
48 */
49 public function getDefaultEntity() {
50 return 'PledgePayment';
51 }
52
53 /**
54 * Explicitly declare the form context.
55 */
56 public function getDefaultContext() {
57 return 'create';
58 }
59
6a488035 60 /**
fe482240 61 * Set variables up before form is built.
6a488035
TO
62 */
63 public function preProcess() {
64 // check for edit permission
65 if (!CRM_Core_Permission::check('edit pledges')) {
0499b0ad 66 CRM_Core_Error::fatal(ts('You do not have permission to access this page.'));
6a488035
TO
67 }
68
69 $this->_id = CRM_Utils_Request::retrieve('ppId', 'Positive', $this);
e2046b33
CW
70
71 CRM_Utils_System::setTitle(ts('Edit Scheduled Pledge Payment'));
6a488035
TO
72 }
73
74 /**
c490a46a 75 * Set default values for the form.
cc28438b 76 * the default values are retrieved from the database.
6a488035 77 */
00be9182 78 public function setDefaultValues() {
be2fb01f 79 $defaults = [];
6a488035
TO
80 if ($this->_id) {
81 $params['id'] = $this->_id;
82 CRM_Pledge_BAO_PledgePayment::retrieve($params, $defaults);
6a488035
TO
83 if (isset($defaults['contribution_id'])) {
84 $this->assign('pledgePayment', TRUE);
85 }
ab6ba136 86 $status = CRM_Core_PseudoConstant::getName('CRM_Pledge_BAO_Pledge', 'status_id', $defaults['status_id']);
6a488035
TO
87 $this->assign('status', $status);
88 }
89 $defaults['option_type'] = 1;
90 return $defaults;
91 }
92
93 /**
fe482240 94 * Build the form object.
6a488035
TO
95 */
96 public function buildQuickForm() {
cc28438b 97 // add various dates
267fb11a 98 $this->addField('scheduled_date', [], TRUE, FALSE);
6a488035
TO
99
100 $this->addMoney('scheduled_amount',
101 ts('Scheduled Amount'), TRUE,
be2fb01f 102 ['readonly' => TRUE],
6a488035
TO
103 TRUE,
104 'currency',
b467b98d
CW
105 NULL,
106 TRUE
6a488035
TO
107 );
108
be2fb01f 109 $optionTypes = [
353ffa53 110 '1' => ts('Adjust Pledge Payment Schedule?'),
6a488035 111 '2' => ts('Adjust Total Pledge Amount?'),
be2fb01f 112 ];
6a488035
TO
113 $element = $this->addRadio('option_type',
114 NULL,
115 $optionTypes,
be2fb01f 116 [], '<br/>'
6a488035
TO
117 );
118
be2fb01f
CW
119 $this->addButtons([
120 [
6a488035
TO
121 'type' => 'next',
122 'name' => ts('Save'),
123 'spacing' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',
6a488035 124 'isDefault' => TRUE,
be2fb01f
CW
125 ],
126 [
6a488035
TO
127 'type' => 'cancel',
128 'name' => ts('Cancel'),
be2fb01f 129 ],
c86d4e7c 130 ]);
6a488035
TO
131 }
132
133 /**
fe482240 134 * Process the form submission.
6a488035
TO
135 */
136 public function postProcess() {
6a488035 137 $formValues = $this->controller->exportValues($this->_name);
267fb11a
SL
138 $params = [
139 'id' => $this->_id,
140 'scheduled_date' => $formValues['scheduled_date'],
141 'currency' => $formValues['currency'],
142 ];
6a488035 143
267fb11a 144 if (CRM_Utils_Date::overdue($params['scheduled_date'])) {
ab6ba136 145 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Overdue');
6a488035
TO
146 }
147 else {
ab6ba136 148 $params['status_id'] = CRM_Core_PseudoConstant::getKey('CRM_Pledge_BAO_Pledge', 'status_id', 'Pending');
6a488035
TO
149 }
150
6a488035
TO
151 $pledgeId = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment', $params['id'], 'pledge_id');
152
153 CRM_Pledge_BAO_PledgePayment::add($params);
267fb11a 154 $adjustTotalAmount = (CRM_Utils_Array::value('option_type', $formValues) == 2);
6a488035 155
6a488035
TO
156 $pledgeScheduledAmount = CRM_Core_DAO::getFieldValue('CRM_Pledge_DAO_PledgePayment',
157 $params['id'],
158 'scheduled_amount',
159 'id'
160 );
161
162 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId, 2);
163 if (($oldestPaymentAmount['count'] != 1) && ($oldestPaymentAmount['id'] == $params['id'])) {
164 $oldestPaymentAmount = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($pledgeId);
165 }
166 if (($formValues['scheduled_amount'] - $pledgeScheduledAmount) >= $oldestPaymentAmount['amount']) {
167 $adjustTotalAmount = TRUE;
168 }
cc28438b 169 // update pledge status
6a488035 170 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($pledgeId,
be2fb01f 171 [$params['id']],
6a488035
TO
172 $params['status_id'],
173 NULL,
174 $formValues['scheduled_amount'],
175 $adjustTotalAmount
176 );
177
178 $statusMsg = ts('Pledge Payment Schedule has been updated.');
179 CRM_Core_Session::setStatus($statusMsg, ts('Saved'), 'success');
180 }
96025800 181
6a488035 182}