3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 class CRM_Batch_BAO_EntityBatch
extends CRM_Batch_DAO_EntityBatch
{
20 * Create entity batch entry.
22 * @param array $params
23 * @return CRM_Batch_DAO_EntityBatch
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.
31 $existingEntityBatch = \Civi\Api4\EntityBatch
::get(FALSE)
32 ->addSelect('id', '=', $params['id'])
35 $batchId = $existingEntityBatch['batch_id'] ??
NULL;
36 $entityId = $existingEntityBatch['entity_id'] ??
NULL;
38 // There should never be a legitimate case where a record has an ID but no batch ID but SyntaxConformanceTest says otherwise.
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)
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.'));
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]));
55 return self
::writeRecord($params);
59 * Remove entries from entity batch.
60 * @param array|int $params
62 * @return CRM_Batch_DAO_EntityBatch
64 public static function del($params) {
65 if (!is_array($params)) {
66 $params = ['id' => $params];
68 return self
::deleteRecord($params);
72 * Get the currency associated with a batch (if any).
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
85 $dao = CRM_Core_DAO
::executeQuery($sql, [1 => [$batchId, 'Positive']]);
91 return $dao->currency
;