Test fix
[civicrm-core.git] / tests / phpunit / api / v3 / TaxContributionPageTest.php
CommitLineData
b5935203 1<?php
b5935203 2/*
3 +--------------------------------------------------------------------+
81621fee 4| CiviCRM version 4.7 |
b5935203 5+--------------------------------------------------------------------+
fa938177 6| Copyright CiviCRM LLC (c) 2004-2016 |
b5935203 7+--------------------------------------------------------------------+
8| This file is a part of CiviCRM. |
9| |
10| CiviCRM is free software; you can copy, modify, and distribute it |
11| under the terms of the GNU Affero General Public License |
12| Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13| |
14| CiviCRM is distributed in the hope that it will be useful, but |
15| WITHOUT ANY WARRANTY; without even the implied warranty of |
16| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17| See the GNU Affero General Public License for more details. |
18| |
19| You should have received a copy of the GNU Affero General Public |
20| License and the CiviCRM Licensing Exception along |
21| with this program; if not, contact CiviCRM LLC |
22| at info[AT]civicrm[DOT]org. If you have questions about the |
23| GNU Affero General Public License or the licensing of CiviCRM, |
24| see the CiviCRM license FAQ at http://civicrm.org/licensing |
25+--------------------------------------------------------------------+
e70a7fc0 26 */
b5935203 27
b5935203 28/**
29 * Class api_v3_TaxContributionPageTest
acb109b7 30 * @group headless
b5935203 31 */
32class api_v3_TaxContributionPageTest extends CiviUnitTestCase {
33 protected $_apiversion = 3;
34 protected $params;
35 protected $financialtypeID;
36 protected $financialAccountId;
37 protected $_entity = 'contribution_page';
38 protected $_priceSetParams = array();
39 protected $_paymentProcessorType;
40 protected $payParams = array();
41 protected $paymentProceParams = array();
42 protected $settingValue = array();
43 protected $setInvoiceSettings;
44 protected $_ids = array();
45 protected $_individualId;
46 protected $financialAccHalftax;
47 protected $financialtypeHalftax;
48 protected $financialRelationHalftax;
49 protected $halfFinancialAccId;
50 protected $halfFinancialTypeId;
51 public $DBResetRequired = TRUE;
52
53 public function setUp() {
54 parent::setUp();
55 $this->_individualId = $this->individualCreate();
56 $this->_orgId = $this->organizationCreate(NULL);
57
58 $this->params = array(
92fcb95f 59 'title' => "Test Contribution Page" . substr(sha1(rand()), 0, 7),
b5935203 60 'financial_type_id' => 1,
61 'payment_processor' => 1,
62 'currency' => 'NZD',
63 'goal_amount' => 350,
64 'is_pay_later' => 1,
65 'pay_later_text' => 'I will pay later',
66 'pay_later_receipt' => "I will pay later",
67 'is_monetary' => TRUE,
21dfd5f5 68 'is_billing_required' => TRUE,
b5935203 69 );
70
71 $this->_priceSetParams = array(
92fcb95f
TO
72 'name' => 'tax_contribution' . substr(sha1(rand()), 0, 7),
73 'title' => 'contributiontax' . substr(sha1(rand()), 0, 7),
b5935203 74 'is_active' => 1,
75 'help_pre' => "Where does your goat sleep",
76 'help_post' => "thank you for your time",
77 'extends' => 2,
78 'financial_type_id' => 3,
79 'is_quick_config' => 0,
21dfd5f5 80 'is_reserved' => 0,
b5935203 81 );
6c6e6187 82 // Financial Account with 20% tax rate
b5935203 83 $financialAccountSetparams = array(
84 #[domain_id] =>
92fcb95f 85 'name' => 'vat full taxrate account' . substr(sha1(rand()), 0, 7),
b5935203 86 'contact_id' => $this->_orgId,
87 'financial_account_type_id' => 2,
88 'is_tax' => 1,
89 'tax_rate' => 20.00,
90 'is_reserved' => 0,
91 'is_active' => 1,
92 'is_default' => 0,
93 );
94
95 $financialAccount = $this->callAPISuccess('financial_account', 'create', $financialAccountSetparams);
96 $this->financialAccountId = $financialAccount['id'];
97
98 // Financial type having 'Sales Tax Account is' with liability financail account
99 $financialType = array(
92fcb95f 100 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7),
b5935203 101 'is_reserved' => 0,
102 'is_active' => 1,
103 );
6c6e6187 104 $priceField = $this->callAPISuccess('financial_type', 'create', $financialType);
b5935203 105 $this->financialtypeID = $priceField['id'];
106 $financialRelationParams = array(
107 'entity_table' => 'civicrm_financial_type',
108 'entity_id' => $this->financialtypeID,
109 'account_relationship' => 10,
21dfd5f5 110 'financial_account_id' => $this->financialAccountId,
b5935203 111 );
112 $financialRelation = CRM_Financial_BAO_FinancialTypeAccount::add($financialRelationParams);
113
114 // Financial type with 5% tax rate
115 $financialAccHalftax = array(
92fcb95f 116 'name' => 'vat half taxrate account' . substr(sha1(rand()), 0, 7),
b5935203 117 'contact_id' => $this->_orgId,
118 'financial_account_type_id' => 2,
119 'is_tax' => 1,
120 'tax_rate' => 5.00,
121 'is_reserved' => 0,
122 'is_active' => 1,
21dfd5f5 123 'is_default' => 0,
b5935203 124 );
125 $halfFinancialAccount = CRM_Financial_BAO_FinancialAccount::add($financialAccHalftax);
126 $this->halfFinancialAccId = $halfFinancialAccount->id;
31037a42 127 $halfFinancialtypeHalftax = array(
92fcb95f 128 'name' => 'grassvariety2' . substr(sha1(rand()), 0, 7),
b5935203 129 'is_reserved' => 0,
130 'is_active' => 1,
131 );
31037a42 132
b5935203 133 $halfFinancialType = CRM_Financial_BAO_FinancialType::add($halfFinancialtypeHalftax);
6c6e6187 134 $this->halfFinancialTypeId = $halfFinancialType->id;
31037a42 135 $financialRelationHalftax = array(
b5935203 136 'entity_table' => 'civicrm_financial_type',
137 'entity_id' => $this->halfFinancialTypeId,
138 'account_relationship' => 10,
21dfd5f5 139 'financial_account_id' => $this->halfFinancialAccId,
b5935203 140 );
141
142 $halfFinancialRelation = CRM_Financial_BAO_FinancialTypeAccount::add($financialRelationHalftax);
143
144 // Enable component contribute setting
6c6e6187 145 $contributeSetting = array(
b5935203 146 'invoicing' => 1,
147 'invoice_prefix' => 'INV_',
148 'credit_notes_prefix' => 'CN_',
149 'due_date' => 10,
150 'due_date_period' => 'days',
151 'notes' => '',
152 'is_email_pdf' => 1,
153 'tax_term' => 'Sales Tax',
154 'tax_display_settings' => 'Inclusive',
155 );
08ef4ddd 156 $setInvoiceSettings = Civi::settings()->set('contribution_invoice_settings', $contributeSetting);
b5935203 157
158 // Payment Processor
159 $paymentProceParams = array(
160 'domain_id' => 1,
92fcb95f 161 'name' => 'dummy' . substr(sha1(rand()), 0, 7),
97502bac 162 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
b5935203 163 'financial_account_id' => 12,
164 'is_active' => 1,
165 'is_default' => 1,
b5935203 166 'user_name' => 'dummy',
167 'url_site' => 'http://dummy.com',
168 'url_recur' => 'http://dummyrecur.com',
169 'class_name' => 'Payment_Dummy',
170 'billing_mode' => 1,
171 'is_recur' => 1,
21dfd5f5 172 'payment_type' => 1,
b5935203 173 );
174 $result = $this->callAPISuccess('payment_processor', 'create', $paymentProceParams);
175 $this->_ids['paymentProcessID'] = $result['id'];
176 require_once 'api/v3/examples/PaymentProcessor/Create.php';
177 $this->assertAPISuccess($result);
178 }
179
00be9182 180 public function tearDown() {
b5935203 181 $this->quickCleanup(array(
182 'civicrm_contribution',
183 'civicrm_contribution_soft',
184 'civicrm_event',
185 'civicrm_contribution_page',
186 'civicrm_participant',
187 'civicrm_participant_payment',
188 'civicrm_line_item',
189 'civicrm_financial_trxn',
190 'civicrm_financial_item',
191 'civicrm_entity_financial_trxn',
192 'civicrm_contact',
193 'civicrm_membership',
194 'civicrm_membership_payment',
21dfd5f5 195 'civicrm_payment_processor',
b5935203 196 ));
197 CRM_Core_PseudoConstant::flush('taxRates');
198 }
199
00be9182 200 public function setUpContributionPage() {
b5935203 201 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
202 if (empty($this->_ids['price_set'])) {
203 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
204 $this->_ids['price_set'][] = $priceSet['id'];
205 }
206 $priceSetID = $this->_price = reset($this->_ids['price_set']);
481a74f4 207 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
b5935203 208
209 if (empty($this->_ids['price_field'])) {
210 $priceField = $this->callAPISuccess('price_field', 'create', array(
211 'price_set_id' => $priceSetID,
212 'label' => 'Goat Breed',
213 'html_type' => 'Radio',
214 ));
215 $this->_ids['price_field'] = array($priceField['id']);
216 }
217 if (empty($this->_ids['price_field_value'])) {
218 $this->callAPISuccess('price_field_value', 'create', array(
219 'price_set_id' => $priceSetID,
220 'price_field_id' => $priceField['id'],
221 'label' => 'Long Haired Goat',
222 'amount' => 100,
21dfd5f5 223 'financial_type_id' => $this->financialtypeID,
b5935203 224 ));
225 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
226 'price_set_id' => $priceSetID,
227 'price_field_id' => $priceField['id'],
228 'label' => 'Shoe-eating Goat',
229 'amount' => 300,
21dfd5f5 230 'financial_type_id' => $this->halfFinancialTypeId,
b5935203 231 ));
232 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
233 }
234 $this->_ids['contribution_page'] = $contributionPageResult['id'];
235 }
236
237 /**
eceb18cc 238 * Online and offline contrbution from above created contrbution page.
b5935203 239 */
00be9182 240 public function testCreateContributionOnline() {
b5935203 241 $this->setUpContributionPage();
242 $params = array(
243 'contact_id' => $this->_individualId,
244 'receive_date' => '20120511',
245 'total_amount' => 100.00,
246 'financial_type_id' => $this->financialtypeID,
247 'contribution_page_id' => $this->_ids['contribution_page'],
248 'payment_processor' => $this->_ids['paymentProcessID'],
249 'trxn_id' => 12345,
250 'invoice_id' => 67890,
251 'source' => 'SSF',
21dfd5f5 252 'contribution_status_id' => 1,
b5935203 253 );
31037a42
EM
254
255 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
b5935203 256 $this->_ids['contributionId'] = $contribution['id'];
a15773db
TM
257 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
258 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
259 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
260 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
261 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
ba4a1892 262 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
a15773db
TM
263 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
264 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
b5935203 265 $this->_checkFinancialRecords($contribution, 'online');
266 }
267
00be9182 268 public function testCreateContributionChainedLineItems() {
b5935203 269 $this->setUpContributionPage();
270 $params = array(
271 'contact_id' => $this->_individualId,
272 'receive_date' => '20120511',
273 'total_amount' => 400.00,
274 'financial_type_id' => $this->financialtypeID,
275 'trxn_id' => 12345,
276 'invoice_id' => 67890,
277 'source' => 'SSF',
278 'contribution_status_id' => 1,
279 'skipLineItem' => 1,
280 'api.line_item.create' => array(
281 array(
282 'price_field_id' => $this->_ids['price_field'],
283 'qty' => 1,
284 'line_total' => '100',
285 'unit_price' => '100',
286 'financial_type_id' => $this->financialtypeID,
287 ),
288 array(
289 'price_field_id' => $this->_ids['price_field'],
290 'qty' => 1,
291 'line_total' => '300',
292 'unit_price' => '300',
293 'financial_type_id' => $this->halfFinancialTypeId,
294 ),
295 ),
296 );
297
5c49fee0 298 $description = "Create Contribution with Nested Line Items.";
b5935203 299 $subfile = "CreateWithNestedLineItems";
6c6e6187 300 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
b5935203 301
6c6e6187 302 $lineItems = $this->callAPISuccess('line_item', 'get', array(
b5935203 303 'entity_id' => $contribution['id'],
304 'contribution_id' => $contribution['id'],
305 'entity_table' => 'civicrm_contribution',
306 'sequential' => 1,
307 ));
308 $this->assertEquals(2, $lineItems['count']);
309 }
310
00be9182 311 public function testCreateContributionPayLaterOnline() {
b5935203 312 $this->setUpContributionPage();
313 $params = array(
314 'contact_id' => $this->_individualId,
315 'receive_date' => '20120511',
316 'total_amount' => 100.00,
317 'financial_type_id' => $this->financialtypeID,
318 'contribution_page_id' => $this->_ids['contribution_page'],
319 'trxn_id' => 12345,
320 'is_pay_later' => 1,
321 'invoice_id' => 67890,
322 'source' => 'SSF',
323 'contribution_status_id' => 2,
324 );
325 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
a15773db
TM
326 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
327 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
328 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
329 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
330 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
ba4a1892 331 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
a15773db
TM
332 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
333 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
b5935203 334 $this->_checkFinancialRecords($contribution, 'payLater');
335 }
92915c55 336
00be9182 337 public function testCreateContributionPendingOnline() {
b5935203 338 $this->setUpContributionPage();
339 $params = array(
340 'contact_id' => $this->_individualId,
341 'receive_date' => '20120511',
342 'total_amount' => 100.00,
343 'financial_type_id' => $this->financialtypeID,
344 'contribution_page_id' => $this->_ids['contribution_page'],
345 'trxn_id' => 12345,
346 'invoice_id' => 67890,
347 'source' => 'SSF',
348 'contribution_status_id' => 2,
349 );
350
351 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
a15773db
TM
352 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
353 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
354 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
355 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
356 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
ba4a1892 357 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
a15773db
TM
358 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
359 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
b5935203 360 $this->_checkFinancialRecords($contribution, 'pending');
361 }
362
79d7553f 363 /**
a76b8bd8 364 * Update a contribution.
365 *
b5935203 366 * Function tests that line items, financial records are updated when contribution amount is changed
367 */
00be9182 368 public function testCreateUpdateContributionChangeTotal() {
b5935203 369 $this->setUpContributionPage();
370 $this->contributionParams = array(
371 'contact_id' => $this->_individualId,
372 'receive_date' => '20120511',
373 'total_amount' => 100.00,
92915c55 374 'financial_type_id' => $this->financialtypeID,
b5935203 375 'source' => 'SSF',
376 'contribution_status_id' => 1,
377 );
378 $contribution = $this->callAPISuccess('contribution', 'create', $this->contributionParams);
6c6e6187 379 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
b5935203 380 'entity_id' => $contribution['id'],
381 'entity_table' => 'civicrm_contribution',
382 'sequential' => 1,
383 'return' => 'line_total',
384 ));
385 $this->assertEquals('100.00', $lineItems);
386 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
387 $this->assertEquals('120.00', $trxnAmount);
388 $newParams = array(
389 'id' => $contribution['id'],
92915c55 390 'financial_type_id' => 1, // without tax rate i.e Donation
21dfd5f5 391 'total_amount' => '300',
b5935203 392 );
a76b8bd8 393 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
b5935203 394
6c6e6187 395 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
b5935203 396 'entity_id' => $contribution['id'],
397 'entity_table' => 'civicrm_contribution',
398 'sequential' => 1,
399 'return' => 'line_total',
400 ));
401
402 $this->assertEquals('300.00', $lineItems);
403 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
404 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
405 $this->assertEquals('300.00', $trxnAmount);
406 $this->assertEquals('300.00', $fitemAmount);
407 }
408
409 /**
100fef9d 410 * @param int $contId
b5935203 411 *
412 * @return null|string
413 */
00be9182 414 public function _getFinancialTrxnAmount($contId) {
b5935203 415 $query = "SELECT
416 SUM( ft.total_amount ) AS total
417 FROM civicrm_financial_trxn AS ft
418 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
419 WHERE ceft.entity_table = 'civicrm_contribution'
420 AND ceft.entity_id = {$contId}";
421 $result = CRM_Core_DAO::singleValueQuery($query);
422 return $result;
423 }
424
425 /**
100fef9d 426 * @param int $contId
b5935203 427 *
428 * @return null|string
429 */
00be9182 430 public function _getFinancialItemAmount($contId) {
b5935203 431 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
432 $query = "SELECT
433 SUM(amount)
434 FROM civicrm_financial_item
435 WHERE entity_table = 'civicrm_line_item'
436 AND entity_id = {$lineItem}";
437 $result = CRM_Core_DAO::singleValueQuery($query);
438 return $result;
439 }
440
441 /**
c490a46a 442 * @param array $params
b5935203 443 * @param $context
444 */
00be9182 445 public function _checkFinancialRecords($params, $context) {
b5935203 446 $entityParams = array(
447 'entity_id' => $params['id'],
448 'entity_table' => 'civicrm_contribution',
449 );
450 if ($context == 'pending') {
451 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
452 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
453 return;
454 }
455 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
456 $trxnParams = array(
457 'id' => $trxn['financial_trxn_id'],
458 );
459 if ($context != 'online' && $context != 'payLater') {
460 $compareParams = array(
461 'to_financial_account_id' => 6,
462 'total_amount' => 120,
463 'status_id' => 1,
464 );
465 }
466 if ($context == 'online') {
467 $compareParams = array(
468 'to_financial_account_id' => 12,
469 'total_amount' => 120,
470 'status_id' => 1,
471 );
472 }
473 elseif ($context == 'payLater') {
474 $compareParams = array(
475 'to_financial_account_id' => 7,
476 'total_amount' => 120,
477 'status_id' => 2,
478 );
479 }
480 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
481 $entityParams = array(
482 'financial_trxn_id' => $trxn['financial_trxn_id'],
483 'entity_table' => 'civicrm_financial_item',
484 );
485 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
486 $fitemParams = array(
487 'id' => $entityTrxn['entity_id'],
488 );
489 $compareParams = array(
490 'amount' => 100,
491 'status_id' => 1,
492 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
493 );
494 if ($context == 'payLater') {
495 $compareParams = array(
496 'amount' => 100,
497 'status_id' => 3,
498 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
499 );
500 }
501 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
502 }
503
504 /**
100fef9d 505 * @param int $financialTypeId
f0be539a 506 * @return int
b5935203 507 */
00be9182 508 public function _getFinancialAccountId($financialTypeId) {
b5935203 509 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
510
511 $searchParams = array(
92915c55
TO
512 'entity_table' => 'civicrm_financial_type',
513 'entity_id' => $financialTypeId,
b5935203 514 'account_relationship' => $accountRel,
515 );
516
517 $result = array();
481a74f4
TO
518 CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result);
519 return CRM_Utils_Array::value('financial_account_id', $result);
b5935203 520 }
521
f0be539a 522 /**
78ab0ca4 523 * Test deleting a contribution.
f0be539a 524 *
78ab0ca4 525 * (It is unclear why this is in this class - it seems like maybe it doesn't test anything not
526 * on the contribution test class & might be copy and paste....).
f0be539a 527 */
00be9182 528 public function testDeleteContribution() {
78ab0ca4 529 $contributionID = $this->contributionCreate(array(
530 'contact_id' => $this->_individualId,
531 'trxn_id' => 12389,
532 'financial_type_id' => $this->financialtypeID,
533 'invoice_id' => 'dfsdf',
534 ));
535 $this->callAPISuccess('contribution', 'delete', array('id' => $contributionID));
b5935203 536 }
96025800 537
b5935203 538}