Commit | Line | Data |
---|---|---|
31a99426 PN |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
4 | | CiviCRM version 4.7 | | |
5 | +--------------------------------------------------------------------+ | |
1f4ea726 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
31a99426 PN |
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 | } | |
b8644ae3 PN |
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 | ||
5f44f698 PN |
129 | /** |
130 | * Delete a Order. | |
131 | * | |
132 | * @param array $params | |
133 | * Input parameters. | |
5f44f698 | 134 | * @return array |
8089541a SL |
135 | * @throws API_Exception |
136 | * @throws CiviCRM_API3_Exception | |
5f44f698 PN |
137 | */ |
138 | function civicrm_api3_order_delete($params) { | |
139 | $contribution = civicrm_api3('Contribution', 'get', array( | |
140 | 'return' => array('is_test'), | |
95c4e89e | 141 | 'id' => $params['id'], |
5f44f698 PN |
142 | )); |
143 | if ($contribution['id'] && $contribution['values'][$contribution['id']]['is_test'] == TRUE) { | |
144 | $result = civicrm_api3('Contribution', 'delete', $params); | |
145 | } | |
146 | else { | |
95c4e89e | 147 | throw new API_Exception('Only test orders can be deleted.'); |
5f44f698 | 148 | } |
95c4e89e | 149 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'delete'); |
5f44f698 PN |
150 | } |
151 | ||
65f7f9f6 | 152 | /** |
9d8e81c8 | 153 | * Cancel an Order. |
65f7f9f6 PN |
154 | * |
155 | * @param array $params | |
156 | * Input parameters. | |
157 | * | |
158 | * @return array | |
159 | */ | |
160 | function civicrm_api3_order_cancel($params) { | |
161 | $contributionStatuses = CRM_Contribute_PseudoConstant::contributionStatus(NULL, 'name'); | |
a5c7fd46 | 162 | $params['contribution_status_id'] = array_search('Cancelled', $contributionStatuses); |
65f7f9f6 | 163 | $result = civicrm_api3('Contribution', 'create', $params); |
9d8e81c8 PN |
164 | CRM_Contribute_BAO_Contribution::transitionComponents($params); |
165 | return civicrm_api3_create_success($result['values'], $params, 'Order', 'cancel'); | |
65f7f9f6 PN |
166 | } |
167 | ||
168 | /** | |
169 | * Adjust Metadata for Cancel action. | |
170 | * | |
171 | * The metadata is used for setting defaults, documentation & validation. | |
172 | * | |
173 | * @param array $params | |
174 | * Array of parameters determined by getfields. | |
175 | */ | |
176 | function _civicrm_api3_order_cancel_spec(&$params) { | |
177 | $params['contribution_id'] = array( | |
178 | 'api.required' => 1 , | |
179 | 'title' => 'Contribution ID', | |
180 | 'type' => CRM_Utils_Type::T_INT, | |
181 | ); | |
182 | } | |
183 | ||
b8644ae3 PN |
184 | /** |
185 | * Adjust Metadata for Create 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_create_spec(&$params) { | |
193 | $params['contact_id'] = array( | |
194 | 'name' => 'contact_id', | |
195 | 'title' => 'Contact ID', | |
196 | 'type' => CRM_Utils_Type::T_INT, | |
197 | 'api.required' => TRUE, | |
198 | ); | |
199 | $params['total_amount'] = array( | |
200 | 'name' => 'total_amount', | |
201 | 'title' => 'Total Amount', | |
202 | 'api.required' => TRUE, | |
203 | ); | |
204 | $params['financial_type_id'] = array( | |
205 | 'name' => 'financial_type_id', | |
206 | 'title' => 'Financial Type', | |
207 | 'type' => CRM_Utils_Type::T_INT, | |
208 | 'api.required' => TRUE, | |
209 | ); | |
210 | } | |
5f44f698 PN |
211 | |
212 | /** | |
213 | * Adjust Metadata for Delete action. | |
214 | * | |
215 | * The metadata is used for setting defaults, documentation & validation. | |
216 | * | |
217 | * @param array $params | |
218 | * Array of parameters determined by getfields. | |
219 | */ | |
220 | function _civicrm_api3_order_delete_spec(&$params) { | |
221 | $params['contribution_id'] = array( | |
222 | 'api.required' => TRUE, | |
223 | 'title' => 'Contribution ID', | |
224 | 'type' => CRM_Utils_Type::T_INT, | |
225 | ); | |
226 | $params['id']['api.aliases'] = array('contribution_id'); | |
227 | } |