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