Merge pull request #22242 from MegaphoneJon/reporting-85
[civicrm-core.git] / CRM / Member / BAO / MembershipPayment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17 class CRM_Member_BAO_MembershipPayment extends CRM_Member_DAO_MembershipPayment {
18
19 /**
20 * Add the membership Payments.
21 *
22 * @param array $params
23 * Reference array contains the values submitted by the form.
24 *
25 *
26 * @return object
27 */
28 public static function create($params) {
29 $hook = empty($params['id']) ? 'create' : 'edit';
30 CRM_Utils_Hook::pre($hook, 'MembershipPayment', CRM_Utils_Array::value('id', $params), $params);
31 $dao = new CRM_Member_DAO_MembershipPayment();
32 $dao->copyValues($params);
33 // We check for membership_id in case we are being called too early in the process. This is
34 // cludgey but is part of the deprecation process (ie. we are trying to do everything
35 // from LineItem::create with a view to eventually removing this fn & the table.
36 if (!civicrm_api3('Membership', 'getcount', ['id' => $params['membership_id']])) {
37 return $dao;
38 }
39
40 //Fixed for avoiding duplicate entry error when user goes
41 //back and forward during payment mode is notify
42 if (!$dao->find(TRUE)) {
43 $dao->save();
44 }
45 CRM_Utils_Hook::post($hook, 'MembershipPayment', $dao->id, $dao);
46 // CRM-14197 we are in the process on phasing out membershipPayment in favour of storing both contribution_id & entity_id (membership_id) on the line items
47 // table. However, at this stage we have both - there is still quite a bit of refactoring to do to set the line_iten entity_id right the first time
48 // however, we can assume at this stage that any contribution id will have only one line item with that membership type in the line item table
49 // OR the caller will have taken responsibility for updating the line items themselves so we will update using SQL here
50 if (!empty($params['isSkipLineItem'])) {
51 // Caller has taken responsibility for updating the line items.
52 return $dao;
53 }
54 if (!isset($params['membership_type_id'])) {
55 $membership_type_id = civicrm_api3('membership', 'getvalue', [
56 'id' => $dao->membership_id,
57 'return' => 'membership_type_id',
58 ]);
59 }
60 else {
61 $membership_type_id = $params['membership_type_id'];
62 }
63 $sql = "UPDATE civicrm_line_item li
64 LEFT JOIN civicrm_price_field_value pv ON pv.id = li.price_field_value_id
65 SET entity_table = 'civicrm_membership', entity_id = %1
66 WHERE pv.membership_type_id = %2
67 AND contribution_id = %3";
68 CRM_Core_DAO::executeQuery($sql, [
69 1 => [$dao->membership_id, 'Integer'],
70 2 => [$membership_type_id, 'Integer'],
71 3 => [$dao->contribution_id, 'Integer'],
72 ]);
73 return $dao;
74 }
75
76 /**
77 * Delete membership Payments.
78 *
79 * @param int $id
80 * @deprecated
81 * @return bool
82 */
83 public static function del($id) {
84 return (bool) self::deleteRecord(['id' => $id]);
85 }
86
87 }