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