don't allow multiple currencies in a batch
[civicrm-core.git] / CRM / Batch / BAO / EntityBatch.php
CommitLineData
a646bdd2
SB
1<?php
2/*
3 +--------------------------------------------------------------------+
bc77d7c0 4 | Copyright CiviCRM LLC. All rights reserved. |
a646bdd2 5 | |
bc77d7c0
TO
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 |
a646bdd2
SB
9 +--------------------------------------------------------------------+
10 */
11
12/**
13 *
14 * @package CRM
ca5cec67 15 * @copyright CiviCRM LLC https://civicrm.org/licensing
a646bdd2
SB
16 */
17class CRM_Batch_BAO_EntityBatch extends CRM_Batch_DAO_EntityBatch {
18
ee20d7be
PN
19 /**
20 * Create entity batch entry.
21 *
22 * @param array $params
6a5fec96 23 * @return CRM_Batch_DAO_EntityBatch
ee20d7be 24 */
6a5fec96 25 public static function create($params) {
df33c0cd
JG
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 }
6a5fec96 55 return self::writeRecord($params);
ee20d7be
PN
56 }
57
58 /**
59 * Remove entries from entity batch.
e989fc66 60 * @param array|int $params
99f76265 61 * @deprecated
ee20d7be
PN
62 * @return CRM_Batch_DAO_EntityBatch
63 */
64 public static function del($params) {
e989fc66 65 if (!is_array($params)) {
be2fb01f 66 $params = ['id' => $params];
e989fc66 67 }
99f76265 68 return self::deleteRecord($params);
ee20d7be
PN
69 }
70
df33c0cd
JG
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
a646bdd2 95}