| 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 $params array of values to initialize the record with |
| 42 | * @param $ids array with one values of id for this participantPayment record (for update) |
| 43 | * |
| 44 | * @return object the partcipant payment record |
| 45 | * @static |
| 46 | */ |
| 47 | static function create(&$params, &$ids) { |
| 48 | if (isset($ids['id'])) { |
| 49 | CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $ids['id'], $params); |
| 50 | } |
| 51 | else { |
| 52 | CRM_Utils_Hook::pre('create', 'ParticipantPayment', NULL, $params); |
| 53 | } |
| 54 | |
| 55 | $participantPayment = new CRM_Event_BAO_ParticipantPayment(); |
| 56 | $participantPayment->copyValues($params); |
| 57 | if (isset($ids['id'])) { |
| 58 | $participantPayment->id = CRM_Utils_Array::value('id', $ids); |
| 59 | } |
| 60 | else { |
| 61 | $participantPayment->find(TRUE); |
| 62 | } |
| 63 | $participantPayment->save(); |
| 64 | |
| 65 | if (isset($ids['id'])) { |
| 66 | CRM_Utils_Hook::post('edit', 'ParticipantPayment', $ids['id'], $participantPayment); |
| 67 | } |
| 68 | else { |
| 69 | CRM_Utils_Hook::post('create', 'ParticipantPayment', NULL, $participantPayment); |
| 70 | } |
| 71 | |
| 72 | //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 |
| 73 | // but if they have a single line item for the contribution we can assume it should refer to the participant line |
| 74 | $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", array(1 => array($participantPayment->contribution_id, 'Integer'))); |
| 75 | if($lineItemCount == 1) { |
| 76 | $sql = "UPDATE civicrm_line_item li |
| 77 | SET entity_table = 'civicrm_participant', entity_id = %1 |
| 78 | WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'"; |
| 79 | CRM_Core_DAO::executeQuery($sql, array(1 => array($participantPayment->participant_id, 'Integer'), 2 => array($participantPayment->contribution_id, 'Integer'))); |
| 80 | } |
| 81 | |
| 82 | return $participantPayment; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Delete the record that are associated with this ParticipantPayment |
| 87 | * Also deletes the associated contribution for this participant |
| 88 | * |
| 89 | * @param array $params associative array whose values match the record to be deleted |
| 90 | * |
| 91 | * @return boolean true if deleted false otherwise |
| 92 | * @static |
| 93 | * @access public |
| 94 | */ |
| 95 | static function deleteParticipantPayment($params) { |
| 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 | } |
| 119 | } |
| 120 | |