version fixes
[civicrm-core.git] / tests / phpunit / WebTest / Contribute / UpdateContributionTest.php
CommitLineData
855be98e
PJ
1<?php
2/*
3 +--------------------------------------------------------------------+
81621fee 4 | CiviCRM version 4.7 |
855be98e 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
855be98e
PJ
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 +--------------------------------------------------------------------+
d25dd0ee 25 */
855be98e
PJ
26
27
28require_once 'CiviTest/CiviSeleniumTestCase.php';
e9479dcf
EM
29
30/**
31 * Class WebTest_Contribute_UpdateContributionTest
32 */
855be98e
PJ
33class WebTest_Contribute_UpdateContributionTest extends CiviSeleniumTestCase {
34
6c6e6187 35 protected function setUp() {
855be98e
PJ
36 parent::setUp();
37 }
38
6c6e6187
TO
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[11]/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[11]/span/a[2]", "total_amount", FALSE);
5815ee4c 57 $this->waitForAjaxContent();
6c6e6187
TO
58 $this->type("total_amount", "90");
59 $this->clickLink('_qf_Contribution_upload', '', FALSE);
5815ee4c 60 $this->waitForAjaxContent();
ea83a78e 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);
6c6e6187
TO
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'));
481a74f4
TO
109 $search = array('entity_id' => $lineItem);
110 $compare = array('status_id' => 1);
6c6e6187
TO
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(
92915c55
TO
122 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
123 'is_reserved' => 1,
124 'is_deductible' => 1,
6c6e6187
TO
125 );
126 $this->addeditFinancialType($financialType);
e07ec072 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']");
6c6e6187
TO
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
92fcb95f 136 $premiumName = 'Premium' . substr(sha1(rand()), 0, 7);
6c6e6187
TO
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
92fcb95f 146 $premiumName2 = 'Premium' . substr(sha1(rand()), 0, 7);
6c6e6187
TO
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
481a74f4 160 $this->select("financial_type_id", "value=1");
6c6e6187
TO
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'");
5815ee4c 182 $this->assertEquals($actualAmount, $cost2, "Verify actual cost for changed premium");
6c6e6187
TO
183
184 $deletedAmount = $this->_getPremiumActualCost($contId, $from, $to, $cost, "'civicrm_contribution'");
5815ee4c 185 $this->assertEquals($deletedAmount, $cost, "Verify actual cost for deleted premium");
6c6e6187
TO
186 }
187
188 public function testDeletePremium() {
189 $this->webtestLogin();
190 $from = 'Premiums';
191 $to = 'Premiums inventory';
192 $financialType = array(
92915c55
TO
193 'name' => 'Test Financial' . substr(sha1(rand()), 0, 7),
194 'is_reserved' => 1,
195 'is_deductible' => 1,
6c6e6187
TO
196 );
197 $this->addeditFinancialType($financialType);
e07ec072 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']");
6c6e6187
TO
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
216 // add contribution with premium
217 $this->openCiviPage("contribute/add", "reset=1&action=add&context=standalone");
218
219 // create new contact using dialog
220 $this->createDialogContact();
221 // select financial type
222 $this->select("financial_type_id", "value=1");
223 // total amount
224 $this->type("total_amount", "100");
225 // fill Premium information
226 $this->click("xpath=//div[@id='Premium']");
227 $this->waitForElementPresent("product_name_0");
228 $this->select('product_name_0', "label=$premiumName ( $sku )");
229 // Clicking save.
230 $this->clickLink("_qf_Contribution_upload");
231 // Is status message correct?
232 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
233 // verify if Contribution is created
234 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
235 //click through to the Contribution edit screen
236 $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"));
237 $contId = explode('&', $contId[1]);
238 $contId = $contId[0];
239 $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);
240 $this->waitForElementPresent("product_name_0");
241 $this->select('product_name_0', "value=0");
242 // Clicking save.
94b3c290
WA
243 $this->click("_qf_Contribution_upload");
244 $this->waitForElementPresent("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]//span/a[text()='View']");
6c6e6187
TO
245
246 //Assertions
247 $actualAmount = $this->_getPremiumActualCost($contId, $from, $to, NULL, "'civicrm_contribution'");
5815ee4c 248 $this->assertEquals($actualAmount, $cost, "Verify actual cost for deleted premium");
6c6e6187
TO
249 }
250
251 public function testChangePaymentInstrument() {
252 $this->webtestLogin();
92fcb95f 253 $label = 'TEST' . substr(sha1(rand()), 0, 7);
6c6e6187
TO
254 $amount = 100.00;
255 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
256 $to = array_search('Accounts Receivable', $financialAccount);
257 $from = array_search('Deposit Bank Account', $financialAccount);
258 $this->addPaymentInstrument($label, $to);
259 $this->_testOfflineContribution($amount);
260 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
261 $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);
262 $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"));
263 $contId = explode('&', $contId[1]);
264 $contId = $contId[0];
265 //change payment processor to newly created value
266 $this->select("payment_instrument_id", "label=$label");
267 $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);
268 $this->waitForAjaxContent();
269 //Assertions
270 $subtractedTotal = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account');
271 $this->assertEquals($subtractedTotal, -$amount, "Verify amount deleted from old account");
272 $totalAmount = $this->_getPremiumActualCost($contId, NULL, 'Accounts Receivable');
273 $this->assertEquals($totalAmount, $amount, "Verify amount for newly inserted values");
274 }
275
276 public function testRefundContribution() {
277 $this->webtestLogin();
278 $amount = 100.00;
279 $this->_testOfflineContribution($amount);
280 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
281 $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');
282 //Contribution status
283 $this->select("contribution_status_id", "label=Refunded");
284 $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"));
285 $contId = explode('&', $contId[1]);
286 $contId = $contId[0];
287 $this->clickAjaxLink("_qf_Contribution_upload", "xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[6][text()='Refunded']");
288
289 //Assertions
290 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
291 $search = array('entity_id' => $lineItem);
292 $compare = array(
92915c55
TO
293 'amount' => '100.00',
294 'status_id' => 1,
6c6e6187
TO
295 );
296 $this->assertDBCompareValues("CRM_Financial_DAO_FinancialItem", $search, $compare);
297 $amount = $this->_getPremiumActualCost($contId, NULL, 'Payment Processor Account', -100.00, "'civicrm_contribution'");
298 $this->assertEquals($amount, '-100.00', 'Verify Financial Trxn Amount.');
299 }
300
301 public function testCancelPayLater() {
302 $this->webtestLogin();
303 $amount = 100.00;
304 $this->_testOfflineContribution($amount, "Pending");
305 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
306 $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);
307 //Contribution status
308 $this->select("contribution_status_id", "label=Cancelled");
309 $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"));
310 $contId = explode('&', $contId[1]);
311 $contId = $contId[0];
312 $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);
313 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
314
315 //Assertions
316 $search = array('id' => $contId);
317 $compare = array('contribution_status_id' => 3);
318 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
319 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
320 $itemParams = array(
92915c55
TO
321 'amount' => '-100.00',
322 'entity_id' => $lineItem,
6c6e6187
TO
323 );
324 $defaults = array();
325 $items = CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $defaults);
326 $this->assertEquals($items->amount, $itemParams['amount'], 'Verify Amount for financial Item');
327 $totalAmount = $this->_getPremiumActualCost($items->id, 'Accounts Receivable', NULL, "-100.00", "'civicrm_financial_item'");
328 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
329 $totalAmount = $this->_getPremiumActualCost($contId, 'Accounts Receivable', NULL, "-100.00", "'civicrm_contribution'");
330 $this->assertEquals($totalAmount, "-$amount", 'Verify Amount for Financial Trxn');
331 }
332
333 public function testChangeFinancialType() {
334 $this->webtestLogin();
335 $amount = 100.00;
336 $this->_testOfflineContribution($amount);
337 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='Edit']");
338 $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);
339 //Contribution status
340 $this->select("financial_type_id", "value=3");
341 $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"));
342 $contId = explode('&', $contId[1]);
343 $contId = $contId[0];
344 $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);
345 $this->waitForText('crm-notification-container', "The contribution record has been saved.");
346
347 //Assertions
481a74f4
TO
348 $search = array('id' => $contId);
349 $compare = array('financial_type_id' => 3);
6c6e6187
TO
350 $this->assertDBCompareValues('CRM_Contribute_DAO_Contribution', $search, $compare);
351
352 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
353 $itemParams = array(
92915c55
TO
354 'amount' => '-100.00',
355 'entity_id' => $lineItem,
6c6e6187
TO
356 );
357 $item1 = $item2 = array();
358 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item1);
359 $this->assertEquals($item1['amount'], "-100.00", "Verify Amount for New Financial Item");
360 $itemParams['amount'] = '100.00';
361 CRM_Financial_BAO_FinancialItem::retrieve($itemParams, $item2);
362 $this->assertEquals($item2['amount'], "100.00", "Verify Amount for New Financial Item");
363
364 $cValue1 = $this->_getPremiumActualCost($contId, NULL, NULL, "-100.00", "'civicrm_contribution'");
365 $fValue1 = $this->_getPremiumActualCost($item1['id'], NULL, NULL, "-100.00", "'civicrm_financial_item'");
366 $this->assertEquals($cValue1, "-100.00", "Verify Amount");
367 $this->assertEquals($fValue1, "-100.00", "Verify Amount");
368 $cValue2 = $this->_getPremiumActualCost($contId, NULL, NULL, "100.00", "'civicrm_contribution'");
369 $fValue2 = $this->_getPremiumActualCost($item2['id'], NULL, NULL, "100.00", "'civicrm_financial_item'");
370 $this->assertEquals($cValue2, "100.00", "Verify Amount");
371 $this->assertEquals($fValue2, "100.00", "Verify Amount");
372 }
855be98e 373
4cbe18b8 374 /**
100fef9d 375 * @param int $entityId
4cbe18b8
EM
376 * @param null $from
377 * @param null $to
378 * @param null $cost
379 * @param null $entityTable
380 * @param string $select
381 *
382 * @return null|string
95ea96be 383 */
acb1052e 384 public function _getPremiumActualCost($entityId, $from = NULL, $to = NULL, $cost = NULL, $entityTable = NULL, $select = "ft.total_amount AS amount") {
6c6e6187
TO
385 $financialAccount = CRM_Contribute_PseudoConstant::financialAccount();
386 $query = "SELECT
855be98e
PJ
387 {$select}
388 FROM civicrm_financial_trxn ft
389 INNER JOIN civicrm_entity_financial_trxn eft ON eft.financial_trxn_id = ft.id AND eft.entity_id = {$entityId}";
6c6e6187
TO
390 if ($entityTable) {
391 $query .= " AND eft.entity_table = {$entityTable}";
392 }
393 if (!empty($to)) {
394 $to = array_search($to, $financialAccount);
395 $query .= " AND ft.to_financial_account_id = {$to}";
396 }
397 if (!empty($from)) {
398 $from = array_search($from, $financialAccount);
399 $query .= " AND ft.from_financial_account_id = {$from}";
400 }
401 if (!empty($cost)) {
402 $query .= " AND eft.amount = {$cost}";
403 }
404 $query .= " ORDER BY ft.id DESC LIMIT 1";
405 $result = CRM_Core_DAO::singleValueQuery($query);
406 return $result;
407 }
855be98e 408
4cbe18b8 409 /**
100fef9d 410 * @param int $contId
4cbe18b8
EM
411 *
412 * @return null|string
95ea96be 413 */
acb1052e 414 public function _getFinancialTrxnAmount($contId) {
6c6e6187 415 $query = "SELECT
855be98e
PJ
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}";
6c6e6187
TO
421 $result = CRM_Core_DAO::singleValueQuery($query);
422 return $result;
423 }
855be98e 424
4cbe18b8 425 /**
100fef9d 426 * @param int $contId
4cbe18b8
EM
427 *
428 * @return null|string
95ea96be 429 */
acb1052e 430 public function _getFinancialItemAmount($contId) {
6c6e6187
TO
431 $lineItem = key(CRM_Price_BAO_LineItem::getLineItems($contId, 'contribution'));
432 $query = "SELECT
855be98e
PJ
433 SUM(amount)
434 FROM civicrm_financial_item
435 WHERE entity_table = 'civicrm_line_item'
436 AND entity_id = {$lineItem}";
6c6e6187
TO
437 $result = CRM_Core_DAO::singleValueQuery($query);
438 return $result;
439 }
855be98e 440
4cbe18b8 441 /**
100fef9d 442 * @param int $contId
4cbe18b8
EM
443 *
444 * @return null|string
95ea96be 445 */
acb1052e 446 public function _getTotalContributedAmount($contId) {
6c6e6187 447 $query = "SELECT
855be98e
PJ
448 SUM(amount)
449 FROM civicrm_entity_financial_trxn
450 WHERE entity_table = 'civicrm_contribution'
451 AND entity_id = {$contId}";
6c6e6187
TO
452 $result = CRM_Core_DAO::singleValueQuery($query);
453 return $result;
454 }
855be98e 455
4cbe18b8 456 /**
4cbe18b8
EM
457 * @param $amount
458 * @param string $status
80f3b91d 459 * @return array
4cbe18b8 460 */
6c6e6187 461 public function _testOfflineContribution($amount, $status = "Completed") {
855be98e 462
6c6e6187 463 $this->openCiviPage("contribute/add", "reset=1&context=standalone", "_qf_Contribution_upload");
855be98e 464
6c6e6187
TO
465 // create new contact using dialog
466 $contact = $this->createDialogContact();
855be98e 467
6c6e6187 468 // select financial type
481a74f4 469 $this->select("financial_type_id", "value=1");
855be98e 470
6c6e6187
TO
471 //Contribution status
472 $this->select("contribution_status_id", "label=$status");
855be98e 473
6c6e6187
TO
474 // total amount
475 $this->type("total_amount", $amount);
855be98e 476
6c6e6187
TO
477 // select payment instrument type
478 $this->select("payment_instrument_id", "label=Credit Card");
855be98e 479
6c6e6187 480 $this->type("trxn_id", "P20901X1" . rand(100, 10000));
855be98e 481
6c6e6187
TO
482 //Custom Data
483 //$this->click('CIVICRM_QFID_3_6');
855be98e 484
6c6e6187
TO
485 // Clicking save.
486 $this->clickLink("_qf_Contribution_upload");
855be98e 487
6c6e6187
TO
488 // Is status message correct?
489 $this->checkCRMAlert("The contribution record has been saved.");
855be98e 490
6c6e6187
TO
491 $this->waitForElementPresent("xpath=//div[@class='view-content']//table[@class='selector row-highlight']//tbody/tr[1]/td[8]/span/a[text()='View']");
492 // verify if Membership is created
855be98e 493
6c6e6187
TO
494 //click through to the Membership view screen
495 $this->click("xpath=//table[@class='selector row-highlight']/tbody/tr[1]/td[8]/span/a[text()='View']");
855be98e 496
6c6e6187 497 $expected = array(
92915c55
TO
498 'Financial Type' => 'Donation',
499 'Total Amount' => '100.00',
500 'Contribution Status' => $status,
6c6e6187
TO
501 );
502 $this->webtestVerifyTabularData($expected);
503 $this->click("_qf_ContributionView_cancel-bottom");
80f3b91d
CW
504
505 return $contact;
6c6e6187 506 }
96025800 507
855be98e 508}