Merge pull request #15833 from yashodha/participant_edit
[civicrm-core.git] / CRM / Event / BAO / ParticipantPayment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
fee14197 4 | CiviCRM version 5 |
6a488035 5 +--------------------------------------------------------------------+
f299f7db 6 | Copyright CiviCRM LLC (c) 2004-2020 |
6a488035
TO
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
d25dd0ee 26 */
6a488035
TO
27
28/**
29 *
30 *
31 * @package CRM
f299f7db 32 * @copyright CiviCRM LLC (c) 2004-2020
6a488035
TO
33 * $Id$
34 *
35 */
36class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment {
37
38 /**
66f9e52b 39 * Creates or updates a participant payment record.
6a488035 40 *
5a4f6742
CW
41 * @param array $params
42 * of values to initialize the record with.
43 * @param array $ids
a5750507 44 * deprecated array.
6a488035 45 *
a6c01b45
CW
46 * @return object
47 * the partcipant payment record
6a488035 48 */
a5750507 49 public static function create(&$params, $ids = []) {
50 $id = CRM_Utils_Array::value('id', $params, CRM_Utils_Array::value('id', $ids));
51 if ($id) {
52 CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $id, $params);
6a488035
TO
53 }
54 else {
55 CRM_Utils_Hook::pre('create', 'ParticipantPayment', NULL, $params);
56 }
57
58 $participantPayment = new CRM_Event_BAO_ParticipantPayment();
59 $participantPayment->copyValues($params);
a5750507 60 if ($id) {
61 $participantPayment->id = $id;
0479b4c8 62 }
6a488035
TO
63 else {
64 $participantPayment->find(TRUE);
65 }
66 $participantPayment->save();
67
a5750507 68 if (empty($participantPayment->contribution_id)) {
69 // For an id update contribution_id may be unknown. We want it
70 // further down so perhaps get it before the hooks.
71 $participantPayment->find(TRUE);
72 }
73 if ($id) {
54334799 74 CRM_Utils_Hook::post('edit', 'ParticipantPayment', $participantPayment->id, $participantPayment);
6a488035
TO
75 }
76 else {
54334799 77 CRM_Utils_Hook::post('create', 'ParticipantPayment', $participantPayment->id, $participantPayment);
6a488035
TO
78 }
79
86cbdcb1
EM
80 //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
81 // but if they have a single line item for the contribution we can assume it should refer to the participant line
be2fb01f 82 $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", [
90b461f1
SL
83 1 => [
84 $participantPayment->contribution_id,
85 'Integer',
86 ],
87 ]);
22e263ad 88 if ($lineItemCount == 1) {
86cbdcb1
EM
89 $sql = "UPDATE civicrm_line_item li
90 SET entity_table = 'civicrm_participant', entity_id = %1
91 WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'";
be2fb01f 92 CRM_Core_DAO::executeQuery($sql, [
90b461f1
SL
93 1 => [$participantPayment->participant_id, 'Integer'],
94 2 => [$participantPayment->contribution_id, 'Integer'],
95 ]);
86cbdcb1
EM
96 }
97
6a488035
TO
98 return $participantPayment;
99 }
100
101 /**
66f9e52b 102 * Delete the record that are associated with this ParticipantPayment.
6a488035
TO
103 * Also deletes the associated contribution for this participant
104 *
d4dd1e85
TO
105 * @param array $params
106 * Associative array whose values match the record to be deleted.
6a488035 107 *
acb1052e 108 * @return bool
a6c01b45 109 * true if deleted false otherwise
6a488035 110 */
00be9182 111 public static function deleteParticipantPayment($params) {
6a488035
TO
112 $participantPayment = new CRM_Event_DAO_ParticipantPayment();
113
114 $valid = FALSE;
115 foreach ($params as $field => $value) {
116 if (!empty($value)) {
117 $participantPayment->$field = $value;
118 $valid = TRUE;
119 }
120 }
121
122 if (!$valid) {
123 CRM_Core_Error::fatal();
124 }
125
126 if ($participantPayment->find(TRUE)) {
127 CRM_Utils_Hook::pre('delete', 'ParticipantPayment', $participantPayment->id, $params);
128 CRM_Contribute_BAO_Contribution::deleteContribution($participantPayment->contribution_id);
129 $participantPayment->delete();
130 CRM_Utils_Hook::post('delete', 'ParticipantPayment', $participantPayment->id, $participantPayment);
131 return $participantPayment;
132 }
133 return FALSE;
134 }
96025800 135
6a488035 136}