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