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