Merge pull request #13854 from eileenmcnaughton/lock
[civicrm-core.git] / api / v3 / Order.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 = [];
46 $params['api.line_item.get'] = ['qty' => ['<>' => 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 * Adjust Metadata for Get action.
66 *
67 * The metadata is used for setting defaults, documentation & validation.
68 *
69 * @param array $params
70 * Array of parameters determined by getfields.
71 */
72 function _civicrm_api3_order_get_spec(&$params) {
73 $params['id']['api.aliases'] = ['order_id'];
74 $params['id']['title'] = ts('Contribution / Order ID');
75 }
76
77 /**
78 * Add or update a Order.
79 *
80 * @param array $params
81 * Input parameters.
82 *
83 * @throws API_Exception
84 * @return array
85 * Api result array
86 */
87 function civicrm_api3_order_create(&$params) {
88
89 $entity = NULL;
90 $entityIds = [];
91 if (CRM_Utils_Array::value('line_items', $params) && is_array($params['line_items'])) {
92 $priceSetID = NULL;
93 CRM_Contribute_BAO_Contribution::checkLineItems($params);
94 foreach ($params['line_items'] as $lineItems) {
95 $entityParams = CRM_Utils_Array::value('params', $lineItems, []);
96 if (!empty($entityParams) && !empty($lineItems['line_item'])) {
97 $item = reset($lineItems['line_item']);
98 $entity = str_replace('civicrm_', '', $item['entity_table']);
99 }
100 if ($entityParams) {
101 if (in_array($entity, ['participant', 'membership'])) {
102 $entityParams['skipLineItem'] = TRUE;
103 $entityResult = civicrm_api3($entity, 'create', $entityParams);
104 $params['contribution_mode'] = $entity;
105 $entityIds[] = $params[$entity . '_id'] = $entityResult['id'];
106 foreach ($lineItems['line_item'] as &$items) {
107 $items['entity_id'] = $entityResult['id'];
108 }
109 }
110 else {
111 // pledge payment
112 }
113 }
114 if (empty($priceSetID)) {
115 $item = reset($lineItems['line_item']);
116 $priceSetID = civicrm_api3('PriceField', 'getvalue', [
117 'return' => 'price_set_id',
118 'id' => $item['price_field_id'],
119 ]);
120 $params['line_item'][$priceSetID] = [];
121 }
122 $params['line_item'][$priceSetID] = array_merge($params['line_item'][$priceSetID], $lineItems['line_item']);
123 }
124 }
125 $contribution = civicrm_api3('Contribution', 'create', $params);
126 // add payments
127 if ($entity && CRM_Utils_Array::value('id', $contribution)) {
128 foreach ($entityIds as $entityId) {
129 $paymentParams = [
130 'contribution_id' => $contribution['id'],
131 $entity . '_id' => $entityId,
132 ];
133 // if entity is pledge then build pledge param
134 if ($entity == 'pledge') {
135 $paymentParams += $entityParams;
136 }
137 $payments = civicrm_api3($entity . '_payment', 'create', $paymentParams);
138 }
139 }
140 return civicrm_api3_create_success(CRM_Utils_Array::value('values', $contribution), $params, 'Order', 'create');
141 }
142
143 /**
144 * Delete a Order.
145 *
146 * @param array $params
147 * Input parameters.
148 * @return array
149 * @throws API_Exception
150 * @throws CiviCRM_API3_Exception
151 */
152 function civicrm_api3_order_delete($params) {
153 $contribution = civicrm_api3('Contribution', 'get', [
154 'return' => ['is_test'],
155 'id' => $params['id'],
156 ]);
157 if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) {
158 $result = civicrm_api3('Contribution', 'delete', $params);
159 }
160 else {
161 throw new API_Exception('Only test orders can be deleted.');
162 }
163 return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete');
164 }
165
166 /**
167 * Cancel an Order.
168 *
169 * @param array $params
170 * Input parameters.
171 *
172 * @return array
173 */
174 function civicrm_api3_order_cancel($params) {
175 $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name');
176 $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses);
177 $result = civicrm_api3('Contribution', 'create', $params);
178 CRM_Contribute_BAO_Contribution::transitionComponents($params);
179 return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel');
180 }
181
182 /**
183 * Adjust Metadata for Cancel action.
184 *
185 * The metadata is used for setting defaults, documentation & validation.
186 *
187 * @param array $params
188 * Array of parameters determined by getfields.
189 */
190 function _civicrm_api3_order_cancel_spec(&$params) {
191 $params['contribution_id'] = [
192 'api.required' => 1 ,
193 'title' => 'Contribution ID',
194 'type' => CRM_Utils_Type::T_INT,
195 ];
196 }
197
198 /**
199 * Adjust Metadata for Create action.
200 *
201 * The metadata is used for setting defaults, documentation & validation.
202 *
203 * @param array $params
204 * Array of parameters determined by getfields.
205 */
206 function _civicrm_api3_order_create_spec(&$params) {
207 $params['contact_id'] = [
208 'name' => 'contact_id',
209 'title' => 'Contact ID',
210 'type' => CRM_Utils_Type::T_INT,
211 'api.required' => TRUE,
212 ];
213 $params['total_amount'] = [
214 'name' => 'total_amount',
215 'title' => 'Total Amount',
216 'api.required' => TRUE,
217 ];
218 $params['financial_type_id'] = [
219 'name' => 'financial_type_id',
220 'title' => 'Financial Type',
221 'type' => CRM_Utils_Type::T_INT,
222 'api.required' => TRUE,
223 'table_name' => 'civicrm_contribution',
224 'entity' => 'Contribution',
225 'bao' => 'CRM_Contribute_BAO_Contribution',
226 'pseudoconstant' => [
227 'table' => 'civicrm_financial_type',
228 'keyColumn' => 'id',
229 'labelColumn' => 'name',
230 ],
231 ];
232 }
233
234 /**
235 * Adjust Metadata for Delete action.
236 *
237 * The metadata is used for setting defaults, documentation & validation.
238 *
239 * @param array $params
240 * Array of parameters determined by getfields.
241 */
242 function _civicrm_api3_order_delete_spec(&$params) {
243 $params['contribution_id'] = [
244 'api.required' => TRUE,
245 'title' => 'Contribution ID',
246 'type' => CRM_Utils_Type::T_INT,
247 ];
248 $params['id']['api.aliases'] = ['contribution_id'];
249 }