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