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