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