Merge pull request #14897 from mattwire/membership_payment2
[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 = [];
39 protected $_paymentProcessorType;
40 protected $payParams = [];
41 protected $paymentProceParams = [];
42 protected $settingValue = [];
43 protected $setInvoiceSettings;
44 protected $_ids = [];
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 = [
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 = [
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 = [
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 = [
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 = [
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 = [
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 = [
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 = [
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 = [
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.ex.php';
166 $this->assertAPISuccess($result);
167 }
168
169 /**
170 * Cleanup after function.
171 */
172 public function tearDown() {
173 $this->quickCleanUpFinancialEntities();
174 parent::tearDown();
175 }
176
177 public function setUpContributionPage() {
178 $contributionPageResult = $this->callAPISuccess($this->_entity, 'create', $this->params);
179 if (empty($this->_ids['price_set'])) {
180 $priceSet = $this->callAPISuccess('price_set', 'create', $this->_priceSetParams);
181 $this->_ids['price_set'][] = $priceSet['id'];
182 }
183 $priceSetID = $this->_price = reset($this->_ids['price_set']);
184 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
185
186 if (empty($this->_ids['price_field'])) {
187 $priceField = $this->callAPISuccess('price_field', 'create', [
188 'price_set_id' => $priceSetID,
189 'label' => 'Goat Breed',
190 'html_type' => 'Radio',
191 ]);
192 $this->_ids['price_field'] = [$priceField['id']];
193 }
194 if (empty($this->_ids['price_field_value'])) {
195 $this->callAPISuccess('price_field_value', 'create', [
196 'price_set_id' => $priceSetID,
197 'price_field_id' => $priceField['id'],
198 'label' => 'Long Haired Goat',
199 'amount' => 100,
200 'financial_type_id' => $this->financialtypeID,
201 ]);
202 $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [
203 'price_set_id' => $priceSetID,
204 'price_field_id' => $priceField['id'],
205 'label' => 'Shoe-eating Goat',
206 'amount' => 300,
207 'financial_type_id' => $this->halfFinancialTypeId,
208 ]);
209 $this->_ids['price_field_value'] = [$priceFieldValue['id']];
210 }
211 $this->_ids['contribution_page'] = $contributionPageResult['id'];
212 }
213
214 /**
215 * Online and offline contrbution from above created contribution page.
216 *
217 * @param string $thousandSeparator
218 * punctuation used to refer to thousands.
219 *
220 * @dataProvider getThousandSeparators
221 */
222 public function testCreateContributionOnline($thousandSeparator) {
223 $this->setCurrencySeparators($thousandSeparator);
224 $this->setUpContributionPage();
225 $params = [
226 'contact_id' => $this->_individualId,
227 'receive_date' => '20120511',
228 'total_amount' => $this->formatMoneyInput(100.00),
229 'financial_type_id' => $this->financialtypeID,
230 'contribution_page_id' => $this->_ids['contribution_page'],
231 'payment_processor' => $this->_ids['paymentProcessID'],
232 'trxn_id' => 12345,
233 'invoice_id' => 67890,
234 'source' => 'SSF',
235 'contribution_status_id' => 1,
236 ];
237
238 $contribution = $this->callAPISuccess('contribution', 'create', $params);
239 $this->_ids['contributionId'] = $contribution['id'];
240 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
241 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
242 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
243 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
244 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
245 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
246 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
247 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
248 $this->_checkFinancialRecords($contribution, 'online');
249 }
250
251 /**
252 * Create contribution with chained line items.
253 *
254 * @param string $thousandSeparator
255 * punctuation used to refer to thousands.
256 *
257 * @dataProvider getThousandSeparators
258 */
259 public function testCreateContributionChainedLineItems($thousandSeparator) {
260 $this->setCurrencySeparators($thousandSeparator);
261 $this->setUpContributionPage();
262 $params = [
263 'contact_id' => $this->_individualId,
264 'receive_date' => '20120511',
265 'total_amount' => 400.00,
266 'financial_type_id' => $this->financialtypeID,
267 'trxn_id' => 12345,
268 'invoice_id' => 67890,
269 'source' => 'SSF',
270 'contribution_status_id' => 1,
271 'skipLineItem' => 1,
272 'api.line_item.create' => [
273 [
274 'price_field_id' => $this->_ids['price_field'],
275 'qty' => 1,
276 'line_total' => '100',
277 'unit_price' => '100',
278 'financial_type_id' => $this->financialtypeID,
279 ],
280 [
281 'price_field_id' => $this->_ids['price_field'],
282 'qty' => 1,
283 'line_total' => '300',
284 'unit_price' => '300',
285 'financial_type_id' => $this->halfFinancialTypeId,
286 ],
287 ],
288 ];
289
290 $contribution = $this->callAPISuccess('contribution', 'create', $params);
291
292 $lineItems = $this->callAPISuccess('line_item', 'get', [
293 'entity_id' => $contribution['id'],
294 'contribution_id' => $contribution['id'],
295 'entity_table' => 'civicrm_contribution',
296 'sequential' => 1,
297 ]);
298 $this->assertEquals(2, $lineItems['count']);
299 }
300
301 public function testCreateContributionPayLaterOnline() {
302 $this->setUpContributionPage();
303 $params = [
304 'contact_id' => $this->_individualId,
305 'receive_date' => '20120511',
306 'total_amount' => 100.00,
307 'financial_type_id' => $this->financialtypeID,
308 'contribution_page_id' => $this->_ids['contribution_page'],
309 'trxn_id' => 12345,
310 'is_pay_later' => 1,
311 'invoice_id' => 67890,
312 'source' => 'SSF',
313 'contribution_status_id' => 2,
314 ];
315 $contribution = $this->callAPISuccess('contribution', 'create', $params, __FUNCTION__, __FILE__);
316 $this->assertEquals($contribution['values'][$contribution['id']]['contact_id'], $this->_individualId);
317 $this->assertEquals($contribution['values'][$contribution['id']]['total_amount'], 120.00);
318 $this->assertEquals($contribution['values'][$contribution['id']]['financial_type_id'], $this->financialtypeID);
319 $this->assertEquals($contribution['values'][$contribution['id']]['trxn_id'], 12345);
320 $this->assertEquals($contribution['values'][$contribution['id']]['invoice_id'], 67890);
321 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
322 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
323 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
324 $this->_checkFinancialRecords($contribution, 'payLater');
325 }
326
327 /**
328 * Test online pending contributions.
329 *
330 * @param string $thousandSeparator
331 * punctuation used to refer to thousands.
332 *
333 * @dataProvider getThousandSeparators
334 */
335 public function testCreateContributionPendingOnline($thousandSeparator) {
336 $this->setCurrencySeparators($thousandSeparator);
337 $this->setUpContributionPage();
338 $params = [
339 'contact_id' => $this->_individualId,
340 'receive_date' => '20120511',
341 'total_amount' => $this->formatMoneyInput(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->callAPISuccess('contribution', 'create', $params, __FUNCTION__, __FILE__);
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);
356 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
357 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
358 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
359 $this->_checkFinancialRecords($contribution, 'pending');
360 $this->setCurrencySeparators($thousandSeparator);
361 }
362
363 /**
364 * Update a contribution.
365 *
366 * Function tests that line items, financial records are updated when contribution amount is changed
367 */
368 public function testCreateUpdateContributionChangeTotal() {
369 $this->setUpContributionPage();
370 $this->contributionParams = [
371 'contact_id' => $this->_individualId,
372 'receive_date' => '20120511',
373 'total_amount' => 100.00,
374 'financial_type_id' => $this->financialtypeID,
375 'source' => 'SSF',
376 'contribution_status_id' => 1,
377 ];
378 $contribution = $this->callAPISuccess('contribution', 'create', $this->contributionParams);
379 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
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 = [
389 'id' => $contribution['id'],
390 // without tax rate i.e Donation
391 'financial_type_id' => 1,
392 'total_amount' => '300',
393 ];
394 $contribution = $this->callAPISuccess('contribution', 'create', $newParams);
395
396 $lineItems = $this->callAPISuccess('line_item', 'getvalue', [
397 'entity_id' => $contribution['id'],
398 'entity_table' => 'civicrm_contribution',
399 'sequential' => 1,
400 'return' => 'line_total',
401 ]);
402
403 $this->assertEquals('300.00', $lineItems);
404 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
405 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
406 $this->assertEquals('300.00', $trxnAmount);
407 $this->assertEquals('300.00', $fitemAmount);
408 }
409
410 /**
411 * @param int $contId
412 *
413 * @return null|string
414 */
415 public function _getFinancialTrxnAmount($contId) {
416 $query = "SELECT
417 SUM( ft.total_amount ) AS total
418 FROM civicrm_financial_trxn AS ft
419 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
420 WHERE ceft.entity_table = 'civicrm_contribution'
421 AND ceft.entity_id = {$contId}";
422 $result = CRM_Core_DAO::singleValueQuery($query);
423 return $result;
424 }
425
426 /**
427 * @param int $contId
428 *
429 * @return null|string
430 */
431 public function _getFinancialItemAmount($contId) {
432 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
433 $query = "SELECT
434 SUM(amount)
435 FROM civicrm_financial_item
436 WHERE entity_table = 'civicrm_line_item'
437 AND entity_id = {$lineItem}";
438 $result = CRM_Core_DAO::singleValueQuery($query);
439 return $result;
440 }
441
442 /**
443 * @param array $params
444 * @param $context
445 */
446 public function _checkFinancialRecords($params, $context) {
447 $entityParams = [
448 'entity_id' => $params['id'],
449 'entity_table' => 'civicrm_contribution',
450 ];
451 if ($context == 'pending') {
452 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
453 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
454 return;
455 }
456 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
457 $trxnParams = [
458 'id' => $trxn['financial_trxn_id'],
459 ];
460 if ($context != 'online' && $context != 'payLater') {
461 $compareParams = [
462 'to_financial_account_id' => 6,
463 'total_amount' => 120,
464 'status_id' => 1,
465 ];
466 }
467 if ($context == 'online') {
468 $compareParams = [
469 'to_financial_account_id' => 12,
470 'total_amount' => 120,
471 'status_id' => 1,
472 ];
473 }
474 elseif ($context == 'payLater') {
475 $compareParams = [
476 'to_financial_account_id' => 7,
477 'total_amount' => 120,
478 'status_id' => 2,
479 ];
480 }
481 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
482 $entityParams = [
483 'financial_trxn_id' => $trxn['financial_trxn_id'],
484 'entity_table' => 'civicrm_financial_item',
485 ];
486 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
487 $fitemParams = [
488 'id' => $entityTrxn['entity_id'],
489 ];
490 $compareParams = [
491 'amount' => 100,
492 'status_id' => 1,
493 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
494 ];
495 if ($context == 'payLater') {
496 $compareParams = [
497 'amount' => 100,
498 'status_id' => 3,
499 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
500 ];
501 }
502 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
503 }
504
505 /**
506 * @param int $financialTypeId
507 * @return int
508 */
509 public function _getFinancialAccountId($financialTypeId) {
510 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
511
512 $searchParams = [
513 'entity_table' => 'civicrm_financial_type',
514 'entity_id' => $financialTypeId,
515 'account_relationship' => $accountRel,
516 ];
517
518 $result = [];
519 CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result);
520 return CRM_Utils_Array::value('financial_account_id', $result);
521 }
522
523 /**
524 * Test deleting a contribution.
525 *
526 * (It is unclear why this is in this class - it seems like maybe it doesn't test anything not
527 * on the contribution test class & might be copy and paste....).
528 */
529 public function testDeleteContribution() {
530 $contributionID = $this->contributionCreate([
531 'contact_id' => $this->_individualId,
532 'trxn_id' => 12389,
533 'financial_type_id' => $this->financialtypeID,
534 'invoice_id' => 'dfsdf',
535 ]);
536 $this->callAPISuccess('contribution', 'delete', ['id' => $contributionID]);
537 }
538
539 }