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