Merge remote-tracking branch 'upstream/4.6' into 4.6-master-2015-11-16-01-17-32
[civicrm-core.git] / tests / phpunit / CRM / Event / BAO / AdditionalPaymentTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28
29 require_once 'CiviTest/CiviUnitTestCase.php';
30 require_once 'CiviTest/Contact.php';
31 require_once 'CiviTest/Event.php';
32 require_once 'CiviTest/Participant.php';
33
34 /**
35 * Class CRM_Event_BAO_AdditionalPaymentTest
36 */
37 class CRM_Event_BAO_AdditionalPaymentTest extends CiviUnitTestCase {
38
39 public function setUp() {
40 parent::setUp();
41 $this->_contactId = Contact::createIndividual();
42 $this->_eventId = Event::create($this->_contactId);
43 }
44
45 public function tearDown() {
46 $this->eventDelete($this->_eventId);
47 $this->quickCleanup(
48 array(
49 'civicrm_contact',
50 'civicrm_contribution',
51 'civicrm_participant',
52 'civicrm_participant_payment',
53 'civicrm_line_item',
54 'civicrm_financial_item',
55 'civicrm_financial_trxn',
56 'civicrm_entity_financial_trxn',
57 ),
58 TRUE
59 );
60 }
61
62 /**
63 * Helper function to record participant with paid contribution.
64 *
65 * @param int $feeTotal
66 * @param int $actualPaidAmt
67 *
68 * @return array
69 * @throws Exception
70 */
71 protected function addParticipantWithPayment($feeTotal, $actualPaidAmt) {
72 $priceSetId = $this->eventPriceSetCreate($feeTotal);
73 CRM_Price_BAO_PriceSet::addTo('civicrm_event', $this->_eventId, $priceSetId);
74
75 // create participant record
76 $eventId = $this->_eventId;
77 $participantParams = array(
78 'send_receipt' => 1,
79 'is_test' => 0,
80 'is_pay_later' => 0,
81 'event_id' => $eventId,
82 'register_date' => date('Y-m-d') . " 00:00:00",
83 'role_id' => 1,
84 'status_id' => 14,
85 'source' => 'Event_' . $eventId,
86 'contact_id' => $this->_contactId,
87 'note' => 'Note added for Event_' . $eventId,
88 'fee_level' => '\ 1Price_Field - 55\ 1',
89 );
90 $participant = $this->callAPISuccess('participant', 'create', $participantParams);
91 $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
92 // create participant contribution with partial payment
93 $contributionParams = array(
94 'total_amount' => $actualPaidAmt,
95 'source' => 'Fall Fundraiser Dinner: Offline registration',
96 'currency' => 'USD',
97 'non_deductible_amount' => 'null',
98 'receipt_date' => date('Y-m-d') . " 00:00:00",
99 'contact_id' => $this->_contactId,
100 'financial_type_id' => 4,
101 'payment_instrument_id' => 4,
102 'contribution_status_id' => 1,
103 'receive_date' => date('Y-m-d') . " 00:00:00",
104 'skipLineItem' => 1,
105 'partial_payment_total' => $feeTotal,
106 'partial_amount_pay' => $actualPaidAmt,
107 );
108
109 $contribution = CRM_Contribute_BAO_Contribution::create($contributionParams);
110 $contributionId = $contribution->id;
111 $participant = $this->callAPISuccessGetSingle('participant', array('id' => $participant['id']));
112
113 // add participant payment entry
114 $this->callAPISuccess('participant_payment', 'create', array(
115 'participant_id' => $participant['id'],
116 'contribution_id' => $contributionId,
117 ));
118
119 // -- processing priceSet using the BAO
120 $lineItem = array();
121 $priceSet = CRM_Price_BAO_PriceSet::getSetDetail($priceSetId, TRUE, FALSE);
122 $priceSet = CRM_Utils_Array::value($priceSetId, $priceSet);
123 $feeBlock = CRM_Utils_Array::value('fields', $priceSet);
124 $params['price_2'] = $feeTotal;
125 CRM_Price_BAO_PriceSet::processAmount($feeBlock,
126 $params, $lineItem
127 );
128 $lineItemVal[$priceSetId] = $lineItem;
129 CRM_Price_BAO_LineItem::processPriceSet($participant['id'], $lineItemVal, $contribution, 'civicrm_participant');
130 return array($participant, $contribution);
131 }
132
133 /**
134 * CRM-13964
135 */
136 public function testAddPartialPayment() {
137 $feeAmt = 100;
138 $amtPaid = 60;
139 $balance = $feeAmt - $amtPaid;
140 list($participant, $contribution) = $this->addParticipantWithPayment($feeAmt, $amtPaid);
141 $paymentInfo = CRM_Contribute_BAO_Contribution::getPaymentInfo($participant['id'], 'event');
142
143 // amount checking
144 $this->assertEquals(round($paymentInfo['total']), $feeAmt, 'Total amount recorded is not proper');
145 $this->assertEquals(round($paymentInfo['paid']), $amtPaid, 'Amount paid is not proper');
146 $this->assertEquals(round($paymentInfo['balance']), $balance, 'Balance amount is not proper');
147
148 // status checking
149 $this->assertEquals($participant['participant_status_id'], 14, 'Status record is not proper for participant');
150 $this->assertEquals($contribution->contribution_status_id, 8, 'Status record is not proper for contribution');
151 }
152
153 }