Merge pull request #17398 from eileenmcnaughton/mem_recur
[civicrm-core.git] / api / v3 / PledgePayment.php
CommitLineData
6a488035 1<?php
6a488035
TO
2/*
3 +--------------------------------------------------------------------+
a30c801b 4 | Copyright CiviCRM LLC. All rights reserved. |
6a488035 5 | |
a30c801b
TO
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 |
6a488035
TO
9 +--------------------------------------------------------------------+
10 */
11
12/**
c28e1768 13 * This api exposes CiviCRM Pledge payment.
6a488035
TO
14 *
15 * @package CiviCRM_APIv3
6a488035
TO
16 */
17
6a488035 18/**
9d32e6f7
EM
19 * Add or update a pledge payment.
20 *
c28e1768
CW
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.
6a488035
TO
24 *
25 * @todo possibly add ability to add payment if there are less payments than pledge installments
c206647d 26 * @todo possibly add ability to recalculate dates if the schedule is changed
6a488035 27 *
cf470720
TO
28 * @param array $params
29 * Input parameters.
6a488035 30 *
a6c01b45 31 * @return array
72b3a70c 32 * API Result
6a488035
TO
33 */
34function civicrm_api3_pledge_payment_create($params) {
35
36 $paymentParams = $params;
8cc574cf 37 if (empty($params['id']) && empty($params['option.create_new'])) {
6a488035
TO
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);
cf8f0fff 48 $result = [];
9b873358 49 if (empty($dao->pledge_id)) {
35671d00 50 $dao->find(TRUE);
6a488035
TO
51 }
52 _civicrm_api3_object_to_array($dao, $result[$dao->id]);
53
6a488035
TO
54 //update pledge status
55 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($dao->pledge_id);
56
244bbdd8 57 return civicrm_api3_create_success($result, $params, 'PledgePayment', 'create', $dao);
6a488035 58}
11e09c59
TO
59
60/**
0aa0303c
EM
61 * Adjust Metadata for Create action.
62 *
63 * The metadata is used for setting defaults, documentation & validation.
6a488035 64 *
cf470720 65 * @param array $params
b081365f 66 * Array of parameters determined by getfields.
6a488035
TO
67 */
68function _civicrm_api3_pledge_payment_create_spec(&$params) {
69 $params['pledge_id']['api.required'] = 1;
70 $params['status_id']['api.required'] = 1;
71}
72
73/**
9d32e6f7 74 * Delete a pledge Payment - Note this deletes the contribution not just the link.
6a488035 75 *
cf470720
TO
76 * @param array $params
77 * Input parameters.
6a488035 78 *
a6c01b45 79 * @return array
72b3a70c 80 * API result
6a488035
TO
81 */
82function civicrm_api3_pledge_payment_delete($params) {
83
84 if (CRM_Pledge_BAO_PledgePayment::del($params['id'])) {
cf8f0fff 85 return civicrm_api3_create_success(['id' => $params['id']], $params, 'PledgePayment', 'delete');
6a488035
TO
86 }
87 else {
88 return civicrm_api3_create_error('Could not delete payment');
89 }
90}
91
92/**
9d32e6f7 93 * Retrieve a set of pledges, given a set of input params.
6a488035 94 *
cf470720
TO
95 * @param array $params
96 * Input parameters.
6a488035 97 *
a6c01b45 98 * @return array
9d32e6f7 99 * array of pledges, if error an array with an error id and error message
6a488035
TO
100 */
101function civicrm_api3_pledge_payment_get($params) {
102
6a488035
TO
103 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
104}