Merge pull request #10487 from mfb/CRM-20713
[civicrm-core.git] / api / v3 / Order.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2017 |
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 * This api exposes CiviCRM Order objects, an abstract entity
30 * comprised of contributions and related line items.
31 *
32 * @package CiviCRM_APIv3
33 */
34
35 /**
36 * Retrieve a set of Order.
37 *
38 * @param array $params
39 * Input parameters.
40 *
41 * @return array
42 * Array of Order, if error an array with an error id and error message
43 */
44 function civicrm_api3_order_get($params) {
45 $contributions = array();
46 $params['api.line_item.get'] = array('qty' => array('<>' => 0));
47 $isSequential = FALSE;
48 if (CRM_Utils_Array::value('sequential', $params)) {
49 $params['sequential'] = 0;
50 $isSequential = TRUE;
51 }
52 $result = civicrm_api3('Contribution', 'get', $params);
53 if (!empty($result['values'])) {
54 foreach ($result['values'] as $key => $contribution) {
55 $contributions[$key] = $contribution;
56 $contributions[$key]['line_items'] = $contribution['api.line_item.get']['values'];
57 unset($contributions[$key]['api.line_item.get']);
58 }
59 }
60 $params['sequential'] = $isSequential;
61 return civicrm_api3_create_success($contributions, $params, 'Order', 'get');
62 }
63
64 /**
65 * Add or update a Order.
66 *
67 * @param array $params
68 * Input parameters.
69 *
70 * @throws API_Exception
71 * @return array
72 * Api result array
73 */
74 function civicrm_api3_order_create(&$params) {
75 $contribution = array();
76 $entity = NULL;
77 $entityIds = array();
78 if (CRM_Utils_Array::value('line_items', $params) && is_array($params['line_items'])) {
79 $priceSetID = NULL;
80 CRM_Contribute_BAO_Contribution::checkLineItems($params);
81 foreach ($params['line_items'] as $lineItems) {
82 $entityParams = CRM_Utils_Array::value('params', $lineItems, array());
83 if (!empty($entityParams) && !empty($lineItems['line_item'])) {
84 $item = reset($lineItems['line_item']);
85 $entity = str_replace('civicrm_', '', $item['entity_table']);
86 }
87 if ($entityParams) {
88 if (in_array($entity, array('participant', 'membership'))) {
89 $entityParams['skipLineItem'] = TRUE;
90 $entityResult = civicrm_api3($entity, 'create', $entityParams);
91 $params['contribution_mode'] = $entity;
92 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
93 foreach ($lineItems['line_item'] as &$items) {
94 $items['entity_id'] = $entityResult['id'];
95 }
96 }
97 else {
98 // pledge payment
99 }
100 }
101 if (empty($priceSetID)) {
102 $item = reset($lineItems['line_item']);
103 $priceSetID = civicrm_api3('PriceField', 'getvalue', array(
104 'return' => 'price_set_id',
105 'id' => $item['price_field_id'],
106 ));
107 $params['line_item'][$priceSetID] = array();
108 }
109 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
110 }
111 }
112 $contribution = civicrm_api3('Contribution', 'create', $params);
113 // add payments
114 if ($entity && CRM_Utils_Array::value('id', $contribution)) {
115 foreach ($entityIds as $entityId) {
116 $paymentParams = array(
117 'contribution_id' => $contribution['id'],
118 $entity . '_id' => $entityId,
119 );
120 // if entity is pledge then build pledge param
121 if ($entity == 'pledge') {
122 $paymentParams += $entityParams;
123 }
124 $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
125 }
126 }
127 return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create');
128 }
129
130 /**
131 * Delete a Order.
132 *
133 * @param array $params
134 * Input parameters.
135 * @return array
136 * @throws API_Exception
137 * @throws CiviCRM_API3_Exception
138 */
139 function civicrm_api3_order_delete($params) {
140 $contribution = civicrm_api3('Contribution', 'get', array(
141 'return' => array('is_test'),
142 'id' => $params['id'],
143 ));
144 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
145 $result = civicrm_api3('Contribution', 'delete', $params);
146 }
147 else {
148 throw new API_Exception('Only test orders can be deleted.');
149 }
150 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
151 }
152
153 /**
154 * Cancel an Order.
155 *
156 * @param array $params
157 * Input parameters.
158 *
159 * @return array
160 */
161 function civicrm_api3_order_cancel($params) {
162 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
163 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
164 $result = civicrm_api3('Contribution', 'create', $params);
165 CRM_Contribute_BAO_Contribution::transitionComponents($params);
166 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
167 }
168
169 /**
170 * Adjust Metadata for Cancel action.
171 *
172 * The metadata is used for setting defaults, documentation & validation.
173 *
174 * @param array $params
175 * Array of parameters determined by getfields.
176 */
177 function _civicrm_api3_order_cancel_spec(&$params) {
178 $params['contribution_id'] = array(
179 'api.required' => 1 ,
180 'title' => 'Contribution ID',
181 'type' => CRM_Utils_Type::T_INT,
182 );
183 }
184
185 /**
186 * Adjust Metadata for Create action.
187 *
188 * The metadata is used for setting defaults, documentation & validation.
189 *
190 * @param array $params
191 * Array of parameters determined by getfields.
192 */
193 function _civicrm_api3_order_create_spec(&$params) {
194 $params['contact_id'] = array(
195 'name' => 'contact_id',
196 'title' => 'Contact ID',
197 'type' => CRM_Utils_Type::T_INT,
198 'api.required' => TRUE,
199 );
200 $params['total_amount'] = array(
201 'name' => 'total_amount',
202 'title' => 'Total Amount',
203 'api.required' => TRUE,
204 );
205 $params['financial_type_id'] = array(
206 'name' => 'financial_type_id',
207 'title' => 'Financial Type',
208 'type' => CRM_Utils_Type::T_INT,
209 'api.required' => TRUE,
210 );
211 }
212
213 /**
214 * Adjust Metadata for Delete action.
215 *
216 * The metadata is used for setting defaults, documentation & validation.
217 *
218 * @param array $params
219 * Array of parameters determined by getfields.
220 */
221 function _civicrm_api3_order_delete_spec(&$params) {
222 $params['contribution_id'] = array(
223 'api.required' => TRUE,
224 'title' => 'Contribution ID',
225 'type' => CRM_Utils_Type::T_INT,
226 );
227 $params['id']['api.aliases'] = array('contribution_id');
228 }