Merge pull request #7895 from cividesk/CRM-18130-master
[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-2015 |
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 $contributeSetting = array(
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 );
156 $setInvoiceSettings = Civi::settings()->set('contribution_invoice_settings', $contributeSetting);
157
158 // Payment Processor
159 $paymentProceParams = array(
160 'domain_id' => 1,
161 'name' => 'dummy' . substr(sha1(rand()), 0, 7),
162 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'),
163 'financial_account_id' => 12,
164 'is_active' => 1,
165 'is_default' => 1,
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,
172 'payment_type' => 1,
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
180 public function tearDown() {
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',
195 'civicrm_payment_processor',
196 ));
197 CRM_Core_PseudoConstant::flush('taxRates');
198 }
199
200 public function setUpContributionPage() {
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']);
207 CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
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,
223 'financial_type_id' => $this->financialtypeID,
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,
230 'financial_type_id' => $this->halfFinancialTypeId,
231 ));
232 $this->_ids['price_field_value'] = array($priceFieldValue['id']);
233 }
234 $this->_ids['contribution_page'] = $contributionPageResult['id'];
235 }
236
237 /**
238 * Online and offline contrbution from above created contrbution page.
239 */
240 public function testCreateContributionOnline() {
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',
252 'contribution_status_id' => 1,
253 );
254
255 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__);
256 $this->_ids['contributionId'] = $contribution['id'];
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);
262 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
263 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
264 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1);
265 $this->_checkFinancialRecords($contribution, 'online');
266 }
267
268 public function testCreateContributionChainedLineItems() {
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
298 $description = "Create Contribution with Nested Line Items.";
299 $subfile = "CreateWithNestedLineItems";
300 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__, __FILE__, $description, $subfile);
301
302 $lineItems = $this->callAPISuccess('line_item', 'get', array(
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
311 public function testCreateContributionPayLaterOnline() {
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__);
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);
331 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
332 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
333 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
334 $this->_checkFinancialRecords($contribution, 'payLater');
335 }
336
337 public function testCreateContributionPendingOnline() {
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__);
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);
357 $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF');
358 $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20);
359 $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2);
360 $this->_checkFinancialRecords($contribution, 'pending');
361 }
362
363 /**
364 * Updation of contrbution.
365 * Function tests that line items, financial records are updated when contribution amount is changed
366 */
367 public function testCreateUpdateContributionChangeTotal() {
368 $this->setUpContributionPage();
369 $this->contributionParams = array(
370 'contact_id' => $this->_individualId,
371 'receive_date' => '20120511',
372 'total_amount' => 100.00,
373 'financial_type_id' => $this->financialtypeID,
374 'source' => 'SSF',
375 'contribution_status_id' => 1,
376 );
377 $contribution = $this->callAPISuccess('contribution', 'create', $this->contributionParams);
378 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
379 'entity_id' => $contribution['id'],
380 'entity_table' => 'civicrm_contribution',
381 'sequential' => 1,
382 'return' => 'line_total',
383 ));
384 $this->assertEquals('100.00', $lineItems);
385 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
386 $this->assertEquals('120.00', $trxnAmount);
387 $newParams = array(
388 'id' => $contribution['id'],
389 'financial_type_id' => 1, // without tax rate i.e Donation
390 'total_amount' => '300',
391 );
392 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
393
394 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
395 'entity_id' => $contribution['id'],
396 'entity_table' => 'civicrm_contribution',
397 'sequential' => 1,
398 'return' => 'line_total',
399 ));
400
401 $this->assertEquals('300.00', $lineItems);
402 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
403 $fitemAmount = $this->_getFinancialItemAmount($contribution['id']);
404 $this->assertEquals('300.00', $trxnAmount);
405 $this->assertEquals('300.00', $fitemAmount);
406 }
407
408 /**
409 * @param int $contId
410 *
411 * @return null|string
412 */
413 public function _getFinancialTrxnAmount($contId) {
414 $query = "SELECT
415 SUM( ft.total_amount ) AS total
416 FROM civicrm_financial_trxn AS ft
417 LEFT JOIN civicrm_entity_financial_trxn AS ceft ON ft.id = ceft.financial_trxn_id
418 WHERE ceft.entity_table = 'civicrm_contribution'
419 AND ceft.entity_id = {$contId}";
420 $result = CRM_Core_DAO::singleValueQuery($query);
421 return $result;
422 }
423
424 /**
425 * @param int $contId
426 *
427 * @return null|string
428 */
429 public function _getFinancialItemAmount($contId) {
430 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
431 $query = "SELECT
432 SUM(amount)
433 FROM civicrm_financial_item
434 WHERE entity_table = 'civicrm_line_item'
435 AND entity_id = {$lineItem}";
436 $result = CRM_Core_DAO::singleValueQuery($query);
437 return $result;
438 }
439
440 /**
441 * @param array $params
442 * @param $context
443 */
444 public function _checkFinancialRecords($params, $context) {
445 $entityParams = array(
446 'entity_id' => $params['id'],
447 'entity_table' => 'civicrm_contribution',
448 );
449 if ($context == 'pending') {
450 $trxn = CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams);
451 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
452 return;
453 }
454 $trxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
455 $trxnParams = array(
456 'id' => $trxn['financial_trxn_id'],
457 );
458 if ($context != 'online' && $context != 'payLater') {
459 $compareParams = array(
460 'to_financial_account_id' => 6,
461 'total_amount' => 120,
462 'status_id' => 1,
463 );
464 }
465 if ($context == 'online') {
466 $compareParams = array(
467 'to_financial_account_id' => 12,
468 'total_amount' => 120,
469 'status_id' => 1,
470 );
471 }
472 elseif ($context == 'payLater') {
473 $compareParams = array(
474 'to_financial_account_id' => 7,
475 'total_amount' => 120,
476 'status_id' => 2,
477 );
478 }
479 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams);
480 $entityParams = array(
481 'financial_trxn_id' => $trxn['financial_trxn_id'],
482 'entity_table' => 'civicrm_financial_item',
483 );
484 $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams));
485 $fitemParams = array(
486 'id' => $entityTrxn['entity_id'],
487 );
488 $compareParams = array(
489 'amount' => 100,
490 'status_id' => 1,
491 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
492 );
493 if ($context == 'payLater') {
494 $compareParams = array(
495 'amount' => 100,
496 'status_id' => 3,
497 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID),
498 );
499 }
500 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
501 }
502
503 /**
504 * @param int $financialTypeId
505 * @return int
506 */
507 public function _getFinancialAccountId($financialTypeId) {
508 $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
509
510 $searchParams = array(
511 'entity_table' => 'civicrm_financial_type',
512 'entity_id' => $financialTypeId,
513 'account_relationship' => $accountRel,
514 );
515
516 $result = array();
517 CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result);
518 return CRM_Utils_Array::value('financial_account_id', $result);
519 }
520
521 /**
522 * Test deleting a contribution.
523 *
524 * (It is unclear why this is in this class - it seems like maybe it doesn't test anything not
525 * on the contribution test class & might be copy and paste....).
526 */
527 public function testDeleteContribution() {
528 $contributionID = $this->contributionCreate(array(
529 'contact_id' => $this->_individualId,
530 'trxn_id' => 12389,
531 'financial_type_id' => $this->financialtypeID,
532 'invoice_id' => 'dfsdf',
533 ));
534 $this->callAPISuccess('contribution', 'delete', array('id' => $contributionID));
535 }
536
537 }