Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
bc77d7c0 | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 TO |
11 | |
12 | /** | |
13 | * | |
14 | * | |
15 | * @package CRM | |
ca5cec67 | 16 | * @copyright CiviCRM LLC https://civicrm.org/licensing |
6a488035 TO |
17 | * $Id$ |
18 | * | |
19 | */ | |
20 | class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment { | |
21 | ||
22 | /** | |
66f9e52b | 23 | * Creates or updates a participant payment record. |
6a488035 | 24 | * |
5a4f6742 CW |
25 | * @param array $params |
26 | * of values to initialize the record with. | |
27 | * @param array $ids | |
a5750507 | 28 | * deprecated array. |
6a488035 | 29 | * |
a6c01b45 CW |
30 | * @return object |
31 | * the partcipant payment record | |
6a488035 | 32 | */ |
a5750507 | 33 | public static function create(&$params, $ids = []) { |
34 | $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids)); | |
35 | if ($id) { | |
36 | CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $id, $params); | |
6a488035 TO |
37 | } |
38 | else { | |
39 | CRM_Utils_Hook::pre('create', 'ParticipantPayment', NULL, $params); | |
40 | } | |
41 | ||
42 | $participantPayment = new CRM_Event_BAO_ParticipantPayment(); | |
43 | $participantPayment->copyValues($params); | |
a5750507 | 44 | if ($id) { |
45 | $participantPayment->id = $id; | |
0479b4c8 | 46 | } |
6a488035 TO |
47 | else { |
48 | $participantPayment->find(TRUE); | |
49 | } | |
50 | $participantPayment->save(); | |
51 | ||
a5750507 | 52 | if (empty($participantPayment->contribution_id)) { |
53 | // For an id update contribution_id may be unknown. We want it | |
54 | // further down so perhaps get it before the hooks. | |
55 | $participantPayment->find(TRUE); | |
56 | } | |
57 | if ($id) { | |
54334799 | 58 | CRM_Utils_Hook::post('edit', 'ParticipantPayment', $participantPayment->id, $participantPayment); |
6a488035 TO |
59 | } |
60 | else { | |
54334799 | 61 | CRM_Utils_Hook::post('create', 'ParticipantPayment', $participantPayment->id, $participantPayment); |
6a488035 TO |
62 | } |
63 | ||
86cbdcb1 EM |
64 | //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 |
65 | // but if they have a single line item for the contribution we can assume it should refer to the participant line | |
be2fb01f | 66 | $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", [ |
90b461f1 SL |
67 | 1 => [ |
68 | $participantPayment->contribution_id, | |
69 | 'Integer', | |
70 | ], | |
71 | ]); | |
22e263ad | 72 | if ($lineItemCount == 1) { |
86cbdcb1 EM |
73 | $sql = "UPDATE civicrm_line_item li |
74 | SET entity_table = 'civicrm_participant', entity_id = %1 | |
75 | WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'"; | |
be2fb01f | 76 | CRM_Core_DAO::executeQuery($sql, [ |
90b461f1 SL |
77 | 1 => [$participantPayment->participant_id, 'Integer'], |
78 | 2 => [$participantPayment->contribution_id, 'Integer'], | |
79 | ]); | |
86cbdcb1 EM |
80 | } |
81 | ||
6a488035 TO |
82 | return $participantPayment; |
83 | } | |
84 | ||
85 | /** | |
66f9e52b | 86 | * Delete the record that are associated with this ParticipantPayment. |
6a488035 TO |
87 | * Also deletes the associated contribution for this participant |
88 | * | |
d4dd1e85 TO |
89 | * @param array $params |
90 | * Associative array whose values match the record to be deleted. | |
6a488035 | 91 | * |
acb1052e | 92 | * @return bool |
a6c01b45 | 93 | * true if deleted false otherwise |
6a488035 | 94 | */ |
00be9182 | 95 | public static function deleteParticipantPayment($params) { |
6a488035 TO |
96 | $participantPayment = new CRM_Event_DAO_ParticipantPayment(); |
97 | ||
98 | $valid = FALSE; | |
99 | foreach ($params as $field => $value) { | |
100 | if (!empty($value)) { | |
101 | $participantPayment->$field = $value; | |
102 | $valid = TRUE; | |
103 | } | |
104 | } | |
105 | ||
106 | if (!$valid) { | |
107 | CRM_Core_Error::fatal(); | |
108 | } | |
109 | ||
110 | if ($participantPayment->find(TRUE)) { | |
111 | CRM_Utils_Hook::pre('delete', 'ParticipantPayment', $participantPayment->id, $params); | |
112 | CRM_Contribute_BAO_Contribution::deleteContribution($participantPayment->contribution_id); | |
113 | $participantPayment->delete(); | |
114 | CRM_Utils_Hook::post('delete', 'ParticipantPayment', $participantPayment->id, $participantPayment); | |
115 | return $participantPayment; | |
116 | } | |
117 | return FALSE; | |
118 | } | |
96025800 | 119 | |
6a488035 | 120 | } |