dev/financial#152 remove determination of source
[civicrm-core.git] / tests / phpunit / CRMTraits / Financial / OrderTrait.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 use Civi\Test\Api3TestTrait;
13
14 /**
15 * Trait OrderTrait
16 *
17 * Trait for setting up orders for tests.
18 */
19 trait CRMTraits_Financial_OrderTrait {
20
21 use Api3TestTrait;
22
23 /**
24 * Create a pending membership from a recurring order.
25 *
26 * @throws \CRM_Core_Exception
27 */
28 public function createRepeatMembershipOrder(): void {
29 $this->createExtraneousContribution();
30 $this->ids['contact'][0] = $this->individualCreate();
31 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
32
33 $contributionRecur = $this->callAPISuccess('ContributionRecur', 'create', array_merge([
34 'contact_id' => $this->_contactID,
35 'amount' => 1000,
36 'sequential' => 1,
37 'installments' => 5,
38 'frequency_unit' => 'Month',
39 'frequency_interval' => 1,
40 'invoice_id' => $this->_invoiceID,
41 'contribution_status_id' => 2,
42 'payment_processor_id' => $this->_paymentProcessorID,
43 // processor provided ID - use contact ID as proxy.
44 'processor_id' => $this->_contactID,
45 ]));
46
47 $orderID = $this->callAPISuccess('Order', 'create', [
48 'total_amount' => '200',
49 'financial_type_id' => 'Donation',
50 'source' => 'Online Contribution: form payment',
51 'contact_id' => $this->_contactID,
52 'contribution_page_id' => $this->_contributionPageID,
53 'payment_processor_id' => $this->_paymentProcessorID,
54 'is_test' => 0,
55 'receive_date' => '2019-07-25 07:34:23',
56 'skipCleanMoney' => TRUE,
57 'contribution_recur_id' => $contributionRecur['id'],
58 'line_items' => [
59 [
60 'params' => [
61 'contact_id' => $this->ids['contact'][0],
62 'membership_type_id' => $this->ids['membership_type'][0],
63 'contribution_recur_id' => $contributionRecur['id'],
64 'source' => 'Payment',
65 ],
66 'line_item' => $this->getMembershipLineItem(),
67 ],
68 ],
69 ])['id'];
70
71 $this->ids['ContributionRecur'][0] = $contributionRecur['id'];
72 $this->ids['Contribution'][0] = $orderID;
73 }
74
75 /**
76 * Create an order with a contribution AND a membership line item.
77 *
78 * @throws \CRM_Core_Exception
79 */
80 protected function createContributionAndMembershipOrder() {
81 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
82 $orderID = $this->callAPISuccess('Order', 'create', [
83 'financial_type_id' => 'Donation',
84 'contact_id' => $this->_contactID,
85 'is_test' => 0,
86 'payment_instrument_id' => 'Check',
87 'receive_date' => date('Y-m-d'),
88 'line_items' => [
89 [
90 'params' => [
91 'contact_id' => $this->_contactID,
92 'source' => 'Payment',
93 ],
94 'line_item' => [
95 [
96 'label' => 'Contribution Amount',
97 'qty' => 1,
98 'unit_price' => 100,
99 'line_total' => 100,
100 'financial_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'),
101 'entity_table' => 'civicrm_contribution',
102 'price_field_id' => $this->callAPISuccessGetValue('price_field', [
103 'return' => 'id',
104 'label' => 'Contribution Amount',
105 'options' => ['limit' => 1, 'sort' => 'id DESC'],
106 ]),
107 'price_field_value_id' => NULL,
108 ],
109 ],
110 ],
111 [
112 'params' => [
113 'contact_id' => $this->_contactID,
114 'membership_type_id' => 'General',
115 'source' => 'Payment',
116 ],
117 'line_item' => $this->getMembershipLineItem(),
118 ],
119 ],
120 ])['id'];
121
122 $this->ids['Contribution'][0] = $orderID;
123 }
124
125 /**
126 * Create an order with more than one membership.
127 *
128 * @throws \CRM_Core_Exception
129 */
130 protected function createMultipleMembershipOrder() {
131 $this->createExtraneousContribution();
132 $this->ids['contact'][0] = $this->individualCreate();
133 $this->ids['contact'][1] = $this->individualCreate();
134 $this->ids['membership_type'][0] = $this->membershipTypeCreate();
135 $this->ids['membership_type'][1] = $this->membershipTypeCreate(['name' => 'Type 2']);
136 $priceFieldID = $this->callAPISuccessGetValue('price_field', [
137 'return' => 'id',
138 'label' => 'Membership Amount',
139 'options' => ['limit' => 1, 'sort' => 'id DESC'],
140 ]);
141 $generalPriceFieldValueID = $this->callAPISuccessGetValue('price_field_value', [
142 'return' => 'id',
143 'label' => 'General',
144 'options' => ['limit' => 1, 'sort' => 'id DESC'],
145 ]);
146
147 $orderID = $this->callAPISuccess('Order', 'create', [
148 'total_amount' => 400,
149 'financial_type_id' => 'Member Dues',
150 'contact_id' => $this->_contactID,
151 'is_test' => 0,
152 'payment_instrument_id' => 'Check',
153 'receive_date' => '2019-07-25 07:34:23',
154 'line_items' => [
155 [
156 'params' => [
157 'contact_id' => $this->ids['contact'][0],
158 'membership_type_id' => $this->ids['membership_type'][0],
159 'source' => 'Payment',
160 ],
161 'line_item' => [
162 [
163 'label' => 'General',
164 'qty' => 1,
165 'unit_price' => 200,
166 'line_total' => 200,
167 'financial_type_id' => 1,
168 'entity_table' => 'civicrm_membership',
169 'price_field_id' => $priceFieldID,
170 'price_field_value_id' => $generalPriceFieldValueID,
171 ],
172 ],
173 ],
174 [
175 'params' => [
176 'contact_id' => $this->ids['contact'][1],
177 'membership_type_id' => $this->ids['membership_type'][0],
178 'source' => 'Payment',
179 ],
180 'line_item' => [
181 [
182 'label' => 'General',
183 'qty' => 1,
184 'unit_price' => 200,
185 'line_total' => 200,
186 'financial_type_id' => 1,
187 'entity_table' => 'civicrm_membership',
188 'price_field_id' => $priceFieldID,
189 'price_field_value_id' => $generalPriceFieldValueID,
190 ],
191 ],
192 ],
193 ],
194 ])['id'];
195
196 $this->ids['Contribution'][0] = $orderID;
197 }
198
199 /**
200 * Create an order for an event.
201 *
202 * @param array $orderParams
203 *
204 * @throws \CRM_Core_Exception
205 */
206 protected function createEventOrder($orderParams = []) {
207 $this->ids['Contribution'][0] = $this->callAPISuccess('Order', 'create', array_merge($this->getParticipantOrderParams(), $orderParams))['id'];
208 $this->ids['Participant'][0] = $this->callAPISuccessGetValue('ParticipantPayment', ['return' => 'participant_id', 'contribution_id' => $this->ids['Contribution'][0]]);
209 }
210
211 /**
212 * Create an extraneous contribution to throw off any 'number one bugs'.
213 *
214 * Ie this means our real data starts from 2 & we won't hit 'pretend passes'
215 * just because the number 1 is used for multiple entities.
216 */
217 protected function createExtraneousContribution() {
218 $this->contributionCreate([
219 'contact_id' => $this->individualCreate(),
220 'is_test' => 1,
221 'financial_type_id' => 1,
222 'invoice_id' => 'abcd',
223 'trxn_id' => 345,
224 'receive_date' => '2019-07-25 07:34:23',
225 ]);
226 }
227
228 /**
229 * @return array[]
230 * @throws \CRM_Core_Exception
231 */
232 protected function getMembershipLineItem(): array {
233 return [
234 [
235 'label' => 'General',
236 'qty' => 1,
237 'unit_price' => 200,
238 'line_total' => 200,
239 'financial_type_id' => 1,
240 'entity_table' => 'civicrm_membership',
241 'price_field_id' => $this->callAPISuccess('price_field', 'getvalue', [
242 'return' => 'id',
243 'label' => 'Membership Amount',
244 'options' => ['limit' => 1, 'sort' => 'id DESC'],
245 ]),
246 'price_field_value_id' => $this->callAPISuccess('price_field_value', 'getvalue', [
247 'return' => 'id',
248 'label' => 'General',
249 'options' => ['limit' => 1, 'sort' => 'id DESC'],
250 ]),
251 ],
252 ];
253 }
254
255 }