fix some spelling:
[civicrm-core.git] / CRM / Event / BAO / ParticipantPayment.php
index 663d0fb55558601e73125fb10b711771f089421d..101448314977aa04618af718ae62b740236bd5c8 100644 (file)
@@ -1,7 +1,7 @@
 <?php
 /*
  +--------------------------------------------------------------------+
- | CiviCRM version 4.5                                                |
+ | CiviCRM version 4.6                                                |
  +--------------------------------------------------------------------+
  | Copyright CiviCRM LLC (c) 2004-2014                                |
  +--------------------------------------------------------------------+
@@ -38,13 +38,16 @@ class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment
   /**
    * Creates or updates a participant payment record
    *
-   * @param $params array of values to initialize the record with
-   * @param $ids    array with one values of id for this participantPayment record (for update)
+   * @param array $params
+   *   of values to initialize the record with.
+   * @param array $ids
+   *   with one values of id for this participantPayment record (for update).
    *
-   * @return object the partcipant payment record
+   * @return object
+   *   the partcipant payment record
    * @static
    */
-  static function create(&$params, &$ids) {
+  public static function create(&$params, &$ids) {
     if (isset($ids['id'])) {
       CRM_Utils_Hook::pre('edit', 'ParticipantPayment', $ids['id'], $params);
     }
@@ -56,7 +59,7 @@ class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment
     $participantPayment->copyValues($params);
     if (isset($ids['id'])) {
       $participantPayment->id = CRM_Utils_Array::value('id', $ids);
-  }
+    }
     else {
       $participantPayment->find(TRUE);
     }
@@ -69,6 +72,24 @@ class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment
       CRM_Utils_Hook::post('create', 'ParticipantPayment', NULL, $participantPayment);
     }
 
+    //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
+    // but if they have a single line item for the contribution we can assume it should refer to the participant line
+    $lineItemCount = CRM_Core_DAO::singleValueQuery("select count(*) FROM civicrm_line_item WHERE contribution_id = %1", array(
+        1 => array(
+          $participantPayment->contribution_id,
+          'Integer'
+        )
+      ));
+    if ($lineItemCount == 1) {
+      $sql = "UPDATE civicrm_line_item li
+      SET entity_table = 'civicrm_participant', entity_id = %1
+      WHERE contribution_id = %2 AND entity_table = 'civicrm_contribution'";
+      CRM_Core_DAO::executeQuery($sql, array(
+          1 => array($participantPayment->participant_id, 'Integer'),
+          2 => array($participantPayment->contribution_id, 'Integer')
+        ));
+    }
+
     return $participantPayment;
   }
 
@@ -76,13 +97,14 @@ class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment
    * Delete the record that are associated with this ParticipantPayment
    * Also deletes the associated contribution for this participant
    *
-   * @param  array  $params   associative array whose values match the record to be deleted
+   * @param array $params
+   *   Associative array whose values match the record to be deleted.
    *
-   * @return boolean  true if deleted false otherwise
+   * @return boolean
+   *   true if deleted false otherwise
    * @static
-   * @access public
    */
-  static function deleteParticipantPayment($params) {
+  public static function deleteParticipantPayment($params) {
     $participantPayment = new CRM_Event_DAO_ParticipantPayment();
 
     $valid = FALSE;
@@ -107,4 +129,3 @@ class CRM_Event_BAO_ParticipantPayment extends CRM_Event_DAO_ParticipantPayment
     return FALSE;
   }
 }
-