Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
e7112fa7 | 6 | | Copyright CiviCRM LLC (c) 2004-2015 | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
e7112fa7 | 31 | * @copyright CiviCRM LLC (c) 2004-2015 |
6a488035 TO |
32 | */ |
33 | ||
34 | /** | |
ce064e4f | 35 | * Batch BAO class. |
6a488035 TO |
36 | */ |
37 | class CRM_Batch_BAO_Batch extends CRM_Batch_DAO_Batch { | |
38 | ||
39 | /** | |
eceb18cc | 40 | * Cache for the current batch object. |
6a488035 TO |
41 | */ |
42 | static $_batch = NULL; | |
43 | ||
44 | /** | |
45 | * Not sure this is the best way to do this. Depends on how exportFinancialBatch() below gets called. | |
46 | * Maybe a parameter to that function is better. | |
47 | */ | |
48 | static $_exportFormat = NULL; | |
49 | ||
50 | /** | |
eceb18cc | 51 | * Create a new batch. |
6a488035 | 52 | * |
82d480a5 | 53 | * @param array $params |
82d480a5 TO |
54 | * @param array $ids |
55 | * Associated array of ids. | |
56 | * @param string $context | |
57 | * String. | |
6a488035 | 58 | * |
a6c01b45 CW |
59 | * @return object |
60 | * $batch batch object | |
6a488035 | 61 | */ |
00be9182 | 62 | public static function create(&$params, $ids = NULL, $context = NULL) { |
a7488080 | 63 | if (empty($params['id'])) { |
6a488035 TO |
64 | $params['name'] = CRM_Utils_String::titleToVar($params['title']); |
65 | } | |
66 | ||
67 | $batch = new CRM_Batch_DAO_Batch(); | |
68 | $batch->copyValues($params); | |
8cc574cf | 69 | if ($context == 'financialBatch' && !empty($ids['batchID'])) { |
6a488035 TO |
70 | $batch->id = $ids['batchID']; |
71 | } | |
72 | $batch->save(); | |
73 | ||
74 | return $batch; | |
75 | } | |
76 | ||
77 | /** | |
eceb18cc | 78 | * Retrieve the information about the batch. |
6a488035 | 79 | * |
82d480a5 TO |
80 | * @param array $params |
81 | * (reference ) an assoc array of name/value pairs. | |
82 | * @param array $defaults | |
83 | * (reference ) an assoc array to hold the flattened values. | |
6a488035 | 84 | * |
a6c01b45 CW |
85 | * @return array |
86 | * CRM_Batch_BAO_Batch object on success, null otherwise | |
6a488035 | 87 | */ |
00be9182 | 88 | public static function retrieve(&$params, &$defaults) { |
6a488035 TO |
89 | $batch = new CRM_Batch_DAO_Batch(); |
90 | $batch->copyValues($params); | |
91 | if ($batch->find(TRUE)) { | |
92 | CRM_Core_DAO::storeValues($batch, $defaults); | |
93 | return $batch; | |
94 | } | |
95 | return NULL; | |
96 | } | |
97 | ||
98 | /** | |
eceb18cc | 99 | * Get profile id associated with the batch type. |
6a488035 | 100 | * |
82d480a5 TO |
101 | * @param int $batchTypeId |
102 | * Batch type id. | |
6a488035 | 103 | * |
a6c01b45 CW |
104 | * @return int |
105 | * $profileId profile id | |
6a488035 | 106 | */ |
00be9182 | 107 | public static function getProfileId($batchTypeId) { |
6a488035 TO |
108 | //retrieve the profile specific to batch type |
109 | switch ($batchTypeId) { | |
110 | case 1: | |
9fa00ed1 | 111 | case 3: |
112 | //batch profile used for pledges | |
6a488035 TO |
113 | $profileName = "contribution_batch_entry"; |
114 | break; | |
115 | ||
116 | case 2: | |
117 | //batch profile used for memberships | |
118 | $profileName = "membership_batch_entry"; | |
04e6444d | 119 | break; |
6a488035 TO |
120 | } |
121 | ||
122 | // get and return the profile id | |
123 | return CRM_Core_DAO::getFieldValue('CRM_Core_BAO_UFGroup', $profileName, 'id', 'name'); | |
124 | } | |
125 | ||
126 | /** | |
eceb18cc | 127 | * Generate batch name. |
6a488035 | 128 | * |
72b3a70c CW |
129 | * @return string |
130 | * batch name | |
6a488035 | 131 | */ |
00be9182 | 132 | public static function generateBatchName() { |
6a488035 TO |
133 | $sql = "SELECT max(id) FROM civicrm_batch"; |
134 | $batchNo = CRM_Core_DAO::singleValueQuery($sql) + 1; | |
135 | return ts('Batch %1', array(1 => $batchNo)) . ': ' . date('Y-m-d'); | |
136 | } | |
137 | ||
138 | /** | |
eceb18cc | 139 | * Create entity batch entry. |
72b3a70c | 140 | * |
82d480a5 | 141 | * @param array $params |
72b3a70c | 142 | * @return array |
6a488035 | 143 | */ |
00be9182 | 144 | public static function addBatchEntity(&$params) { |
6a488035 TO |
145 | $entityBatch = new CRM_Batch_DAO_EntityBatch(); |
146 | $entityBatch->copyValues($params); | |
147 | $entityBatch->save(); | |
148 | return $entityBatch; | |
149 | } | |
150 | ||
151 | /** | |
eceb18cc | 152 | * Remove entries from entity batch. |
82d480a5 | 153 | * @param array $params |
c490a46a | 154 | * @return CRM_Batch_DAO_EntityBatch |
6a488035 | 155 | */ |
00be9182 | 156 | public static function removeBatchEntity($params) { |
6a488035 TO |
157 | $entityBatch = new CRM_Batch_DAO_EntityBatch(); |
158 | $entityBatch->copyValues($params); | |
159 | $entityBatch->delete(); | |
160 | return $entityBatch; | |
161 | } | |
162 | ||
163 | /** | |
eceb18cc | 164 | * Delete batch entry. |
6a488035 | 165 | * |
82d480a5 TO |
166 | * @param int $batchId |
167 | * Batch id. | |
6a488035 | 168 | * |
72b3a70c | 169 | * @return bool |
6a488035 | 170 | */ |
00be9182 | 171 | public static function deleteBatch($batchId) { |
6a488035 TO |
172 | // delete entry from batch table |
173 | $batch = new CRM_Batch_DAO_Batch(); | |
174 | $batch->id = $batchId; | |
175 | $batch->delete(); | |
691df66d | 176 | return TRUE; |
6a488035 TO |
177 | } |
178 | ||
6a488035 | 179 | /** |
eceb18cc | 180 | * wrapper for ajax batch selector. |
6a488035 | 181 | * |
82d480a5 TO |
182 | * @param array $params |
183 | * Associated array for params record id. | |
6a488035 | 184 | * |
a6c01b45 CW |
185 | * @return array |
186 | * associated array of batch list | |
6a488035 TO |
187 | */ |
188 | public function getBatchListSelector(&$params) { | |
189 | // format the params | |
190 | $params['offset'] = ($params['page'] - 1) * $params['rp']; | |
191 | $params['rowCount'] = $params['rp']; | |
192 | $params['sort'] = CRM_Utils_Array::value('sortBy', $params); | |
193 | ||
194 | // get batches | |
195 | $batches = self::getBatchList($params); | |
196 | ||
197 | // get batch totals for open batches | |
198 | $fetchTotals = array(); | |
691df66d | 199 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name')); |
2d818e4a PN |
200 | $batchStatus = array( |
201 | array_search('Open', $batchStatus), | |
202 | array_search('Reopened', $batchStatus), | |
203 | ); | |
6a488035 TO |
204 | if ($params['context'] == 'financialBatch') { |
205 | foreach ($batches as $id => $batch) { | |
2d818e4a | 206 | if (in_array($batch['status_id'], $batchStatus)) { |
6a488035 TO |
207 | $fetchTotals[] = $id; |
208 | } | |
209 | } | |
210 | } | |
211 | $totals = self::batchTotals($fetchTotals); | |
212 | ||
213 | // add count | |
214 | $params['total'] = self::getBatchCount($params); | |
215 | ||
216 | // format params and add links | |
217 | $batchList = array(); | |
218 | ||
219 | foreach ($batches as $id => $value) { | |
220 | $batch = array(); | |
221 | if ($params['context'] == 'financialBatch') { | |
222 | $batch['check'] = $value['check']; | |
223 | } | |
224 | $batch['batch_name'] = $value['title']; | |
957bbb1d | 225 | $batch['total'] = ''; |
6a488035 TO |
226 | $batch['payment_instrument'] = $value['payment_instrument']; |
227 | $batch['item_count'] = CRM_Utils_Array::value('item_count', $value); | |
957bbb1d | 228 | $batch['type'] = $value['batch_type']; |
a7488080 | 229 | if (!empty($value['total'])) { |
6a488035 TO |
230 | $batch['total'] = CRM_Utils_Money::format($value['total']); |
231 | } | |
232 | ||
233 | // Compare totals with actuals | |
234 | if (isset($totals[$id])) { | |
235 | $batch['item_count'] = self::displayTotals($totals[$id]['item_count'], $batch['item_count']); | |
236 | $batch['total'] = self::displayTotals(CRM_Utils_Money::format($totals[$id]['total']), $batch['total']); | |
237 | } | |
238 | $batch['status'] = $value['batch_status']; | |
239 | $batch['created_by'] = $value['created_by']; | |
240 | $batch['links'] = $value['action']; | |
241 | $batchList[$id] = $batch; | |
242 | } | |
243 | return $batchList; | |
244 | } | |
245 | ||
246 | /** | |
eceb18cc | 247 | * Get list of batches. |
6a488035 | 248 | * |
82d480a5 TO |
249 | * @param array $params |
250 | * Associated array for params. | |
102fe859 EM |
251 | * |
252 | * @return array | |
6a488035 | 253 | */ |
00be9182 | 254 | public static function getBatchList(&$params) { |
6a488035 TO |
255 | $whereClause = self::whereClause($params); |
256 | ||
257 | if (!empty($params['rowCount']) && is_numeric($params['rowCount']) | |
258 | && is_numeric($params['offset']) && $params['rowCount'] > 0 | |
259 | ) { | |
260 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
261 | } | |
262 | ||
263 | $orderBy = ' ORDER BY batch.id desc'; | |
264 | if (!empty($params['sort'])) { | |
21d32567 | 265 | $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); |
6a488035 TO |
266 | } |
267 | ||
268 | $query = " | |
269 | SELECT batch.*, c.sort_name created_by | |
270 | FROM civicrm_batch batch | |
271 | INNER JOIN civicrm_contact c ON batch.created_id = c.id | |
272 | WHERE {$whereClause} | |
273 | {$orderBy} | |
274 | {$limit}"; | |
275 | ||
276 | $object = CRM_Core_DAO::executeQuery($query, $params, TRUE, 'CRM_Batch_DAO_Batch'); | |
a7488080 | 277 | if (!empty($params['context'])) { |
6a488035 TO |
278 | $links = self::links($params['context']); |
279 | } | |
280 | else { | |
281 | $links = self::links(); | |
282 | } | |
283 | ||
97afa853 AS |
284 | $batchTypes = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'type_id'); |
285 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); | |
2d818e4a | 286 | $batchStatusByName = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name')); |
6a488035 TO |
287 | $paymentInstrument = CRM_Contribute_PseudoConstant::paymentInstrument(); |
288 | ||
289 | $results = array(); | |
290 | while ($object->fetch()) { | |
291 | $values = array(); | |
292 | $newLinks = $links; | |
293 | CRM_Core_DAO::storeValues($object, $values); | |
294 | $action = array_sum(array_keys($newLinks)); | |
295 | ||
2d818e4a | 296 | if ($values['status_id'] == array_search('Closed', $batchStatusByName) && $params['context'] != 'financialBatch') { |
6a488035 TO |
297 | $newLinks = array(); |
298 | } | |
299 | elseif ($params['context'] == 'financialBatch') { | |
6c552737 | 300 | $values['check'] = "<input type='checkbox' id='check_" . |
0b0941e2 DL |
301 | $object->id . |
302 | "' name='check_" . | |
303 | $object->id . | |
304 | "' value='1' data-status_id='" . | |
92fcb95f | 305 | $values['status_id'] . "' class='select-row'></input>"; |
6a488035 | 306 | |
2d818e4a PN |
307 | switch ($batchStatusByName[$values['status_id']]) { |
308 | case 'Open': | |
6a488035 TO |
309 | CRM_Utils_Array::remove($newLinks, 'reopen', 'download'); |
310 | break; | |
691df66d | 311 | |
2d818e4a | 312 | case 'Closed': |
6a488035 TO |
313 | CRM_Utils_Array::remove($newLinks, 'close', 'edit', 'download'); |
314 | break; | |
691df66d | 315 | |
2d818e4a | 316 | case 'Exported': |
6a488035 TO |
317 | CRM_Utils_Array::remove($newLinks, 'close', 'edit', 'reopen', 'export'); |
318 | } | |
319 | } | |
a7488080 | 320 | if (!empty($values['type_id'])) { |
6a488035 TO |
321 | $values['batch_type'] = $batchTypes[$values['type_id']]; |
322 | } | |
323 | $values['batch_status'] = $batchStatus[$values['status_id']]; | |
324 | $values['created_by'] = $object->created_by; | |
325 | $values['payment_instrument'] = ''; | |
326 | if (!empty($object->payment_instrument_id)) { | |
327 | $values['payment_instrument'] = $paymentInstrument[$object->payment_instrument_id]; | |
328 | } | |
329 | $tokens = array('id' => $object->id, 'status' => $values['status_id']); | |
2d818e4a | 330 | if ($values['status_id'] == array_search('Exported', $batchStatusByName)) { |
691df66d | 331 | $aid = CRM_Core_OptionGroup::getValue('activity_type', 'Export Accounting Batch'); |
0b0941e2 | 332 | $activityParams = array('source_record_id' => $object->id, 'activity_type_id' => $aid); |
6a488035 | 333 | $exportActivity = CRM_Activity_BAO_Activity::retrieve($activityParams, $val); |
0b0941e2 | 334 | $fid = CRM_Core_DAO::getFieldValue('CRM_Core_DAO_EntityFile', $exportActivity->id, 'file_id', 'entity_id'); |
6a488035 TO |
335 | $tokens = array_merge(array('eid' => $exportActivity->id, 'fid' => $fid), $tokens); |
336 | } | |
337 | $values['action'] = CRM_Core_Action::formLink( | |
338 | $newLinks, | |
339 | $action, | |
c51d1602 | 340 | $tokens, |
87dab4a4 AH |
341 | ts('more'), |
342 | FALSE, | |
343 | 'batch.selector.row', | |
344 | 'Batch', | |
345 | $object->id | |
6a488035 TO |
346 | ); |
347 | $results[$object->id] = $values; | |
348 | } | |
349 | ||
350 | return $results; | |
351 | } | |
352 | ||
353 | /** | |
eceb18cc | 354 | * Get count of batches. |
6a488035 | 355 | * |
82d480a5 TO |
356 | * @param array $params |
357 | * Associated array for params. | |
102fe859 EM |
358 | * |
359 | * @return null|string | |
6a488035 | 360 | */ |
b11c92be | 361 | public static function getBatchCount(&$params) { |
6a488035 TO |
362 | $args = array(); |
363 | $whereClause = self::whereClause($params, $args); | |
364 | $query = " SELECT COUNT(*) FROM civicrm_batch batch | |
365 | INNER JOIN civicrm_contact c ON batch.created_id = c.id | |
366 | WHERE {$whereClause}"; | |
367 | return CRM_Core_DAO::singleValueQuery($query); | |
368 | } | |
369 | ||
370 | /** | |
eceb18cc | 371 | * Format where clause for getting lists of batches. |
6a488035 | 372 | * |
82d480a5 TO |
373 | * @param array $params |
374 | * Associated array for params. | |
102fe859 EM |
375 | * |
376 | * @return string | |
6a488035 | 377 | */ |
b11c92be | 378 | public static function whereClause($params) { |
6a488035 TO |
379 | $clauses = array(); |
380 | // Exclude data-entry batches | |
2d818e4a | 381 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id', array('labelColumn' => 'name')); |
6a488035 | 382 | if (empty($params['status_id'])) { |
2d818e4a | 383 | $clauses[] = 'batch.status_id <> ' . array_search('Data Entry', $batchStatus); |
6a488035 TO |
384 | } |
385 | ||
386 | $fields = array( | |
387 | 'title' => 'String', | |
388 | 'sort_name' => 'String', | |
389 | 'status_id' => 'Integer', | |
390 | 'payment_instrument_id' => 'Integer', | |
391 | 'item_count' => 'Integer', | |
392 | 'total' => 'Float', | |
393 | ); | |
394 | ||
395 | foreach ($fields as $field => $type) { | |
396 | $table = $field == 'sort_name' ? 'c' : 'batch'; | |
397 | if (isset($params[$field])) { | |
398 | $value = CRM_Utils_Type::escape($params[$field], $type, FALSE); | |
399 | if ($value && $type == 'String') { | |
400 | $clauses[] = "$table.$field LIKE '%$value%'"; | |
401 | } | |
402 | elseif ($value && $type == 'Float') { | |
403 | $clauses[] = "$table.$field = '$value'"; | |
404 | } | |
405 | elseif ($value) { | |
2d818e4a PN |
406 | if ($field == 'status_id' && $value == array_search('Open', $batchStatus)) { |
407 | $clauses[] = "$table.$field IN ($value," . array_search('Reopened', $batchStatus) . ')'; | |
408 | } | |
409 | else { | |
410 | $clauses[] = "$table.$field = $value"; | |
411 | } | |
6a488035 TO |
412 | } |
413 | } | |
414 | } | |
415 | return $clauses ? implode(' AND ', $clauses) : '1'; | |
416 | } | |
417 | ||
418 | /** | |
eceb18cc | 419 | * Define action links. |
6a488035 | 420 | * |
102fe859 EM |
421 | * @param null $context |
422 | * | |
a6c01b45 CW |
423 | * @return array |
424 | * array of action links | |
6a488035 | 425 | */ |
00be9182 | 426 | public function links($context = NULL) { |
6a488035 TO |
427 | if ($context == 'financialBatch') { |
428 | $links = array( | |
691df66d | 429 | 'transaction' => array( |
353ffa53 TO |
430 | 'name' => ts('Transactions'), |
431 | 'url' => 'civicrm/batchtransaction', | |
432 | 'qs' => 'reset=1&bid=%%id%%', | |
6a488035 TO |
433 | 'title' => ts('View/Add Transactions to Batch'), |
434 | ), | |
691df66d | 435 | 'edit' => array( |
353ffa53 TO |
436 | 'name' => ts('Edit'), |
437 | 'url' => 'civicrm/financial/batch', | |
438 | 'qs' => 'reset=1&action=update&id=%%id%%&context=1', | |
6a488035 TO |
439 | 'title' => ts('Edit Batch'), |
440 | ), | |
691df66d | 441 | 'close' => array( |
353ffa53 | 442 | 'name' => ts('Close'), |
6a488035 | 443 | 'title' => ts('Close Batch'), |
353ffa53 | 444 | 'url' => '#', |
6a488035 TO |
445 | 'extra' => 'rel="close"', |
446 | ), | |
691df66d | 447 | 'export' => array( |
353ffa53 | 448 | 'name' => ts('Export'), |
6a488035 | 449 | 'title' => ts('Export Batch'), |
353ffa53 | 450 | 'url' => '#', |
6a488035 TO |
451 | 'extra' => 'rel="export"', |
452 | ), | |
691df66d | 453 | 'reopen' => array( |
353ffa53 | 454 | 'name' => ts('Re-open'), |
6a488035 | 455 | 'title' => ts('Re-open Batch'), |
353ffa53 | 456 | 'url' => '#', |
6a488035 TO |
457 | 'extra' => 'rel="reopen"', |
458 | ), | |
691df66d | 459 | 'delete' => array( |
353ffa53 | 460 | 'name' => ts('Delete'), |
6a488035 | 461 | 'title' => ts('Delete Batch'), |
353ffa53 | 462 | 'url' => '#', |
6a488035 TO |
463 | 'extra' => 'rel="delete"', |
464 | ), | |
465 | 'download' => array( | |
353ffa53 TO |
466 | 'name' => ts('Download'), |
467 | 'url' => 'civicrm/file', | |
468 | 'qs' => 'reset=1&id=%%fid%%&eid=%%eid%%', | |
6a488035 | 469 | 'title' => ts('Download Batch'), |
21dfd5f5 | 470 | ), |
6a488035 TO |
471 | ); |
472 | } | |
473 | else { | |
474 | $links = array( | |
475 | CRM_Core_Action::COPY => array( | |
476 | 'name' => ts('Enter records'), | |
477 | 'url' => 'civicrm/batch/entry', | |
478 | 'qs' => 'id=%%id%%&reset=1', | |
479 | 'title' => ts('Batch Data Entry'), | |
480 | ), | |
481 | CRM_Core_Action::UPDATE => array( | |
482 | 'name' => ts('Edit'), | |
483 | 'url' => 'civicrm/batch', | |
484 | 'qs' => 'action=update&id=%%id%%&reset=1', | |
485 | 'title' => ts('Edit Batch'), | |
486 | ), | |
487 | CRM_Core_Action::DELETE => array( | |
488 | 'name' => ts('Delete'), | |
489 | 'url' => 'civicrm/batch', | |
490 | 'qs' => 'action=delete&id=%%id%%', | |
491 | 'title' => ts('Delete Batch'), | |
21dfd5f5 | 492 | ), |
6a488035 TO |
493 | ); |
494 | } | |
495 | return $links; | |
496 | } | |
497 | ||
498 | /** | |
eceb18cc | 499 | * Get batch list. |
6a488035 | 500 | * |
a6c01b45 | 501 | * @return array |
72b3a70c | 502 | * all batches excluding batches with data entry in progress |
6a488035 | 503 | */ |
00be9182 | 504 | public static function getBatches() { |
691df66d | 505 | $dataEntryStatusId = CRM_Core_OptionGroup::getValue('batch_status', 'Data Entry', 'name'); |
acb4ca2f | 506 | $query = "SELECT id, title |
6a488035 | 507 | FROM civicrm_batch |
acb4ca2f DG |
508 | WHERE item_count >= 1 |
509 | AND status_id != {$dataEntryStatusId} | |
510 | ORDER BY id DESC"; | |
6a488035 TO |
511 | |
512 | $batches = array(); | |
513 | $dao = CRM_Core_DAO::executeQuery($query); | |
481a74f4 | 514 | while ($dao->fetch()) { |
6a488035 TO |
515 | $batches[$dao->id] = $dao->title; |
516 | } | |
517 | return $batches; | |
518 | } | |
519 | ||
520 | ||
6a488035 | 521 | /** |
eceb18cc | 522 | * Calculate sum of all entries in a batch. |
6a488035 TO |
523 | * Used to validate and update item_count and total when closing an accounting batch |
524 | * | |
525 | * @param array $batchIds | |
526 | * @return array | |
527 | */ | |
00be9182 | 528 | public static function batchTotals($batchIds) { |
6a488035 TO |
529 | $totals = array_fill_keys($batchIds, array('item_count' => 0, 'total' => 0)); |
530 | if ($batchIds) { | |
531 | $sql = "SELECT eb.batch_id, COUNT(tx.id) AS item_count, SUM(tx.total_amount) AS total | |
532 | FROM civicrm_entity_batch eb | |
533 | INNER JOIN civicrm_financial_trxn tx ON tx.id = eb.entity_id AND eb.entity_table = 'civicrm_financial_trxn' | |
534 | WHERE eb.batch_id IN (" . implode(',', $batchIds) . ") | |
535 | GROUP BY eb.batch_id"; | |
536 | $dao = CRM_Core_DAO::executeQuery($sql); | |
537 | while ($dao->fetch()) { | |
538 | $totals[$dao->batch_id] = (array) $dao; | |
539 | } | |
540 | $dao->free(); | |
541 | } | |
542 | return $totals; | |
543 | } | |
544 | ||
545 | /** | |
eceb18cc | 546 | * Format markup for comparing two totals. |
6a488035 | 547 | * |
72b3a70c CW |
548 | * @param $actual |
549 | * calculated total | |
550 | * @param $expected | |
551 | * user-entered total | |
6a488035 TO |
552 | * @return array |
553 | */ | |
00be9182 | 554 | public static function displayTotals($actual, $expected) { |
6a488035 TO |
555 | $class = 'actual-value'; |
556 | if ($expected && $expected != $actual) { | |
557 | $class .= ' crm-error'; | |
558 | } | |
559 | $actualTitle = ts('Current Total'); | |
560 | $output = "<span class='$class' title='$actualTitle'>$actual</span>"; | |
561 | if ($expected) { | |
562 | $expectedTitle = ts('Expected Total'); | |
563 | $output .= " / <span class='expected-value' title='$expectedTitle'>$expected</span>"; | |
564 | } | |
565 | return $output; | |
566 | } | |
567 | ||
568 | /** | |
569 | * Function for exporting financial accounts, currently we support CSV and IIF format | |
570 | * @see http://wiki.civicrm.org/confluence/display/CRM/CiviAccounts+Specifications+-++Batches#CiviAccountsSpecifications-Batches-%C2%A0Overviewofimplementation | |
571 | * | |
82d480a5 TO |
572 | * @param array $batchIds |
573 | * Associated array of batch ids. | |
574 | * @param string $exportFormat | |
575 | * Export format. | |
6a488035 | 576 | */ |
00be9182 | 577 | public static function exportFinancialBatch($batchIds, $exportFormat) { |
6a488035 TO |
578 | if (empty($batchIds)) { |
579 | CRM_Core_Error::fatal(ts('No batches were selected.')); | |
580 | return; | |
581 | } | |
582 | if (empty($exportFormat)) { | |
583 | CRM_Core_Error::fatal(ts('No export format selected.')); | |
584 | return; | |
585 | } | |
586 | self::$_exportFormat = $exportFormat; | |
587 | ||
588 | // Instantiate appropriate exporter based on user-selected format. | |
589 | $exporterClass = "CRM_Financial_BAO_ExportFormat_" . self::$_exportFormat; | |
481a74f4 | 590 | if (class_exists($exporterClass)) { |
6a488035 TO |
591 | $exporter = new $exporterClass(); |
592 | } | |
593 | else { | |
594 | CRM_Core_Error::fatal("Could not locate exporter: $exporterClass"); | |
595 | } | |
596 | switch (self::$_exportFormat) { | |
597 | case 'CSV': | |
598 | foreach ($batchIds as $batchId) { | |
599 | $export[$batchId] = $exporter->generateExportQuery($batchId); | |
600 | } | |
601 | $exporter->makeCSV($export); | |
602 | break; | |
603 | ||
604 | case 'IIF': | |
605 | foreach ($batchIds as $batchId) { | |
606 | $export[$batchId] = $exporter->generateExportQuery($batchId); | |
607 | } | |
608 | $exporter->makeIIF($export); | |
609 | break; | |
610 | } | |
611 | } | |
612 | ||
e0ef6999 EM |
613 | /** |
614 | * @param array $batchIds | |
615 | * @param $status | |
616 | */ | |
00be9182 | 617 | public static function closeReOpen($batchIds = array(), $status) { |
f0ebbc90 | 618 | $batchStatus = CRM_Core_PseudoConstant::get('CRM_Batch_DAO_Batch', 'status_id'); |
481a74f4 TO |
619 | $params['status_id'] = CRM_Utils_Array::key($status, $batchStatus); |
620 | $session = CRM_Core_Session::singleton(); | |
6a488035 | 621 | $params['modified_date'] = date('YmdHis'); |
481a74f4 | 622 | $params['modified_id'] = $session->get('userID'); |
6a488035 TO |
623 | foreach ($batchIds as $key => $value) { |
624 | $params['id'] = $ids['batchID'] = $value; | |
625 | self::create($params, $ids); | |
626 | } | |
691df66d | 627 | $url = CRM_Utils_System::url('civicrm/financial/financialbatches', "reset=1&batchStatus={$params['status_id']}"); |
6a488035 TO |
628 | CRM_Utils_System::redirect($url); |
629 | } | |
630 | ||
631 | /** | |
eceb18cc | 632 | * Retrieve financial items assigned for a batch. |
6a488035 TO |
633 | * |
634 | * @param int $entityID | |
635 | * @param array $returnValues | |
72b3a70c | 636 | * @param bool $notPresent |
100fef9d | 637 | * @param array $params |
102fe859 EM |
638 | * @param bool $getCount |
639 | * | |
72b3a70c | 640 | * @return CRM_Core_DAO |
6a488035 | 641 | */ |
00be9182 | 642 | public static function getBatchFinancialItems($entityID, $returnValues, $notPresent = NULL, $params = NULL, $getCount = FALSE) { |
6a488035 TO |
643 | if (!$getCount) { |
644 | if (!empty($params['rowCount']) && | |
645 | $params['rowCount'] > 0 | |
646 | ) { | |
647 | $limit = " LIMIT {$params['offset']}, {$params['rowCount']} "; | |
648 | } | |
649 | } | |
650 | // action is taken depending upon the mode | |
651 | $select = 'civicrm_financial_trxn.id '; | |
481a74f4 | 652 | if (!empty($returnValues)) { |
92fcb95f | 653 | $select .= " , " . implode(' , ', $returnValues); |
6a488035 TO |
654 | } |
655 | ||
656 | $orderBy = " ORDER BY civicrm_financial_trxn.id"; | |
21d32567 DL |
657 | if (!empty($params['sort'])) { |
658 | $orderBy = ' ORDER BY ' . CRM_Utils_Type::escape($params['sort'], 'String'); | |
6a488035 TO |
659 | } |
660 | ||
661 | $from = "civicrm_financial_trxn | |
662 | LEFT JOIN civicrm_entity_financial_trxn ON civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id | |
cc55c603 DG |
663 | LEFT JOIN civicrm_entity_batch ON civicrm_entity_batch.entity_table = 'civicrm_financial_trxn' |
664 | AND civicrm_entity_batch.entity_id = civicrm_financial_trxn.id | |
6a488035 TO |
665 | LEFT JOIN civicrm_contribution ON civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id |
666 | LEFT JOIN civicrm_financial_type ON civicrm_financial_type.id = civicrm_contribution.financial_type_id | |
667 | LEFT JOIN civicrm_contact contact_a ON contact_a.id = civicrm_contribution.contact_id | |
668 | LEFT JOIN civicrm_contribution_soft ON civicrm_contribution_soft.contribution_id = civicrm_contribution.id | |
669 | "; | |
670 | ||
6c552737 TO |
671 | $searchFields = array( |
672 | 'sort_name', | |
673 | 'financial_type_id', | |
674 | 'contribution_page_id', | |
30d46f2f | 675 | 'payment_instrument_id', |
676 | 'contribution_trxn_id', | |
6c552737 TO |
677 | 'contribution_source', |
678 | 'contribution_currency_type', | |
679 | 'contribution_pay_later', | |
680 | 'contribution_recurring', | |
681 | 'contribution_test', | |
682 | 'contribution_thankyou_date_is_not_null', | |
683 | 'contribution_receipt_date_is_not_null', | |
684 | 'contribution_pcp_made_through_id', | |
685 | 'contribution_pcp_display_in_roll', | |
686 | 'contribution_date_relative', | |
687 | 'contribution_amount_low', | |
688 | 'contribution_amount_high', | |
689 | 'contribution_in_honor_of', | |
690 | 'contact_tags', | |
691 | 'group', | |
692 | 'contribution_date_relative', | |
693 | 'contribution_date_high', | |
694 | 'contribution_date_low', | |
695 | 'contribution_check_number', | |
696 | 'contribution_status_id', | |
697 | ); | |
6a488035 TO |
698 | $values = array(); |
699 | foreach ($searchFields as $field) { | |
700 | if (isset($params[$field])) { | |
701 | $values[$field] = $params[$field]; | |
702 | if ($field == 'sort_name') { | |
703 | $from .= " LEFT JOIN civicrm_contact contact_b ON contact_b.id = civicrm_contribution.contact_id | |
704 | LEFT JOIN civicrm_email ON contact_b.id = civicrm_email.contact_id"; | |
705 | } | |
706 | if ($field == 'contribution_in_honor_of') { | |
707 | $from .= " LEFT JOIN civicrm_contact contact_b ON contact_b.id = civicrm_contribution.contact_id"; | |
708 | } | |
709 | if ($field == 'contact_tags') { | |
710 | $from .= " LEFT JOIN civicrm_entity_tag `civicrm_entity_tag-{$params[$field]}` ON `civicrm_entity_tag-{$params[$field]}`.entity_id = contact_a.id"; | |
711 | } | |
712 | if ($field == 'group') { | |
713 | $from .= " LEFT JOIN civicrm_group_contact `civicrm_group_contact-{$params[$field]}` ON contact_a.id = `civicrm_group_contact-{$params[$field]}`.contact_id "; | |
714 | } | |
715 | if ($field == 'contribution_date_relative') { | |
716 | $relativeDate = explode('.', $params[$field]); | |
717 | $date = CRM_Utils_Date::relativeToAbsolute($relativeDate[0], $relativeDate[1]); | |
718 | $values['contribution_date_low'] = $date['from']; | |
719 | $values['contribution_date_high'] = $date['to']; | |
720 | } | |
721 | $searchParams = CRM_Contact_BAO_Query::convertFormValues($values); | |
722 | $query = new CRM_Contact_BAO_Query($searchParams, | |
723 | CRM_Contribute_BAO_Query::defaultReturnProperties(CRM_Contact_BAO_Query::MODE_CONTRIBUTE, | |
724 | FALSE | |
691df66d | 725 | ), NULL, FALSE, FALSE, CRM_Contact_BAO_Query::MODE_CONTRIBUTE |
6a488035 TO |
726 | ); |
727 | if ($field == 'contribution_date_high' || $field == 'contribution_date_low') { | |
728 | $query->dateQueryBuilder($params[$field], 'civicrm_contribution', 'contribution_date', 'receive_date', 'Contribution Date'); | |
729 | } | |
730 | } | |
731 | } | |
732 | if (!empty($query->_where[0])) { | |
0b0941e2 | 733 | $where = implode(' AND ', $query->_where[0]) . |
ba9d40b9 | 734 | " AND civicrm_entity_batch.batch_id IS NULL |
0b0941e2 | 735 | AND civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution'"; |
30d46f2f | 736 | $where = str_replace('civicrm_contribution.payment_instrument_id', 'civicrm_financial_trxn.payment_instrument_id', $where); |
6a488035 TO |
737 | $searchValue = TRUE; |
738 | } | |
739 | else { | |
740 | $searchValue = FALSE; | |
741 | } | |
742 | ||
743 | if (!$searchValue) { | |
744 | if (!$notPresent) { | |
691df66d | 745 | $where = " ( civicrm_entity_batch.batch_id = {$entityID} |
6a488035 TO |
746 | AND civicrm_entity_batch.entity_table = 'civicrm_financial_trxn' |
747 | AND civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution') "; | |
748 | } | |
749 | else { | |
750 | $where = " ( civicrm_entity_batch.batch_id IS NULL | |
751 | AND civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution')"; | |
752 | } | |
753 | } | |
754 | ||
0b0941e2 DL |
755 | $sql = " |
756 | SELECT {$select} | |
757 | FROM {$from} | |
758 | WHERE {$where} | |
759 | {$orderBy} | |
6a488035 TO |
760 | "; |
761 | ||
762 | if (isset($limit)) { | |
763 | $sql .= "{$limit}"; | |
764 | } | |
765 | ||
766 | $result = CRM_Core_DAO::executeQuery($sql); | |
767 | return $result; | |
768 | } | |
769 | ||
770 | /** | |
eceb18cc | 771 | * Get batch names. |
6a488035 TO |
772 | * @param string $batchIds |
773 | * | |
a6c01b45 CW |
774 | * @return array |
775 | * array of batches | |
6a488035 | 776 | */ |
00be9182 | 777 | public static function getBatchNames($batchIds) { |
6a488035 TO |
778 | $query = 'SELECT id, title |
779 | FROM civicrm_batch | |
86bfa4f6 | 780 | WHERE id IN (' . $batchIds . ')'; |
6a488035 TO |
781 | |
782 | $batches = array(); | |
783 | $dao = CRM_Core_DAO::executeQuery($query); | |
481a74f4 | 784 | while ($dao->fetch()) { |
6a488035 TO |
785 | $batches[$dao->id] = $dao->title; |
786 | } | |
787 | return $batches; | |
788 | } | |
789 | ||
790 | /** | |
eceb18cc | 791 | * Function get batch statuses. |
6a488035 TO |
792 | * |
793 | * @param string $batchIds | |
794 | * | |
a6c01b45 CW |
795 | * @return array |
796 | * array of batches | |
6a488035 | 797 | */ |
00be9182 | 798 | public static function getBatchStatuses($batchIds) { |
6a488035 TO |
799 | $query = 'SELECT id, status_id |
800 | FROM civicrm_batch | |
92fcb95f | 801 | WHERE id IN (' . $batchIds . ')'; |
6a488035 TO |
802 | |
803 | $batches = array(); | |
804 | $dao = CRM_Core_DAO::executeQuery($query); | |
481a74f4 | 805 | while ($dao->fetch()) { |
6a488035 TO |
806 | $batches[$dao->id] = $dao->status_id; |
807 | } | |
808 | return $batches; | |
809 | } | |
96025800 | 810 | |
6a488035 | 811 | } |