Merge pull request #17656 from civicrm/5.27
[civicrm-core.git] / api / v3 / PledgePayment.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 * This api exposes CiviCRM Pledge payment.
14 *
15 * @package CiviCRM_APIv3
16 */
17
18 /**
19 * Add or update a pledge payment.
20 *
21 * Pledge Payment API doesn't actually add a pledge.
22 * If the request is to 'create' and 'id' is not passed in
23 * the oldest pledge with no associated contribution is updated.
24 *
25 * @todo possibly add ability to add payment if there are less payments than pledge installments
26 * @todo possibly add ability to recalculate dates if the schedule is changed
27 *
28 * @param array $params
29 * Input parameters.
30 *
31 * @return array
32 * API Result
33 */
34 function civicrm_api3_pledge_payment_create($params) {
35
36 $paymentParams = $params;
37 if (empty($params['id']) && empty($params['option.create_new'])) {
38 $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']);
39 if (empty($paymentDetails)) {
40 return civicrm_api3_create_error("There are no unmatched payment on this pledge. Pass in the pledge_payment id to specify one or 'option.create_new' to create one");
41 }
42 elseif (is_array($paymentDetails)) {
43 $paymentParams = array_merge($params, $paymentDetails);
44 }
45 }
46
47 $dao = CRM_Pledge_BAO_PledgePayment::add($paymentParams);
48 $result = [];
49 if (empty($dao->pledge_id)) {
50 $dao->find(TRUE);
51 }
52 _civicrm_api3_object_to_array($dao, $result[$dao->id]);
53
54 //update pledge status
55 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($dao->pledge_id);
56
57 return civicrm_api3_create_success($result, $params, 'PledgePayment', 'create', $dao);
58 }
59
60 /**
61 * Adjust Metadata for Create action.
62 *
63 * The metadata is used for setting defaults, documentation & validation.
64 *
65 * @param array $params
66 * Array of parameters determined by getfields.
67 */
68 function _civicrm_api3_pledge_payment_create_spec(&$params) {
69 $params['pledge_id']['api.required'] = 1;
70 $params['status_id']['api.required'] = 1;
71 }
72
73 /**
74 * Delete a pledge Payment - Note this deletes the contribution not just the link.
75 *
76 * @param array $params
77 * Input parameters.
78 *
79 * @return array
80 * API result
81 */
82 function civicrm_api3_pledge_payment_delete($params) {
83
84 if (CRM_Pledge_BAO_PledgePayment::del($params['id'])) {
85 return civicrm_api3_create_success(['id' => $params['id']], $params, 'PledgePayment', 'delete');
86 }
87 else {
88 return civicrm_api3_create_error('Could not delete payment');
89 }
90 }
91
92 /**
93 * Retrieve a set of pledges, given a set of input params.
94 *
95 * @param array $params
96 * Input parameters.
97 *
98 * @return array
99 * array of pledges, if error an array with an error id and error message
100 */
101 function civicrm_api3_pledge_payment_get($params) {
102
103 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
104 }