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