Merge pull request #4897 from totten/master-cleanup2
[civicrm-core.git] / CRM / Event / BAO / ParticipantPayment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 *
30 *
31 * @package CRM
32 * @copyright CiviCRM LLC (c) 2004-2014
33 * $Id$
34 *
35 */
36 class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment {
37
38 /**
39 * Creates or updates a participant payment record
40 *
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).
45 *
46 * @return object
47 * the partcipant payment record
48 * @static
49 */
50 public static function create(&$params, &$ids) {
51 if (isset($ids['id'])) {
52 CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $ids['id'], $params);
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);
60 if (isset($ids['id'])) {
61 $participantPayment->id = CRM_Utils_Array::value('id', $ids);
62 }
63 else {
64 $participantPayment->find(TRUE);
65 }
66 $participantPayment->save();
67
68 if (isset($ids['id'])) {
69 CRM_Utils_Hook::post('edit', 'ParticipantPayment', $ids['id'], $participantPayment);
70 }
71 else {
72 CRM_Utils_Hook::post('create', 'ParticipantPayment', NULL, $participantPayment);
73 }
74
75 //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
76 // but if they have a single line item for the contribution we can assume it should refer to the participant line
77 $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", array(
78 1 => array(
79 $participantPayment->contribution_id,
80 'Integer'
81 )
82 ));
83 if ($lineItemCount == 1) {
84 $sql = "UPDATE civicrm_line_item li
85 SET entity_table = 'civicrm_participant', entity_id = %1
86 WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'";
87 CRM_Core_DAO::executeQuery($sql, array(
88 1 => array($participantPayment->participant_id, 'Integer'),
89 2 => array($participantPayment->contribution_id, 'Integer')
90 ));
91 }
92
93 return $participantPayment;
94 }
95
96 /**
97 * Delete the record that are associated with this ParticipantPayment
98 * Also deletes the associated contribution for this participant
99 *
100 * @param array $params
101 * Associative array whose values match the record to be deleted.
102 *
103 * @return boolean
104 * true if deleted false otherwise
105 * @static
106 */
107 public static function deleteParticipantPayment($params) {
108 $participantPayment = new CRM_Event_DAO_ParticipantPayment();
109
110 $valid = FALSE;
111 foreach ($params as $field => $value) {
112 if (!empty($value)) {
113 $participantPayment->$field = $value;
114 $valid = TRUE;
115 }
116 }
117
118 if (!$valid) {
119 CRM_Core_Error::fatal();
120 }
121
122 if ($participantPayment->find(TRUE)) {
123 CRM_Utils_Hook::pre('delete', 'ParticipantPayment', $participantPayment->id, $params);
124 CRM_Contribute_BAO_Contribution::deleteContribution($participantPayment->contribution_id);
125 $participantPayment->delete();
126 CRM_Utils_Hook::post('delete', 'ParticipantPayment', $participantPayment->id, $participantPayment);
127 return $participantPayment;
128 }
129 return FALSE;
130 }
131 }