Merge pull request #21583 from mattwire/propertybagsimplebits
[civicrm-core.git] / CRM / Batch / BAO / EntityBatch.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_Batch_BAO_EntityBatch extends CRM_Batch_DAO_EntityBatch {
18
19 /**
20 * Create entity batch entry.
21 *
22 * @param array $params
23 * @return CRM_Batch_DAO_EntityBatch
24 */
25 public static function create($params) {
26 // Only write the EntityBatch record if the financial trxn and batch match on currency and payment instrument.
27 $batchId = $params['batch_id'] ?? NULL;
28 $entityId = $params['entity_id'] ?? NULL;
29 // Not having a batch ID and entity ID is only acceptable on an update.
30 if (!$batchId) {
31 $existingEntityBatch = \Civi\Api4\EntityBatch::get(FALSE)
32 ->addSelect('id', '=', $params['id'])
33 ->execute()
34 ->first();
35 $batchId = $existingEntityBatch['batch_id'] ?? NULL;
36 $entityId = $existingEntityBatch['entity_id'] ?? NULL;
37 }
38 // There should never be a legitimate case where a record has an ID but no batch ID but SyntaxConformanceTest says otherwise.
39 if ($batchId) {
40 $batchCurrency = self::getBatchCurrency($batchId);
41 $batchPID = (int) CRM_Core_DAO::getFieldValue('CRM_Batch_DAO_Batch', $batchId, 'payment_instrument_id');
42 $trxn = \Civi\Api4\FinancialTrxn::get(FALSE)
43 ->addSelect('currency', 'payment_instrument_id')
44 ->addWhere('id', '=', $entityId)
45 ->execute()
46 ->first();
47 if ($batchCurrency && $batchCurrency !== $trxn['currency']) {
48 throw new \CRM_Core_Exception(ts('You can not add items of two different currencies to a single contribution batch.'));
49 }
50 if ($batchPID && $trxn && $batchPID !== $trxn['payment_instrument_id']) {
51 $paymentInstrument = CRM_Core_PseudoConstant::getLabel('CRM_Batch_BAO_Batch', 'payment_instrument_id', $batchPID);
52 throw new \CRM_Core_Exception(ts('This batch is configured to include only transactions using %1 payment method. If you want to include other transactions, please edit the batch first and modify the Payment Method.', [1 => $paymentInstrument]));
53 }
54 }
55 return self::writeRecord($params);
56 }
57
58 /**
59 * Remove entries from entity batch.
60 * @param array|int $params
61 * @deprecated
62 * @return CRM_Batch_DAO_EntityBatch
63 */
64 public static function del($params) {
65 if (!is_array($params)) {
66 $params = ['id' => $params];
67 }
68 return self::deleteRecord($params);
69 }
70
71 /**
72 * Get the currency associated with a batch (if any).
73 *
74 * @param int $batchId
75 *
76 */
77 public static function getBatchCurrency($batchId) : ?string {
78 $sql = "SELECT DISTINCT ft.currency
79 FROM civicrm_batch batch
80 JOIN civicrm_entity_batch eb
81 ON batch.id = eb.batch_id
82 JOIN civicrm_financial_trxn ft
83 ON eb.entity_id = ft.id
84 WHERE batch.id = %1";
85 $dao = CRM_Core_DAO::executeQuery($sql, [1 => [$batchId, 'Positive']]);
86 if ($dao->N === 0) {
87 return NULL;
88 }
89 else {
90 $dao->fetch();
91 return $dao->currency;
92 }
93 }
94
95 }