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