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 | |
8022372f GC |
525 | /** |
526 | * Check credit note id creation | |
527 | * when a contribution is cancelled or refunded | |
528 | * createCreditNoteId(); | |
529 | */ | |
530 | public function testCreateCreditNoteId() { | |
2449fe69 | 531 | $contactId = $this->individualCreate(); |
8022372f | 532 | |
9099cab3 | 533 | $param = [ |
8022372f GC |
534 | 'contact_id' => $contactId, |
535 | 'currency' => 'USD', | |
536 | 'financial_type_id' => 1, | |
537 | 'contribution_status_id' => 3, | |
538 | 'payment_instrument_id' => 1, | |
539 | 'source' => 'STUDENT', | |
540 | 'receive_date' => '20080522000000', | |
541 | 'receipt_date' => '20080522000000', | |
542 | 'id' => NULL, | |
543 | 'non_deductible_amount' => 0.00, | |
544 | 'total_amount' => 300.00, | |
545 | 'fee_amount' => 5, | |
546 | 'net_amount' => 295, | |
547 | 'trxn_id' => '76ereeswww835', | |
548 | 'invoice_id' => '93ed39a9e9hd621bs0eafe3da82', | |
549 | 'thankyou_date' => '20080522', | |
d0c97775 | 550 | 'sequential' => TRUE, |
9099cab3 | 551 | ]; |
8022372f GC |
552 | |
553 | $creditNoteId = CRM_Contribute_BAO_Contribution::createCreditNoteId(); | |
d0c97775 | 554 | $contribution = $this->callAPISuccess('Contribution', 'create', $param)['values'][0]; |
555 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
556 | $this->assertEquals($creditNoteId, $contribution['creditnote_id'], 'Check if credit note id is created correctly.'); | |
8022372f GC |
557 | } |
558 | ||
b04e4b11 PN |
559 | /** |
560 | * Create() method (create and update modes). | |
561 | */ | |
562 | public function testIsPaymentFlag() { | |
2449fe69 | 563 | $contactId = $this->individualCreate(); |
b04e4b11 | 564 | |
d0c97775 | 565 | $params = [ |
b04e4b11 PN |
566 | 'contact_id' => $contactId, |
567 | 'currency' => 'USD', | |
568 | 'financial_type_id' => 1, | |
569 | 'contribution_status_id' => 1, | |
570 | 'payment_instrument_id' => 1, | |
571 | 'source' => 'STUDENT', | |
572 | 'receive_date' => '20080522000000', | |
573 | 'receipt_date' => '20080522000000', | |
574 | 'non_deductible_amount' => 0.00, | |
575 | 'total_amount' => 200.00, | |
576 | 'fee_amount' => 5, | |
577 | 'net_amount' => 195, | |
578 | 'trxn_id' => '22ereerwww4444xx', | |
579 | 'invoice_id' => '86ed39c9e9ee6ef6541621ce0eafe7eb81', | |
580 | 'thankyou_date' => '20080522', | |
d0c97775 | 581 | 'sequential' => TRUE, |
582 | ]; | |
583 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; | |
b04e4b11 | 584 | |
d0c97775 | 585 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id creation.'); |
586 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
b04e4b11 | 587 | |
9099cab3 | 588 | $trxnArray = [ |
b04e4b11 PN |
589 | 'trxn_id' => $params['trxn_id'], |
590 | 'is_payment' => 1, | |
9099cab3 CW |
591 | ]; |
592 | $defaults = []; | |
b04e4b11 | 593 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
4ba3c75f | 594 | $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.'); |
b04e4b11 | 595 | //update contribution amount |
d0c97775 | 596 | $params['id'] = $contribution['id']; |
b04e4b11 | 597 | $params['total_amount'] = 150; |
d0c97775 | 598 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
b04e4b11 | 599 | |
d0c97775 | 600 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .'); |
601 | $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for Amount updation.'); | |
9099cab3 | 602 | $trxnArray = [ |
b04e4b11 PN |
603 | 'trxn_id' => $params['trxn_id'], |
604 | 'is_payment' => 1, | |
9099cab3 CW |
605 | ]; |
606 | $defaults = []; | |
b04e4b11 | 607 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
4ba3c75f | 608 | $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); |
b04e4b11 PN |
609 | $trxnArray['is_payment'] = 0; |
610 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); | |
4ba3c75f | 611 | $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.'); |
b04e4b11 PN |
612 | } |
613 | ||
a387acc9 PN |
614 | /** |
615 | * Create() method (create and update modes). | |
616 | */ | |
617 | public function testIsPaymentFlagForPending() { | |
2449fe69 | 618 | $contactId = $this->individualCreate(); |
a387acc9 | 619 | |
9099cab3 | 620 | $params = [ |
a387acc9 PN |
621 | 'contact_id' => $contactId, |
622 | 'currency' => 'USD', | |
623 | 'financial_type_id' => 1, | |
624 | 'contribution_status_id' => 2, | |
625 | 'payment_instrument_id' => 1, | |
626 | 'source' => 'STUDENT', | |
627 | 'is_pay_later' => 1, | |
628 | 'receive_date' => '20080522000000', | |
629 | 'receipt_date' => '20080522000000', | |
630 | 'non_deductible_amount' => 0.00, | |
631 | 'total_amount' => 200.00, | |
632 | 'fee_amount' => 5, | |
633 | 'net_amount' => 195, | |
634 | 'trxn_id' => '22ereerwww4444yy', | |
635 | 'invoice_id' => '86ed39c9e9yy6ef6541621ce0eafe7eb81', | |
636 | 'thankyou_date' => '20080522', | |
d0c97775 | 637 | 'sequential' => TRUE, |
9099cab3 | 638 | ]; |
a387acc9 | 639 | |
d0c97775 | 640 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
a387acc9 | 641 | |
d0c97775 | 642 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transaction id creation.'); |
643 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
a387acc9 | 644 | |
9099cab3 | 645 | $trxnArray = [ |
a387acc9 PN |
646 | 'trxn_id' => $params['trxn_id'], |
647 | 'is_payment' => 0, | |
9099cab3 CW |
648 | ]; |
649 | $defaults = []; | |
a387acc9 PN |
650 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
651 | $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); | |
652 | $trxnArray['is_payment'] = 1; | |
653 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); | |
654 | $this->assertEquals(NULL, $financialTrxn, 'Mismatch count for is payment flag.'); | |
655 | //update contribution amount | |
d0c97775 | 656 | $params['id'] = $contribution['id']; |
a387acc9 PN |
657 | $params['contribution_status_id'] = 1; |
658 | ||
d0c97775 | 659 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
a387acc9 | 660 | |
d0c97775 | 661 | $this->assertEquals($params['trxn_id'], $contribution['trxn_id'], 'Check for transcation id .'); |
662 | $this->assertEquals($params['contribution_status_id'], $contribution['contribution_status_id'], 'Check for status updation.'); | |
9099cab3 | 663 | $trxnArray = [ |
a387acc9 PN |
664 | 'trxn_id' => $params['trxn_id'], |
665 | 'is_payment' => 1, | |
9099cab3 CW |
666 | ]; |
667 | $defaults = []; | |
a387acc9 PN |
668 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); |
669 | $this->assertEquals(1, $financialTrxn->N, 'Mismatch count for is payment flag.'); | |
670 | $trxnArray['is_payment'] = 0; | |
671 | $financialTrxn = CRM_Core_BAO_FinancialTrxn::retrieve($trxnArray, $defaults); | |
672 | $this->assertEquals(2, $financialTrxn->N, 'Mismatch count for is payment flag.'); | |
a387acc9 PN |
673 | } |
674 | ||
eec619df | 675 | /** |
3fc37a30 | 676 | * addPayments() method (add and edit modes of participant). |
677 | * | |
678 | * Add Payments is part of an old, flawed, code flow. | |
eec619df PN |
679 | */ |
680 | public function testAddPayments() { | |
3fc37a30 | 681 | $contribution = $this->addParticipantWithContribution(); |
682 | // Delete existing financial_trxns. This is because we are testing a code flow we | |
683 | // want to deprecate & remove & the test relies on bad data asa starting point. | |
684 | // End goal is the Order.create->Payment.create flow. | |
685 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_entity_financial_trxn WHERE entity_table = "civicrm_financial_item"'); | |
d0c97775 | 686 | CRM_Contribute_BAO_Contribution::addPayments([$contribution]); |
0a5651eb PN |
687 | $this->checkItemValues($contribution); |
688 | } | |
689 | ||
690 | /** | |
691 | * checks db values for financial item | |
692 | */ | |
624195c8 | 693 | public function checkItemValues($contribution) { |
876b8ab0 | 694 | $toFinancialAccount = CRM_Contribute_PseudoConstant::getRelationalFinancialAccount(4, 'Accounts Receivable Account is'); |
2449fe69 | 695 | $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 |
696 | INNER JOIN civicrm_entity_financial_trxn eft1 ON (eft1.financial_trxn_id = eft.financial_trxn_id AND eft1.entity_table = 'civicrm_financial_item') |
697 | WHERE eft.entity_id = %1 AND ft.to_financial_account_id <> %2"; | |
698 | ||
9099cab3 CW |
699 | $queryParams[1] = [$contribution->id, 'Integer']; |
700 | $queryParams[2] = [$toFinancialAccount, 'Integer']; | |
eec619df PN |
701 | |
702 | $dao = CRM_Core_DAO::executeQuery($query, $queryParams); | |
9099cab3 | 703 | $amounts = [100.00, 50.00]; |
eec619df PN |
704 | while ($dao->fetch()) { |
705 | $this->assertEquals(150.00, $dao->total_amount, 'Mismatch of total amount paid.'); | |
19084b68 | 706 | $this->assertEquals($dao->amount, array_pop($amounts), 'Mismatch of amount proportionally assigned to financial item'); |
eec619df | 707 | } |
eec619df PN |
708 | } |
709 | ||
0a5651eb PN |
710 | /** |
711 | * assignProportionalLineItems() method (add and edit modes of participant) | |
3fc37a30 | 712 | * |
713 | * @throws \CRM_Core_Exception | |
714 | * @throws \CiviCRM_API3_Exception | |
0a5651eb PN |
715 | */ |
716 | public function testAssignProportionalLineItems() { | |
3fc37a30 | 717 | $contribution = $this->addParticipantWithContribution(); |
718 | // Delete existing financial_trxns. This is because we are testing a code flow we | |
719 | // want to deprecate & remove & the test relies on bad data asa starting point. | |
720 | // End goal is the Order.create->Payment.create flow. | |
721 | CRM_Core_DAO::executeQuery('DELETE FROM civicrm_entity_financial_trxn WHERE entity_table = "civicrm_financial_item"'); | |
9099cab3 | 722 | $params = [ |
0a5651eb PN |
723 | 'contribution_id' => $contribution->id, |
724 | 'total_amount' => 150.00, | |
9099cab3 | 725 | ]; |
0a5651eb PN |
726 | $trxn = new CRM_Financial_DAO_FinancialTrxn(); |
727 | $trxn->orderBy('id DESC'); | |
728 | $trxn->find(TRUE); | |
8de1ade9 | 729 | CRM_Contribute_BAO_Contribution::assignProportionalLineItems($params, $trxn->id, $contribution->total_amount); |
0a5651eb PN |
730 | $this->checkItemValues($contribution); |
731 | } | |
732 | ||
eec619df PN |
733 | /** |
734 | * Add participant with contribution | |
735 | * | |
736 | * @return array | |
3fc37a30 | 737 | * |
738 | * @throws \CRM_Core_Exception | |
739 | * @throws \CiviCRM_API3_Exception | |
eec619df | 740 | */ |
78c99516 | 741 | public function addParticipantWithContribution() { |
eec619df | 742 | // creating price set, price field |
2449fe69 | 743 | $this->_contactId = $this->individualCreate(); |
3fc37a30 | 744 | $event = $this->eventCreatePaid([]); |
2449fe69 | 745 | $this->_eventId = $event['id']; |
3fc37a30 | 746 | $priceSetId = $this->priceSetID; |
9099cab3 | 747 | $paramsField = [ |
eec619df PN |
748 | 'label' => 'Price Field', |
749 | 'name' => CRM_Utils_String::titleToVar('Price Field'), | |
750 | 'html_type' => 'CheckBox', | |
9099cab3 CW |
751 | 'option_label' => ['1' => 'Price Field 1', '2' => 'Price Field 2'], |
752 | 'option_value' => ['1' => 100, '2' => 200], | |
753 | 'option_name' => ['1' => 'Price Field 1', '2' => 'Price Field 2'], | |
754 | 'option_weight' => ['1' => 1, '2' => 2], | |
755 | 'option_amount' => ['1' => 100, '2' => 200], | |
eec619df PN |
756 | 'is_display_amounts' => 1, |
757 | 'weight' => 1, | |
758 | 'options_per_line' => 1, | |
9099cab3 | 759 | 'is_active' => ['1' => 1, '2' => 1], |
3fc37a30 | 760 | 'price_set_id' => $this->priceSetID, |
eec619df PN |
761 | 'is_enter_qty' => 1, |
762 | 'financial_type_id' => CRM_Core_DAO::getFieldValue('CRM_Financial_DAO_FinancialType', 'Event Fee', 'id', 'name'), | |
9099cab3 | 763 | ]; |
eec619df | 764 | $priceField = CRM_Price_BAO_PriceField::create($paramsField); |
9099cab3 | 765 | $eventParams = [ |
eec619df PN |
766 | 'id' => $this->_eventId, |
767 | 'financial_type_id' => 4, | |
768 | 'is_monetary' => 1, | |
9099cab3 | 769 | ]; |
eec619df PN |
770 | CRM_Event_BAO_Event::create($eventParams); |
771 | CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId); | |
e4ba8498 | 772 | |
9099cab3 CW |
773 | $priceFields = $this->callAPISuccess('PriceFieldValue', 'get', ['price_field_id' => $priceField->id]); |
774 | $participantParams = [ | |
eec619df PN |
775 | 'financial_type_id' => 4, |
776 | 'event_id' => $this->_eventId, | |
777 | 'role_id' => 1, | |
778 | 'status_id' => 14, | |
779 | 'fee_currency' => 'USD', | |
780 | 'contact_id' => $this->_contactId, | |
9099cab3 | 781 | ]; |
eec619df | 782 | $participant = CRM_Event_BAO_Participant::add($participantParams); |
9099cab3 | 783 | $contributionParams = [ |
3fc37a30 | 784 | 'total_amount' => 300, |
eec619df PN |
785 | 'currency' => 'USD', |
786 | 'contact_id' => $this->_contactId, | |
787 | 'financial_type_id' => 4, | |
3fc37a30 | 788 | 'contribution_status_id' => 'Pending', |
eec619df PN |
789 | 'contribution_mode' => 'participant', |
790 | 'participant_id' => $participant->id, | |
d0c97775 | 791 | 'sequential' => TRUE, |
3fc37a30 | 792 | 'api.Payment.create' => ['total_amount' => 150], |
9099cab3 | 793 | ]; |
eec619df PN |
794 | |
795 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 796 | $lineItems[1][$key] = [ |
eec619df PN |
797 | 'price_field_id' => $priceField['price_field_id'], |
798 | 'price_field_value_id' => $priceField['id'], | |
799 | 'label' => $priceField['label'], | |
800 | 'field_title' => $priceField['label'], | |
801 | 'qty' => 1, | |
802 | 'unit_price' => $priceField['amount'], | |
803 | 'line_total' => $priceField['amount'], | |
804 | 'financial_type_id' => $priceField['financial_type_id'], | |
9099cab3 | 805 | ]; |
eec619df PN |
806 | } |
807 | $contributionParams['line_item'] = $lineItems; | |
d0c97775 | 808 | $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams)['values'][0]; |
eec619df | 809 | |
9099cab3 | 810 | $paymentParticipant = [ |
eec619df | 811 | 'participant_id' => $participant->id, |
d0c97775 | 812 | 'contribution_id' => $contribution['id'], |
9099cab3 | 813 | ]; |
a5750507 | 814 | CRM_Event_BAO_ParticipantPayment::create($paymentParticipant); |
eec619df | 815 | |
d0c97775 | 816 | $contributionObject = new CRM_Contribute_BAO_Contribution(); |
817 | $contributionObject->id = $contribution['id']; | |
818 | $contributionObject->find(TRUE); | |
819 | ||
3fc37a30 | 820 | return $contributionObject; |
eec619df PN |
821 | } |
822 | ||
5c8b902b | 823 | /** |
a4a31ffe | 824 | * checkLineItems() check if total amount matches the sum of line total |
5c8b902b PN |
825 | */ |
826 | public function testcheckLineItems() { | |
9099cab3 | 827 | $params = [ |
5c8b902b PN |
828 | 'contact_id' => 202, |
829 | 'receive_date' => '2010-01-20', | |
830 | 'total_amount' => 100, | |
831 | 'financial_type_id' => 3, | |
9099cab3 CW |
832 | 'line_items' => [ |
833 | [ | |
834 | 'line_item' => [ | |
835 | [ | |
5c8b902b PN |
836 | 'entity_table' => 'civicrm_contribution', |
837 | 'price_field_id' => 8, | |
838 | 'price_field_value_id' => 16, | |
839 | 'label' => 'test 1', | |
840 | 'qty' => 1, | |
841 | 'unit_price' => 100, | |
842 | 'line_total' => 100, | |
9099cab3 CW |
843 | ], |
844 | [ | |
5c8b902b PN |
845 | 'entity_table' => 'civicrm_contribution', |
846 | 'price_field_id' => 8, | |
847 | 'price_field_value_id' => 17, | |
848 | 'label' => 'Test 2', | |
849 | 'qty' => 1, | |
850 | 'unit_price' => 200, | |
851 | 'line_total' => 200, | |
852 | 'financial_type_id' => 1, | |
9099cab3 CW |
853 | ], |
854 | ], | |
855 | 'params' => [], | |
856 | ], | |
857 | ], | |
858 | ]; | |
c16c6ad8 | 859 | |
af004c04 | 860 | try { |
2449fe69 | 861 | CRM_Contribute_BAO_Contribution::checkLineItems($params); |
af004c04 PN |
862 | $this->fail("Missed expected exception"); |
863 | } | |
c16c6ad8 CR |
864 | catch (CRM_Contribute_Exception_CheckLineItemsException $e) { |
865 | $this->assertEquals( | |
866 | CRM_Contribute_Exception_CheckLineItemsException::LINE_ITEM_DIFFERRING_TOTAL_EXCEPTON_MSG, | |
867 | $e->getMessage() | |
868 | ); | |
af004c04 | 869 | } |
c16c6ad8 | 870 | |
5c8b902b PN |
871 | $this->assertEquals(3, $params['line_items'][0]['line_item'][0]['financial_type_id']); |
872 | $params['total_amount'] = 300; | |
c16c6ad8 | 873 | |
af004c04 | 874 | CRM_Contribute_BAO_Contribution::checkLineItems($params); |
5c8b902b PN |
875 | } |
876 | ||
c16c6ad8 CR |
877 | /** |
878 | * Tests CRM_Contribute_BAO_Contribution::checkLineItems() method works with | |
879 | * floating point values. | |
880 | */ | |
881 | public function testCheckLineItemsWithFloatingPointValues() { | |
9099cab3 | 882 | $params = [ |
c16c6ad8 CR |
883 | 'contact_id' => 202, |
884 | 'receive_date' => date('Y-m-d'), | |
885 | 'total_amount' => 16.67, | |
886 | 'financial_type_id' => 3, | |
9099cab3 CW |
887 | 'line_items' => [ |
888 | [ | |
889 | 'line_item' => [ | |
890 | [ | |
c16c6ad8 CR |
891 | 'entity_table' => 'civicrm_contribution', |
892 | 'price_field_id' => 8, | |
893 | 'price_field_value_id' => 16, | |
894 | 'label' => 'test 1', | |
895 | 'qty' => 1, | |
896 | 'unit_price' => 14.85, | |
897 | 'line_total' => 14.85, | |
9099cab3 CW |
898 | ], |
899 | [ | |
c16c6ad8 CR |
900 | 'entity_table' => 'civicrm_contribution', |
901 | 'price_field_id' => 8, | |
902 | 'price_field_value_id' => 17, | |
903 | 'label' => 'Test 2', | |
904 | 'qty' => 1, | |
905 | 'unit_price' => 1.66, | |
906 | 'line_total' => 1.66, | |
907 | 'financial_type_id' => 1, | |
9099cab3 CW |
908 | ], |
909 | [ | |
c16c6ad8 CR |
910 | 'entity_table' => 'civicrm_contribution', |
911 | 'price_field_id' => 8, | |
912 | 'price_field_value_id' => 17, | |
913 | 'label' => 'Test 2', | |
914 | 'qty' => 1, | |
915 | 'unit_price' => 0.16, | |
916 | 'line_total' => 0.16, | |
917 | 'financial_type_id' => 1, | |
9099cab3 CW |
918 | ], |
919 | ], | |
920 | 'params' => [], | |
921 | ], | |
922 | ], | |
923 | ]; | |
c16c6ad8 CR |
924 | |
925 | $foundException = FALSE; | |
926 | ||
927 | try { | |
928 | CRM_Contribute_BAO_Contribution::checkLineItems($params); | |
929 | } | |
930 | catch (CRM_Contribute_Exception_CheckLineItemsException $e) { | |
931 | $foundException = TRUE; | |
932 | } | |
933 | ||
934 | $this->assertFalse($foundException); | |
935 | } | |
936 | ||
88f7a518 E |
937 | /** |
938 | * Test activity amount updation. | |
939 | */ | |
940 | public function testActivityCreate() { | |
941 | $contactId = $this->individualCreate(); | |
9099cab3 | 942 | $defaults = []; |
88f7a518 | 943 | |
9099cab3 | 944 | $params = [ |
88f7a518 E |
945 | 'contact_id' => $contactId, |
946 | 'currency' => 'USD', | |
947 | 'financial_type_id' => 1, | |
948 | 'contribution_status_id' => 1, | |
949 | 'payment_instrument_id' => 1, | |
950 | 'source' => 'STUDENT', | |
951 | 'receive_date' => '20080522000000', | |
952 | 'receipt_date' => '20080522000000', | |
953 | 'non_deductible_amount' => 0.00, | |
954 | 'total_amount' => 100.00, | |
955 | 'trxn_id' => '22ereerwww444444', | |
956 | 'invoice_id' => '86ed39c9e9ee6ef6031621ce0eafe7eb81', | |
957 | 'thankyou_date' => '20160519', | |
d0c97775 | 958 | 'sequential' => 1, |
9099cab3 | 959 | ]; |
88f7a518 | 960 | |
d0c97775 | 961 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
88f7a518 | 962 | |
d0c97775 | 963 | $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.'); |
964 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
88f7a518 E |
965 | |
966 | // Check amount in activity. | |
9099cab3 | 967 | $activityParams = [ |
d0c97775 | 968 | 'source_record_id' => $contribution['id'], |
d66c61b6 | 969 | 'activity_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Activity_BAO_Activity', 'activity_type_id', 'Contribution'), |
9099cab3 | 970 | ]; |
d66c61b6 | 971 | // @todo use api instead. |
88f7a518 E |
972 | $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults); |
973 | ||
d0c97775 | 974 | $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.'); |
88f7a518 E |
975 | $this->assertEquals("$ 100.00 - STUDENT", $activity->subject, 'Check for total amount in activity.'); |
976 | ||
d0c97775 | 977 | $params['id'] = $contribution['id']; |
88f7a518 E |
978 | $params['total_amount'] = 200; |
979 | ||
d0c97775 | 980 | $contribution = $this->callAPISuccess('Contribution', 'create', $params)['values'][0]; |
88f7a518 | 981 | |
d0c97775 | 982 | $this->assertEquals($params['total_amount'], $contribution['total_amount'], 'Check for total amount in contribution.'); |
983 | $this->assertEquals($contactId, $contribution['contact_id'], 'Check for contact id creation.'); | |
88f7a518 E |
984 | |
985 | // Retrieve activity again. | |
986 | $activity = CRM_Activity_BAO_Activity::retrieve($activityParams, $defaults); | |
987 | ||
d0c97775 | 988 | $this->assertEquals($contribution['id'], $activity->source_record_id, 'Check for activity associated with contribution.'); |
88f7a518 E |
989 | $this->assertEquals("$ 200.00 - STUDENT", $activity->subject, 'Check for total amount in activity.'); |
990 | } | |
991 | ||
5c3d600f PN |
992 | /** |
993 | * Test allowUpdateRevenueRecognitionDate. | |
af595ac2 | 994 | * |
995 | * @throws \CRM_Core_Exception | |
5c3d600f PN |
996 | */ |
997 | public function testAllowUpdateRevenueRecognitionDate() { | |
998 | $contactId = $this->individualCreate(); | |
9099cab3 | 999 | $params = [ |
5c3d600f PN |
1000 | 'contact_id' => $contactId, |
1001 | 'receive_date' => '2010-01-20', | |
1002 | 'total_amount' => 100, | |
4303ea89 | 1003 | 'financial_type_id' => 4, |
af595ac2 | 1004 | 'contribution_status_id' => 'Pending', |
9099cab3 | 1005 | ]; |
af595ac2 | 1006 | $order = $this->callAPISuccess('Order', 'create', $params); |
5c3d600f PN |
1007 | $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']); |
1008 | $this->assertTrue($allowUpdate); | |
1009 | ||
1010 | $event = $this->eventCreate(); | |
9099cab3 | 1011 | $params = [ |
5c3d600f PN |
1012 | 'contact_id' => $contactId, |
1013 | 'receive_date' => '2010-01-20', | |
1014 | 'total_amount' => 300, | |
8484a5f0 | 1015 | 'financial_type_id' => $this->getFinancialTypeId('Event Fee'), |
af595ac2 | 1016 | 'contribution_status_id' => 'Pending', |
9099cab3 | 1017 | ]; |
5c3d600f PN |
1018 | $priceFields = $this->createPriceSet('event', $event['id']); |
1019 | foreach ($priceFields['values'] as $key => $priceField) { | |
9099cab3 | 1020 | $lineItems[$key] = [ |
5c3d600f PN |
1021 | 'price_field_id' => $priceField['price_field_id'], |
1022 | 'price_field_value_id' => $priceField['id'], | |
1023 | 'label' => $priceField['label'], | |
1024 | 'field_title' => $priceField['label'], | |
1025 | 'qty' => 1, | |
1026 | 'unit_price' => $priceField['amount'], | |
1027 | 'line_total' => $priceField['amount'], | |
1028 | 'financial_type_id' => $priceField['financial_type_id'], | |
1029 | 'entity_table' => 'civicrm_participant', | |
9099cab3 | 1030 | ]; |
5c3d600f | 1031 | } |
9099cab3 | 1032 | $params['line_items'][] = [ |
5c3d600f | 1033 | 'line_item' => $lineItems, |
9099cab3 | 1034 | 'params' => [ |
5c3d600f PN |
1035 | 'contact_id' => $contactId, |
1036 | 'event_id' => $event['id'], | |
1037 | 'status_id' => 1, | |
1038 | 'role_id' => 1, | |
1039 | 'register_date' => '2007-07-21 00:00:00', | |
1040 | 'source' => 'Online Event Registration: API Testing', | |
9099cab3 CW |
1041 | ], |
1042 | ]; | |
af595ac2 | 1043 | $order = $this->callAPISuccess('Order', 'create', $params); |
5c3d600f PN |
1044 | $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']); |
1045 | $this->assertFalse($allowUpdate); | |
1046 | ||
9099cab3 | 1047 | $params = [ |
5c3d600f PN |
1048 | 'contact_id' => $contactId, |
1049 | 'receive_date' => '2010-01-20', | |
1050 | 'total_amount' => 200, | |
8484a5f0 | 1051 | 'financial_type_id' => $this->getFinancialTypeId('Member Dues'), |
af595ac2 | 1052 | 'contribution_status_id' => 'Pending', |
9099cab3 | 1053 | ]; |
5c3d600f PN |
1054 | $membershipType = $this->membershipTypeCreate(); |
1055 | $priceFields = $this->createPriceSet(); | |
9099cab3 | 1056 | $lineItems = []; |
5c3d600f | 1057 | foreach ($priceFields['values'] as $key => $priceField) { |
9099cab3 | 1058 | $lineItems[$key] = [ |
5c3d600f PN |
1059 | 'price_field_id' => $priceField['price_field_id'], |
1060 | 'price_field_value_id' => $priceField['id'], | |
1061 | 'label' => $priceField['label'], | |
1062 | 'field_title' => $priceField['label'], | |
1063 | 'qty' => 1, | |
1064 | 'unit_price' => $priceField['amount'], | |
1065 | 'line_total' => $priceField['amount'], | |
1066 | 'financial_type_id' => $priceField['financial_type_id'], | |
1067 | 'entity_table' => 'civicrm_membership', | |
1068 | 'membership_type_id' => $membershipType, | |
9099cab3 | 1069 | ]; |
5c3d600f | 1070 | } |
9099cab3 CW |
1071 | $params['line_items'][] = [ |
1072 | 'line_item' => [array_pop($lineItems)], | |
1073 | 'params' => [ | |
5c3d600f PN |
1074 | 'contact_id' => $contactId, |
1075 | 'membership_type_id' => $membershipType, | |
1076 | 'join_date' => '2006-01-21', | |
1077 | 'start_date' => '2006-01-21', | |
1078 | 'end_date' => '2006-12-21', | |
1079 | 'source' => 'Payment', | |
1080 | 'is_override' => 1, | |
1081 | 'status_id' => 1, | |
9099cab3 CW |
1082 | ], |
1083 | ]; | |
af595ac2 | 1084 | $order = $this->callAPISuccess('Order', 'create', $params); |
5c3d600f PN |
1085 | $allowUpdate = CRM_Contribute_BAO_Contribution::allowUpdateRevenueRecognitionDate($order['id']); |
1086 | $this->assertFalse($allowUpdate); | |
1087 | } | |
1088 | ||
d9553c2e PN |
1089 | /** |
1090 | * Test calculateFinancialItemAmount(). | |
1091 | */ | |
1092 | public function testcalculateFinancialItemAmount() { | |
9099cab3 CW |
1093 | $testParams = [ |
1094 | [ | |
1095 | 'params' => [], | |
1096 | 'amountParams' => [ | |
d9553c2e PN |
1097 | 'line_total' => 100, |
1098 | 'previous_line_total' => 300, | |
1099 | 'diff' => 1, | |
9099cab3 | 1100 | ], |
d9553c2e PN |
1101 | 'context' => 'changedAmount', |
1102 | 'expectedItemAmount' => -200, | |
9099cab3 CW |
1103 | ], |
1104 | [ | |
1105 | 'params' => [], | |
1106 | 'amountParams' => [ | |
d9553c2e PN |
1107 | 'line_total' => 100, |
1108 | 'previous_line_total' => 100, | |
1109 | 'diff' => -1, | |
9099cab3 | 1110 | ], |
efb16c63 | 1111 | // Most contexts are ignored. Removing refs to change payment instrument so placeholder. |
1112 | 'context' => 'not null', | |
d9553c2e | 1113 | 'expectedItemAmount' => -100, |
9099cab3 CW |
1114 | ], |
1115 | [ | |
1116 | 'params' => [ | |
d9553c2e PN |
1117 | 'is_quick_config' => TRUE, |
1118 | 'total_amount' => 110, | |
1119 | 'tax_amount' => 10, | |
9099cab3 CW |
1120 | ], |
1121 | 'amountParams' => [ | |
d9553c2e | 1122 | 'item_amount' => 100, |
9099cab3 | 1123 | ], |
d9553c2e PN |
1124 | 'context' => 'changedAmount', |
1125 | 'expectedItemAmount' => 100, | |
9099cab3 CW |
1126 | ], |
1127 | [ | |
1128 | 'params' => [ | |
d9553c2e PN |
1129 | 'is_quick_config' => TRUE, |
1130 | 'total_amount' => 110, | |
1131 | 'tax_amount' => 10, | |
9099cab3 CW |
1132 | ], |
1133 | 'amountParams' => [ | |
d9553c2e | 1134 | 'item_amount' => NULL, |
9099cab3 | 1135 | ], |
d9553c2e PN |
1136 | 'context' => 'changedAmount', |
1137 | 'expectedItemAmount' => 110, | |
9099cab3 CW |
1138 | ], |
1139 | [ | |
1140 | 'params' => [ | |
d9553c2e PN |
1141 | 'is_quick_config' => TRUE, |
1142 | 'total_amount' => 110, | |
1143 | 'tax_amount' => 10, | |
9099cab3 CW |
1144 | ], |
1145 | 'amountParams' => [ | |
d9553c2e | 1146 | 'item_amount' => NULL, |
9099cab3 | 1147 | ], |
d9553c2e PN |
1148 | 'context' => NULL, |
1149 | 'expectedItemAmount' => 100, | |
9099cab3 CW |
1150 | ], |
1151 | ]; | |
d9553c2e PN |
1152 | foreach ($testParams as $params) { |
1153 | $itemAmount = CRM_Contribute_BAO_Contribution::calculateFinancialItemAmount($params['params'], $params['amountParams'], $params['context']); | |
1154 | $this->assertEquals($itemAmount, $params['expectedItemAmount'], 'Invalid Financial Item amount.'); | |
1155 | } | |
1156 | } | |
1157 | ||
d934a732 PN |
1158 | /** |
1159 | * Test recording of amount with comma separator. | |
b4c48831 | 1160 | * |
1161 | * @throws \CRM_Core_Exception | |
d934a732 PN |
1162 | */ |
1163 | public function testCommaSeparatorAmount() { | |
1164 | $contactId = $this->individualCreate(); | |
1165 | ||
9099cab3 | 1166 | $params = [ |
d934a732 PN |
1167 | 'contact_id' => $contactId, |
1168 | 'currency' => 'USD', | |
1169 | 'financial_type_id' => 1, | |
b4c48831 | 1170 | 'contribution_status_id' => 'Pending', |
d934a732 PN |
1171 | 'payment_instrument_id' => 1, |
1172 | 'receive_date' => '20080522000000', | |
1173 | 'receipt_date' => '20080522000000', | |
b4c48831 | 1174 | 'total_amount' => '20,000.00', |
1175 | 'api.Payment.create' => ['total_amount' => '8,000.00'], | |
9099cab3 | 1176 | ]; |
d934a732 | 1177 | |
b4c48831 | 1178 | $contribution = $this->callAPISuccess('Order', 'create', $params); |
d0c97775 | 1179 | $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); |
d934a732 PN |
1180 | $financialTrxn = $this->callAPISuccessGetSingle( |
1181 | 'FinancialTrxn', | |
9099cab3 | 1182 | [ |
d934a732 | 1183 | 'id' => $lastFinancialTrxnId['financialTrxnId'], |
9099cab3 CW |
1184 | 'return' => ['total_amount'], |
1185 | ] | |
d934a732 | 1186 | ); |
b4c48831 | 1187 | $this->assertEquals($financialTrxn['total_amount'], 8000, 'Invalid amount.'); |
d934a732 PN |
1188 | } |
1189 | ||
adbc354b PN |
1190 | /** |
1191 | * Test for function getSalesTaxFinancialAccounts(). | |
1192 | */ | |
1193 | public function testgetSalesTaxFinancialAccounts() { | |
1194 | $this->enableTaxAndInvoicing(); | |
1195 | $financialType = $this->createFinancialType(); | |
1196 | $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']); | |
9099cab3 | 1197 | $expectedResult = [$financialAccount->financial_account_id => $financialAccount->financial_account_id]; |
adbc354b PN |
1198 | $financialType = $this->createFinancialType(); |
1199 | $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']); | |
1200 | $expectedResult[$financialAccount->financial_account_id] = $financialAccount->financial_account_id; | |
1201 | $salesTaxFinancialAccount = CRM_Contribute_BAO_Contribution::getSalesTaxFinancialAccounts(); | |
1202 | $this->assertTrue(($salesTaxFinancialAccount == $expectedResult), 'Function returned wrong values.'); | |
1203 | } | |
1204 | ||
0409b821 PN |
1205 | /** |
1206 | * Test for function createProportionalEntry(). | |
83644f47 | 1207 | * |
1208 | * @param string $thousandSeparator | |
1209 | * punctuation used to refer to thousands. | |
1210 | * | |
1211 | * @dataProvider getThousandSeparators | |
0409b821 | 1212 | */ |
83644f47 | 1213 | public function testCreateProportionalEntry($thousandSeparator) { |
1214 | $this->setCurrencySeparators($thousandSeparator); | |
2a4a2f00 | 1215 | list($contribution, $financialAccount) = $this->createContributionWithTax(); |
9099cab3 | 1216 | $params = [ |
0409b821 PN |
1217 | 'total_amount' => 55, |
1218 | 'to_financial_account_id' => $financialAccount->financial_account_id, | |
1219 | 'payment_instrument_id' => 1, | |
1220 | 'trxn_date' => date('Ymd'), | |
1221 | 'status_id' => 1, | |
0409b821 | 1222 | 'entity_id' => $contribution['id'], |
9099cab3 | 1223 | ]; |
3137781d | 1224 | $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params); |
9099cab3 | 1225 | $entityParams = [ |
0409b821 PN |
1226 | 'contribution_total_amount' => $contribution['total_amount'], |
1227 | 'trxn_total_amount' => 55, | |
1228 | 'line_item_amount' => 100, | |
9099cab3 | 1229 | ]; |
0409b821 | 1230 | $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']); |
9099cab3 | 1231 | $eftParams = [ |
0409b821 | 1232 | 'entity_table' => 'civicrm_financial_item', |
cf28d075 | 1233 | 'entity_id' => $previousLineItem['id'], |
3137781d | 1234 | 'financial_trxn_id' => (string) $financialTrxn['id'], |
9099cab3 | 1235 | ]; |
0409b821 | 1236 | CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams); |
9099cab3 | 1237 | $trxnTestArray = array_merge($eftParams, [ |
3137781d | 1238 | 'amount' => '50.00', |
9099cab3 | 1239 | ]); |
cf28d075 | 1240 | $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray); |
0409b821 PN |
1241 | } |
1242 | ||
c364c544 MW |
1243 | /** |
1244 | * Test for function createProportionalEntry with zero amount(). | |
1245 | * | |
1246 | * @param string $thousandSeparator | |
1247 | * punctuation used to refer to thousands. | |
1248 | * | |
1249 | * @dataProvider getThousandSeparators | |
1250 | */ | |
1251 | public function testCreateProportionalEntryZeroAmount($thousandSeparator) { | |
1252 | $this->setCurrencySeparators($thousandSeparator); | |
9099cab3 CW |
1253 | list($contribution, $financialAccount) = $this->createContributionWithTax(['total_amount' => 0]); |
1254 | $params = [ | |
c364c544 MW |
1255 | 'total_amount' => 0, |
1256 | 'to_financial_account_id' => $financialAccount->financial_account_id, | |
1257 | 'payment_instrument_id' => 1, | |
1258 | 'trxn_date' => date('Ymd'), | |
1259 | 'status_id' => 1, | |
1260 | 'entity_id' => $contribution['id'], | |
9099cab3 | 1261 | ]; |
c364c544 | 1262 | $financialTrxn = $this->callAPISuccess('FinancialTrxn', 'create', $params); |
9099cab3 | 1263 | $entityParams = [ |
c364c544 MW |
1264 | 'contribution_total_amount' => $contribution['total_amount'], |
1265 | 'trxn_total_amount' => 0, | |
1266 | 'line_item_amount' => 0, | |
9099cab3 | 1267 | ]; |
c364c544 | 1268 | $previousLineItem = CRM_Financial_BAO_FinancialItem::getPreviousFinancialItem($contribution['id']); |
9099cab3 | 1269 | $eftParams = [ |
c364c544 MW |
1270 | 'entity_table' => 'civicrm_financial_item', |
1271 | 'entity_id' => $previousLineItem['id'], | |
1272 | 'financial_trxn_id' => (string) $financialTrxn['id'], | |
9099cab3 | 1273 | ]; |
c364c544 | 1274 | CRM_Contribute_BAO_Contribution::createProportionalEntry($entityParams, $eftParams); |
9099cab3 | 1275 | $trxnTestArray = array_merge($eftParams, [ |
c364c544 | 1276 | 'amount' => '0.00', |
9099cab3 | 1277 | ]); |
c364c544 MW |
1278 | $this->callAPISuccessGetSingle('EntityFinancialTrxn', $eftParams, $trxnTestArray); |
1279 | } | |
1280 | ||
646bc565 PN |
1281 | /** |
1282 | * Test for function getLastFinancialItemIds(). | |
1283 | */ | |
1284 | public function testgetLastFinancialItemIds() { | |
2a4a2f00 | 1285 | list($contribution, $financialAccount) = $this->createContributionWithTax(); |
646bc565 PN |
1286 | list($ftIds, $taxItems) = CRM_Contribute_BAO_Contribution::getLastFinancialItemIds($contribution['id']); |
1287 | $this->assertEquals(count($ftIds), 1, 'Invalid count.'); | |
1288 | $this->assertEquals(count($taxItems), 1, 'Invalid count.'); | |
1289 | foreach ($taxItems as $value) { | |
1290 | $this->assertEquals($value['amount'], 10, 'Invalid tax amount.'); | |
1291 | } | |
1292 | } | |
1293 | ||
2a4a2f00 | 1294 | /** |
97c818fd | 1295 | * Test to ensure proportional entries are creating when adding a payment.. |
1296 | * | |
1297 | * In this test we create a pending contribution for $110 consisting of $100 contribution and $10 tax. | |
1298 | * | |
1299 | * We pay $50, resulting in it being allocated as $45.45 paymnt & $4.55 tax. This is in equivalent proportions | |
1300 | * 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 | |
1301 | * saved un-rounded). | |
2a4a2f00 | 1302 | */ |
97c818fd | 1303 | public function testCreateProportionalFinancialEntriesViaPaymentCreate() { |
1304 | list($contribution, $financialAccount) = $this->createContributionWithTax([], FALSE); | |
1305 | $params = [ | |
3137781d | 1306 | 'total_amount' => 50, |
2a4a2f00 PN |
1307 | 'to_financial_account_id' => $financialAccount->financial_account_id, |
1308 | 'payment_instrument_id' => 1, | |
1309 | 'trxn_date' => date('Ymd'), | |
1310 | 'status_id' => 1, | |
2a4a2f00 | 1311 | 'entity_id' => $contribution['id'], |
97c818fd | 1312 | 'contribution_id' => $contribution['id'], |
1313 | ]; | |
1314 | $financialTrxn = $this->callAPISuccess('Payment', 'create', $params); | |
1315 | $eftParams = [ | |
2a4a2f00 PN |
1316 | 'entity_table' => 'civicrm_financial_item', |
1317 | 'financial_trxn_id' => $financialTrxn['id'], | |
97c818fd | 1318 | ]; |
2a4a2f00 PN |
1319 | $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams); |
1320 | $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.'); | |
97c818fd | 1321 | $testAmount = [4.55, 45.45]; |
2a4a2f00 | 1322 | foreach ($entityFinancialTrxn['values'] as $value) { |
97c818fd | 1323 | $this->assertEquals(array_pop($testAmount), $value['amount'], 'Invalid amount stored in civicrm_entity_financial_trxn.'); |
77641de4 PN |
1324 | } |
1325 | } | |
1326 | ||
1327 | /** | |
1328 | * Test to check if amount is proportionally asigned for PI change. | |
1329 | */ | |
1330 | public function testProportionallyAssignedForPIChange() { | |
1331 | list($contribution, $financialAccount) = $this->createContributionWithTax(); | |
9099cab3 | 1332 | $params = [ |
77641de4 PN |
1333 | 'id' => $contribution['id'], |
1334 | 'payment_instrument_id' => 3, | |
9099cab3 | 1335 | ]; |
77641de4 PN |
1336 | $this->callAPISuccess('Contribution', 'create', $params); |
1337 | $lastFinancialTrxnId = CRM_Core_BAO_FinancialTrxn::getFinancialTrxnId($contribution['id'], 'DESC'); | |
9099cab3 | 1338 | $eftParams = [ |
77641de4 PN |
1339 | 'entity_table' => 'civicrm_financial_item', |
1340 | 'financial_trxn_id' => $lastFinancialTrxnId['financialTrxnId'], | |
9099cab3 | 1341 | ]; |
77641de4 PN |
1342 | $entityFinancialTrxn = $this->callAPISuccess('EntityFinancialTrxn', 'Get', $eftParams); |
1343 | $this->assertEquals($entityFinancialTrxn['count'], 2, 'Invalid count.'); | |
9099cab3 | 1344 | $testAmount = [10, 100]; |
77641de4 PN |
1345 | foreach ($entityFinancialTrxn['values'] as $value) { |
1346 | $this->assertEquals($value['amount'], array_pop($testAmount), 'Invalid amount stored in civicrm_entity_financial_trxn.'); | |
2a4a2f00 PN |
1347 | } |
1348 | } | |
1349 | ||
646bc565 PN |
1350 | /** |
1351 | * Function to create contribution with tax. | |
1352 | */ | |
9099cab3 | 1353 | public function createContributionWithTax($params = [], $isCompleted = TRUE) { |
c364c544 MW |
1354 | if (!isset($params['total_amount'])) { |
1355 | $params['total_amount'] = 100; | |
1356 | } | |
646bc565 PN |
1357 | $contactId = $this->individualCreate(); |
1358 | $this->enableTaxAndInvoicing(); | |
1359 | $financialType = $this->createFinancialType(); | |
1360 | $financialAccount = $this->relationForFinancialTypeWithFinancialAccount($financialType['id']); | |
1361 | $form = new CRM_Contribute_Form_Contribution(); | |
1362 | ||
9099cab3 | 1363 | $form->testSubmit([ |
39b959db SL |
1364 | 'total_amount' => $params['total_amount'], |
1365 | 'financial_type_id' => $financialType['id'], | |
1366 | 'contact_id' => $contactId, | |
97c818fd | 1367 | 'contribution_status_id' => $isCompleted ? 1 : 2, |
39b959db | 1368 | 'price_set_id' => 0, |
9099cab3 | 1369 | ], CRM_Core_Action::ADD); |
646bc565 | 1370 | $contribution = $this->callAPISuccessGetSingle('Contribution', |
9099cab3 | 1371 | [ |
646bc565 | 1372 | 'contact_id' => $contactId, |
9099cab3 CW |
1373 | 'return' => ['tax_amount', 'total_amount'], |
1374 | ] | |
646bc565 | 1375 | ); |
9099cab3 | 1376 | return [$contribution, $financialAccount]; |
646bc565 PN |
1377 | } |
1378 | ||
75c07fde JP |
1379 | /** |
1380 | * Test processOnBehalfOrganization() function. | |
1381 | */ | |
1382 | public function testProcessOnBehalfOrganization() { | |
9099cab3 | 1383 | $orgInfo = [ |
75c07fde JP |
1384 | 'phone' => '11111111', |
1385 | 'email' => 'testorg@gmail.com', | |
1386 | 'street_address' => 'test Street', | |
1387 | 'city' => 'test City', | |
1388 | 'state_province' => 'AA', | |
1389 | 'postal_code' => '222222', | |
1390 | 'country' => 'United States', | |
9099cab3 | 1391 | ]; |
75c07fde | 1392 | $contactID = $this->individualCreate(); |
9099cab3 CW |
1393 | $orgId = $this->organizationCreate(['organization_name' => 'testorg1']); |
1394 | $orgCount = $this->callAPISuccessGetCount('Contact', [ | |
75c07fde JP |
1395 | 'contact_type' => "Organization", |
1396 | 'organization_name' => "testorg1", | |
9099cab3 | 1397 | ]); |
75c07fde JP |
1398 | $this->assertEquals($orgCount, 1); |
1399 | ||
9099cab3 CW |
1400 | $values = $params = []; |
1401 | $behalfOrganization = [ | |
75c07fde | 1402 | 'organization_name' => 'testorg1', |
9099cab3 CW |
1403 | 'phone' => [ |
1404 | 1 => [ | |
75c07fde JP |
1405 | 'phone' => $orgInfo['phone'], |
1406 | 'is_primary' => 1, | |
9099cab3 CW |
1407 | ], |
1408 | ], | |
1409 | 'email' => [ | |
1410 | 1 => [ | |
75c07fde JP |
1411 | 'email' => $orgInfo['email'], |
1412 | 'is_primary' => 1, | |
9099cab3 CW |
1413 | ], |
1414 | ], | |
1415 | 'address' => [ | |
1416 | 3 => [ | |
75c07fde JP |
1417 | 'street_address' => $orgInfo['street_address'], |
1418 | 'city' => $orgInfo['city'], | |
1419 | 'location_type_id' => 3, | |
1420 | 'postal_code' => $orgInfo['postal_code'], | |
1421 | 'country' => 'US', | |
1422 | 'state_province' => 'AA', | |
1423 | 'is_primary' => 1, | |
9099cab3 CW |
1424 | ], |
1425 | ], | |
1426 | ]; | |
1427 | $fields = [ | |
75c07fde JP |
1428 | 'organization_name' => 1, |
1429 | 'phone-3-1' => 1, | |
1430 | 'email-3' => 1, | |
1431 | 'street_address-3' => 1, | |
1432 | 'city-3' => 1, | |
1433 | 'postal_code-3' => 1, | |
1434 | 'country-3' => 1, | |
1435 | 'state_province-3' => 1, | |
9099cab3 | 1436 | ]; |
75c07fde JP |
1437 | CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields); |
1438 | ||
1439 | //Check whether new organisation is not created. | |
9099cab3 | 1440 | $result = $this->callAPISuccess('Contact', 'get', [ |
75c07fde JP |
1441 | 'contact_type' => "Organization", |
1442 | 'organization_name' => "testorg1", | |
9099cab3 | 1443 | ]); |
75c07fde JP |
1444 | $this->assertEquals($result['count'], 1); |
1445 | ||
1446 | //Assert all org values are updated. | |
1447 | foreach ($orgInfo as $key => $val) { | |
1448 | $this->assertEquals($result['values'][$orgId][$key], $val); | |
1449 | } | |
1450 | ||
1451 | //Check if alert is assigned to params if more than 1 dupe exists. | |
9099cab3 | 1452 | $orgId = $this->organizationCreate(['organization_name' => 'testorg1', 'email' => 'testorg@gmail.com']); |
75c07fde JP |
1453 | CRM_Contribute_Form_Contribution_Confirm::processOnBehalfOrganization($behalfOrganization, $contactID, $values, $params, $fields); |
1454 | $this->assertEquals($params['onbehalf_dupe_alert'], 1); | |
1455 | } | |
1456 | ||
7e2ec997 E |
1457 | /** |
1458 | * Test for replaceContributionTokens. | |
1459 | * This function tests whether the contribution tokens are replaced with values from contribution. | |
1460 | */ | |
1461 | public function testReplaceContributionTokens() { | |
9945400a | 1462 | $customGroup = $this->customGroupCreate(['extends' => 'Contribution', 'title' => 'contribution stuff']); |
875e076b | 1463 | $customField = $this->customFieldOptionValueCreate($customGroup, 'myCustomField'); |
7e2ec997 | 1464 | $contactId1 = $this->individualCreate(); |
9099cab3 | 1465 | $params = [ |
7e2ec997 E |
1466 | 'contact_id' => $contactId1, |
1467 | 'receive_date' => '20120511', | |
1468 | 'total_amount' => 100.00, | |
1469 | 'financial_type_id' => 1, | |
1470 | 'trxn_id' => 12345, | |
1471 | 'invoice_id' => 67890, | |
1472 | 'source' => 'SSF', | |
1473 | 'contribution_status_id' => 2, | |
875e076b | 1474 | "custom_{$customField['id']}" => 'value1', |
9099cab3 | 1475 | ]; |
7e2ec997 E |
1476 | $contribution1 = $this->contributionCreate($params); |
1477 | $contactId2 = $this->individualCreate(); | |
9099cab3 | 1478 | $params = [ |
7e2ec997 E |
1479 | 'contact_id' => $contactId2, |
1480 | 'receive_date' => '20150511', | |
1481 | 'total_amount' => 200.00, | |
1482 | 'financial_type_id' => 1, | |
1483 | 'trxn_id' => 6789, | |
1484 | 'invoice_id' => 12345, | |
1485 | 'source' => 'ABC', | |
1486 | 'contribution_status_id' => 1, | |
875e076b | 1487 | "custom_{$customField['id']}" => 'value2', |
9099cab3 | 1488 | ]; |
7e2ec997 | 1489 | $contribution2 = $this->contributionCreate($params); |
9099cab3 | 1490 | $ids = [$contribution1, $contribution2]; |
7e2ec997 E |
1491 | |
1492 | $subject = "This is a test for contribution ID: {contribution.contribution_id}"; | |
1493 | $text = "Contribution Amount: {contribution.total_amount}"; | |
1494 | $html = "<p>Contribution Source: {contribution.contribution_source}</p></br> | |
1495 | <p>Contribution Invoice ID: {contribution.invoice_id}</p></br> | |
875e076b | 1496 | <p>Contribution Receive Date: {contribution.receive_date}</p></br> |
596a28fc | 1497 | <p>Contribution Custom Field: {contribution.custom_{$customField['id']}}</p></br>"; |
7e2ec997 E |
1498 | |
1499 | $subjectToken = CRM_Utils_Token::getTokens($subject); | |
1500 | $messageToken = CRM_Utils_Token::getTokens($text); | |
1501 | $messageToken = array_merge($messageToken, CRM_Utils_Token::getTokens($html)); | |
1502 | ||
1503 | $contributionDetails = CRM_Contribute_BAO_Contribution::replaceContributionTokens( | |
1504 | $ids, | |
1505 | $subject, | |
1506 | $subjectToken, | |
1507 | $text, | |
1508 | $html, | |
1509 | $messageToken, | |
1510 | TRUE | |
1511 | ); | |
1512 | ||
1513 | $this->assertEquals("Contribution Amount: $ 100.00", $contributionDetails[$contactId1]['text'], "The text does not match"); | |
1514 | $this->assertEquals("<p>Contribution Source: ABC</p></br> | |
1515 | <p>Contribution Invoice ID: 12345</p></br> | |
875e076b | 1516 | <p>Contribution Receive Date: May 11th, 2015</p></br> |
596a28fc | 1517 | <p>Contribution Custom Field: Label2</p></br>", $contributionDetails[$contactId2]['html'], "The html does not match"); |
7e2ec997 E |
1518 | } |
1519 | ||
7b5169d0 PN |
1520 | /** |
1521 | * Test for contribution with deferred revenue. | |
1522 | */ | |
1523 | public function testContributionWithDeferredRevenue() { | |
1524 | $contactId = $this->individualCreate(); | |
1525 | Civi::settings()->set('deferred_revenue_enabled', TRUE); | |
9099cab3 | 1526 | $params = [ |
7b5169d0 PN |
1527 | 'contact_id' => $contactId, |
1528 | 'receive_date' => '20120511', | |
1529 | 'total_amount' => 100.00, | |
1530 | 'financial_type_id' => 'Event Fee', | |
1531 | 'trxn_id' => 12345, | |
1532 | 'invoice_id' => 67890, | |
1533 | 'source' => 'SSF', | |
1534 | 'contribution_status_id' => 'Completed', | |
1535 | 'revenue_recognition_date' => date('Ymd', strtotime("+3 month")), | |
9099cab3 | 1536 | ]; |
7b5169d0 PN |
1537 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
1538 | ||
9099cab3 | 1539 | $this->callAPISuccessGetCount('EntityFinancialTrxn', [ |
7b5169d0 PN |
1540 | 'entity_table' => "civicrm_contribution", |
1541 | 'entity_id' => $contribution['id'], | |
9099cab3 | 1542 | ], 2); |
7b5169d0 | 1543 | |
9099cab3 | 1544 | $checkAgainst = [ |
7b5169d0 PN |
1545 | 'financial_trxn_id.to_financial_account_id.name' => 'Deferred Revenue - Event Fee', |
1546 | 'financial_trxn_id.from_financial_account_id.name' => 'Event Fee', | |
f818aed5 | 1547 | 'financial_trxn_id' => '2', |
9099cab3 CW |
1548 | ]; |
1549 | $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', [ | |
1550 | 'return' => [ | |
7b5169d0 PN |
1551 | "financial_trxn_id.from_financial_account_id.name", |
1552 | "financial_trxn_id.to_financial_account_id.name", | |
1553 | "financial_trxn_id", | |
9099cab3 | 1554 | ], |
7b5169d0 PN |
1555 | 'entity_table' => "civicrm_contribution", |
1556 | 'entity_id' => $contribution['id'], | |
1557 | 'financial_trxn_id.is_payment' => 0, | |
9099cab3 | 1558 | ], $checkAgainst); |
7b5169d0 | 1559 | |
9099cab3 | 1560 | $result = $this->callAPISuccessGetSingle('EntityFinancialTrxn', [ |
7b5169d0 PN |
1561 | 'entity_table' => "civicrm_financial_item", |
1562 | 'financial_trxn_id' => $result['financial_trxn_id'], | |
9099cab3 CW |
1563 | 'return' => ['entity_id'], |
1564 | ]); | |
7b5169d0 | 1565 | |
9099cab3 | 1566 | $checkAgainst = [ |
7b5169d0 PN |
1567 | 'financial_account_id.name' => 'Deferred Revenue - Event Fee', |
1568 | 'id' => $result['entity_id'], | |
9099cab3 CW |
1569 | ]; |
1570 | $result = $this->callAPISuccessGetSingle('FinancialItem', [ | |
7b5169d0 | 1571 | 'id' => $result['entity_id'], |
9099cab3 CW |
1572 | 'return' => ["financial_account_id.name"], |
1573 | ], $checkAgainst); | |
7b5169d0 PN |
1574 | } |
1575 | ||
a495d83a PN |
1576 | /** |
1577 | * https://lab.civicrm.org/dev/financial/issues/56 | |
1578 | * Changing financial type on a contribution records correct financial items | |
1579 | */ | |
1580 | public function testChangingFinancialTypeWithoutTax() { | |
1581 | $ids = $values = []; | |
1582 | $contactId = $this->individualCreate(); | |
9099cab3 | 1583 | $params = [ |
a495d83a PN |
1584 | 'contact_id' => $contactId, |
1585 | 'receive_date' => date('YmdHis'), | |
1586 | 'total_amount' => 100.00, | |
1587 | 'financial_type_id' => 'Donation', | |
1588 | 'contribution_status_id' => 'Completed', | |
9099cab3 | 1589 | ]; |
a495d83a PN |
1590 | /* first test the scenario when sending an email */ |
1591 | $contributionId = $this->callAPISuccess( | |
1592 | 'contribution', | |
1593 | 'create', | |
1594 | $params | |
1595 | )['id']; | |
1596 | ||
1597 | // Update Financial Type. | |
1598 | $this->callAPISuccess('contribution', 'create', [ | |
1599 | 'id' => $contributionId, | |
1600 | 'financial_type_id' => 'Event Fee', | |
1601 | ]); | |
1602 | ||
1603 | // Get line item | |
1604 | $lineItem = $this->callAPISuccessGetSingle('LineItem', [ | |
1605 | 'contribution_id' => $contributionId, | |
1606 | 'return' => ["financial_type_id.name", "line_total"], | |
1607 | ]); | |
1608 | ||
1609 | $this->assertEquals( | |
1610 | $lineItem['line_total'], | |
1611 | 100.00, | |
1612 | 'Invalid line amount.' | |
1613 | ); | |
1614 | ||
1615 | $this->assertEquals( | |
1616 | $lineItem['financial_type_id.name'], | |
1617 | 'Event Fee', | |
1618 | 'Invalid Financial Type stored.' | |
1619 | ); | |
1620 | ||
1621 | // Get Financial Items. | |
1622 | $financialItems = $this->callAPISuccess('FinancialItem', 'get', [ | |
1623 | 'entity_id' => $lineItem['id'], | |
1624 | 'sequential' => 1, | |
1625 | 'entity_table' => 'civicrm_line_item', | |
1626 | 'options' => ['sort' => "id"], | |
1627 | 'return' => ["financial_account_id.name", "amount", "description"], | |
1628 | ]); | |
1629 | ||
1630 | $this->assertEquals($financialItems['count'], 3, 'Count mismatch.'); | |
1631 | ||
1632 | $toCheck = [ | |
1633 | ['Donation', 100.00], | |
1634 | ['Donation', -100.00], | |
1635 | ['Event Fee', 100.00], | |
1636 | ]; | |
1637 | ||
1638 | foreach ($financialItems['values'] as $key => $values) { | |
1639 | $this->assertEquals( | |
1640 | $values['financial_account_id.name'], | |
1641 | $toCheck[$key][0], | |
1642 | 'Invalid Financial Account stored.' | |
1643 | ); | |
1644 | $this->assertEquals( | |
1645 | $values['amount'], | |
1646 | $toCheck[$key][1], | |
1647 | 'Amount mismatch.' | |
1648 | ); | |
1649 | $this->assertEquals( | |
1650 | $values['description'], | |
1651 | 'Contribution Amount', | |
1652 | 'Description mismatch.' | |
1653 | ); | |
1654 | } | |
1655 | ||
1656 | // Check transactions. | |
1657 | $financialTransactions = $this->callAPISuccess('EntityFinancialTrxn', 'get', [ | |
1658 | 'return' => ["financial_trxn_id"], | |
1659 | 'entity_table' => "civicrm_contribution", | |
1660 | 'entity_id' => $contributionId, | |
1661 | 'sequential' => 1, | |
1662 | ]); | |
1663 | $this->assertEquals($financialTransactions['count'], 3, 'Count mismatch.'); | |
1664 | ||
1665 | foreach ($financialTransactions['values'] as $key => $values) { | |
1666 | $this->callAPISuccessGetCount('EntityFinancialTrxn', [ | |
1667 | 'financial_trxn_id' => $values['financial_trxn_id'], | |
1668 | 'amount' => $toCheck[$key][1], | |
1669 | 'financial_trxn_id.total_amount' => $toCheck[$key][1], | |
1670 | ], 2); | |
1671 | } | |
1672 | } | |
1673 | ||
1db3ddea KE |
1674 | /** |
1675 | * CRM-21424 Check if the receipt update is set after composing the receipt message | |
1676 | */ | |
1677 | public function testSendMailUpdateReceiptDate() { | |
9099cab3 | 1678 | $ids = $values = []; |
1db3ddea | 1679 | $contactId = $this->individualCreate(); |
9099cab3 | 1680 | $params = [ |
1db3ddea KE |
1681 | 'contact_id' => $contactId, |
1682 | 'receive_date' => '20120511', | |
1683 | 'total_amount' => 100.00, | |
1684 | 'financial_type_id' => 'Donation', | |
1685 | 'source' => 'SSF', | |
1686 | 'contribution_status_id' => 'Completed', | |
9099cab3 | 1687 | ]; |
1db3ddea KE |
1688 | /* first test the scenario when sending an email */ |
1689 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
1690 | $contributionId = $contribution['id']; | |
1691 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null'); | |
9099cab3 | 1692 | $input = ['receipt_update' => 0]; |
1db3ddea KE |
1693 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values); |
1694 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null'); | |
9099cab3 | 1695 | $input = ['receipt_update' => 1]; |
1db3ddea KE |
1696 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values); |
1697 | $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set'); | |
1698 | ||
1699 | /* repeat the same scenario for downloading a pdf */ | |
1700 | $contribution = $this->callAPISuccess('contribution', 'create', $params); | |
1701 | $contributionId = $contribution['id']; | |
1702 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After creating receipt date must be null'); | |
9099cab3 | 1703 | $input = ['receipt_update' => 0]; |
1db3ddea KE |
1704 | /* setting the lasast parameter (returnmessagetext) to TRUE is done by the download of the pdf */ |
1705 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE); | |
1706 | $this->assertDBNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail, with the explicit instruction not to update receipt date stays null'); | |
9099cab3 | 1707 | $input = ['receipt_update' => 1]; |
1db3ddea KE |
1708 | CRM_Contribute_BAO_Contribution::sendMail($input, $ids, $contributionId, $values, TRUE); |
1709 | $this->assertDBNotNull('CRM_Contribute_BAO_Contribution', $contributionId, 'receipt_date', 'id', 'After sendMail with the permission to allow update receipt date must be set'); | |
1710 | } | |
1711 | ||
6a488035 | 1712 | } |