| 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 | * |
| 15 | * @package CRM |
| 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
| 17 | */ |
| 18 | class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment { |
| 19 | |
| 20 | /** |
| 21 | * Creates or updates a participant payment record. |
| 22 | * |
| 23 | * @param array $params |
| 24 | * of values to initialize the record with. |
| 25 | * @param array $ids |
| 26 | * deprecated array. |
| 27 | * |
| 28 | * @return object |
| 29 | * the partcipant payment record |
| 30 | */ |
| 31 | public static function create(&$params, $ids = []) { |
| 32 | $id = $params['id'] ?? $ids['id'] ?? NULL; |
| 33 | if ($id) { |
| 34 | CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $id, $params); |
| 35 | } |
| 36 | else { |
| 37 | CRM_Utils_Hook::pre('create', 'ParticipantPayment', NULL, $params); |
| 38 | } |
| 39 | |
| 40 | $participantPayment = new CRM_Event_BAO_ParticipantPayment(); |
| 41 | $participantPayment->copyValues($params); |
| 42 | if ($id) { |
| 43 | $participantPayment->id = $id; |
| 44 | } |
| 45 | else { |
| 46 | $participantPayment->find(TRUE); |
| 47 | } |
| 48 | $participantPayment->save(); |
| 49 | |
| 50 | if (empty($participantPayment->contribution_id)) { |
| 51 | // For an id update contribution_id may be unknown. We want it |
| 52 | // further down so perhaps get it before the hooks. |
| 53 | $participantPayment->find(TRUE); |
| 54 | } |
| 55 | if ($id) { |
| 56 | CRM_Utils_Hook::post('edit', 'ParticipantPayment', $participantPayment->id, $participantPayment); |
| 57 | } |
| 58 | else { |
| 59 | CRM_Utils_Hook::post('create', 'ParticipantPayment', $participantPayment->id, $participantPayment); |
| 60 | } |
| 61 | |
| 62 | //generally if people are creating participant_payments via the api they won't be setting the line item correctly - we can't help them if they are doing complex transactions |
| 63 | // but if they have a single line item for the contribution we can assume it should refer to the participant line |
| 64 | $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", [ |
| 65 | 1 => [ |
| 66 | $participantPayment->contribution_id, |
| 67 | 'Integer', |
| 68 | ], |
| 69 | ]); |
| 70 | if ($lineItemCount == 1) { |
| 71 | $sql = "UPDATE civicrm_line_item li |
| 72 | SET entity_table = 'civicrm_participant', entity_id = %1 |
| 73 | WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'"; |
| 74 | CRM_Core_DAO::executeQuery($sql, [ |
| 75 | 1 => [$participantPayment->participant_id, 'Integer'], |
| 76 | 2 => [$participantPayment->contribution_id, 'Integer'], |
| 77 | ]); |
| 78 | } |
| 79 | |
| 80 | return $participantPayment; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Delete the record that are associated with this ParticipantPayment. |
| 85 | * Also deletes the associated contribution for this participant |
| 86 | * |
| 87 | * @param array $params |
| 88 | * Associative array whose values match the record to be deleted. |
| 89 | * |
| 90 | * @return bool |
| 91 | * true if deleted false otherwise |
| 92 | */ |
| 93 | public static function deleteParticipantPayment($params) { |
| 94 | $participantPayment = new CRM_Event_DAO_ParticipantPayment(); |
| 95 | |
| 96 | $valid = FALSE; |
| 97 | foreach ($params as $field => $value) { |
| 98 | if (!empty($value)) { |
| 99 | $participantPayment->$field = $value; |
| 100 | $valid = TRUE; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if (!$valid) { |
| 105 | throw new CRM_Core_Exception('Cannot delete participant payment'); |
| 106 | } |
| 107 | |
| 108 | if ($participantPayment->find(TRUE)) { |
| 109 | CRM_Utils_Hook::pre('delete', 'ParticipantPayment', $participantPayment->id, $params); |
| 110 | CRM_Contribute_BAO_Contribution::deleteContribution($participantPayment->contribution_id); |
| 111 | $participantPayment->delete(); |
| 112 | CRM_Utils_Hook::post('delete', 'ParticipantPayment', $participantPayment->id, $participantPayment); |
| 113 | return $participantPayment; |
| 114 | } |
| 115 | return FALSE; |
| 116 | } |
| 117 | |
| 118 | } |