a51bcea5a4ee1e7f46819245f9043406c4a1e1d9
[civicrm-core.git] / api / v3 / PledgePayment.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * File for the CiviCRM APIv3 Pledge functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Pledge_Payment
33 *
34 * @copyright CiviCRM LLC (c) 2004-2014
35 * @version $Id: PledgePayment.php
36 *
37 */
38
39 /**
40 * Add or update a plege payment. Pledge Payment API doesn't actually add a pledge
41 * if the request is to 'create' and 'id' is not passed in
42 * the oldest pledge with no associated contribution is updated
43 *
44 * @todo possibly add ability to add payment if there are less payments than pledge installments
45 * @todo possibly add ability to recalculate dates if the schedule is changed
46 *
47 * @param array $params
48 * Input parameters.
49 * {@getfields PledgePayment_create}
50 * @example PledgePaymentCreate.php
51 *
52 * @return array
53 * API Result
54 * @static
55 * @access public
56 */
57 function civicrm_api3_pledge_payment_create($params) {
58
59 $paymentParams = $params;
60 if (empty($params['id']) && empty($params['option.create_new'])) {
61 $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']);
62 if (empty($paymentDetails)) {
63 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");
64 }
65 elseif (is_array($paymentDetails)) {
66 $paymentParams = array_merge($params, $paymentDetails);
67 }
68 }
69
70 $dao = CRM_Pledge_BAO_PledgePayment::add($paymentParams);
71 if (empty($dao->pledge_id)) {
72 $dao->find(TRUE);
73 }
74 _civicrm_api3_object_to_array($dao, $result[$dao->id]);
75
76 //update pledge status
77 CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($dao->pledge_id);
78
79 return civicrm_api3_create_success($result, $params, 'pledge_payment', 'create', $dao);
80 }
81
82 /**
83 * Adjust Metadata for Create action
84 *
85 * The metadata is used for setting defaults, documentation & validation
86 * @param array $params
87 * Array or parameters determined by getfields.
88 */
89 function _civicrm_api3_pledge_payment_create_spec(&$params) {
90 $params['pledge_id']['api.required'] = 1;
91 $params['status_id']['api.required'] = 1;
92 }
93
94 /**
95 * Delete a pledge Payment - Note this deletes the contribution not just the link
96 *
97 * @param array $params
98 * Input parameters.
99 * {@getfields PledgePayment_delete}
100 * @example PledgePaymentDelete.php
101 *
102 * @return array
103 * API result
104 * @static
105 * @access public
106 */
107 function civicrm_api3_pledge_payment_delete($params) {
108
109 if (CRM_Pledge_BAO_PledgePayment::del($params['id'])) {
110 return civicrm_api3_create_success(array('id' => $params['id']), $params, 'pledge_payment', 'delete');
111 }
112 else {
113 return civicrm_api3_create_error('Could not delete payment');
114 }
115 }
116
117 /**
118 * Retrieve a set of pledges, given a set of input params
119 *
120 * @param array $params
121 * Input parameters.
122 * {@getfields PledgePayment_get}
123 * @example PledgePaymentGet.php *
124 *
125 * @return array
126 * (reference ) array of pledges, if error an array with an error id and error message
127 * @static
128 * @access public
129 */
130 function civicrm_api3_pledge_payment_get($params) {
131
132 return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params);
133 }
134
135 /**
136 * @param int $pledgeId
137 * @param int $paymentStatusId
138 * @param $paymentIds
139 *
140 * @return mixed
141 */
142 function updatePledgePayments($pledgeId, $paymentStatusId, $paymentIds) {
143
144 $result = updatePledgePayments($pledgeId, $paymentStatusId, $paymentIds = NULL);
145 return $result;
146 }
147
148 /**
149 * Gets field for civicrm_pledge_payment functions
150 *
151 * @param array $params
152 *
153 * @return array
154 * fields valid for other functions
155 */
156 function civicrm_api3_pledge_payment_get_spec(&$params) {
157 $params['option.create_new'] = array('title' => "Create new field rather than update an unpaid payment");
158 }