Commit | Line | Data |
---|---|---|
6a488035 TO |
1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7d61e75f | 4 | | Copyright CiviCRM LLC. All rights reserved. | |
6a488035 | 5 | | | |
7d61e75f TO |
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 | | |
6a488035 | 9 | +--------------------------------------------------------------------+ |
d25dd0ee | 10 | */ |
6a488035 | 11 | |
4cbe18b8 EM |
12 | /** |
13 | * Class CRM_Contribute_BAO_ContributionTest | |
acb109b7 | 14 | * @group headless |
4cbe18b8 | 15 | */ |
6a488035 | 16 | class CRM_Contribute_BAO_ContributionTest extends CiviUnitTestCase { |
6a488035 | 17 | |
6b60d32c | 18 | use CRMTraits_Financial_FinancialACLTrait; |
53666099 | 19 | use CRMTraits_Financial_PriceSetTrait; |
6b60d32c | 20 | |
6a488035 | 21 | /** |
2449fe69 | 22 | * Clean up after tests. |
23 | */ | |
24 | public function tearDown() { | |
25 | $this->quickCleanUpFinancialEntities(); | |
2449fe69 | 26 | parent::tearDown(); |
27 | } | |
28 | ||
29 | /** | |
30 | * Test create method (create and update modes). | |
3fc37a30 | 31 | * |
32 | * @throws \CRM_Core_Exception | |
6a488035 | 33 | */ |
00be9182 | 34 | public function testCreate() { |
2449fe69 | 35 | $contactId = $this->individualCreate(); |
6a488035 | 36 | |
9099cab3 | 37 | $params = [ |
6a488035 TO |
38 | 'contact_id' => $contactId, |
39 | 'currency' => 'USD', | |
92915c55 | 40 | 'financial_type_id' => 1, |
6a488035 TO |
41 | 'contribution_status_id' => 1, |
42 | 'payment_instrument_id' => 1, | |
43 | 'source' => 'STUDENT', | |
44 | 'receive_date' => '20080522000000', | |
45 | 'receipt_date' => '20080522000000', | |
46 | 'non_deductible_amount' => 0.00, | |
47 | 'total_amount' => 200.00, | |
48 | 'fee_amount' => 5, | |
49 | 'net_amount' => 195, | |
50 | 'trxn_id' => '22ereerwww444444', | |
51 | 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81', | |
52 | 'thankyou_date' => '20080522', | |
d0c97775 | 53 | 'sequential' => TRUE, |
9099cab3 | 54 | ]; |
6a488035 | 55 | |
d0c97775 | 56 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
6a488035 | 57 | |
d0c97775 | 58 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.'); |
59 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
6a488035 TO |
60 | |
61 | //update contribution amount | |
d0c97775 | 62 | $params['id'] = $contribution['id']; |
6a488035 TO |
63 | $params['fee_amount'] = 10; |
64 | $params['net_amount'] = 190; | |
65 | ||
d0c97775 | 66 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
6a488035 | 67 | |
d0c97775 | 68 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .'); |
69 | $this->assertEquals($params['net_amount'], $contribution['net_amount'], 'Check for Amount updation.'); | |
6a488035 TO |
70 | } |
71 | ||
72 | /** | |
f43ac8d8 | 73 | * Create() method with custom data. |
6a488035 | 74 | */ |
00be9182 | 75 | public function testCreateWithCustomData() { |
2449fe69 | 76 | $contactId = $this->individualCreate(); |
6a488035 TO |
77 | |
78 | //create custom data | |
9099cab3 | 79 | $customGroup = $this->customGroupCreate(['extends' => 'Contribution']); |
2449fe69 | 80 | $customGroupID = $customGroup['id']; |
81 | $customGroup = $customGroup['values'][$customGroupID]; | |
82 | ||
9099cab3 | 83 | $fields = [ |
6a488035 TO |
84 | 'label' => 'testFld', |
85 | 'data_type' => 'String', | |
86 | 'html_type' => 'Text', | |
87 | 'is_active' => 1, | |
2449fe69 | 88 | 'custom_group_id' => $customGroupID, |
9099cab3 | 89 | ]; |
6a488035 TO |
90 | $customField = CRM_Core_BAO_CustomField::create($fields); |
91 | ||
9099cab3 | 92 | $params = [ |
6a488035 TO |
93 | 'contact_id' => $contactId, |
94 | 'currency' => 'USD', | |
92915c55 | 95 | 'financial_type_id' => 1, |
6a488035 TO |
96 | 'contribution_status_id' => 1, |
97 | 'payment_instrument_id' => 1, | |
98 | 'source' => 'STUDENT', | |
99 | 'receive_date' => '20080522000000', | |
100 | 'receipt_date' => '20080522000000', | |
101 | 'id' => NULL, | |
102 | 'non_deductible_amount' => 0.00, | |
103 | 'total_amount' => 200.00, | |
104 | 'fee_amount' => 5, | |
105 | 'net_amount' => 195, | |
106 | 'trxn_id' => '22ereerwww322323', | |
107 | 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70', | |
108 | 'thankyou_date' => '20080522', | |
d0c97775 | 109 | 'skipCleanMoney' => TRUE, |
9099cab3 | 110 | ]; |
6a488035 | 111 | |
9099cab3 CW |
112 | $params['custom'] = [ |
113 | $customField->id => [ | |
114 | -1 => [ | |
6a488035 TO |
115 | 'value' => 'Test custom value', |
116 | 'type' => 'String', | |
117 | 'custom_field_id' => $customField->id, | |
2449fe69 | 118 | 'custom_group_id' => $customGroupID, |
119 | 'table_name' => $customGroup['table_name'], | |
6a488035 TO |
120 | 'column_name' => $customField->column_name, |
121 | 'file_id' => NULL, | |
9099cab3 CW |
122 | ], |
123 | ], | |
124 | ]; | |
6a488035 | 125 | |
2449fe69 | 126 | $contribution = CRM_Contribute_BAO_Contribution::create($params); |
6a488035 TO |
127 | |
128 | // Check that the custom field value is saved | |
9099cab3 | 129 | $customValueParams = [ |
6a488035 TO |
130 | 'entityID' => $contribution->id, |
131 | 'custom_' . $customField->id => 1, | |
9099cab3 | 132 | ]; |
6a488035 TO |
133 | $values = CRM_Core_BAO_CustomValueTable::getValues($customValueParams); |
134 | $this->assertEquals('Test custom value', $values['custom_' . $customField->id], 'Check the custom field value'); | |
135 | ||
136 | $this->assertEquals($params['trxn_id'], $contribution->trxn_id, 'Check for transcation id creation.'); | |
137 | $this->assertEquals($contactId, $contribution->contact_id, 'Check for contact id for Conribution.'); | |
6a488035 TO |
138 | } |
139 | ||
d73f286e SL |
140 | /** |
141 | * CRM-21026 Test ContributionCount after contribution created with disabled FT | |
142 | */ | |
143 | public function testContributionCountDisabledFinancialType() { | |
144 | $contactId = $this->individualCreate(); | |
9099cab3 | 145 | $financialType = [ |
d73f286e SL |
146 | 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7), |
147 | 'is_reserved' => 0, | |
148 | 'is_active' => 0, | |
9099cab3 | 149 | ]; |
d73f286e | 150 | $finType = $this->callAPISuccess('financial_type', 'create', $financialType); |
9099cab3 | 151 | $params = [ |
d73f286e SL |
152 | 'contact_id' => $contactId, |
153 | 'currency' => 'USD', | |
154 | 'financial_type_id' => $finType['id'], | |
155 | 'contribution_status_id' => 1, | |
156 | 'payment_instrument_id' => 1, | |
157 | 'source' => 'STUDENT', | |
158 | 'receive_date' => '20080522000000', | |
159 | 'receipt_date' => '20080522000000', | |
160 | 'id' => NULL, | |
161 | 'non_deductible_amount' => 0.00, | |
162 | 'total_amount' => 200.00, | |
163 | 'fee_amount' => 5, | |
164 | 'net_amount' => 195, | |
165 | 'trxn_id' => '22ereerwww322323', | |
166 | 'invoice_id' => '22ed39c9e9ee6ef6031621ce0eafe6da70', | |
167 | 'thankyou_date' => '20080522', | |
9099cab3 | 168 | ]; |
d0c97775 | 169 | $this->callAPISuccess('Contribution', 'create', $params); |
9099cab3 | 170 | $this->callAPISuccess('financial_type', 'create', ['is_active' => 0, 'id' => $finType['id']]); |
d73f286e SL |
171 | $contributionCount = CRM_Contribute_BAO_Contribution::contributionCount($contactId); |
172 | $this->assertEquals(1, $contributionCount); | |
173 | } | |
174 | ||
6a488035 | 175 | /** |
100fef9d | 176 | * DeleteContribution() method |
6a488035 | 177 | */ |
00be9182 | 178 | public function testDeleteContribution() { |
2449fe69 | 179 | $contactId = $this->individualCreate(); |
6a488035 | 180 | |
9099cab3 | 181 | $params = [ |
6a488035 TO |
182 | 'contact_id' => $contactId, |
183 | 'currency' => 'USD', | |
92915c55 | 184 | 'financial_type_id' => 1, |
6a488035 TO |
185 | 'contribution_status_id' => 1, |
186 | 'payment_instrument_id' => 1, | |
187 | 'source' => 'STUDENT', | |
188 | 'receive_date' => '20080522000000', | |
189 | 'receipt_date' => '20080522000000', | |
190 | 'id' => NULL, | |
191 | 'non_deductible_amount' => 0.00, | |
192 | 'total_amount' => 200.00, | |
193 | 'fee_amount' => 5, | |
194 | 'net_amount' => 195, | |
195 | 'trxn_id' => '33ereerwww322323', | |
196 | 'invoice_id' => '33ed39c9e9ee6ef6031621ce0eafe6da70', | |
197 | 'thankyou_date' => '20080522', | |
55ee9063 | 198 | 'sequential' => TRUE, |
9099cab3 | 199 | ]; |
6a488035 | 200 | |
55ee9063 | 201 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
6a488035 | 202 | |
55ee9063 | 203 | CRM_Contribute_BAO_Contribution::deleteContribution($contribution['id']); |
6a488035 | 204 | |
55ee9063 | 205 | $this->assertDBNull('CRM_Contribute_DAO_Contribution', $contribution['trxn_id'], |
6a488035 TO |
206 | 'id', 'trxn_id', 'Database check for deleted Contribution.' |
207 | ); | |
6a488035 TO |
208 | } |
209 | ||
210 | /** | |
2449fe69 | 211 | * Create honor-contact method. |
6a488035 | 212 | */ |
2449fe69 | 213 | public function testCreateAndGetHonorContact() { |
6a488035 | 214 | $firstName = 'John_' . substr(sha1(rand()), 0, 7); |
92915c55 TO |
215 | $lastName = 'Smith_' . substr(sha1(rand()), 0, 7); |
216 | $email = "{$firstName}.{$lastName}@example.com"; | |
6a488035 | 217 | |
8381af80 | 218 | //Get profile id of name honoree_individual used to create profileContact |
219 | $honoreeProfileId = NULL; | |
220 | $ufGroupDAO = new CRM_Core_DAO_UFGroup(); | |
221 | $ufGroupDAO->name = 'honoree_individual'; | |
222 | if ($ufGroupDAO->find(TRUE)) { | |
223 | $honoreeProfileId = $ufGroupDAO->id; | |
224 | } | |
225 | ||
9099cab3 | 226 | $params = [ |
8381af80 | 227 | 'prefix_id' => 3, |
228 | 'first_name' => $firstName, | |
229 | 'last_name' => $lastName, | |
230 | 'email-1' => $email, | |
9099cab3 CW |
231 | ]; |
232 | $softParam = ['soft_credit_type_id' => 1]; | |
8381af80 | 233 | |
7b4b0b68 SL |
234 | $null = []; |
235 | $honoreeContactId = CRM_Contact_BAO_Contact::createProfileContact($params, $null, | |
92915c55 TO |
236 | NULL, NULL, $honoreeProfileId |
237 | ); | |
6a488035 | 238 | |
8381af80 | 239 | $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName, |
6a488035 TO |
240 | 'Database check for created honor contact record.' |
241 | ); | |
242 | //create contribution on behalf of honary. | |
243 | ||
9099cab3 | 244 | $contactId = $this->individualCreate(['first_name' => 'John', 'last_name' => 'Doe']); |
6a488035 | 245 | |
9099cab3 | 246 | $param = [ |
6a488035 TO |
247 | 'contact_id' => $contactId, |
248 | 'currency' => 'USD', | |
92915c55 | 249 | 'financial_type_id' => 4, |
6a488035 TO |
250 | 'contribution_status_id' => 1, |
251 | 'receive_date' => date('Ymd'), | |
252 | 'total_amount' => 66, | |
d0c97775 | 253 | 'sequential' => 1, |
9099cab3 | 254 | ]; |
6a488035 | 255 | |
d0c97775 | 256 | $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0]; |
257 | $id = $contribution['id']; | |
2449fe69 | 258 | $softParam['contact_id'] = $honoreeContactId; |
8381af80 | 259 | $softParam['contribution_id'] = $id; |
d0c97775 | 260 | $softParam['currency'] = $contribution['currency']; |
261 | $softParam['amount'] = $contribution['total_amount']; | |
8381af80 | 262 | |
263 | //Create Soft Contribution for honoree contact | |
264 | CRM_Contribute_BAO_ContributionSoft::add($softParam); | |
265 | ||
266 | $this->assertDBCompareValue('CRM_Contribute_DAO_ContributionSoft', $id, 'contact_id', | |
267 | 'contribution_id', $honoreeContactId, 'Check DB for honor contact of the contribution' | |
6a488035 | 268 | ); |
e4f46be0 | 269 | //get honorary information |
8381af80 | 270 | $getHonorContact = CRM_Contribute_BAO_Contribution::getHonorContacts($honoreeContactId); |
9099cab3 CW |
271 | $this->assertEquals([ |
272 | $id => [ | |
4ff25fbb | 273 | 'honor_type' => 'In Honor of', |
2449fe69 | 274 | 'honorId' => $contactId, |
275 | 'display_name' => 'Mr. John Doe II', | |
4ff25fbb | 276 | 'type' => 'Event Fee', |
277 | 'type_id' => '4', | |
278 | 'amount' => '$ 66.00', | |
279 | 'source' => NULL, | |
755ca72c | 280 | 'receive_date' => date('Y-m-d 00:00:00'), |
4ff25fbb | 281 | 'contribution_status' => 'Completed', |
9099cab3 CW |
282 | ], |
283 | ], $getHonorContact); | |
6a488035 | 284 | |
8381af80 | 285 | $this->assertDBCompareValue('CRM_Contact_DAO_Contact', $honoreeContactId, 'first_name', 'id', $firstName, |
6a488035 TO |
286 | 'Database check for created honor contact record.' |
287 | ); | |
288 | ||
289 | //get annual contribution information | |
290 | $annual = CRM_Contribute_BAO_Contribution::annual($contactId); | |
291 | ||
d0c97775 | 292 | $currencySymbol = CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_Currency', CRM_Core_Config::singleton()->defaultCurrency, 'symbol', 'name'); |
6a488035 TO |
293 | $this->assertDBCompareValue('CRM_Contribute_DAO_Contribution', $id, 'total_amount', |
294 | 'id', ltrim($annual[2], $currencySymbol), 'Check DB for total amount of the contribution' | |
295 | ); | |
6a488035 TO |
296 | } |
297 | ||
6b60d32c | 298 | /** |
299 | * Test that financial type data is not added to the annual query if acls not enabled. | |
300 | */ | |
301 | public function testAnnualQueryWithFinancialACLsEnabled() { | |
302 | $this->enableFinancialACLs(); | |
303 | $this->createLoggedInUserWithFinancialACL(); | |
304 | $permittedFinancialType = CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'financial_type_id', 'Donation'); | |
305 | $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]); | |
306 | $this->assertContains('SUM(total_amount) as amount,', $sql); | |
307 | $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql); | |
308 | $this->assertContains('b.financial_type_id IN (' . $permittedFinancialType . ')', $sql); | |
309 | ||
310 | // Run it to make sure it's not bad sql. | |
311 | CRM_Core_DAO::executeQuery($sql); | |
312 | $this->disableFinancialACLs(); | |
313 | } | |
314 | ||
53666099 | 315 | /** |
316 | * Test the annual query returns a correct result when multiple line items are present. | |
317 | */ | |
318 | public function testAnnualWithMultipleLineItems() { | |
319 | $contactID = $this->createLoggedInUserWithFinancialACL(); | |
320 | $this->createContributionWithTwoLineItemsAgainstPriceSet([ | |
39b959db SL |
321 | 'contact_id' => $contactID, |
322 | ] | |
53666099 | 323 | ); |
324 | $this->enableFinancialACLs(); | |
325 | $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([$contactID]); | |
326 | $result = CRM_Core_DAO::executeQuery($sql); | |
327 | $result->fetch(); | |
328 | $this->assertEquals(300, $result->amount); | |
329 | $this->assertEquals(1, $result->count); | |
330 | $this->disableFinancialACLs(); | |
331 | } | |
332 | ||
6b60d32c | 333 | /** |
334 | * Test that financial type data is not added to the annual query if acls not enabled. | |
335 | */ | |
336 | public function testAnnualQueryWithFinancialACLsDisabled() { | |
337 | $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]); | |
338 | $this->assertContains('SUM(total_amount) as amount,', $sql); | |
339 | $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql); | |
340 | $this->assertNotContains('b.financial_type_id', $sql); | |
341 | //$this->assertNotContains('line_item', $sql); | |
342 | // Run it to make sure it's not bad sql. | |
343 | CRM_Core_DAO::executeQuery($sql); | |
344 | } | |
345 | ||
c77f8667 | 346 | /** |
347 | * Test that financial type data is not added to the annual query if acls not enabled. | |
348 | */ | |
349 | public function testAnnualQueryWithFinancialHook() { | |
9099cab3 | 350 | $this->hookClass->setHook('civicrm_selectWhereClause', [$this, 'aclIdNoZero']); |
c77f8667 | 351 | $sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]); |
352 | $this->assertContains('SUM(total_amount) as amount,', $sql); | |
353 | $this->assertContains('WHERE b.contact_id IN (1,2,3)', $sql); | |
354 | $this->assertContains('b.id NOT IN (0)', $sql); | |
355 | $this->assertNotContains('b.financial_type_id', $sql); | |
356 | CRM_Core_DAO::executeQuery($sql); | |
357 | } | |
358 | ||
359 | /** | |
360 | * Add ACL denying values LIKE '0'. | |
361 | * | |
362 | * @param string $entity | |
363 | * @param string $clauses | |
364 | */ | |
365 | public function aclIdNoZero($entity, &$clauses) { | |
366 | if ($entity != 'Contribution') { | |
367 | return; | |
368 | } | |
369 | $clauses['id'] = "NOT IN (0)"; | |
370 | } | |
371 | ||
6a488035 | 372 | /** |
eceb18cc | 373 | * Display sort name during. |
b581842f | 374 | * Update multiple contributions |
6a488035 TO |
375 | * sortName(); |
376 | */ | |
00be9182 | 377 | public function testsortName() { |
9099cab3 | 378 | $params = [ |
6a488035 TO |
379 | 'first_name' => 'Shane', |
380 | 'last_name' => 'Whatson', | |
381 | 'contact_type' => 'Individual', | |
9099cab3 | 382 | ]; |
6a488035 TO |
383 | |
384 | $contact = CRM_Contact_BAO_Contact::add($params); | |
385 | ||
386 | //Now check $contact is object of contact DAO.. | |
387 | $this->assertInstanceOf('CRM_Contact_DAO_Contact', $contact, 'Check for created object'); | |
388 | ||
389 | $contactId = $contact->id; | |
9099cab3 | 390 | $param = [ |
6a488035 TO |
391 | 'contact_id' => $contactId, |
392 | 'currency' => 'USD', | |
92915c55 | 393 | 'financial_type_id' => 1, |
6a488035 TO |
394 | 'contribution_status_id' => 1, |
395 | 'payment_instrument_id' => 1, | |
396 | 'source' => 'STUDENT', | |
397 | 'receive_date' => '20080522000000', | |
398 | 'receipt_date' => '20080522000000', | |
399 | 'id' => NULL, | |
400 | 'non_deductible_amount' => 0.00, | |
401 | 'total_amount' => 300.00, | |
402 | 'fee_amount' => 5, | |
403 | 'net_amount' => 295, | |
404 | 'trxn_id' => '22ereerwww323', | |
405 | 'invoice_id' => '22ed39c9e9ee621ce0eafe6da70', | |
406 | 'thankyou_date' => '20080522', | |
d0c97775 | 407 | 'sequential' => TRUE, |
9099cab3 | 408 | ]; |
6a488035 | 409 | |
d0c97775 | 410 | $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0]; |
6a488035 | 411 | |
d0c97775 | 412 | $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); |
413 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
6a488035 | 414 | |
b581842f | 415 | //display sort name during Update multiple contributions |
d0c97775 | 416 | $sortName = CRM_Contribute_BAO_Contribution::sortName($contribution['id']); |
6a488035 TO |
417 | |
418 | $this->assertEquals('Whatson, Shane', $sortName, 'Check for sort name.'); | |
6a488035 TO |
419 | } |
420 | ||
421 | /** | |
eceb18cc | 422 | * Add premium during online Contribution. |
6a488035 TO |
423 | * |
424 | * AddPremium(); | |
425 | */ | |
00be9182 | 426 | public function testAddPremium() { |
2449fe69 | 427 | $contactId = $this->individualCreate(); |
6a488035 | 428 | |
9099cab3 | 429 | $params = [ |
6a488035 TO |
430 | 'name' => 'TEST Premium', |
431 | 'sku' => 111, | |
432 | 'imageOption' => 'noImage', | |
433 | 'MAX_FILE_SIZE' => 2097152, | |
434 | 'price' => 100.00, | |
435 | 'cost' => 90.00, | |
436 | 'min_contribution' => 100, | |
437 | 'is_active' => 1, | |
9099cab3 | 438 | ]; |
32f27499 | 439 | $premium = CRM_Contribute_BAO_Product::create($params); |
6a488035 TO |
440 | |
441 | $this->assertEquals('TEST Premium', $premium->name, 'Check for premium name.'); | |
442 | ||
9099cab3 | 443 | $contributionParams = [ |
6a488035 TO |
444 | 'contact_id' => $contactId, |
445 | 'currency' => 'USD', | |
92915c55 | 446 | 'financial_type_id' => 1, |
6a488035 TO |
447 | 'contribution_status_id' => 1, |
448 | 'payment_instrument_id' => 1, | |
449 | 'source' => 'STUDENT', | |
450 | 'receive_date' => '20080522000000', | |
451 | 'receipt_date' => '20080522000000', | |
452 | 'id' => NULL, | |
453 | 'non_deductible_amount' => 0.00, | |
454 | 'total_amount' => 300.00, | |
455 | 'fee_amount' => 5, | |
456 | 'net_amount' => 295, | |
457 | 'trxn_id' => '33erdfrwvw434', | |
458 | 'invoice_id' => '98ed34f7u9hh672ce0eafe8fb92', | |
459 | 'thankyou_date' => '20080522', | |
d0c97775 | 460 | 'sequential' => TRUE, |
9099cab3 | 461 | ]; |
a275d4b6 | 462 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0]; |
6a488035 | 463 | |
a275d4b6 | 464 | $this->assertEquals($contributionParams['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); |
d0c97775 | 465 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); |
6a488035 TO |
466 | |
467 | //parameter for adding premium to contribution | |
9099cab3 | 468 | $data = [ |
6a488035 | 469 | 'product_id' => $premium->id, |
d0c97775 | 470 | 'contribution_id' => $contribution['id'], |
6a488035 TO |
471 | 'product_option' => NULL, |
472 | 'quantity' => 1, | |
9099cab3 | 473 | ]; |
6a488035 TO |
474 | $contributionProduct = CRM_Contribute_BAO_Contribution::addPremium($data); |
475 | $this->assertEquals($contributionProduct->product_id, $premium->id, 'Check for Product id .'); | |
476 | ||
477 | //Delete Product | |
37828d4f | 478 | CRM_Contribute_BAO_Product::del($premium->id); |
6a488035 TO |
479 | $this->assertDBNull('CRM_Contribute_DAO_Product', $premium->name, |
480 | 'id', 'name', 'Database check for deleted Product.' | |
481 | ); | |
6a488035 TO |
482 | } |
483 | ||
484 | /** | |
fe482240 | 485 | * Check duplicate contribution id. |
6a488035 TO |
486 | * during the contribution import |
487 | * checkDuplicateIds(); | |
488 | */ | |
00be9182 | 489 | public function testcheckDuplicateIds() { |
2449fe69 | 490 | $contactId = $this->individualCreate(); |
6a488035 | 491 | |
9099cab3 | 492 | $param = [ |
6a488035 TO |
493 | 'contact_id' => $contactId, |
494 | 'currency' => 'USD', | |
92915c55 | 495 | 'financial_type_id' => 1, |
6a488035 TO |
496 | 'contribution_status_id' => 1, |
497 | 'payment_instrument_id' => 1, | |
498 | 'source' => 'STUDENT', | |
499 | 'receive_date' => '20080522000000', | |
500 | 'receipt_date' => '20080522000000', | |
501 | 'id' => NULL, | |
502 | 'non_deductible_amount' => 0.00, | |
503 | 'total_amount' => 300.00, | |
504 | 'fee_amount' => 5, | |
505 | 'net_amount' => 295, | |
506 | 'trxn_id' => '76ereeswww835', | |
507 | 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82', | |
508 | 'thankyou_date' => '20080522', | |
d0c97775 | 509 | 'sequential' => TRUE, |
9099cab3 | 510 | ]; |
6a488035 | 511 | |
d0c97775 | 512 | $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0]; |
6a488035 | 513 | |
d0c97775 | 514 | $this->assertEquals($param['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); |
515 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
9099cab3 | 516 | $data = [ |
d0c97775 | 517 | 'id' => $contribution['id'], |
518 | 'trxn_id' => $contribution['trxn_id'], | |
519 | 'invoice_id' => $contribution['invoice_id'], | |
9099cab3 | 520 | ]; |
6a488035 | 521 | $contributionID = CRM_Contribute_BAO_Contribution::checkDuplicateIds($data); |
d0c97775 | 522 | $this->assertEquals($contributionID, $contribution['id'], 'Check for duplicate transcation id .'); |
6a488035 | 523 | } |
96025800 | 524 | |
b04e4b11 PN |
525 | /** |
526 | * Create() method (create and update modes). | |
527 | */ | |
528 | public function testIsPaymentFlag() { | |
2449fe69 | 529 | $contactId = $this->individualCreate(); |
b04e4b11 | 530 | |
d0c97775 | 531 | $params = [ |
b04e4b11 PN |
532 | 'contact_id' => $contactId, |
533 | 'currency' => 'USD', | |
534 | 'financial_type_id' => 1, | |
535 | 'contribution_status_id' => 1, | |
536 | 'payment_instrument_id' => 1, | |
537 | 'source' => 'STUDENT', | |
538 | 'receive_date' => '20080522000000', | |
539 | 'receipt_date' => '20080522000000', | |
540 | 'non_deductible_amount' => 0.00, | |
541 | 'total_amount' => 200.00, | |
542 | 'fee_amount' => 5, | |
543 | 'net_amount' => 195, | |
544 | 'trxn_id' => '22ereerwww4444xx', | |
545 | 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81', | |
546 | 'thankyou_date' => '20080522', | |
d0c97775 | 547 | 'sequential' => TRUE, |
548 | ]; | |
549 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; | |
b04e4b11 | 550 | |
d0c97775 | 551 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); |
552 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
b04e4b11 | 553 | |
9099cab3 | 554 | $trxnArray = [ |
b04e4b11 PN |
555 | 'trxn_id' => $params['trxn_id'], |
556 | 'is_payment' => 1, | |
9099cab3 CW |
557 | ]; |
558 | $defaults = []; | |
b04e4b11 | 559 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
4ba3c75f | 560 | $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.'); |
b04e4b11 | 561 | //update contribution amount |
d0c97775 | 562 | $params['id'] = $contribution['id']; |
b04e4b11 | 563 | $params['total_amount'] = 150; |
d0c97775 | 564 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
b04e4b11 | 565 | |
d0c97775 | 566 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .'); |
567 | $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for Amount updation.'); | |
9099cab3 | 568 | $trxnArray = [ |
b04e4b11 PN |
569 | 'trxn_id' => $params['trxn_id'], |
570 | 'is_payment' => 1, | |
9099cab3 CW |
571 | ]; |
572 | $defaults = []; | |
b04e4b11 | 573 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
4ba3c75f | 574 | $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); |
b04e4b11 PN |
575 | $trxnArray['is_payment'] = 0; |
576 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); | |
4ba3c75f | 577 | $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.'); |
b04e4b11 PN |
578 | } |
579 | ||
a387acc9 PN |
580 | /** |
581 | * Create() method (create and update modes). | |
582 | */ | |
583 | public function testIsPaymentFlagForPending() { | |
2449fe69 | 584 | $contactId = $this->individualCreate(); |
a387acc9 | 585 | |
9099cab3 | 586 | $params = [ |
a387acc9 PN |
587 | 'contact_id' => $contactId, |
588 | 'currency' => 'USD', | |
589 | 'financial_type_id' => 1, | |
590 | 'contribution_status_id' => 2, | |
591 | 'payment_instrument_id' => 1, | |
592 | 'source' => 'STUDENT', | |
593 | 'is_pay_later' => 1, | |
594 | 'receive_date' => '20080522000000', | |
595 | 'receipt_date' => '20080522000000', | |
596 | 'non_deductible_amount' => 0.00, | |
597 | 'total_amount' => 200.00, | |
598 | 'fee_amount' => 5, | |
599 | 'net_amount' => 195, | |
600 | 'trxn_id' => '22ereerwww4444yy', | |
601 | 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81', | |
602 | 'thankyou_date' => '20080522', | |
d0c97775 | 603 | 'sequential' => TRUE, |
9099cab3 | 604 | ]; |
a387acc9 | 605 | |
d0c97775 | 606 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
a387acc9 | 607 | |
d0c97775 | 608 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.'); |
609 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
a387acc9 | 610 | |
9099cab3 | 611 | $trxnArray = [ |
a387acc9 PN |
612 | 'trxn_id' => $params['trxn_id'], |
613 | 'is_payment' => 0, | |
9099cab3 CW |
614 | ]; |
615 | $defaults = []; | |
a387acc9 PN |
616 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
617 | $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); | |
618 | $trxnArray['is_payment'] = 1; | |
619 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); | |
620 | $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.'); | |
621 | //update contribution amount | |
d0c97775 | 622 | $params['id'] = $contribution['id']; |
a387acc9 PN |
623 | $params['contribution_status_id'] = 1; |
624 | ||
d0c97775 | 625 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
a387acc9 | 626 | |
d0c97775 | 627 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .'); |
628 | $this->assertEquals($params['contribution_status_id'], $contribution['contribution_status_id'], 'Check for status updation.'); | |
9099cab3 | 629 | $trxnArray = [ |
a387acc9 PN |
630 | 'trxn_id' => $params['trxn_id'], |
631 | 'is_payment' => 1, | |
9099cab3 CW |
632 | ]; |
633 | $defaults = []; | |
a387acc9 PN |
634 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
635 | $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.'); | |
636 | $trxnArray['is_payment'] = 0; | |
637 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); | |
638 | $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); | |
a387acc9 PN |
639 | } |
640 | ||
0a5651eb PN |
641 | /** |
642 | * checks db values for financial item | |
643 | */ | |
624195c8 | 644 | public function checkItemValues($contribution) { |
876b8ab0 | 645 | $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is'); |
2449fe69 | 646 | $query = "SELECT eft1.entity_id, ft.total_amount, eft1.amount FROM civicrm_financial_trxn ft INNER JOIN civicrm_entity_financial_trxn eft ON (eft.financial_trxn_id = ft.id AND eft.entity_table = 'civicrm_contribution') |
eec619df PN |
647 | INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item') |
648 | WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; | |
649 | ||
9099cab3 CW |
650 | $queryParams[1] = [$contribution->id, 'Integer']; |
651 | $queryParams[2] = [$toFinancialAccount, 'Integer']; | |
eec619df PN |
652 | |
653 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
9099cab3 | 654 | $amounts = [100.00, 50.00]; |
eec619df PN |
655 | while ($dao->fetch()) { |
656 | $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.'); | |
19084b68 | 657 | $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item'); |
eec619df | 658 | } |
eec619df PN |
659 | } |
660 | ||
0a5651eb PN |
661 | /** |
662 | * assignProportionalLineItems() method (add and edit modes of participant) | |
3fc37a30 | 663 | * |
664 | * @throws \CRM_Core_Exception | |
665 | * @throws \CiviCRM_API3_Exception | |
0a5651eb PN |
666 | */ |
667 | public function testAssignProportionalLineItems() { | |
3fc37a30 | 668 | $contribution = $this->addParticipantWithContribution(); |
669 | // Delete existing financial_trxns. This is because we are testing a code flow we | |
670 | // want to deprecate & remove & the test relies on bad data asa starting point. | |
671 | // End goal is the Order.create->Payment.create flow. | |
672 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_entity_financial_trxn WHERE entity_table = "civicrm_financial_item"'); | |
9099cab3 | 673 | $params = [ |
0a5651eb PN |
674 | 'contribution_id' => $contribution->id, |
675 | 'total_amount' => 150.00, | |
9099cab3 | 676 | ]; |
0a5651eb PN |
677 | $trxn = new CRM_Financial_DAO_FinancialTrxn(); |
678 | $trxn->orderBy('id DESC'); | |
679 | $trxn->find(TRUE); | |
8de1ade9 | 680 | CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution->total_amount); |
0a5651eb PN |
681 | $this->checkItemValues($contribution); |
682 | } | |
683 | ||
eec619df PN |
684 | /** |
685 | * Add participant with contribution | |
686 | * | |
687 | * @return array | |
3fc37a30 | 688 | * |
689 | * @throws \CRM_Core_Exception | |
690 | * @throws \CiviCRM_API3_Exception | |
eec619df | 691 | */ |
78c99516 | 692 | public function addParticipantWithContribution() { |
eec619df | 693 | // creating price set, price field |
2449fe69 | 694 | $this->_contactId = $this->individualCreate(); |
3fc37a30 | 695 | $event = $this->eventCreatePaid([]); |
2449fe69 | 696 | $this->_eventId = $event['id']; |
3fc37a30 | 697 | $priceSetId = $this->priceSetID; |
9099cab3 | 698 | $paramsField = [ |
eec619df PN |
699 | 'label' => 'Price Field', |
700 | 'name' => CRM_Utils_String::titleToVar('Price Field'), | |
701 | 'html_type' => 'CheckBox', | |
9099cab3 CW |
702 | 'option_label' => ['1' => 'Price Field 1', '2' => 'Price Field 2'], |
703 | 'option_value' => ['1' => 100, '2' => 200], | |
704 | 'option_name' => ['1' => 'Price Field 1', '2' => 'Price Field 2'], | |
705 | 'option_weight' => ['1' => 1, '2' => 2], | |
706 | 'option_amount' => ['1' => 100, '2' => 200], | |
eec619df PN |
707 | 'is_display_amounts' => 1, |
708 | 'weight' => 1, | |
709 | 'options_per_line' => 1, | |
9099cab3 | 710 | 'is_active' => ['1' => 1, '2' => 1], |
3fc37a30 | 711 | 'price_set_id' => $this->priceSetID, |
eec619df PN |
712 | 'is_enter_qty' => 1, |
713 | 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'), | |
9099cab3 | 714 | ]; |
eec619df | 715 | $priceField = CRM_Price_BAO_PriceField::create($paramsField); |
9099cab3 | 716 | $eventParams = [ |
eec619df PN |
717 | 'id' => $this->_eventId, |
718 | 'financial_type_id' => 4, | |
719 | 'is_monetary' => 1, | |
9099cab3 | 720 | ]; |
eec619df PN |
721 | CRM_Event_BAO_Event::create($eventParams); |
722 | CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId); | |
e4ba8498 | 723 | |
9099cab3 CW |
724 | $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id]); |
725 | $participantParams = [ | |
eec619df PN |
726 | 'financial_type_id' => 4, |
727 | 'event_id' => $this->_eventId, | |
728 | 'role_id' => 1, | |
729 | 'status_id' => 14, | |
730 | 'fee_currency' => 'USD', | |
731 | 'contact_id' => $this->_contactId, | |
9099cab3 | 732 | ]; |
eec619df | 733 | $participant = CRM_Event_BAO_Participant::add($participantParams); |
9099cab3 | 734 | $contributionParams = [ |
3fc37a30 | 735 | 'total_amount' => 300, |
eec619df PN |
736 | 'currency' => 'USD', |
737 | 'contact_id' => $this->_contactId, | |
738 | 'financial_type_id' => 4, | |
3fc37a30 | 739 | 'contribution_status_id' => 'Pending', |
eec619df PN |
740 | 'contribution_mode' => 'participant', |
741 | 'participant_id' => $participant->id, | |
d0c97775 | 742 | 'sequential' => TRUE, |
3fc37a30 | 743 | 'api.Payment.create' => ['total_amount' => 150], |
9099cab3 | 744 | ]; |
eec619df PN |
745 | |
746 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 747 | $lineItems[1][$key] = [ |
eec619df PN |
748 | 'price_field_id' => $priceField['price_field_id'], |
749 | 'price_field_value_id' => $priceField['id'], | |
750 | 'label' => $priceField['label'], | |
751 | 'field_title' => $priceField['label'], | |
752 | 'qty' => 1, | |
753 | 'unit_price' => $priceField['amount'], | |
754 | 'line_total' => $priceField['amount'], | |
755 | 'financial_type_id' => $priceField['financial_type_id'], | |
9099cab3 | 756 | ]; |
eec619df PN |
757 | } |
758 | $contributionParams['line_item'] = $lineItems; | |
d0c97775 | 759 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0]; |
eec619df | 760 | |
9099cab3 | 761 | $paymentParticipant = [ |
eec619df | 762 | 'participant_id' => $participant->id, |
d0c97775 | 763 | 'contribution_id' => $contribution['id'], |
9099cab3 | 764 | ]; |
a5750507 | 765 | CRM_Event_BAO_ParticipantPayment::create($paymentParticipant); |
eec619df | 766 | |
d0c97775 | 767 | $contributionObject = new CRM_Contribute_BAO_Contribution(); |
768 | $contributionObject->id = $contribution['id']; | |
769 | $contributionObject->find(TRUE); | |
770 | ||
3fc37a30 | 771 | return $contributionObject; |
eec619df PN |
772 | } |
773 | ||
5c8b902b | 774 | /** |
a4a31ffe | 775 | * checkLineItems() check if total amount matches the sum of line total |
5c8b902b PN |
776 | */ |
777 | public function testcheckLineItems() { | |
9099cab3 | 778 | $params = [ |
5c8b902b PN |
779 | 'contact_id' => 202, |
780 | 'receive_date' => '2010-01-20', | |
781 | 'total_amount' => 100, | |
782 | 'financial_type_id' => 3, | |
9099cab3 CW |
783 | 'line_items' => [ |
784 | [ | |
785 | 'line_item' => [ | |
786 | [ | |
5c8b902b PN |
787 | 'entity_table' => 'civicrm_contribution', |
788 | 'price_field_id' => 8, | |
789 | 'price_field_value_id' => 16, | |
790 | 'label' => 'test 1', | |
791 | 'qty' => 1, | |
792 | 'unit_price' => 100, | |
793 | 'line_total' => 100, | |
9099cab3 CW |
794 | ], |
795 | [ | |
5c8b902b PN |
796 | 'entity_table' => 'civicrm_contribution', |
797 | 'price_field_id' => 8, | |
798 | 'price_field_value_id' => 17, | |
799 | 'label' => 'Test 2', | |
800 | 'qty' => 1, | |
801 | 'unit_price' => 200, | |
802 | 'line_total' => 200, | |
803 | 'financial_type_id' => 1, | |
9099cab3 CW |
804 | ], |
805 | ], | |
806 | 'params' => [], | |
807 | ], | |
808 | ], | |
809 | ]; | |
c16c6ad8 | 810 | |
af004c04 | 811 | try { |
2449fe69 | 812 | CRM_Contribute_BAO_Contribution::checkLineItems($params); |
af004c04 PN |
813 | $this->fail("Missed expected exception"); |
814 | } | |
c16c6ad8 CR |
815 | catch (CRM_Contribute_Exception_CheckLineItemsException $e) { |
816 | $this->assertEquals( | |
817 | CRM_Contribute_Exception_CheckLineItemsException::LINE_ITEM_DIFFERRING_TOTAL_EXCEPTON_MSG, | |
818 | $e->getMessage() | |
819 | ); | |
af004c04 | 820 | } |
c16c6ad8 | 821 | |
5c8b902b PN |
822 | $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']); |
823 | $params['total_amount'] = 300; | |
c16c6ad8 | 824 | |
af004c04 | 825 | CRM_Contribute_BAO_Contribution::checkLineItems($params); |
5c8b902b PN |
826 | } |
827 | ||
c16c6ad8 CR |
828 | /** |
829 | * Tests CRM_Contribute_BAO_Contribution::checkLineItems() method works with | |
830 | * floating point values. | |
831 | */ | |
832 | public function testCheckLineItemsWithFloatingPointValues() { | |
9099cab3 | 833 | $params = [ |
c16c6ad8 CR |
834 | 'contact_id' => 202, |
835 | 'receive_date' => date('Y-m-d'), | |
836 | 'total_amount' => 16.67, | |
837 | 'financial_type_id' => 3, | |
9099cab3 CW |
838 | 'line_items' => [ |
839 | [ | |
840 | 'line_item' => [ | |
841 | [ | |
c16c6ad8 CR |
842 | 'entity_table' => 'civicrm_contribution', |
843 | 'price_field_id' => 8, | |
844 | 'price_field_value_id' => 16, | |
845 | 'label' => 'test 1', | |
846 | 'qty' => 1, | |
847 | 'unit_price' => 14.85, | |
848 | 'line_total' => 14.85, | |
9099cab3 CW |
849 | ], |
850 | [ | |
c16c6ad8 CR |
851 | 'entity_table' => 'civicrm_contribution', |
852 | 'price_field_id' => 8, | |
853 | 'price_field_value_id' => 17, | |
854 | 'label' => 'Test 2', | |
855 | 'qty' => 1, | |
856 | 'unit_price' => 1.66, | |
857 | 'line_total' => 1.66, | |
858 | 'financial_type_id' => 1, | |
9099cab3 CW |
859 | ], |
860 | [ | |
c16c6ad8 CR |
861 | 'entity_table' => 'civicrm_contribution', |
862 | 'price_field_id' => 8, | |
863 | 'price_field_value_id' => 17, | |
864 | 'label' => 'Test 2', | |
865 | 'qty' => 1, | |
866 | 'unit_price' => 0.16, | |
867 | 'line_total' => 0.16, | |
868 | 'financial_type_id' => 1, | |
9099cab3 CW |
869 | ], |
870 | ], | |
871 | 'params' => [], | |
872 | ], | |
873 | ], | |
874 | ]; | |
c16c6ad8 CR |
875 | |
876 | $foundException = FALSE; | |
877 | ||
878 | try { | |
879 | CRM_Contribute_BAO_Contribution::checkLineItems($params); | |
880 | } | |
881 | catch (CRM_Contribute_Exception_CheckLineItemsException $e) { | |
882 | $foundException = TRUE; | |
883 | } | |
884 | ||
885 | $this->assertFalse($foundException); | |
886 | } | |
887 | ||
88f7a518 E |
888 | /** |
889 | * Test activity amount updation. | |
890 | */ | |
891 | public function testActivityCreate() { | |
892 | $contactId = $this->individualCreate(); | |
9099cab3 | 893 | $defaults = []; |
88f7a518 | 894 | |
9099cab3 | 895 | $params = [ |
88f7a518 E |
896 | 'contact_id' => $contactId, |
897 | 'currency' => 'USD', | |
898 | 'financial_type_id' => 1, | |
899 | 'contribution_status_id' => 1, | |
900 | 'payment_instrument_id' => 1, | |
901 | 'source' => 'STUDENT', | |
902 | 'receive_date' => '20080522000000', | |
903 | 'receipt_date' => '20080522000000', | |
904 | 'non_deductible_amount' => 0.00, | |
905 | 'total_amount' => 100.00, | |
906 | 'trxn_id' => '22ereerwww444444', | |
907 | 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81', | |
908 | 'thankyou_date' => '20160519', | |
d0c97775 | 909 | 'sequential' => 1, |
9099cab3 | 910 | ]; |
88f7a518 | 911 | |
d0c97775 | 912 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
88f7a518 | 913 | |
d0c97775 | 914 | $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.'); |
915 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
88f7a518 E |
916 | |
917 | // Check amount in activity. | |
9099cab3 | 918 | $activityParams = [ |
d0c97775 | 919 | 'source_record_id' => $contribution['id'], |
d66c61b6 | 920 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contribution'), |
9099cab3 | 921 | ]; |
d66c61b6 | 922 | // @todo use api instead. |
88f7a518 E |
923 | $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults); |
924 | ||
d0c97775 | 925 | $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.'); |
88f7a518 E |
926 | $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.'); |
927 | ||
d0c97775 | 928 | $params['id'] = $contribution['id']; |
88f7a518 E |
929 | $params['total_amount'] = 200; |
930 | ||
d0c97775 | 931 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
88f7a518 | 932 | |
d0c97775 | 933 | $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.'); |
934 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
88f7a518 E |
935 | |
936 | // Retrieve activity again. | |
937 | $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults); | |
938 | ||
d0c97775 | 939 | $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.'); |
88f7a518 E |
940 | $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.'); |
941 | } | |
942 | ||
5c3d600f PN |
943 | /** |
944 | * Test allowUpdateRevenueRecognitionDate. | |
af595ac2 | 945 | * |
946 | * @throws \CRM_Core_Exception | |
5c3d600f PN |
947 | */ |
948 | public function testAllowUpdateRevenueRecognitionDate() { | |
949 | $contactId = $this->individualCreate(); | |
9099cab3 | 950 | $params = [ |
5c3d600f PN |
951 | 'contact_id' => $contactId, |
952 | 'receive_date' => '2010-01-20', | |
953 | 'total_amount' => 100, | |
4303ea89 | 954 | 'financial_type_id' => 4, |
af595ac2 | 955 | 'contribution_status_id' => 'Pending', |
9099cab3 | 956 | ]; |
af595ac2 | 957 | $order = $this->callAPISuccess('Order', 'create', $params); |
5c3d600f PN |
958 | $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']); |
959 | $this->assertTrue($allowUpdate); | |
960 | ||
961 | $event = $this->eventCreate(); | |
9099cab3 | 962 | $params = [ |
5c3d600f PN |
963 | 'contact_id' => $contactId, |
964 | 'receive_date' => '2010-01-20', | |
965 | 'total_amount' => 300, | |
8484a5f0 | 966 | 'financial_type_id' => $this->getFinancialTypeId('Event Fee'), |
af595ac2 | 967 | 'contribution_status_id' => 'Pending', |
9099cab3 | 968 | ]; |
5c3d600f PN |
969 | $priceFields = $this->createPriceSet('event', $event['id']); |
970 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 971 | $lineItems[$key] = [ |
5c3d600f PN |
972 | 'price_field_id' => $priceField['price_field_id'], |
973 | 'price_field_value_id' => $priceField['id'], | |
974 | 'label' => $priceField['label'], | |
975 | 'field_title' => $priceField['label'], | |
976 | 'qty' => 1, | |
977 | 'unit_price' => $priceField['amount'], | |
978 | 'line_total' => $priceField['amount'], | |
979 | 'financial_type_id' => $priceField['financial_type_id'], | |
980 | 'entity_table' => 'civicrm_participant', | |
9099cab3 | 981 | ]; |
5c3d600f | 982 | } |
9099cab3 | 983 | $params['line_items'][] = [ |
5c3d600f | 984 | 'line_item' => $lineItems, |
9099cab3 | 985 | 'params' => [ |
5c3d600f PN |
986 | 'contact_id' => $contactId, |
987 | 'event_id' => $event['id'], | |
988 | 'status_id' => 1, | |
989 | 'role_id' => 1, | |
990 | 'register_date' => '2007-07-21 00:00:00', | |
991 | 'source' => 'Online Event Registration: API Testing', | |
9099cab3 CW |
992 | ], |
993 | ]; | |
af595ac2 | 994 | $order = $this->callAPISuccess('Order', 'create', $params); |
5c3d600f PN |
995 | $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']); |
996 | $this->assertFalse($allowUpdate); | |
997 | ||
9099cab3 | 998 | $params = [ |
5c3d600f PN |
999 | 'contact_id' => $contactId, |
1000 | 'receive_date' => '2010-01-20', | |
1001 | 'total_amount' => 200, | |
8484a5f0 | 1002 | 'financial_type_id' => $this->getFinancialTypeId('Member Dues'), |
af595ac2 | 1003 | 'contribution_status_id' => 'Pending', |
9099cab3 | 1004 | ]; |
5c3d600f PN |
1005 | $membershipType = $this->membershipTypeCreate(); |
1006 | $priceFields = $this->createPriceSet(); | |
9099cab3 | 1007 | $lineItems = []; |
5c3d600f | 1008 | foreach ($priceFields['values'] as $key => $priceField) { |
9099cab3 | 1009 | $lineItems[$key] = [ |
5c3d600f PN |
1010 | 'price_field_id' => $priceField['price_field_id'], |
1011 | 'price_field_value_id' => $priceField['id'], | |
1012 | 'label' => $priceField['label'], | |
1013 | 'field_title' => $priceField['label'], | |
1014 | 'qty' => 1, | |
1015 | 'unit_price' => $priceField['amount'], | |
1016 | 'line_total' => $priceField['amount'], | |
1017 | 'financial_type_id' => $priceField['financial_type_id'], | |
1018 | 'entity_table' => 'civicrm_membership', | |
1019 | 'membership_type_id' => $membershipType, | |
9099cab3 | 1020 | ]; |
5c3d600f | 1021 | } |
9099cab3 CW |
1022 | $params['line_items'][] = [ |
1023 | 'line_item' => [array_pop($lineItems)], | |
1024 | 'params' => [ | |
5c3d600f PN |
1025 | 'contact_id' => $contactId, |
1026 | 'membership_type_id' => $membershipType, | |
1027 | 'join_date' => '2006-01-21', | |
1028 | 'start_date' => '2006-01-21', | |
1029 | 'end_date' => '2006-12-21', | |
1030 | 'source' => 'Payment', | |
1031 | 'is_override' => 1, | |
1032 | 'status_id' => 1, | |
9099cab3 CW |
1033 | ], |
1034 | ]; | |
af595ac2 | 1035 | $order = $this->callAPISuccess('Order', 'create', $params); |
5c3d600f PN |
1036 | $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']); |
1037 | $this->assertFalse($allowUpdate); | |
1038 | } | |
1039 | ||
d9553c2e PN |
1040 | /** |
1041 | * Test calculateFinancialItemAmount(). | |
1042 | */ | |
1043 | public function testcalculateFinancialItemAmount() { | |
9099cab3 CW |
1044 | $testParams = [ |
1045 | [ | |
1046 | 'params' => [], | |
1047 | 'amountParams' => [ | |
d9553c2e PN |
1048 | 'line_total' => 100, |
1049 | 'previous_line_total' => 300, | |
1050 | 'diff' => 1, | |
9099cab3 | 1051 | ], |
d9553c2e PN |
1052 | 'context' => 'changedAmount', |
1053 | 'expectedItemAmount' => -200, | |
9099cab3 CW |
1054 | ], |
1055 | [ | |
1056 | 'params' => [], | |
1057 | 'amountParams' => [ | |
d9553c2e PN |
1058 | 'line_total' => 100, |
1059 | 'previous_line_total' => 100, | |
1060 | 'diff' => -1, | |
9099cab3 | 1061 | ], |
efb16c63 | 1062 | // Most contexts are ignored. Removing refs to change payment instrument so placeholder. |
1063 | 'context' => 'not null', | |
d9553c2e | 1064 | 'expectedItemAmount' => -100, |
9099cab3 CW |
1065 | ], |
1066 | [ | |
1067 | 'params' => [ | |
d9553c2e PN |
1068 | 'is_quick_config' => TRUE, |
1069 | 'total_amount' => 110, | |
1070 | 'tax_amount' => 10, | |
9099cab3 CW |
1071 | ], |
1072 | 'amountParams' => [ | |
d9553c2e | 1073 | 'item_amount' => 100, |
9099cab3 | 1074 | ], |
d9553c2e PN |
1075 | 'context' => 'changedAmount', |
1076 | 'expectedItemAmount' => 100, | |
9099cab3 CW |
1077 | ], |
1078 | [ | |
1079 | 'params' => [ | |
d9553c2e PN |
1080 | 'is_quick_config' => TRUE, |
1081 | 'total_amount' => 110, | |
1082 | 'tax_amount' => 10, | |
9099cab3 CW |
1083 | ], |
1084 | 'amountParams' => [ | |
d9553c2e | 1085 | 'item_amount' => NULL, |
9099cab3 | 1086 | ], |
d9553c2e PN |
1087 | 'context' => 'changedAmount', |
1088 | 'expectedItemAmount' => 110, | |
9099cab3 CW |
1089 | ], |
1090 | [ | |
1091 | 'params' => [ | |
d9553c2e PN |
1092 | 'is_quick_config' => TRUE, |
1093 | 'total_amount' => 110, | |
1094 | 'tax_amount' => 10, | |
9099cab3 CW |
1095 | ], |
1096 | 'amountParams' => [ | |
d9553c2e | 1097 | 'item_amount' => NULL, |
9099cab3 | 1098 | ], |
d9553c2e PN |
1099 | 'context' => NULL, |
1100 | 'expectedItemAmount' => 100, | |
9099cab3 CW |
1101 | ], |
1102 | ]; | |
d9553c2e PN |
1103 | foreach ($testParams as $params) { |
1104 | $itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']); | |
1105 | $this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.'); | |
1106 | } | |
1107 | } | |
1108 | ||
d934a732 PN |
1109 | /** |
1110 | * Test recording of amount with comma separator. | |
b4c48831 | 1111 | * |
1112 | * @throws \CRM_Core_Exception | |
d934a732 PN |
1113 | */ |
1114 | public function testCommaSeparatorAmount() { | |
1115 | $contactId = $this->individualCreate(); | |
1116 | ||
9099cab3 | 1117 | $params = [ |
d934a732 PN |
1118 | 'contact_id' => $contactId, |
1119 | 'currency' => 'USD', | |
1120 | 'financial_type_id' => 1, | |
b4c48831 | 1121 | 'contribution_status_id' => 'Pending', |
d934a732 PN |
1122 | 'payment_instrument_id' => 1, |
1123 | 'receive_date' => '20080522000000', | |
1124 | 'receipt_date' => '20080522000000', | |
b4c48831 | 1125 | 'total_amount' => '20,000.00', |
1126 | 'api.Payment.create' => ['total_amount' => '8,000.00'], | |
9099cab3 | 1127 | ]; |
d934a732 | 1128 | |
b4c48831 | 1129 | $contribution = $this->callAPISuccess('Order', 'create', $params); |
d0c97775 | 1130 | $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); |
d934a732 PN |
1131 | $financialTrxn = $this->callAPISuccessGetSingle( |
1132 | 'FinancialTrxn', | |
9099cab3 | 1133 | [ |
d934a732 | 1134 | 'id' => $lastFinancialTrxnId['financialTrxnId'], |
9099cab3 CW |
1135 | 'return' => ['total_amount'], |
1136 | ] | |
d934a732 | 1137 | ); |
b4c48831 | 1138 | $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid amount.'); |
d934a732 PN |
1139 | } |
1140 | ||
adbc354b PN |
1141 | /** |
1142 | * Test for function getSalesTaxFinancialAccounts(). | |
1143 | */ | |
1144 | public function testgetSalesTaxFinancialAccounts() { | |
1145 | $this->enableTaxAndInvoicing(); | |
1146 | $financialType = $this->createFinancialType(); | |
1147 | $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']); | |
9099cab3 | 1148 | $expectedResult = [$financialAccount->financial_account_id => $financialAccount->financial_account_id]; |
adbc354b PN |
1149 | $financialType = $this->createFinancialType(); |
1150 | $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']); | |
1151 | $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id; | |
1152 | $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts(); | |
1153 | $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.'); | |
1154 | } | |
1155 | ||
0409b821 PN |
1156 | /** |
1157 | * Test for function createProportionalEntry(). | |
83644f47 | 1158 | * |
1159 | * @param string $thousandSeparator | |
1160 | * punctuation used to refer to thousands. | |
1161 | * | |
1162 | * @dataProvider getThousandSeparators | |
0409b821 | 1163 | */ |
83644f47 | 1164 | public function testCreateProportionalEntry($thousandSeparator) { |
1165 | $this->setCurrencySeparators($thousandSeparator); | |
2a4a2f00 | 1166 | list($contribution, $financialAccount) = $this->createContributionWithTax(); |
9099cab3 | 1167 | $params = [ |
0409b821 PN |
1168 | 'total_amount' => 55, |
1169 | 'to_financial_account_id' => $financialAccount->financial_account_id, | |
1170 | 'payment_instrument_id' => 1, | |
1171 | 'trxn_date' => date('Ymd'), | |
1172 | 'status_id' => 1, | |
0409b821 | 1173 | 'entity_id' => $contribution['id'], |
9099cab3 | 1174 | ]; |
3137781d | 1175 | $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params); |
9099cab3 | 1176 | $entityParams = [ |
0409b821 PN |
1177 | 'contribution_total_amount' => $contribution['total_amount'], |
1178 | 'trxn_total_amount' => 55, | |
1179 | 'line_item_amount' => 100, | |
9099cab3 | 1180 | ]; |
0409b821 | 1181 | $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']); |
9099cab3 | 1182 | $eftParams = [ |
0409b821 | 1183 | 'entity_table' => 'civicrm_financial_item', |
cf28d075 | 1184 | 'entity_id' => $previousLineItem['id'], |
3137781d | 1185 | 'financial_trxn_id' => (string) $financialTrxn['id'], |
9099cab3 | 1186 | ]; |
0409b821 | 1187 | CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams); |
9099cab3 | 1188 | $trxnTestArray = array_merge($eftParams, [ |
3137781d | 1189 | 'amount' => '50.00', |
9099cab3 | 1190 | ]); |
cf28d075 | 1191 | $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray); |
0409b821 PN |
1192 | } |
1193 | ||
c364c544 MW |
1194 | /** |
1195 | * Test for function createProportionalEntry with zero amount(). | |
1196 | * | |
1197 | * @param string $thousandSeparator | |
1198 | * punctuation used to refer to thousands. | |
1199 | * | |
1200 | * @dataProvider getThousandSeparators | |
1201 | */ | |
1202 | public function testCreateProportionalEntryZeroAmount($thousandSeparator) { | |
1203 | $this->setCurrencySeparators($thousandSeparator); | |
9099cab3 CW |
1204 | list($contribution, $financialAccount) = $this->createContributionWithTax(['total_amount' => 0]); |
1205 | $params = [ | |
c364c544 MW |
1206 | 'total_amount' => 0, |
1207 | 'to_financial_account_id' => $financialAccount->financial_account_id, | |
1208 | 'payment_instrument_id' => 1, | |
1209 | 'trxn_date' => date('Ymd'), | |
1210 | 'status_id' => 1, | |
1211 | 'entity_id' => $contribution['id'], | |
9099cab3 | 1212 | ]; |
c364c544 | 1213 | $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params); |
9099cab3 | 1214 | $entityParams = [ |
c364c544 MW |
1215 | 'contribution_total_amount' => $contribution['total_amount'], |
1216 | 'trxn_total_amount' => 0, | |
1217 | 'line_item_amount' => 0, | |
9099cab3 | 1218 | ]; |
c364c544 | 1219 | $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']); |
9099cab3 | 1220 | $eftParams = [ |
c364c544 MW |
1221 | 'entity_table' => 'civicrm_financial_item', |
1222 | 'entity_id' => $previousLineItem['id'], | |
1223 | 'financial_trxn_id' => (string) $financialTrxn['id'], | |
9099cab3 | 1224 | ]; |
c364c544 | 1225 | CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams); |
9099cab3 | 1226 | $trxnTestArray = array_merge($eftParams, [ |
c364c544 | 1227 | 'amount' => '0.00', |
9099cab3 | 1228 | ]); |
c364c544 MW |
1229 | $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray); |
1230 | } | |
1231 | ||
646bc565 PN |
1232 | /** |
1233 | * Test for function getLastFinancialItemIds(). | |
1234 | */ | |
1235 | public function testgetLastFinancialItemIds() { | |
2a4a2f00 | 1236 | list($contribution, $financialAccount) = $this->createContributionWithTax(); |
646bc565 PN |
1237 | list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']); |
1238 | $this->assertEquals(count($ftIds), 1, 'Invalid count.'); | |
1239 | $this->assertEquals(count($taxItems), 1, 'Invalid count.'); | |
1240 | foreach ($taxItems as $value) { | |
1241 | $this->assertEquals($value['amount'], 10, 'Invalid tax amount.'); | |
1242 | } | |
1243 | } | |
1244 | ||
2a4a2f00 | 1245 | /** |
97c818fd | 1246 | * Test to ensure proportional entries are creating when adding a payment.. |
1247 | * | |
1248 | * In this test we create a pending contribution for $110 consisting of $100 contribution and $10 tax. | |
1249 | * | |
1250 | * We pay $50, resulting in it being allocated as $45.45 paymnt & $4.55 tax. This is in equivalent proportions | |
1251 | * to the original payment - ie. .0909 of the $110 is 10 & that * 50 is $4.54 (note the rounding seems wrong as it should be | |
1252 | * saved un-rounded). | |
2a4a2f00 | 1253 | */ |
97c818fd | 1254 | public function testCreateProportionalFinancialEntriesViaPaymentCreate() { |
1255 | list($contribution, $financialAccount) = $this->createContributionWithTax([], FALSE); | |
1256 | $params = [ | |
3137781d | 1257 | 'total_amount' => 50, |
2a4a2f00 PN |
1258 | 'to_financial_account_id' => $financialAccount->financial_account_id, |
1259 | 'payment_instrument_id' => 1, | |
1260 | 'trxn_date' => date('Ymd'), | |
1261 | 'status_id' => 1, | |
2a4a2f00 | 1262 | 'entity_id' => $contribution['id'], |
97c818fd | 1263 | 'contribution_id' => $contribution['id'], |
1264 | ]; | |
1265 | $financialTrxn = $this->callAPISuccess('Payment', 'create', $params); | |
1266 | $eftParams = [ | |
2a4a2f00 PN |
1267 | 'entity_table' => 'civicrm_financial_item', |
1268 | 'financial_trxn_id' => $financialTrxn['id'], | |
97c818fd | 1269 | ]; |
2a4a2f00 PN |
1270 | $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams); |
1271 | $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.'); | |
97c818fd | 1272 | $testAmount = [4.55, 45.45]; |
2a4a2f00 | 1273 | foreach ($entityFinancialTrxn['values'] as $value) { |
97c818fd | 1274 | $this->assertEquals(array_pop($testAmount), $value['amount'], 'Invalid amount stored in civicrm_entity_financial_trxn.'); |
77641de4 PN |
1275 | } |
1276 | } | |
1277 | ||
1278 | /** | |
1279 | * Test to check if amount is proportionally asigned for PI change. | |
1280 | */ | |
1281 | public function testProportionallyAssignedForPIChange() { | |
1282 | list($contribution, $financialAccount) = $this->createContributionWithTax(); | |
9099cab3 | 1283 | $params = [ |
77641de4 PN |
1284 | 'id' => $contribution['id'], |
1285 | 'payment_instrument_id' => 3, | |
9099cab3 | 1286 | ]; |
77641de4 PN |
1287 | $this->callAPISuccess('Contribution', 'create', $params); |
1288 | $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); | |
9099cab3 | 1289 | $eftParams = [ |
77641de4 PN |
1290 | 'entity_table' => 'civicrm_financial_item', |
1291 | 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'], | |
9099cab3 | 1292 | ]; |
77641de4 PN |
1293 | $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams); |
1294 | $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.'); | |
9099cab3 | 1295 | $testAmount = [10, 100]; |
77641de4 PN |
1296 | foreach ($entityFinancialTrxn['values'] as $value) { |
1297 | $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.'); | |
2a4a2f00 PN |
1298 | } |
1299 | } | |
1300 | ||
646bc565 PN |
1301 | /** |
1302 | * Function to create contribution with tax. | |
1303 | */ | |
9099cab3 | 1304 | public function createContributionWithTax($params = [], $isCompleted = TRUE) { |
c364c544 MW |
1305 | if (!isset($params['total_amount'])) { |
1306 | $params['total_amount'] = 100; | |
1307 | } | |
646bc565 PN |
1308 | $contactId = $this->individualCreate(); |
1309 | $this->enableTaxAndInvoicing(); | |
1310 | $financialType = $this->createFinancialType(); | |
1311 | $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']); | |
1312 | $form = new CRM_Contribute_Form_Contribution(); | |
1313 | ||
9099cab3 | 1314 | $form->testSubmit([ |
39b959db SL |
1315 | 'total_amount' => $params['total_amount'], |
1316 | 'financial_type_id' => $financialType['id'], | |
1317 | 'contact_id' => $contactId, | |
97c818fd | 1318 | 'contribution_status_id' => $isCompleted ? 1 : 2, |
39b959db | 1319 | 'price_set_id' => 0, |
9099cab3 | 1320 | ], CRM_Core_Action::ADD); |
646bc565 | 1321 | $contribution = $this->callAPISuccessGetSingle('Contribution', |
9099cab3 | 1322 | [ |
646bc565 | 1323 | 'contact_id' => $contactId, |
9099cab3 CW |
1324 | 'return' => ['tax_amount', 'total_amount'], |
1325 | ] | |
646bc565 | 1326 | ); |
9099cab3 | 1327 | return [$contribution, $financialAccount]; |
646bc565 PN |
1328 | } |
1329 | ||
75c07fde JP |
1330 | /** |
1331 | * Test processOnBehalfOrganization() function. | |
1332 | */ | |
1333 | public function testProcessOnBehalfOrganization() { | |
9099cab3 | 1334 | $orgInfo = [ |
75c07fde JP |
1335 | 'phone' => '11111111', |
1336 | 'email' => 'testorg@gmail.com', | |
1337 | 'street_address' => 'test Street', | |
1338 | 'city' => 'test City', | |
1339 | 'state_province' => 'AA', | |
1340 | 'postal_code' => '222222', | |
1341 | 'country' => 'United States', | |
9099cab3 | 1342 | ]; |
75c07fde | 1343 | $contactID = $this->individualCreate(); |
9099cab3 CW |
1344 | $orgId = $this->organizationCreate(['organization_name' => 'testorg1']); |
1345 | $orgCount = $this->callAPISuccessGetCount('Contact', [ | |
75c07fde JP |
1346 | 'contact_type' => "Organization", |
1347 | 'organization_name' => "testorg1", | |
9099cab3 | 1348 | ]); |
75c07fde JP |
1349 | $this->assertEquals($orgCount, 1); |
1350 | ||
9099cab3 CW |
1351 | $values = $params = []; |
1352 | $behalfOrganization = [ | |
75c07fde | 1353 | 'organization_name' => 'testorg1', |
9099cab3 CW |
1354 | 'phone' => [ |
1355 | 1 => [ | |
75c07fde JP |
1356 | 'phone' => $orgInfo['phone'], |
1357 | 'is_primary' => 1, | |
9099cab3 CW |
1358 | ], |
1359 | ], | |
1360 | 'email' => [ | |
1361 | 1 => [ | |
75c07fde JP |
1362 | 'email' => $orgInfo['email'], |
1363 | 'is_primary' => 1, | |
9099cab3 CW |
1364 | ], |
1365 | ], | |
1366 | 'address' => [ | |
1367 | 3 => [ | |
75c07fde JP |
1368 | 'street_address' => $orgInfo['street_address'], |
1369 | 'city' => $orgInfo['city'], | |
1370 | 'location_type_id' => 3, | |
1371 | 'postal_code' => $orgInfo['postal_code'], | |
1372 | 'country' => 'US', | |
1373 | 'state_province' => 'AA', | |
1374 | 'is_primary' => 1, | |
9099cab3 CW |
1375 | ], |
1376 | ], | |
1377 | ]; | |
1378 | $fields = [ | |
75c07fde JP |
1379 | 'organization_name' => 1, |
1380 | 'phone-3-1' => 1, | |
1381 | 'email-3' => 1, | |
1382 | 'street_address-3' => 1, | |
1383 | 'city-3' => 1, | |
1384 | 'postal_code-3' => 1, | |
1385 | 'country-3' => 1, | |
1386 | 'state_province-3' => 1, | |
9099cab3 | 1387 | ]; |
75c07fde JP |
1388 | CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields); |
1389 | ||
1390 | //Check whether new organisation is not created. | |
9099cab3 | 1391 | $result = $this->callAPISuccess('Contact', 'get', [ |
75c07fde JP |
1392 | 'contact_type' => "Organization", |
1393 | 'organization_name' => "testorg1", | |
9099cab3 | 1394 | ]); |
75c07fde JP |
1395 | $this->assertEquals($result['count'], 1); |
1396 | ||
1397 | //Assert all org values are updated. | |
1398 | foreach ($orgInfo as $key => $val) { | |
1399 | $this->assertEquals($result['values'][$orgId][$key], $val); | |
1400 | } | |
1401 | ||
1402 | //Check if alert is assigned to params if more than 1 dupe exists. | |
9099cab3 | 1403 | $orgId = $this->organizationCreate(['organization_name' => 'testorg1', 'email' => 'testorg@gmail.com']); |
75c07fde JP |
1404 | CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields); |
1405 | $this->assertEquals($params['onbehalf_dupe_alert'], 1); | |
1406 | } | |
1407 | ||
7e2ec997 E |
1408 | /** |
1409 | * Test for replaceContributionTokens. | |
1410 | * This function tests whether the contribution tokens are replaced with values from contribution. | |
1411 | */ | |
1412 | public function testReplaceContributionTokens() { | |
9945400a | 1413 | $customGroup = $this->customGroupCreate(['extends' => 'Contribution', 'title' => 'contribution stuff']); |
875e076b | 1414 | $customField = $this->customFieldOptionValueCreate($customGroup, 'myCustomField'); |
7e2ec997 | 1415 | $contactId1 = $this->individualCreate(); |
9099cab3 | 1416 | $params = [ |
7e2ec997 E |
1417 | 'contact_id' => $contactId1, |
1418 | 'receive_date' => '20120511', | |
1419 | 'total_amount' => 100.00, | |
1420 | 'financial_type_id' => 1, | |
1421 | 'trxn_id' => 12345, | |
1422 | 'invoice_id' => 67890, | |
1423 | 'source' => 'SSF', | |
1424 | 'contribution_status_id' => 2, | |
875e076b | 1425 | "custom_{$customField['id']}" => 'value1', |
9099cab3 | 1426 | ]; |
7e2ec997 E |
1427 | $contribution1 = $this->contributionCreate($params); |
1428 | $contactId2 = $this->individualCreate(); | |
9099cab3 | 1429 | $params = [ |
7e2ec997 E |
1430 | 'contact_id' => $contactId2, |
1431 | 'receive_date' => '20150511', | |
1432 | 'total_amount' => 200.00, | |
1433 | 'financial_type_id' => 1, | |
1434 | 'trxn_id' => 6789, | |
1435 | 'invoice_id' => 12345, | |
1436 | 'source' => 'ABC', | |
1437 | 'contribution_status_id' => 1, | |
875e076b | 1438 | "custom_{$customField['id']}" => 'value2', |
9099cab3 | 1439 | ]; |
7e2ec997 | 1440 | $contribution2 = $this->contributionCreate($params); |
9099cab3 | 1441 | $ids = [$contribution1, $contribution2]; |
7e2ec997 E |
1442 | |
1443 | $subject = "This is a test for contribution ID: {contribution.contribution_id}"; | |
1444 | $text = "Contribution Amount: {contribution.total_amount}"; | |
1445 | $html = "<p>Contribution Source: {contribution.contribution_source}</p></br> | |
1446 | <p>Contribution Invoice ID: {contribution.invoice_id}</p></br> | |
875e076b | 1447 | <p>Contribution Receive Date: {contribution.receive_date}</p></br> |
596a28fc | 1448 | <p>Contribution Custom Field: {contribution.custom_{$customField['id']}}</p></br>"; |
7e2ec997 E |
1449 | |
1450 | $subjectToken = CRM_Utils_Token::getTokens($subject); | |
1451 | $messageToken = CRM_Utils_Token::getTokens($text); | |
1452 | $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html)); | |
1453 | ||
1454 | $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens( | |
1455 | $ids, | |
1456 | $subject, | |
1457 | $subjectToken, | |
1458 | $text, | |
1459 | $html, | |
1460 | $messageToken, | |
1461 | TRUE | |
1462 | ); | |
1463 | ||
1464 | $this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match"); | |
1465 | $this->assertEquals("<p>Contribution Source: ABC</p></br> | |
1466 | <p>Contribution Invoice ID: 12345</p></br> | |
875e076b | 1467 | <p>Contribution Receive Date: May 11th, 2015</p></br> |
596a28fc | 1468 | <p>Contribution Custom Field: Label2</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match"); |
7e2ec997 E |
1469 | } |
1470 | ||
7b5169d0 PN |
1471 | /** |
1472 | * Test for contribution with deferred revenue. | |
1473 | */ | |
1474 | public function testContributionWithDeferredRevenue() { | |
1475 | $contactId = $this->individualCreate(); | |
1476 | Civi::settings()->set('deferred_revenue_enabled', TRUE); | |
9099cab3 | 1477 | $params = [ |
7b5169d0 PN |
1478 | 'contact_id' => $contactId, |
1479 | 'receive_date' => '20120511', | |
1480 | 'total_amount' => 100.00, | |
1481 | 'financial_type_id' => 'Event Fee', | |
1482 | 'trxn_id' => 12345, | |
1483 | 'invoice_id' => 67890, | |
1484 | 'source' => 'SSF', | |
1485 | 'contribution_status_id' => 'Completed', | |
1486 | 'revenue_recognition_date' => date('Ymd', strtotime("+3 month")), | |
9099cab3 | 1487 | ]; |
7b5169d0 PN |
1488 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
1489 | ||
9099cab3 | 1490 | $this->callAPISuccessGetCount('EntityFinancialTrxn', [ |
7b5169d0 PN |
1491 | 'entity_table' => "civicrm_contribution", |
1492 | 'entity_id' => $contribution['id'], | |
9099cab3 | 1493 | ], 2); |
7b5169d0 | 1494 | |
9099cab3 | 1495 | $checkAgainst = [ |
7b5169d0 PN |
1496 | 'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee', |
1497 | 'financial_trxn_id.from_financial_account_id.name' => 'Event Fee', | |
f818aed5 | 1498 | 'financial_trxn_id' => '2', |
9099cab3 CW |
1499 | ]; |
1500 | $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', [ | |
1501 | 'return' => [ | |
7b5169d0 PN |
1502 | "financial_trxn_id.from_financial_account_id.name", |
1503 | "financial_trxn_id.to_financial_account_id.name", | |
1504 | "financial_trxn_id", | |
9099cab3 | 1505 | ], |
7b5169d0 PN |
1506 | 'entity_table' => "civicrm_contribution", |
1507 | 'entity_id' => $contribution['id'], | |
1508 | 'financial_trxn_id.is_payment' => 0, | |
9099cab3 | 1509 | ], $checkAgainst); |
7b5169d0 | 1510 | |
9099cab3 | 1511 | $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', [ |
7b5169d0 PN |
1512 | 'entity_table' => "civicrm_financial_item", |
1513 | 'financial_trxn_id' => $result['financial_trxn_id'], | |
9099cab3 CW |
1514 | 'return' => ['entity_id'], |
1515 | ]); | |
7b5169d0 | 1516 | |
9099cab3 | 1517 | $checkAgainst = [ |
7b5169d0 PN |
1518 | 'financial_account_id.name' => 'Deferred Revenue - Event Fee', |
1519 | 'id' => $result['entity_id'], | |
9099cab3 CW |
1520 | ]; |
1521 | $result = $this->callAPISuccessGetSingle('FinancialItem', [ | |
7b5169d0 | 1522 | 'id' => $result['entity_id'], |
9099cab3 CW |
1523 | 'return' => ["financial_account_id.name"], |
1524 | ], $checkAgainst); | |
7b5169d0 PN |
1525 | } |
1526 | ||
a495d83a PN |
1527 | /** |
1528 | * https://lab.civicrm.org/dev/financial/issues/56 | |
1529 | * Changing financial type on a contribution records correct financial items | |
1530 | */ | |
1531 | public function testChangingFinancialTypeWithoutTax() { | |
1532 | $ids = $values = []; | |
1533 | $contactId = $this->individualCreate(); | |
9099cab3 | 1534 | $params = [ |
a495d83a PN |
1535 | 'contact_id' => $contactId, |
1536 | 'receive_date' => date('YmdHis'), | |
1537 | 'total_amount' => 100.00, | |
1538 | 'financial_type_id' => 'Donation', | |
1539 | 'contribution_status_id' => 'Completed', | |
9099cab3 | 1540 | ]; |
a495d83a PN |
1541 | /* first test the scenario when sending an email */ |
1542 | $contributionId = $this->callAPISuccess( | |
1543 | 'contribution', | |
1544 | 'create', | |
1545 | $params | |
1546 | )['id']; | |
1547 | ||
1548 | // Update Financial Type. | |
1549 | $this->callAPISuccess('contribution', 'create', [ | |
1550 | 'id' => $contributionId, | |
1551 | 'financial_type_id' => 'Event Fee', | |
1552 | ]); | |
1553 | ||
1554 | // Get line item | |
1555 | $lineItem = $this->callAPISuccessGetSingle('LineItem', [ | |
1556 | 'contribution_id' => $contributionId, | |
1557 | 'return' => ["financial_type_id.name", "line_total"], | |
1558 | ]); | |
1559 | ||
1560 | $this->assertEquals( | |
1561 | $lineItem['line_total'], | |
1562 | 100.00, | |
1563 | 'Invalid line amount.' | |
1564 | ); | |
1565 | ||
1566 | $this->assertEquals( | |
1567 | $lineItem['financial_type_id.name'], | |
1568 | 'Event Fee', | |
1569 | 'Invalid Financial Type stored.' | |
1570 | ); | |
1571 | ||
1572 | // Get Financial Items. | |
1573 | $financialItems = $this->callAPISuccess('FinancialItem', 'get', [ | |
1574 | 'entity_id' => $lineItem['id'], | |
1575 | 'sequential' => 1, | |
1576 | 'entity_table' => 'civicrm_line_item', | |
1577 | 'options' => ['sort' => "id"], | |
1578 | 'return' => ["financial_account_id.name", "amount", "description"], | |
1579 | ]); | |
1580 | ||
1581 | $this->assertEquals($financialItems['count'], 3, 'Count mismatch.'); | |
1582 | ||
1583 | $toCheck = [ | |
1584 | ['Donation', 100.00], | |
1585 | ['Donation', -100.00], | |
1586 | ['Event Fee', 100.00], | |
1587 | ]; | |
1588 | ||
1589 | foreach ($financialItems['values'] as $key => $values) { | |
1590 | $this->assertEquals( | |
1591 | $values['financial_account_id.name'], | |
1592 | $toCheck[$key][0], | |
1593 | 'Invalid Financial Account stored.' | |
1594 | ); | |
1595 | $this->assertEquals( | |
1596 | $values['amount'], | |
1597 | $toCheck[$key][1], | |
1598 | 'Amount mismatch.' | |
1599 | ); | |
1600 | $this->assertEquals( | |
1601 | $values['description'], | |
1602 | 'Contribution Amount', | |
1603 | 'Description mismatch.' | |
1604 | ); | |
1605 | } | |
1606 | ||
1607 | // Check transactions. | |
1608 | $financialTransactions = $this->callAPISuccess('EntityFinancialTrxn', 'get', [ | |
1609 | 'return' => ["financial_trxn_id"], | |
1610 | 'entity_table' => "civicrm_contribution", | |
1611 | 'entity_id' => $contributionId, | |
1612 | 'sequential' => 1, | |
1613 | ]); | |
1614 | $this->assertEquals($financialTransactions['count'], 3, 'Count mismatch.'); | |
1615 | ||
1616 | foreach ($financialTransactions['values'] as $key => $values) { | |
1617 | $this->callAPISuccessGetCount('EntityFinancialTrxn', [ | |
1618 | 'financial_trxn_id' => $values['financial_trxn_id'], | |
1619 | 'amount' => $toCheck[$key][1], | |
1620 | 'financial_trxn_id.total_amount' => $toCheck[$key][1], | |
1621 | ], 2); | |
1622 | } | |
1623 | } | |
1624 | ||
1db3ddea KE |
1625 | /** |
1626 | * CRM-21424 Check if the receipt update is set after composing the receipt message | |
1627 | */ | |
1628 | public function testSendMailUpdateReceiptDate() { | |
9099cab3 | 1629 | $ids = $values = []; |
1db3ddea | 1630 | $contactId = $this->individualCreate(); |
9099cab3 | 1631 | $params = [ |
1db3ddea KE |
1632 | 'contact_id' => $contactId, |
1633 | 'receive_date' => '20120511', | |
1634 | 'total_amount' => 100.00, | |
1635 | 'financial_type_id' => 'Donation', | |
1636 | 'source' => 'SSF', | |
1637 | 'contribution_status_id' => 'Completed', | |
9099cab3 | 1638 | ]; |
1db3ddea KE |
1639 | /* first test the scenario when sending an email */ |
1640 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
1641 | $contributionId = $contribution['id']; | |
1642 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null'); | |
9099cab3 | 1643 | $input = ['receipt_update' => 0]; |
1db3ddea KE |
1644 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values); |
1645 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null'); | |
9099cab3 | 1646 | $input = ['receipt_update' => 1]; |
1db3ddea KE |
1647 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values); |
1648 | $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set'); | |
1649 | ||
1650 | /* repeat the same scenario for downloading a pdf */ | |
1651 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
1652 | $contributionId = $contribution['id']; | |
1653 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null'); | |
9099cab3 | 1654 | $input = ['receipt_update' => 0]; |
1db3ddea KE |
1655 | /* setting the lasast parameter (returnmessagetext) to TRUE is done by the download of the pdf */ |
1656 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE); | |
1657 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null'); | |
9099cab3 | 1658 | $input = ['receipt_update' => 1]; |
1db3ddea KE |
1659 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE); |
1660 | $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set'); | |
1661 | } | |
1662 | ||
6a488035 | 1663 | } |