Merge pull request #7423 from eileenmcnaughton/CRM-17691
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.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 along with this program; if not, contact CiviCRM LLC |
21 | at info[AT]civicrm[DOT]org. If you have questions about the |
22 | GNU Affero General Public License or the licensing of CiviCRM, |
23 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
24 +--------------------------------------------------------------------+
25 */
26
27
28 require_once 'CiviTest/CiviSeleniumTestCase.php';
29
30 /**
31 * Class WebTest_Contribute_UpdateContributionTest
32 */
33 class WebTest_Contribute_UpdateContributionTest extends CiviSeleniumTestCase {
34
35 protected function setUp() {
36 parent::setUp();
37 }
38
39 public function testChangeContributionAmount() {
40 $this->webtestLogin();
41 $amount = 100;
42 //Offline Pay Later Contribution
43 $contact = $this->_testOfflineContribution($amount, "Pending");
44
45 $this->openCiviPage("contribute/search", "reset=1", "contribution_date_low");
46
47 $this->type("sort_name", $contact['sort_name']);
48 $this->click("_qf_Search_refresh");
49
50 $this->waitForElementPresent("xpath=//*[@id='Search']//div[@id='contributionSearch']");
51 $contriIDOff = explode('&', $this->getAttribute("xpath=//div[@id='contributionSearch']//table[@class='selector row-highlight']/tbody/tr[1]//td//span/a[1]@href"));
52 if (!empty($contriIDOff)) {
53 $contriIDOff = substr($contriIDOff[1], (strrpos($contriIDOff[1], '=') + 1));
54 }
55
56 $this->clickLink("xpath=//tr[@id='rowid{$contriIDOff}']//td//span/a[2]", "total_amount", FALSE);
57 $this->waitForAjaxContent();
58 $this->type("total_amount", "90");
59 $this->clickLink('_qf_Contribution_upload', '', FALSE);
60 $this->waitForAjaxContent();
61 $this->waitForText('crm-notification-container', "The sum of fee amount and net amount must be equal to total amount");
62 $this->type("net_amount", "90");
63 $this->clickLink('_qf_Contribution_upload', '', FALSE);
64
65 // Is status message correct?
66 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
67
68 //For Contribution
69 $searchParams = array('id' => $contriIDOff);
70 $compareParams = array('total_amount' => '90.00');
71 //For LineItem
72 $lineItemSearchParams = array('entity_id' => $contriIDOff);
73 $lineItemCompareParams = array('line_total' => '90.00');
74
75 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compareParams);
76 $this->assertDBCompareValues('CRM_Price_DAO_LineItem', $lineItemSearchParams, $lineItemCompareParams);
77
78 $total = $this->_getTotalContributedAmount($contriIDOff);
79 $compare = array('total_amount' => $total);
80 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
81
82 $amount = $this->_getFinancialItemAmount($contriIDOff);
83 $compare = array('total_amount' => $amount);
84 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
85
86 $financialTrxnAmount = $this->_getFinancialTrxnAmount($contriIDOff);
87 $compare = array('total_amount' => $financialTrxnAmount);
88 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $searchParams, $compare);
89 }
90
91 public function testPayLater() {
92 $this->webtestLogin();
93 $amount = 100.00;
94 //Offline Pay Later Contribution
95 $this->_testOfflineContribution($amount, "Pending");
96 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
97 $contId = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
98 $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom');
99 $this->select("contribution_status_id", "label=Completed");
100 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
101 $this->checkCRMAlert("The contribution record has been saved.");
102
103 //Assertions
104 $search = array('id' => $contId);
105 $compare = array('contribution_status_id' => 1);
106 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
107
108 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
109 $search = array('entity_id' => $lineItem);
110 $compare = array('status_id' => 1);
111 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
112
113 $status = $this->_getPremiumActualCost($contId, 'Accounts Receivable', 'Payment Processor Account', NULL, "'civicrm_contribution'", "ft.status_id as status");
114 $this->assertEquals($status, '1', "Verify Completed Status");
115 }
116
117 public function testChangePremium() {
118 $this->webtestLogin();
119 $from = 'Premiums';
120 $to = 'Premiums inventory';
121 $financialType = array(
122 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
123 'is_reserved' => 1,
124 'is_deductible' => 1,
125 );
126 $this->addeditFinancialType($financialType);
127 $this->waitForElementPresent("xpath=//div[@id='ltype']/div/table/tbody//tr/td/div[text()='" . $financialType['name'] . "']/../../td[7]/span/a[text()='Accounts']");
128 $this->click("xpath=//div[@id='ltype']/div/table/tbody//tr/td/div[text()='" . $financialType['name'] . "']/../../td[7]/span/a[text()='Accounts']");
129 $this->waitForElementPresent("xpath=//a[@id='newfinancialTypeAccount']");
130 $this->clickAjaxLink("xpath=//a[@id='newfinancialTypeAccount']", "_qf_FinancialTypeAccount_cancel-botttom");
131 $this->select("account_relationship", "label=Premiums Inventory Account is");
132 $this->waitForElementPresent("xpath=//select[@id='financial_account_id']/option[3][text()='Deposit Bank Account']");
133 $this->select("financial_account_id", "label=$to");
134 $this->clickAjaxLink("_qf_FinancialTypeAccount_next-botttom", "xpath=//a[@id='newfinancialTypeAccount']");
135
136 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
137 $amount = 500;
138 $sku = 'SKU';
139 $price = 300;
140 $cost = 3.00;
141 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
142 // add premium
143 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
144
145 //add second premium
146 $premiumName2 = 'Premium' . substr(sha1(rand()), 0, 7);
147 $amount2 = 600;
148 $sku2 = 'SKU';
149 $price2 = 200;
150 $cost2 = 2.00;
151 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
152 $this->addPremium($premiumName2, $sku2, $amount2, $price2, $cost2, $financialType['name']);
153
154 // add contribution with premium
155 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
156
157 // create new contact using dialog
158 $this->createDialogContact();
159 // select financial type
160 $this->select("financial_type_id", "value=1");
161 // total amount
162 $this->type("total_amount", "100");
163 // fill Premium information
164 $this->clickAjaxLink("xpath=//div[@id='Premium']", "product_name_0");
165 $this->select('product_name_0', "label=$premiumName ( $sku )");
166 // Clicking save.
167 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
168 // Is status message correct?
169 $this->checkCRMAlert("The contribution record has been saved.");
170 // verify if Contribution is created
171 //click through to the Contribution edit screen
172 $contId = $this->urlArg('id', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
173 $this->clickAjaxLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "product_name_0");
174
175 $this->select('product_name_0', "label=$premiumName2 ( $sku2 )");
176 // Clicking save.
177 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[7][text()='$premiumName2']");
178 $this->checkCRMAlert("The contribution record has been saved.");
179
180 //Assertions
181 $actualAmount = $this->_getPremiumActualCost($contId, $to, $from, $cost2, "'civicrm_contribution'");
182 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
183
184 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
185 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
186 }
187
188 public function testDeletePremium() {
189 $this->webtestLogin();
190 $from = 'Premiums';
191 $to = 'Premiums inventory';
192 $financialType = array(
193 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
194 'is_reserved' => 1,
195 'is_deductible' => 1,
196 );
197 $this->addeditFinancialType($financialType);
198 $this->waitForElementPresent("xpath=//div[@id='ltype']/div/table/tbody//tr/td/div[text()='" . $financialType['name'] . "']/../../td[7]/span/a[text()='Accounts']");
199 $this->click("xpath=//div[@id='ltype']/div/table/tbody//tr/td/div[text()='" . $financialType['name'] . "']/../../td[7]/span/a[text()='Accounts']");
200 $this->waitForElementPresent("xpath=//a[@id='newfinancialTypeAccount']");
201 $this->clickLink("xpath=//a[@id='newfinancialTypeAccount']", "_qf_FinancialTypeAccount_cancel-botttom", FALSE);
202
203 $this->select("account_relationship", "label=Premiums Inventory Account is");
204 $this->waitForElementPresent("xpath=//select[@id='financial_account_id']/option[3][text()='Deposit Bank Account']");
205 $this->select("financial_account_id", "label=$to");
206 $this->clickLink("_qf_FinancialTypeAccount_next-botttom", "xpath=//a[@id='newfinancialTypeAccount']", FALSE);
207 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
208 $amount = 500;
209 $sku = 'SKU';
210 $price = 300;
211 $cost = 3.00;
212 $this->openCiviPage("admin/contribute/managePremiums", "action=add&reset=1");
213 // add premium
214 $this->addPremium($premiumName, $sku, $amount, $price, $cost, $financialType['name']);
215 // add contribution with premium
216 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
217
218 // create new contact using dialog
219 $this->createDialogContact();
220 // select financial type
221 $this->select("financial_type_id", "value=1");
222 // total amount
223 $this->type("total_amount", "100");
224 // fill Premium information
225 $this->click("xpath=//div[@id='Premium']");
226 $this->waitForElementPresent("product_name_0");
227 $this->select('product_name_0', "label=$premiumName ( $sku )");
228 // Clicking save.
229 $this->clickLink("_qf_Contribution_upload");
230 // Is status message correct?
231 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
232 // verify if Contribution is created
233 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
234 //click through to the Contribution edit screen
235 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
236 $contId = explode('&', $contId[1]);
237 $contId = $contId[0];
238 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", "_qf_Contribution_upload-bottom", FALSE);
239 $this->waitForElementPresent("product_name_0");
240 $this->select('product_name_0', "value=0");
241 // Clicking save.
242 $this->click("_qf_Contribution_upload");
243 $this->waitForElementPresent("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]//span/a[text()='View']");
244
245 //Assertions
246 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
247 $this->assertEquals($actualAmount, NULL, "Verify actual cost for deleted premium");
248 }
249
250 public function testChangePaymentInstrument() {
251 $this->webtestLogin();
252 $label = 'TEST' . substr(sha1(rand()), 0, 7);
253 $amount = 100.00;
254 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
255 $to = array_search('Accounts Receivable', $financialAccount);
256 $from = array_search('Deposit Bank Account', $financialAccount);
257 $this->addPaymentInstrument($label, $to);
258 $this->_testOfflineContribution($amount);
259 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
260 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE);
261 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
262 $contId = explode('&', $contId[1]);
263 $contId = $contId[0];
264 //change payment processor to newly created value
265 $this->select("payment_instrument_id", "label=$label");
266 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE);
267 $this->waitForAjaxContent();
268 //Assertions
269 $subtractedTotal = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account');
270 $this->assertEquals($subtractedTotal, -$amount, "Verify amount deleted from old account");
271 $totalAmount = $this->_getPremiumActualCost($contId, NULL, 'Accounts Receivable');
272 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
273 }
274
275 public function testRefundContribution() {
276 $this->webtestLogin();
277 $amount = 100.00;
278 $this->_testOfflineContribution($amount);
279 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span//a[text()='Edit']");
280 $this->clickAjaxLink("xpath=//form[@class='CRM_Contribute_Form_Search crm-search-form']/div[2]/table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom');
281 //Contribution status
282 $this->select("contribution_status_id", "label=Refunded");
283 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
284 $contId = explode('&', $contId[1]);
285 $contId = $contId[0];
286 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[6][text()='Refunded']");
287
288 //Assertions
289 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
290 $search = array('entity_id' => $lineItem);
291 $compare = array(
292 'amount' => '100.00',
293 'status_id' => 1,
294 );
295 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
296 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
297 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
298 }
299
300 public function testCancelPayLater() {
301 $this->webtestLogin();
302 $amount = 100.00;
303 $this->_testOfflineContribution($amount, "Pending");
304 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
305 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE);
306 //Contribution status
307 $this->select("contribution_status_id", "label=Cancelled");
308 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
309 $contId = explode('&', $contId[1]);
310 $contId = $contId[0];
311 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE);
312 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
313
314 //Assertions
315 $search = array('id' => $contId);
316 $compare = array('contribution_status_id' => 3);
317 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
318 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
319 $itemParams = array(
320 'amount' => '-100.00',
321 'entity_id' => $lineItem,
322 );
323 $defaults = array();
324 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
325 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
326 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
327 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
328 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
329 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
330 }
331
332 public function testChangeFinancialType() {
333 $this->webtestLogin();
334 $amount = 100.00;
335 $this->_testOfflineContribution($amount);
336 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
337 $this->clickLink("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", '_qf_Contribution_cancel-bottom', FALSE);
338 //Contribution status
339 $this->select("financial_type_id", "value=3");
340 $contId = explode('&id=', $this->getAttribute("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']@href"));
341 $contId = explode('&', $contId[1]);
342 $contId = $contId[0];
343 $this->clickLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']", FALSE);
344 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
345
346 //Assertions
347 $search = array('id' => $contId);
348 $compare = array('financial_type_id' => 3);
349 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
350
351 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
352 $itemParams = array(
353 'amount' => '-100.00',
354 'entity_id' => $lineItem,
355 );
356 $item1 = $item2 = array();
357 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
358 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
359 $itemParams['amount'] = '100.00';
360 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
361 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
362
363 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
364 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
365 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
366 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
367 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
368 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
369 $this->assertEquals($cValue2, "100.00", "Verify Amount");
370 $this->assertEquals($fValue2, "100.00", "Verify Amount");
371 }
372
373 /**
374 * @param int $entityId
375 * @param null $from
376 * @param null $to
377 * @param null $cost
378 * @param null $entityTable
379 * @param string $select
380 *
381 * @return null|string
382 */
383 public function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
384 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
385 $query = "SELECT
386 {$select}
387 FROM civicrm_financial_trxn ft
388 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
389 if ($entityTable) {
390 $query .= " AND eft.entity_table = {$entityTable}";
391 }
392 if (!empty($to)) {
393 $to = array_search($to, $financialAccount);
394 $query .= " AND ft.to_financial_account_id = {$to}";
395 }
396 if (!empty($from)) {
397 $from = array_search($from, $financialAccount);
398 $query .= " AND ft.from_financial_account_id = {$from}";
399 }
400 if (!empty($cost)) {
401 $query .= " AND eft.amount = {$cost}";
402 }
403 $query .= " ORDER BY ft.id DESC LIMIT 1";
404 $result = CRM_Core_DAO::singleValueQuery($query);
405 return $result;
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 int $contId
442 *
443 * @return null|string
444 */
445 public function _getTotalContributedAmount($contId) {
446 $query = "SELECT
447 SUM(amount)
448 FROM civicrm_entity_financial_trxn
449 WHERE entity_table = 'civicrm_contribution'
450 AND entity_id = {$contId}";
451 $result = CRM_Core_DAO::singleValueQuery($query);
452 return $result;
453 }
454
455 /**
456 * @param $amount
457 * @param string $status
458 * @return array
459 */
460 public function _testOfflineContribution($amount, $status = "Completed") {
461
462 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
463
464 // create new contact using dialog
465 $contact = $this->createDialogContact();
466
467 // select financial type
468 $this->select("financial_type_id", "value=1");
469
470 //Contribution status
471 $this->select("contribution_status_id", "label=$status");
472
473 // total amount
474 $this->type("total_amount", $amount);
475
476 // select payment instrument type
477 $this->select("payment_instrument_id", "label=Credit Card");
478
479 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
480
481 //Custom Data
482 //$this->click('CIVICRM_QFID_3_6');
483
484 // Clicking save.
485 $this->clickLink("_qf_Contribution_upload");
486
487 // Is status message correct?
488 $this->checkCRMAlert("The contribution record has been saved.");
489
490 $this->waitForElementPresent("xpath=//form[@class='CRM_Contribute_Form_Search crm-search-form']/div[2]/table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span//a[text()='View']");
491 // verify if Membership is created
492
493 //click through to the Membership view screen
494 $this->click("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='View']");
495
496 $expected = array(
497 'Financial Type' => 'Donation',
498 'Total Amount' => '100.00',
499 'Contribution Status' => $status,
500 );
501 $this->webtestVerifyTabularData($expected);
502 $this->click("_qf_ContributionView_cancel-bottom");
503
504 return $contact;
505 }
506
507 }