Merge pull request #13374 from cividesk/dev-627
[civicrm-core.git] / tests / phpunit / api / v3 / TaxContributionPageTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 +--------------------------------------------------------------------+
26 */
27
28 /**
29 * Class api_v3_TaxContributionPageTest
30 * @group headless
31 */
32 class 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(
59 'title' => "Test Contribution Page" . substr(sha1(rand()), 0, 7),
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,
68 'is_billing_required' => TRUE,
69 );
70
71 $this->_priceSetParams = array(
72 'name' => 'tax_contribution' . substr(sha1(rand()), 0, 7),
73 'title' => 'contributiontax' . substr(sha1(rand()), 0, 7),
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,
80 'is_reserved' => 0,
81 );
82 // Financial Account with 20% tax rate
83 $financialAccountSetparams = array(
84 #[domain_id] =>
85 'name' => 'vat full taxrate account' . substr(sha1(rand()), 0, 7),
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(
100 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7),
101 'is_reserved' => 0,
102 'is_active' => 1,
103 );
104 $priceField = $this->callAPISuccess('financial_type', 'create', $financialType);
105 $this->financialtypeID = $priceField['id'];
106 $financialRelationParams = array(
107 'entity_table' => 'civicrm_financial_type',
108 'entity_id' => $this->financialtypeID,
109 'account_relationship' => 10,
110 'financial_account_id' => $this->financialAccountId,
111 );
112 $financialRelation = CRM_Financial_BAO_FinancialTypeAccount::add($financialRelationParams);
113
114 // Financial type with 5% tax rate
115 $financialAccHalftax = array(
116 'name' => 'vat half taxrate account' . substr(sha1(rand()), 0, 7),
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,
123 'is_default' => 0,
124 );
125 $halfFinancialAccount = CRM_Financial_BAO_FinancialAccount::add($financialAccHalftax);
126 $this->halfFinancialAccId = $halfFinancialAccount->id;
127 $halfFinancialtypeHalftax = array(
128 'name' => 'grassvariety2' . substr(sha1(rand()), 0, 7),
129 'is_reserved' => 0,
130 'is_active' => 1,
131 );
132
133 $halfFinancialType = CRM_Financial_BAO_FinancialType::add($halfFinancialtypeHalftax);
134 $this->halfFinancialTypeId = $halfFinancialType->id;
135 $financialRelationHalftax = array(
136 'entity_table' => 'civicrm_financial_type',
137 'entity_id' => $this->halfFinancialTypeId,
138 'account_relationship' => 10,
139 'financial_account_id' => $this->halfFinancialAccId,
140 );
141
142 $halfFinancialRelation = CRM_Financial_BAO_FinancialTypeAccount::add($financialRelationHalftax);
143
144 // Enable component contribute setting
145 $setInvoiceSettings = $this->enableTaxAndInvoicing();
146
147 // Payment Processor
148 $paymentProceParams = array(
149 'domain_id' => 1,
150 'name' => 'dummy' . substr(sha1(rand()), 0, 7),
151 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
152 'financial_account_id' => 12,
153 'is_active' => 1,
154 'is_default' => 1,
155 'user_name' => 'dummy',
156 'url_site' => 'http://dummy.com',
157 'url_recur' => 'http://dummyrecur.com',
158 'class_name' => 'Payment_Dummy',
159 'billing_mode' => 1,
160 'is_recur' => 1,
161 'payment_type' => 1,
162 );
163 $result = $this->callAPISuccess('payment_processor', 'create', $paymentProceParams);
164 $this->_ids['paymentProcessID'] = $result['id'];
165 require_once 'api/v3/examples/PaymentProcessor/Create.php';
166 $this->assertAPISuccess($result);
167 }
168
169 public function tearDown() {
170 $this->quickCleanUpFinancialEntities();
171 }
172
173 public function setUpContributionPage() {
174 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
175 if (empty($this->_ids['price_set'])) {
176 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
177 $this->_ids['price_set'][] = $priceSet['id'];
178 }
179 $priceSetID = $this->_price = reset($this->_ids['price_set']);
180 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
181
182 if (empty($this->_ids['price_field'])) {
183 $priceField = $this->callAPISuccess('price_field', 'create', array(
184 'price_set_id' => $priceSetID,
185 'label' => 'Goat Breed',
186 'html_type' => 'Radio',
187 ));
188 $this->_ids['price_field'] = array($priceField['id']);
189 }
190 if (empty($this->_ids['price_field_value'])) {
191 $this->callAPISuccess('price_field_value', 'create', array(
192 'price_set_id' => $priceSetID,
193 'price_field_id' => $priceField['id'],
194 'label' => 'Long Haired Goat',
195 'amount' => 100,
196 'financial_type_id' => $this->financialtypeID,
197 ));
198 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', array(
199 'price_set_id' => $priceSetID,
200 'price_field_id' => $priceField['id'],
201 'label' => 'Shoe-eating Goat',
202 'amount' => 300,
203 'financial_type_id' => $this->halfFinancialTypeId,
204 ));
205 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
206 }
207 $this->_ids['contribution_page'] = $contributionPageResult['id'];
208 }
209
210 /**
211 * Online and offline contrbution from above created contribution page.
212 *
213 * @param string $thousandSeparator
214 * punctuation used to refer to thousands.
215 *
216 * @dataProvider getThousandSeparators
217 */
218 public function testCreateContributionOnline($thousandSeparator) {
219 $this->setCurrencySeparators($thousandSeparator);
220 $this->setUpContributionPage();
221 $params = array(
222 'contact_id' => $this->_individualId,
223 'receive_date' => '20120511',
224 'total_amount' => $this->formatMoneyInput(100.00),
225 'financial_type_id' => $this->financialtypeID,
226 'contribution_page_id' => $this->_ids['contribution_page'],
227 'payment_processor' => $this->_ids['paymentProcessID'],
228 'trxn_id' => 12345,
229 'invoice_id' => 67890,
230 'source' => 'SSF',
231 'contribution_status_id' => 1,
232 );
233
234 $contribution = $this->callAPISuccess('contribution', 'create', $params);
235 $this->_ids['contributionId'] = $contribution['id'];
236 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
237 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
238 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
239 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
240 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
241 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
242 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
243 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
244 $this->_checkFinancialRecords($contribution, 'online');
245 }
246
247 /**
248 * Create contribution with chained line items.
249 *
250 * @param string $thousandSeparator
251 * punctuation used to refer to thousands.
252 *
253 * @dataProvider getThousandSeparators
254 */
255 public function testCreateContributionChainedLineItems($thousandSeparator) {
256 $this->setCurrencySeparators($thousandSeparator);
257 $this->setUpContributionPage();
258 $params = array(
259 'contact_id' => $this->_individualId,
260 'receive_date' => '20120511',
261 'total_amount' => 400.00,
262 'financial_type_id' => $this->financialtypeID,
263 'trxn_id' => 12345,
264 'invoice_id' => 67890,
265 'source' => 'SSF',
266 'contribution_status_id' => 1,
267 'skipLineItem' => 1,
268 'api.line_item.create' => array(
269 array(
270 'price_field_id' => $this->_ids['price_field'],
271 'qty' => 1,
272 'line_total' => '100',
273 'unit_price' => '100',
274 'financial_type_id' => $this->financialtypeID,
275 ),
276 array(
277 'price_field_id' => $this->_ids['price_field'],
278 'qty' => 1,
279 'line_total' => '300',
280 'unit_price' => '300',
281 'financial_type_id' => $this->halfFinancialTypeId,
282 ),
283 ),
284 );
285
286 $contribution = $this->callAPISuccess('contribution', 'create', $params);
287
288 $lineItems = $this->callAPISuccess('line_item', 'get', array(
289 'entity_id' => $contribution['id'],
290 'contribution_id' => $contribution['id'],
291 'entity_table' => 'civicrm_contribution',
292 'sequential' => 1,
293 ));
294 $this->assertEquals(2, $lineItems['count']);
295 }
296
297 public function testCreateContributionPayLaterOnline() {
298 $this->setUpContributionPage();
299 $params = array(
300 'contact_id' => $this->_individualId,
301 'receive_date' => '20120511',
302 'total_amount' => 100.00,
303 'financial_type_id' => $this->financialtypeID,
304 'contribution_page_id' => $this->_ids['contribution_page'],
305 'trxn_id' => 12345,
306 'is_pay_later' => 1,
307 'invoice_id' => 67890,
308 'source' => 'SSF',
309 'contribution_status_id' => 2,
310 );
311 $contribution = $this->callAPISuccess('contribution', 'create', $params, __FUNCTION__, __FILE__);
312 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
313 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
314 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
315 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
316 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
317 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
318 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
319 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
320 $this->_checkFinancialRecords($contribution, 'payLater');
321 }
322
323 /**
324 * Test online pending contributions.
325 *
326 * @param string $thousandSeparator
327 * punctuation used to refer to thousands.
328 *
329 * @dataProvider getThousandSeparators
330 */
331 public function testCreateContributionPendingOnline($thousandSeparator) {
332 $this->setCurrencySeparators($thousandSeparator);
333 $this->setUpContributionPage();
334 $params = array(
335 'contact_id' => $this->_individualId,
336 'receive_date' => '20120511',
337 'total_amount' => $this->formatMoneyInput(100.00),
338 'financial_type_id' => $this->financialtypeID,
339 'contribution_page_id' => $this->_ids['contribution_page'],
340 'trxn_id' => 12345,
341 'invoice_id' => 67890,
342 'source' => 'SSF',
343 'contribution_status_id' => 2,
344 );
345
346 $contribution = $this->callAPISuccess('contribution', 'create', $params, __FUNCTION__, __FILE__);
347 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
348 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
349 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
350 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
351 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
352 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
353 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
354 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
355 $this->_checkFinancialRecords($contribution, 'pending');
356 $this->setCurrencySeparators($thousandSeparator);
357 }
358
359 /**
360 * Update a contribution.
361 *
362 * Function tests that line items, financial records are updated when contribution amount is changed
363 */
364 public function testCreateUpdateContributionChangeTotal() {
365 $this->setUpContributionPage();
366 $this->contributionParams = array(
367 'contact_id' => $this->_individualId,
368 'receive_date' => '20120511',
369 'total_amount' => 100.00,
370 'financial_type_id' => $this->financialtypeID,
371 'source' => 'SSF',
372 'contribution_status_id' => 1,
373 );
374 $contribution = $this->callAPISuccess('contribution', 'create', $this->contributionParams);
375 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
376 'entity_id' => $contribution['id'],
377 'entity_table' => 'civicrm_contribution',
378 'sequential' => 1,
379 'return' => 'line_total',
380 ));
381 $this->assertEquals('100.00', $lineItems);
382 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
383 $this->assertEquals('120.00', $trxnAmount);
384 $newParams = array(
385 'id' => $contribution['id'],
386 // without tax rate i.e Donation
387 'financial_type_id' => 1,
388 'total_amount' => '300',
389 );
390 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
391
392 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
393 'entity_id' => $contribution['id'],
394 'entity_table' => 'civicrm_contribution',
395 'sequential' => 1,
396 'return' => 'line_total',
397 ));
398
399 $this->assertEquals('300.00', $lineItems);
400 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
401 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
402 $this->assertEquals('300.00', $trxnAmount);
403 $this->assertEquals('300.00', $fitemAmount);
404 }
405
406 /**
407 * @param int $contId
408 *
409 * @return null|string
410 */
411 public function _getFinancialTrxnAmount($contId) {
412 $query = "SELECT
413 SUM( ft.total_amount ) AS total
414 FROM civicrm_financial_trxn AS ft
415 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
416 WHERE ceft.entity_table = 'civicrm_contribution'
417 AND ceft.entity_id = {$contId}";
418 $result = CRM_Core_DAO::singleValueQuery($query);
419 return $result;
420 }
421
422 /**
423 * @param int $contId
424 *
425 * @return null|string
426 */
427 public function _getFinancialItemAmount($contId) {
428 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
429 $query = "SELECT
430 SUM(amount)
431 FROM civicrm_financial_item
432 WHERE entity_table = 'civicrm_line_item'
433 AND entity_id = {$lineItem}";
434 $result = CRM_Core_DAO::singleValueQuery($query);
435 return $result;
436 }
437
438 /**
439 * @param array $params
440 * @param $context
441 */
442 public function _checkFinancialRecords($params, $context) {
443 $entityParams = array(
444 'entity_id' => $params['id'],
445 'entity_table' => 'civicrm_contribution',
446 );
447 if ($context == 'pending') {
448 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
449 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
450 return;
451 }
452 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
453 $trxnParams = array(
454 'id' => $trxn['financial_trxn_id'],
455 );
456 if ($context != 'online' && $context != 'payLater') {
457 $compareParams = array(
458 'to_financial_account_id' => 6,
459 'total_amount' => 120,
460 'status_id' => 1,
461 );
462 }
463 if ($context == 'online') {
464 $compareParams = array(
465 'to_financial_account_id' => 12,
466 'total_amount' => 120,
467 'status_id' => 1,
468 );
469 }
470 elseif ($context == 'payLater') {
471 $compareParams = array(
472 'to_financial_account_id' => 7,
473 'total_amount' => 120,
474 'status_id' => 2,
475 );
476 }
477 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
478 $entityParams = array(
479 'financial_trxn_id' => $trxn['financial_trxn_id'],
480 'entity_table' => 'civicrm_financial_item',
481 );
482 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
483 $fitemParams = array(
484 'id' => $entityTrxn['entity_id'],
485 );
486 $compareParams = array(
487 'amount' => 100,
488 'status_id' => 1,
489 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
490 );
491 if ($context == 'payLater') {
492 $compareParams = array(
493 'amount' => 100,
494 'status_id' => 3,
495 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
496 );
497 }
498 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
499 }
500
501 /**
502 * @param int $financialTypeId
503 * @return int
504 */
505 public function _getFinancialAccountId($financialTypeId) {
506 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
507
508 $searchParams = array(
509 'entity_table' => 'civicrm_financial_type',
510 'entity_id' => $financialTypeId,
511 'account_relationship' => $accountRel,
512 );
513
514 $result = array();
515 CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result);
516 return CRM_Utils_Array::value('financial_account_id', $result);
517 }
518
519 /**
520 * Test deleting a contribution.
521 *
522 * (It is unclear why this is in this class - it seems like maybe it doesn't test anything not
523 * on the contribution test class & might be copy and paste....).
524 */
525 public function testDeleteContribution() {
526 $contributionID = $this->contributionCreate(array(
527 'contact_id' => $this->_individualId,
528 'trxn_id' => 12389,
529 'financial_type_id' => $this->financialtypeID,
530 'invoice_id' => 'dfsdf',
531 ));
532 $this->callAPISuccess('contribution', 'delete', array('id' => $contributionID));
533 }
534
535 }