Commit | Line | Data |
---|---|---|
b5935203 | 1 | <?php |
b5935203 | 2 | /* |
3 | +--------------------------------------------------------------------+ | |
2fe49090 | 4 | | CiviCRM version 5 | |
b5935203 | 5 | +--------------------------------------------------------------------+ |
6b83d5bd | 6 | | Copyright CiviCRM LLC (c) 2004-2019 | |
b5935203 | 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 | +--------------------------------------------------------------------+ | |
e70a7fc0 | 26 | */ |
b5935203 | 27 | |
b5935203 | 28 | /** |
29 | * Class api_v3_TaxContributionPageTest | |
acb109b7 | 30 | * @group headless |
b5935203 | 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'; | |
9099cab3 | 38 | protected $_priceSetParams = []; |
b5935203 | 39 | protected $_paymentProcessorType; |
9099cab3 CW |
40 | protected $payParams = []; |
41 | protected $paymentProceParams = []; | |
42 | protected $settingValue = []; | |
b5935203 | 43 | protected $setInvoiceSettings; |
9099cab3 | 44 | protected $_ids = []; |
b5935203 | 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 | ||
9099cab3 | 58 | $this->params = [ |
92fcb95f | 59 | 'title' => "Test Contribution Page" . substr(sha1(rand()), 0, 7), |
b5935203 | 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, | |
21dfd5f5 | 68 | 'is_billing_required' => TRUE, |
9099cab3 | 69 | ]; |
b5935203 | 70 | |
9099cab3 | 71 | $this->_priceSetParams = [ |
92fcb95f TO |
72 | 'name' => 'tax_contribution' . substr(sha1(rand()), 0, 7), |
73 | 'title' => 'contributiontax' . substr(sha1(rand()), 0, 7), | |
b5935203 | 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, | |
21dfd5f5 | 80 | 'is_reserved' => 0, |
9099cab3 | 81 | ]; |
6c6e6187 | 82 | // Financial Account with 20% tax rate |
9099cab3 | 83 | $financialAccountSetparams = [ |
b5935203 | 84 | #[domain_id] => |
92fcb95f | 85 | 'name' => 'vat full taxrate account' . substr(sha1(rand()), 0, 7), |
b5935203 | 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, | |
9099cab3 | 93 | ]; |
b5935203 | 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 | |
9099cab3 | 99 | $financialType = [ |
92fcb95f | 100 | 'name' => 'grassvariety1' . substr(sha1(rand()), 0, 7), |
b5935203 | 101 | 'is_reserved' => 0, |
102 | 'is_active' => 1, | |
9099cab3 | 103 | ]; |
6c6e6187 | 104 | $priceField = $this->callAPISuccess('financial_type', 'create', $financialType); |
b5935203 | 105 | $this->financialtypeID = $priceField['id']; |
9099cab3 | 106 | $financialRelationParams = [ |
b5935203 | 107 | 'entity_table' => 'civicrm_financial_type', |
108 | 'entity_id' => $this->financialtypeID, | |
109 | 'account_relationship' => 10, | |
21dfd5f5 | 110 | 'financial_account_id' => $this->financialAccountId, |
9099cab3 | 111 | ]; |
b5935203 | 112 | $financialRelation = CRM_Financial_BAO_FinancialTypeAccount::add($financialRelationParams); |
113 | ||
114 | // Financial type with 5% tax rate | |
9099cab3 | 115 | $financialAccHalftax = [ |
92fcb95f | 116 | 'name' => 'vat half taxrate account' . substr(sha1(rand()), 0, 7), |
b5935203 | 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, | |
21dfd5f5 | 123 | 'is_default' => 0, |
9099cab3 | 124 | ]; |
b5935203 | 125 | $halfFinancialAccount = CRM_Financial_BAO_FinancialAccount::add($financialAccHalftax); |
126 | $this->halfFinancialAccId = $halfFinancialAccount->id; | |
9099cab3 | 127 | $halfFinancialtypeHalftax = [ |
92fcb95f | 128 | 'name' => 'grassvariety2' . substr(sha1(rand()), 0, 7), |
b5935203 | 129 | 'is_reserved' => 0, |
130 | 'is_active' => 1, | |
9099cab3 | 131 | ]; |
31037a42 | 132 | |
b5935203 | 133 | $halfFinancialType = CRM_Financial_BAO_FinancialType::add($halfFinancialtypeHalftax); |
6c6e6187 | 134 | $this->halfFinancialTypeId = $halfFinancialType->id; |
9099cab3 | 135 | $financialRelationHalftax = [ |
b5935203 | 136 | 'entity_table' => 'civicrm_financial_type', |
137 | 'entity_id' => $this->halfFinancialTypeId, | |
138 | 'account_relationship' => 10, | |
21dfd5f5 | 139 | 'financial_account_id' => $this->halfFinancialAccId, |
9099cab3 | 140 | ]; |
b5935203 | 141 | |
142 | $halfFinancialRelation = CRM_Financial_BAO_FinancialTypeAccount::add($financialRelationHalftax); | |
143 | ||
144 | // Enable component contribute setting | |
ec5da26a | 145 | $setInvoiceSettings = $this->enableTaxAndInvoicing(); |
b5935203 | 146 | |
147 | // Payment Processor | |
9099cab3 | 148 | $paymentProceParams = [ |
b5935203 | 149 | 'domain_id' => 1, |
92fcb95f | 150 | 'name' => 'dummy' . substr(sha1(rand()), 0, 7), |
97502bac | 151 | 'payment_processor_type_id' => CRM_Core_PseudoConstant::getKey('CRM_Financial_BAO_PaymentProcessor', 'payment_processor_type_id', 'Dummy'), |
b5935203 | 152 | 'financial_account_id' => 12, |
153 | 'is_active' => 1, | |
154 | 'is_default' => 1, | |
b5935203 | 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, | |
21dfd5f5 | 161 | 'payment_type' => 1, |
9099cab3 | 162 | ]; |
b5935203 | 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 | ||
fda18dc3 | 169 | /** |
170 | * Cleanup after function. | |
171 | */ | |
00be9182 | 172 | public function tearDown() { |
83644f47 | 173 | $this->quickCleanUpFinancialEntities(); |
fda18dc3 | 174 | parent::tearDown(); |
b5935203 | 175 | } |
176 | ||
00be9182 | 177 | public function setUpContributionPage() { |
b5935203 | 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']); | |
481a74f4 | 184 | CRM_Price_BAO_PriceSet::addTo('civicrm_contribution_page', $contributionPageResult['id'], $priceSetID); |
b5935203 | 185 | |
186 | if (empty($this->_ids['price_field'])) { | |
9099cab3 | 187 | $priceField = $this->callAPISuccess('price_field', 'create', [ |
b5935203 | 188 | 'price_set_id' => $priceSetID, |
189 | 'label' => 'Goat Breed', | |
190 | 'html_type' => 'Radio', | |
9099cab3 CW |
191 | ]); |
192 | $this->_ids['price_field'] = [$priceField['id']]; | |
b5935203 | 193 | } |
194 | if (empty($this->_ids['price_field_value'])) { | |
9099cab3 | 195 | $this->callAPISuccess('price_field_value', 'create', [ |
b5935203 | 196 | 'price_set_id' => $priceSetID, |
197 | 'price_field_id' => $priceField['id'], | |
198 | 'label' => 'Long Haired Goat', | |
199 | 'amount' => 100, | |
21dfd5f5 | 200 | 'financial_type_id' => $this->financialtypeID, |
9099cab3 CW |
201 | ]); |
202 | $priceFieldValue = $this->callAPISuccess('price_field_value', 'create', [ | |
b5935203 | 203 | 'price_set_id' => $priceSetID, |
204 | 'price_field_id' => $priceField['id'], | |
205 | 'label' => 'Shoe-eating Goat', | |
206 | 'amount' => 300, | |
21dfd5f5 | 207 | 'financial_type_id' => $this->halfFinancialTypeId, |
9099cab3 CW |
208 | ]); |
209 | $this->_ids['price_field_value'] = [$priceFieldValue['id']]; | |
b5935203 | 210 | } |
211 | $this->_ids['contribution_page'] = $contributionPageResult['id']; | |
212 | } | |
213 | ||
214 | /** | |
83644f47 | 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 | |
b5935203 | 221 | */ |
83644f47 | 222 | public function testCreateContributionOnline($thousandSeparator) { |
223 | $this->setCurrencySeparators($thousandSeparator); | |
b5935203 | 224 | $this->setUpContributionPage(); |
9099cab3 | 225 | $params = [ |
b5935203 | 226 | 'contact_id' => $this->_individualId, |
227 | 'receive_date' => '20120511', | |
83644f47 | 228 | 'total_amount' => $this->formatMoneyInput(100.00), |
b5935203 | 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', | |
21dfd5f5 | 235 | 'contribution_status_id' => 1, |
9099cab3 | 236 | ]; |
31037a42 | 237 | |
3c27d467 | 238 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
b5935203 | 239 | $this->_ids['contributionId'] = $contribution['id']; |
a15773db TM |
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); | |
ba4a1892 | 245 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); |
a15773db TM |
246 | $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20); |
247 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 1); | |
b5935203 | 248 | $this->_checkFinancialRecords($contribution, 'online'); |
249 | } | |
250 | ||
83644f47 | 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); | |
b5935203 | 261 | $this->setUpContributionPage(); |
9099cab3 | 262 | $params = [ |
b5935203 | 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, | |
9099cab3 CW |
272 | 'api.line_item.create' => [ |
273 | [ | |
b5935203 | 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, | |
9099cab3 CW |
279 | ], |
280 | [ | |
b5935203 | 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, | |
9099cab3 CW |
286 | ], |
287 | ], | |
288 | ]; | |
b5935203 | 289 | |
3c27d467 | 290 | $contribution = $this->callAPISuccess('contribution', 'create', $params); |
b5935203 | 291 | |
9099cab3 | 292 | $lineItems = $this->callAPISuccess('line_item', 'get', [ |
b5935203 | 293 | 'entity_id' => $contribution['id'], |
294 | 'contribution_id' => $contribution['id'], | |
295 | 'entity_table' => 'civicrm_contribution', | |
296 | 'sequential' => 1, | |
9099cab3 | 297 | ]); |
b5935203 | 298 | $this->assertEquals(2, $lineItems['count']); |
299 | } | |
300 | ||
00be9182 | 301 | public function testCreateContributionPayLaterOnline() { |
b5935203 | 302 | $this->setUpContributionPage(); |
9099cab3 | 303 | $params = [ |
b5935203 | 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, | |
9099cab3 | 314 | ]; |
3c27d467 | 315 | $contribution = $this->callAPISuccess('contribution', 'create', $params, __FUNCTION__, __FILE__); |
a15773db TM |
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); | |
ba4a1892 | 321 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); |
a15773db TM |
322 | $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20); |
323 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2); | |
b5935203 | 324 | $this->_checkFinancialRecords($contribution, 'payLater'); |
325 | } | |
92915c55 | 326 | |
83644f47 | 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); | |
b5935203 | 337 | $this->setUpContributionPage(); |
9099cab3 | 338 | $params = [ |
b5935203 | 339 | 'contact_id' => $this->_individualId, |
340 | 'receive_date' => '20120511', | |
83644f47 | 341 | 'total_amount' => $this->formatMoneyInput(100.00), |
b5935203 | 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, | |
9099cab3 | 348 | ]; |
b5935203 | 349 | |
3c27d467 | 350 | $contribution = $this->callAPISuccess('contribution', 'create', $params, __FUNCTION__, __FILE__); |
a15773db TM |
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); | |
ba4a1892 | 356 | $this->assertEquals($contribution['values'][$contribution['id']]['source'], 'SSF'); |
a15773db TM |
357 | $this->assertEquals($contribution['values'][$contribution['id']]['tax_amount'], 20); |
358 | $this->assertEquals($contribution['values'][$contribution['id']]['contribution_status_id'], 2); | |
b5935203 | 359 | $this->_checkFinancialRecords($contribution, 'pending'); |
83644f47 | 360 | $this->setCurrencySeparators($thousandSeparator); |
b5935203 | 361 | } |
362 | ||
79d7553f | 363 | /** |
a76b8bd8 | 364 | * Update a contribution. |
365 | * | |
b5935203 | 366 | * Function tests that line items, financial records are updated when contribution amount is changed |
367 | */ | |
00be9182 | 368 | public function testCreateUpdateContributionChangeTotal() { |
b5935203 | 369 | $this->setUpContributionPage(); |
9099cab3 | 370 | $this->contributionParams = [ |
b5935203 | 371 | 'contact_id' => $this->_individualId, |
372 | 'receive_date' => '20120511', | |
373 | 'total_amount' => 100.00, | |
92915c55 | 374 | 'financial_type_id' => $this->financialtypeID, |
b5935203 | 375 | 'source' => 'SSF', |
376 | 'contribution_status_id' => 1, | |
9099cab3 | 377 | ]; |
b5935203 | 378 | $contribution = $this->callAPISuccess('contribution', 'create', $this->contributionParams); |
9099cab3 | 379 | $lineItems = $this->callAPISuccess('line_item', 'getvalue', [ |
b5935203 | 380 | 'entity_id' => $contribution['id'], |
381 | 'entity_table' => 'civicrm_contribution', | |
382 | 'sequential' => 1, | |
383 | 'return' => 'line_total', | |
9099cab3 | 384 | ]); |
b5935203 | 385 | $this->assertEquals('100.00', $lineItems); |
386 | $trxnAmount = $this->_getFinancialTrxnAmount($contribution['id']); | |
387 | $this->assertEquals('120.00', $trxnAmount); | |
9099cab3 | 388 | $newParams = [ |
b5935203 | 389 | 'id' => $contribution['id'], |
39b959db SL |
390 | // without tax rate i.e Donation |
391 | 'financial_type_id' => 1, | |
21dfd5f5 | 392 | 'total_amount' => '300', |
9099cab3 | 393 | ]; |
a76b8bd8 | 394 | $contribution = $this->callAPISuccess('contribution', 'create', $newParams); |
b5935203 | 395 | |
9099cab3 | 396 | $lineItems = $this->callAPISuccess('line_item', 'getvalue', [ |
b5935203 | 397 | 'entity_id' => $contribution['id'], |
398 | 'entity_table' => 'civicrm_contribution', | |
399 | 'sequential' => 1, | |
400 | 'return' => 'line_total', | |
9099cab3 | 401 | ]); |
b5935203 | 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 | /** | |
100fef9d | 411 | * @param int $contId |
b5935203 | 412 | * |
413 | * @return null|string | |
414 | */ | |
00be9182 | 415 | public function _getFinancialTrxnAmount($contId) { |
b5935203 | 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 | /** | |
100fef9d | 427 | * @param int $contId |
b5935203 | 428 | * |
429 | * @return null|string | |
430 | */ | |
00be9182 | 431 | public function _getFinancialItemAmount($contId) { |
b5935203 | 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 | /** | |
c490a46a | 443 | * @param array $params |
b5935203 | 444 | * @param $context |
445 | */ | |
00be9182 | 446 | public function _checkFinancialRecords($params, $context) { |
9099cab3 | 447 | $entityParams = [ |
b5935203 | 448 | 'entity_id' => $params['id'], |
449 | 'entity_table' => 'civicrm_contribution', | |
9099cab3 | 450 | ]; |
b5935203 | 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)); | |
9099cab3 | 457 | $trxnParams = [ |
b5935203 | 458 | 'id' => $trxn['financial_trxn_id'], |
9099cab3 | 459 | ]; |
b5935203 | 460 | if ($context != 'online' && $context != 'payLater') { |
9099cab3 | 461 | $compareParams = [ |
b5935203 | 462 | 'to_financial_account_id' => 6, |
463 | 'total_amount' => 120, | |
464 | 'status_id' => 1, | |
9099cab3 | 465 | ]; |
b5935203 | 466 | } |
467 | if ($context == 'online') { | |
9099cab3 | 468 | $compareParams = [ |
b5935203 | 469 | 'to_financial_account_id' => 12, |
470 | 'total_amount' => 120, | |
471 | 'status_id' => 1, | |
9099cab3 | 472 | ]; |
b5935203 | 473 | } |
474 | elseif ($context == 'payLater') { | |
9099cab3 | 475 | $compareParams = [ |
b5935203 | 476 | 'to_financial_account_id' => 7, |
477 | 'total_amount' => 120, | |
478 | 'status_id' => 2, | |
9099cab3 | 479 | ]; |
b5935203 | 480 | } |
481 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialTrxn', $trxnParams, $compareParams); | |
9099cab3 | 482 | $entityParams = [ |
b5935203 | 483 | 'financial_trxn_id' => $trxn['financial_trxn_id'], |
484 | 'entity_table' => 'civicrm_financial_item', | |
9099cab3 | 485 | ]; |
b5935203 | 486 | $entityTrxn = current(CRM_Financial_BAO_FinancialItem::retrieveEntityFinancialTrxn($entityParams)); |
9099cab3 | 487 | $fitemParams = [ |
b5935203 | 488 | 'id' => $entityTrxn['entity_id'], |
9099cab3 CW |
489 | ]; |
490 | $compareParams = [ | |
b5935203 | 491 | 'amount' => 100, |
492 | 'status_id' => 1, | |
493 | 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID), | |
9099cab3 | 494 | ]; |
b5935203 | 495 | if ($context == 'payLater') { |
9099cab3 | 496 | $compareParams = [ |
b5935203 | 497 | 'amount' => 100, |
498 | 'status_id' => 3, | |
499 | 'financial_account_id' => $this->_getFinancialAccountId($this->financialtypeID), | |
9099cab3 | 500 | ]; |
b5935203 | 501 | } |
502 | $this->assertDBCompareValues('CRM_Financial_DAO_FinancialItem', $fitemParams, $compareParams); | |
503 | } | |
504 | ||
505 | /** | |
100fef9d | 506 | * @param int $financialTypeId |
f0be539a | 507 | * @return int |
b5935203 | 508 | */ |
00be9182 | 509 | public function _getFinancialAccountId($financialTypeId) { |
b5935203 | 510 | $accountRel = key(CRM_Core_PseudoConstant::accountOptionValues('account_relationship', NULL, " AND v.name LIKE 'Income Account is' ")); |
511 | ||
9099cab3 | 512 | $searchParams = [ |
92915c55 TO |
513 | 'entity_table' => 'civicrm_financial_type', |
514 | 'entity_id' => $financialTypeId, | |
b5935203 | 515 | 'account_relationship' => $accountRel, |
9099cab3 | 516 | ]; |
b5935203 | 517 | |
9099cab3 | 518 | $result = []; |
481a74f4 TO |
519 | CRM_Financial_BAO_FinancialTypeAccount::retrieve($searchParams, $result); |
520 | return CRM_Utils_Array::value('financial_account_id', $result); | |
b5935203 | 521 | } |
522 | ||
f0be539a | 523 | /** |
78ab0ca4 | 524 | * Test deleting a contribution. |
f0be539a | 525 | * |
78ab0ca4 | 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....). | |
f0be539a | 528 | */ |
00be9182 | 529 | public function testDeleteContribution() { |
9099cab3 | 530 | $contributionID = $this->contributionCreate([ |
78ab0ca4 | 531 | 'contact_id' => $this->_individualId, |
532 | 'trxn_id' => 12389, | |
533 | 'financial_type_id' => $this->financialtypeID, | |
534 | 'invoice_id' => 'dfsdf', | |
9099cab3 CW |
535 | ]); |
536 | $this->callAPISuccess('contribution', 'delete', ['id' => $contributionID]); | |
b5935203 | 537 | } |
96025800 | 538 | |
b5935203 | 539 | } |