codespell: CRM/*
[civicrm-core.git] / CRM / Event / BAO / ParticipantPayment.php
CommitLineData
6a488035
TO
1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
6a488035 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 32 * @copyright CiviCRM LLC (c) 2004-2015
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
44 * with one values of id for this participantPayment record (for update).
6a488035 45 *
a6c01b45
CW
46 * @return object
47 * the partcipant payment record
6a488035 48 */
00be9182 49 public static function create(&$params, &$ids) {
6a488035
TO
50 if (isset($ids['id'])) {
51 CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $ids['id'], $params);
52 }
53 else {
54 CRM_Utils_Hook::pre('create', 'ParticipantPayment', NULL, $params);
55 }
56
57 $participantPayment = new CRM_Event_BAO_ParticipantPayment();
58 $participantPayment->copyValues($params);
59 if (isset($ids['id'])) {
60 $participantPayment->id = CRM_Utils_Array::value('id', $ids);
0479b4c8 61 }
6a488035
TO
62 else {
63 $participantPayment->find(TRUE);
64 }
65 $participantPayment->save();
66
67 if (isset($ids['id'])) {
68 CRM_Utils_Hook::post('edit', 'ParticipantPayment', $ids['id'], $participantPayment);
69 }
70 else {
71 CRM_Utils_Hook::post('create', 'ParticipantPayment', NULL, $participantPayment);
72 }
73
86cbdcb1
EM
74 //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
75 // but if they have a single line item for the contribution we can assume it should refer to the participant line
353ffa53
TO
76 $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", array(
77 1 => array(
78 $participantPayment->contribution_id,
acb1052e
WA
79 'Integer',
80 ),
353ffa53 81 ));
22e263ad 82 if ($lineItemCount == 1) {
86cbdcb1
EM
83 $sql = "UPDATE civicrm_line_item li
84 SET entity_table = 'civicrm_participant', entity_id = %1
85 WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'";
353ffa53
TO
86 CRM_Core_DAO::executeQuery($sql, array(
87 1 => array($participantPayment->participant_id, 'Integer'),
acb1052e 88 2 => array($participantPayment->contribution_id, 'Integer'),
353ffa53 89 ));
86cbdcb1
EM
90 }
91
6a488035
TO
92 return $participantPayment;
93 }
94
95 /**
66f9e52b 96 * Delete the record that are associated with this ParticipantPayment.
6a488035
TO
97 * Also deletes the associated contribution for this participant
98 *
d4dd1e85
TO
99 * @param array $params
100 * Associative array whose values match the record to be deleted.
6a488035 101 *
acb1052e 102 * @return bool
a6c01b45 103 * true if deleted false otherwise
6a488035 104 */
00be9182 105 public static function deleteParticipantPayment($params) {
6a488035
TO
106 $participantPayment = new CRM_Event_DAO_ParticipantPayment();
107
108 $valid = FALSE;
109 foreach ($params as $field => $value) {
110 if (!empty($value)) {
111 $participantPayment->$field = $value;
112 $valid = TRUE;
113 }
114 }
115
116 if (!$valid) {
117 CRM_Core_Error::fatal();
118 }
119
120 if ($participantPayment->find(TRUE)) {
121 CRM_Utils_Hook::pre('delete', 'ParticipantPayment', $participantPayment->id, $params);
122 CRM_Contribute_BAO_Contribution::deleteContribution($participantPayment->contribution_id);
123 $participantPayment->delete();
124 CRM_Utils_Hook::post('delete', 'ParticipantPayment', $participantPayment->id, $participantPayment);
125 return $participantPayment;
126 }
127 return FALSE;
128 }
96025800 129
6a488035 130}