a3b80ff7d86102dd78c6140bef42a796ec75e31d
[civicrm-core.git] / CRM / Pledge / BAO / PledgeBlock.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Pledge_BAO_PledgeBlock extends CRM_Pledge_DAO_PledgeBlock {
18
19 /**
20 * Class constructor.
21 */
22 public function __construct() {
23 parent::__construct();
24 }
25
26 /**
27 * Retrieve DB object based on input parameters.
28 *
29 * It also stores all the retrieved values in the default array.
30 *
31 * @param array $params
32 * (reference ) an assoc array of name/value pairs.
33 * @param array $defaults
34 * (reference ) an assoc array to hold the flattened values.
35 *
36 * @return CRM_Pledge_BAO_PledgeBlock
37 */
38 public static function retrieve(&$params, &$defaults) {
39 $pledgeBlock = new CRM_Pledge_DAO_PledgeBlock();
40 $pledgeBlock->copyValues($params);
41 if ($pledgeBlock->find(TRUE)) {
42 CRM_Core_DAO::storeValues($pledgeBlock, $defaults);
43 return $pledgeBlock;
44 }
45 return NULL;
46 }
47
48 /**
49 * Takes an associative array and creates a pledgeBlock object.
50 *
51 * @param array $params
52 * (reference ) an assoc array of name/value pairs.
53 *
54 * @return CRM_Pledge_BAO_PledgeBlock
55 */
56 public static function &create(&$params) {
57 $transaction = new CRM_Core_Transaction();
58 $pledgeBlock = self::add($params);
59
60 if (is_a($pledgeBlock, 'CRM_Core_Error')) {
61 $pledgeBlock->rollback();
62 return $pledgeBlock;
63 }
64
65 $params['id'] = $pledgeBlock->id;
66
67 $transaction->commit();
68
69 return $pledgeBlock;
70 }
71
72 /**
73 * Add or update pledgeBlock.
74 *
75 * @param array $params
76 *
77 * @return object
78 */
79 public static function add($params) {
80 // FIXME: This is assuming checkbox input like ['foo' => 1, 'bar' => 0, 'baz' => 1]. Not API friendly.
81 if (!empty($params['pledge_frequency_unit']) && is_array($params['pledge_frequency_unit'])) {
82 $params['pledge_frequency_unit'] = array_keys(array_filter($params['pledge_frequency_unit']));
83 }
84 return self::writeRecord($params);
85 }
86
87 /**
88 * Delete the pledgeBlock.
89 *
90 * @param int $id
91 * PledgeBlock id.
92 *
93 * @return mixed|null
94 */
95 public static function deletePledgeBlock($id) {
96 CRM_Utils_Hook::pre('delete', 'PledgeBlock', $id, CRM_Core_DAO::$_nullArray);
97
98 $transaction = new CRM_Core_Transaction();
99
100 $results = NULL;
101
102 $dao = new CRM_Pledge_DAO_PledgeBlock();
103 $dao->id = $id;
104 $results = $dao->delete();
105
106 $transaction->commit();
107
108 CRM_Utils_Hook::post('delete', 'PledgeBlock', $dao->id, $dao);
109
110 return $results;
111 }
112
113 /**
114 * Return Pledge Block info in Contribution Pages.
115 *
116 * @param int $pageID
117 * Contribution page id.
118 *
119 * @return array
120 */
121 public static function getPledgeBlock($pageID) {
122 $pledgeBlock = [];
123
124 $dao = new CRM_Pledge_DAO_PledgeBlock();
125 $dao->entity_table = 'civicrm_contribution_page';
126 $dao->entity_id = $pageID;
127 if ($dao->find(TRUE)) {
128 CRM_Core_DAO::storeValues($dao, $pledgeBlock);
129 }
130
131 return $pledgeBlock;
132 }
133
134 /**
135 * Build Pledge Block in Contribution Pages.
136 *
137 * @param CRM_Core_Form $form
138 */
139 public static function buildPledgeBlock($form) {
140 //build pledge payment fields.
141 if (!empty($form->_values['pledge_id'])) {
142 //get all payments required details.
143 $allPayments = [];
144 $returnProperties = array(
145 'status_id',
146 'scheduled_date',
147 'scheduled_amount',
148 'currency',
149 );
150 CRM_Core_DAO::commonRetrieveAll('CRM_Pledge_DAO_PledgePayment', 'pledge_id',
151 $form->_values['pledge_id'], $allPayments, $returnProperties
152 );
153 // get all status
154 $allStatus = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
155
156 $nextPayment = [];
157 $isNextPayment = FALSE;
158 $overduePayments = [];
159 foreach ($allPayments as $payID => $value) {
160 if ($allStatus[$value['status_id']] == 'Overdue') {
161 $overduePayments[$payID] = array(
162 'id' => $payID,
163 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
164 'scheduled_amount_currency' => $value['currency'],
165 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
166 '%B %d'
167 ),
168 );
169 }
170 elseif (!$isNextPayment &&
171 $allStatus[$value['status_id']] == 'Pending'
172 ) {
173 // get the next payment.
174 $nextPayment = array(
175 'id' => $payID,
176 'scheduled_amount' => CRM_Utils_Rule::cleanMoney($value['scheduled_amount']),
177 'scheduled_amount_currency' => $value['currency'],
178 'scheduled_date' => CRM_Utils_Date::customFormat($value['scheduled_date'],
179 '%B %d'
180 ),
181 );
182 $isNextPayment = TRUE;
183 }
184 }
185
186 // build check box array for payments.
187 $payments = [];
188 if (!empty($overduePayments)) {
189 foreach ($overduePayments as $id => $payment) {
190 $label = ts("%1 - due on %2 (overdue)", array(
191 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $payment), CRM_Utils_Array::value('scheduled_amount_currency', $payment)),
192 2 => $payment['scheduled_date'] ?? NULL,
193 ));
194 $paymentID = $payment['id'] ?? NULL;
195 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $payment)));
196 }
197 }
198
199 if (!empty($nextPayment)) {
200 $label = ts("%1 - due on %2", array(
201 1 => CRM_Utils_Money::format(CRM_Utils_Array::value('scheduled_amount', $nextPayment), CRM_Utils_Array::value('scheduled_amount_currency', $nextPayment)),
202 2 => $nextPayment['scheduled_date'] ?? NULL,
203 ));
204 $paymentID = $nextPayment['id'] ?? NULL;
205 $payments[] = $form->createElement('checkbox', $paymentID, NULL, $label, array('amount' => CRM_Utils_Array::value('scheduled_amount', $nextPayment)));
206 }
207 // give error if empty or build form for payment.
208 if (empty($payments)) {
209 throw new CRM_Core_Exception(ts('Oops. It looks like there is no valid payment status for online payment.'));
210 }
211 else {
212 $form->assign('is_pledge_payment', TRUE);
213 $form->addGroup($payments, 'pledge_amount', ts('Make Pledge Payment(s):'), '<br />');
214 }
215 }
216 else {
217
218 $pledgeBlock = self::getPledgeBlock($form->_id);
219
220 // build form for pledge creation.
221 $pledgeOptions = array(
222 '0' => ts('I want to make a one-time contribution'),
223 '1' => ts('I pledge to contribute this amount every'),
224 );
225 $form->addRadio('is_pledge', ts('Pledge Frequency Interval'), $pledgeOptions,
226 NULL, array('<br/>')
227 );
228 $form->addElement('text', 'pledge_installments', ts('Installments'), ['size' => 3, 'aria-label' => ts('Installments')]);
229
230 if (!empty($pledgeBlock['is_pledge_interval'])) {
231 $form->assign('is_pledge_interval', CRM_Utils_Array::value('is_pledge_interval', $pledgeBlock));
232 $form->addElement('text', 'pledge_frequency_interval', NULL, ['size' => 3, 'aria-label' => ts('Frequency Intervals')]);
233 }
234 else {
235 $form->add('hidden', 'pledge_frequency_interval', 1);
236 }
237 // Frequency unit drop-down label suffixes switch from *ly to *(s)
238 $freqUnitVals = explode(CRM_Core_DAO::VALUE_SEPARATOR, $pledgeBlock['pledge_frequency_unit']);
239 $freqUnits = [];
240 $frequencyUnits = CRM_Core_OptionGroup::values('recur_frequency_units');
241 foreach ($freqUnitVals as $key => $val) {
242 if (array_key_exists($val, $frequencyUnits)) {
243 $freqUnits[$val] = !empty($pledgeBlock['is_pledge_interval']) ? "{$frequencyUnits[$val]}(s)" : $frequencyUnits[$val];
244 }
245 }
246 $form->addElement('select', 'pledge_frequency_unit', NULL, $freqUnits, ['aria-label' => ts('Frequency Units')]);
247 // CRM-18854
248 if (!empty($pledgeBlock['is_pledge_start_date_visible'])) {
249 if (!empty($pledgeBlock['pledge_start_date'])) {
250 $defaults = [];
251 $date = (array) json_decode($pledgeBlock['pledge_start_date']);
252 foreach ($date as $field => $value) {
253 switch ($field) {
254 case 'contribution_date':
255 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
256 $paymentDate = $value = date('Y-m-d');
257 $defaults['start_date'] = $value;
258 $form->assign('is_date', TRUE);
259 break;
260
261 case 'calendar_date':
262 $form->add('datepicker', 'start_date', ts('First installment payment'), [], FALSE, ['time' => FALSE]);
263 $defaults['start_date'] = $value;
264 $form->assign('is_date', TRUE);
265 $paymentDate = $value;
266 break;
267
268 case 'calendar_month':
269 $month = CRM_Utils_Date::getCalendarDayOfMonth();
270 $form->add('select', 'start_date', ts('Day of month installments paid'), $month);
271 $paymentDate = CRM_Pledge_BAO_Pledge::getPaymentDate($value);
272 $defaults['start_date'] = $paymentDate;
273 break;
274
275 default:
276 break;
277
278 }
279 $form->setDefaults($defaults);
280 $form->assign('start_date_display', $paymentDate);
281 $form->assign('start_date_editable', FALSE);
282 if (!empty($pledgeBlock['is_pledge_start_date_editable'])) {
283 $form->assign('start_date_editable', TRUE);
284 if ($field == 'calendar_month') {
285 $form->assign('is_date', FALSE);
286 $form->setDefaults(array('start_date' => $value));
287 }
288 }
289 }
290 }
291 }
292 }
293 }
294
295 }