Merge pull request #17435 from seamuslee001/dev_membership_24_revert
[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 * $Id$
17 *
18 */
19 class CRM_Member_BAO_MembershipPayment extends CRM_Member_DAO_MembershipPayment {
20
21 /**
22 * Class constructor.
23 */
24 public function __construct() {
25 parent::__construct();
26 }
27
28 /**
29 * Add the membership Payments.
30 *
31 * @param array $params
32 * Reference array contains the values submitted by the form.
33 *
34 *
35 * @return object
36 */
37 public static function create($params) {
38 $hook = empty($params['id']) ? 'create' : 'edit';
39 CRM_Utils_Hook::pre($hook, 'MembershipPayment', CRM_Utils_Array::value('id', $params), $params);
40 $dao = new CRM_Member_DAO_MembershipPayment();
41 $dao->copyValues($params);
42 // We check for membership_id in case we are being called too early in the process. This is
43 // cludgey but is part of the deprecation process (ie. we are trying to do everything
44 // from LineItem::create with a view to eventually removing this fn & the table.
45 if (!civicrm_api3('Membership', 'getcount', ['id' => $params['membership_id']])) {
46 return $dao;
47 }
48
49 //Fixed for avoiding duplicate entry error when user goes
50 //back and forward during payment mode is notify
51 if (!$dao->find(TRUE)) {
52 $dao->save();
53 }
54 CRM_Utils_Hook::post($hook, 'MembershipPayment', $dao->id, $dao);
55 // 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
56 // 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
57 // 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
58 // OR the caller will have taken responsibility for updating the line items themselves so we will update using SQL here
59 if (!empty($params['isSkipLineItem'])) {
60 // Caller has taken responsibility for updating the line items.
61 return $dao;
62 }
63 if (!isset($params['membership_type_id'])) {
64 $membership_type_id = civicrm_api3('membership', 'getvalue', [
65 'id' => $dao->membership_id,
66 'return' => 'membership_type_id',
67 ]);
68 }
69 else {
70 $membership_type_id = $params['membership_type_id'];
71 }
72 $sql = "UPDATE civicrm_line_item li
73 LEFT JOIN civicrm_price_field_value pv ON pv.id = li.price_field_value_id
74 SET entity_table = 'civicrm_membership', entity_id = %1
75 WHERE pv.membership_type_id = %2
76 AND contribution_id = %3";
77 CRM_Core_DAO::executeQuery($sql, [
78 1 => [$dao->membership_id, 'Integer'],
79 2 => [$membership_type_id, 'Integer'],
80 3 => [$dao->contribution_id, 'Integer'],
81 ]);
82 return $dao;
83 }
84
85 /**
86 * Delete membership Payments.
87 *
88 * @param int $id
89 *
90 * @return bool
91 */
92 public static function del($id) {
93 $dao = new CRM_Member_DAO_MembershipPayment();
94 $dao->id = $id;
95 $result = FALSE;
96 if ($dao->find(TRUE)) {
97 $dao->delete();
98 $result = TRUE;
99 }
100 return $result;
101 }
102
103 }