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