Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
6a488035 TO |
2 | /* |
3 | +--------------------------------------------------------------------+ | |
39de6fd5 | 4 | | CiviCRM version 4.6 | |
6a488035 | 5 | +--------------------------------------------------------------------+ |
731a0992 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
6a488035 TO |
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 | * | |
731a0992 | 34 | * @copyright CiviCRM LLC (c) 2004-2014 |
6a488035 TO |
35 | * @version $Id: PledgePayment.php |
36 | * | |
37 | */ | |
38 | ||
6a488035 TO |
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 | |
c206647d | 45 | * @todo possibly add ability to recalculate dates if the schedule is changed |
6a488035 | 46 | * |
cf470720 TO |
47 | * @param array $params |
48 | * Input parameters. | |
6a488035 TO |
49 | * {@getfields PledgePayment_create} |
50 | * @example PledgePaymentCreate.php | |
51 | * | |
a6c01b45 | 52 | * @return array |
72b3a70c | 53 | * API Result |
6a488035 TO |
54 | */ |
55 | function civicrm_api3_pledge_payment_create($params) { | |
56 | ||
57 | $paymentParams = $params; | |
8cc574cf | 58 | if (empty($params['id']) && empty($params['option.create_new'])) { |
6a488035 TO |
59 | $paymentDetails = CRM_Pledge_BAO_PledgePayment::getOldestPledgePayment($params['pledge_id']); |
60 | if (empty($paymentDetails)) { | |
61 | 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"); | |
62 | } | |
63 | elseif (is_array($paymentDetails)) { | |
64 | $paymentParams = array_merge($params, $paymentDetails); | |
65 | } | |
66 | } | |
67 | ||
68 | $dao = CRM_Pledge_BAO_PledgePayment::add($paymentParams); | |
906e6120 | 69 | $result = array(); |
9b873358 | 70 | if (empty($dao->pledge_id)) { |
35671d00 | 71 | $dao->find(TRUE); |
6a488035 TO |
72 | } |
73 | _civicrm_api3_object_to_array($dao, $result[$dao->id]); | |
74 | ||
6a488035 TO |
75 | //update pledge status |
76 | CRM_Pledge_BAO_PledgePayment::updatePledgePaymentStatus($dao->pledge_id); | |
77 | ||
78 | return civicrm_api3_create_success($result, $params, 'pledge_payment', 'create', $dao); | |
79 | } | |
11e09c59 TO |
80 | |
81 | /** | |
6a488035 TO |
82 | * Adjust Metadata for Create action |
83 | * | |
84 | * The metadata is used for setting defaults, documentation & validation | |
cf470720 TO |
85 | * @param array $params |
86 | * Array or parameters determined by getfields. | |
6a488035 TO |
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 | * | |
cf470720 TO |
96 | * @param array $params |
97 | * Input parameters. | |
6a488035 TO |
98 | * {@getfields PledgePayment_delete} |
99 | * @example PledgePaymentDelete.php | |
100 | * | |
a6c01b45 | 101 | * @return array |
72b3a70c | 102 | * API result |
6a488035 TO |
103 | */ |
104 | function civicrm_api3_pledge_payment_delete($params) { | |
105 | ||
106 | if (CRM_Pledge_BAO_PledgePayment::del($params['id'])) { | |
35671d00 | 107 | return civicrm_api3_create_success(array('id' => $params['id']), $params, 'pledge_payment', 'delete'); |
6a488035 TO |
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 | * | |
cf470720 TO |
117 | * @param array $params |
118 | * Input parameters. | |
6a488035 TO |
119 | * {@getfields PledgePayment_get} |
120 | * @example PledgePaymentGet.php * | |
121 | * | |
a6c01b45 | 122 | * @return array |
72b3a70c | 123 | * (reference ) array of pledges, if error an array with an error id and error message |
6a488035 TO |
124 | */ |
125 | function civicrm_api3_pledge_payment_get($params) { | |
126 | ||
6a488035 TO |
127 | return _civicrm_api3_basic_get(_civicrm_api3_get_BAO(__FUNCTION__), $params); |
128 | } | |
129 | ||
11e09c59 | 130 | /** |
6a488035 TO |
131 | * Gets field for civicrm_pledge_payment functions |
132 | * | |
c490a46a | 133 | * @param array $params |
ae5ffbb7 | 134 | * Modifiable list of fields allowed for the PledgePayment.get action. |
6a488035 TO |
135 | */ |
136 | function civicrm_api3_pledge_payment_get_spec(&$params) { | |
137 | $params['option.create_new'] = array('title' => "Create new field rather than update an unpaid payment"); | |
138 | } |