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