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