3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
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. |
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. |
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 +--------------------------------------------------------------------+
29 * Class api_v3_TaxContributionPageTest
32 class api_v3_TaxContributionPageTest
extends CiviUnitTestCase
{
33 protected $_apiversion = 3;
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;
53 public function setUp() {
55 $this->_individualId
= $this->individualCreate();
56 $this->_orgId
= $this->organizationCreate(NULL);
58 $this->params
= array(
59 'title' => "Test Contribution Page" . substr(sha1(rand()), 0, 7),
60 'financial_type_id' => 1,
61 'payment_processor' => 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,
71 $this->_priceSetParams
= array(
72 'name' => 'tax_contribution' . substr(sha1(rand()), 0, 7),
73 'title' => 'contributiontax' . substr(sha1(rand()), 0, 7),
75 'help_pre' => "Where does your goat sleep",
76 'help_post' => "thank you for your time",
78 'financial_type_id' => 3,
79 'is_quick_config' => 0,
82 // Financial Account with 20% tax rate
83 $financialAccountSetparams = array(
85 'name' => 'vat full taxrate account' . substr(sha1(rand()), 0, 7),
86 'contact_id' => $this->_orgId
,
87 'financial_account_type_id' => 2,
95 $financialAccount = $this->callAPISuccess('financial_account', 'create', $financialAccountSetparams);
96 $this->financialAccountId
= $financialAccount['id'];
98 // Financial type having 'Sales Tax Account is' with liability financail account
99 $financialType = array(
100 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7),
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
,
112 $financialRelation = CRM_Financial_BAO_FinancialTypeAccount
::add($financialRelationParams);
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,
125 $halfFinancialAccount = CRM_Financial_BAO_FinancialAccount
::add($financialAccHalftax);
126 $this->halfFinancialAccId
= $halfFinancialAccount->id
;
127 $halfFinancialtypeHalftax = array(
128 'name' => 'grassvariety2' . substr(sha1(rand()), 0, 7),
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
,
142 $halfFinancialRelation = CRM_Financial_BAO_FinancialTypeAccount
::add($financialRelationHalftax);
144 // Enable component contribute setting
145 $contributeSetting = array(
147 'invoice_prefix' => 'INV_',
148 'credit_notes_prefix' => 'CN_',
150 'due_date_period' => 'days',
153 'tax_term' => 'Sales Tax',
154 'tax_display_settings' => 'Inclusive',
156 $setInvoiceSettings = Civi
::settings()->set('contribution_invoice_settings', $contributeSetting);
159 $paymentProceParams = array(
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,
166 'user_name' => 'dummy',
167 'url_site' => 'http://dummy.com',
168 'url_recur' => 'http://dummyrecur.com',
169 'class_name' => 'Payment_Dummy',
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);
180 public function tearDown() {
181 $this->quickCleanup(array(
182 'civicrm_contribution',
183 'civicrm_contribution_soft',
185 'civicrm_contribution_page',
186 'civicrm_participant',
187 'civicrm_participant_payment',
189 'civicrm_financial_trxn',
190 'civicrm_financial_item',
191 'civicrm_entity_financial_trxn',
193 'civicrm_membership',
194 'civicrm_membership_payment',
195 'civicrm_payment_processor',
197 CRM_Core_PseudoConstant
::flush('taxRates');
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'];
206 $priceSetID = $this->_price
= reset($this->_ids
['price_set']);
207 CRM_Price_BAO_PriceSet
::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID);
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',
215 $this->_ids
['price_field'] = array($priceField['id']);
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',
223 'financial_type_id' => $this->financialtypeID
,
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',
230 'financial_type_id' => $this->halfFinancialTypeId
,
232 $this->_ids
['price_field_value'] = array($priceFieldValue['id']);
234 $this->_ids
['contribution_page'] = $contributionPageResult['id'];
238 * Online and offline contrbution from above created contrbution page.
240 public function testCreateContributionOnline() {
241 $this->setUpContributionPage();
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'],
250 'invoice_id' => 67890,
252 'contribution_status_id' => 1,
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');
268 public function testCreateContributionChainedLineItems() {
269 $this->setUpContributionPage();
271 'contact_id' => $this->_individualId
,
272 'receive_date' => '20120511',
273 'total_amount' => 400.00,
274 'financial_type_id' => $this->financialtypeID
,
276 'invoice_id' => 67890,
278 'contribution_status_id' => 1,
280 'api.line_item.create' => array(
282 'price_field_id' => $this->_ids
['price_field'],
284 'line_total' => '100',
285 'unit_price' => '100',
286 'financial_type_id' => $this->financialtypeID
,
289 'price_field_id' => $this->_ids
['price_field'],
291 'line_total' => '300',
292 'unit_price' => '300',
293 'financial_type_id' => $this->halfFinancialTypeId
,
298 $description = "Create Contribution with Nested Line Items.";
299 $subfile = "CreateWithNestedLineItems";
300 $contribution = $this->callAPIAndDocument('contribution', 'create', $params, __FUNCTION__
, __FILE__
, $description, $subfile);
302 $lineItems = $this->callAPISuccess('line_item', 'get', array(
303 'entity_id' => $contribution['id'],
304 'contribution_id' => $contribution['id'],
305 'entity_table' => 'civicrm_contribution',
308 $this->assertEquals(2, $lineItems['count']);
311 public function testCreateContributionPayLaterOnline() {
312 $this->setUpContributionPage();
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'],
321 'invoice_id' => 67890,
323 'contribution_status_id' => 2,
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');
337 public function testCreateContributionPendingOnline() {
338 $this->setUpContributionPage();
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'],
346 'invoice_id' => 67890,
348 'contribution_status_id' => 2,
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');
364 * Updation of contrbution.
365 * Function tests that line items, financial records are updated when contribution amount is changed
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
,
375 'contribution_status_id' => 1,
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',
382 'return' => 'line_total',
384 $this->assertEquals('100.00', $lineItems);
385 $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']);
386 $this->assertEquals('120.00', $trxnAmount);
388 'id' => $contribution['id'],
389 'financial_type_id' => 1, // without tax rate i.e Donation
390 'total_amount' => '300',
392 $contribution = $this->callAPISuccess('contribution', 'update', $newParams);
394 $lineItems = $this->callAPISuccess('line_item', 'getvalue', array(
395 'entity_id' => $contribution['id'],
396 'entity_table' => 'civicrm_contribution',
398 'return' => 'line_total',
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);
411 * @return null|string
413 public function _getFinancialTrxnAmount($contId) {
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);
427 * @return null|string
429 public function _getFinancialItemAmount($contId) {
430 $lineItem = key(CRM_Price_BAO_LineItem
::getLineItems($contId, 'contribution'));
433 FROM civicrm_financial_item
434 WHERE entity_table = 'civicrm_line_item'
435 AND entity_id = {$lineItem}";
436 $result = CRM_Core_DAO
::singleValueQuery($query);
441 * @param array $params
444 public function _checkFinancialRecords($params, $context) {
445 $entityParams = array(
446 'entity_id' => $params['id'],
447 'entity_table' => 'civicrm_contribution',
449 if ($context == 'pending') {
450 $trxn = CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams);
451 $this->assertNull($trxn, 'No Trxn to be created until IPN callback');
454 $trxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
456 'id' => $trxn['financial_trxn_id'],
458 if ($context != 'online' && $context != 'payLater') {
459 $compareParams = array(
460 'to_financial_account_id' => 6,
461 'total_amount' => 120,
465 if ($context == 'online') {
466 $compareParams = array(
467 'to_financial_account_id' => 12,
468 'total_amount' => 120,
472 elseif ($context == 'payLater') {
473 $compareParams = array(
474 'to_financial_account_id' => 7,
475 'total_amount' => 120,
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',
484 $entityTrxn = current(CRM_Financial_BAO_FinancialItem
::retrieveEntityFinancialTrxn($entityParams));
485 $fitemParams = array(
486 'id' => $entityTrxn['entity_id'],
488 $compareParams = array(
491 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID
),
493 if ($context == 'payLater') {
494 $compareParams = array(
497 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID
),
500 $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams);
504 * @param int $financialTypeId
507 public function _getFinancialAccountId($financialTypeId) {
508 $accountRel = key(CRM_Core_PseudoConstant
::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' "));
510 $searchParams = array(
511 'entity_table' => 'civicrm_financial_type',
512 'entity_id' => $financialTypeId,
513 'account_relationship' => $accountRel,
517 CRM_Financial_BAO_FinancialTypeAccount
::retrieve($searchParams, $result);
518 return CRM_Utils_Array
::value('financial_account_id', $result);
522 * Test deleting a contribution.
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....).
527 public function testDeleteContribution() {
528 $contributionID = $this->contributionCreate(array(
529 'contact_id' => $this->_individualId
,
531 'financial_type_id' => $this->financialtypeID
,
532 'invoice_id' => 'dfsdf',
534 $this->callAPISuccess('contribution', 'delete', array('id' => $contributionID));