Order api updates to fix participant handling & deprecate creating 'completed'
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Task / InvoiceTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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 * Test APIv3 civicrm_contribute_* functions
30 *
31 * @package CiviCRM_APIv3
32 * @subpackage API_Contribution
33 * @group headless
34 */
35 class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
36
37 protected $_individualId;
38
39 /**
40 * Clean up after each test.
41 */
42 public function tearDown() {
43 $this->quickCleanUpFinancialEntities();
44 CRM_Utils_Hook::singleton()->reset();
45 }
46
47 /**
48 * CRM-17815 - Test due date and payment advice block in generated
49 * invoice pdf for pending and completed contributions
50 */
51 public function testInvoiceForDueDate() {
52 $contactIds = [];
53 $params = [
54 'output' => 'pdf_invoice',
55 'forPage' => 1,
56 ];
57
58 $this->_individualId = $this->individualCreate();
59 $contributionParams = [
60 'contact_id' => $this->_individualId,
61 'total_amount' => 100,
62 'financial_type_id' => 'Donation',
63 ];
64 $result = $this->callAPISuccess('Contribution', 'create', $contributionParams);
65
66 $contributionParams['contribution_status_id'] = 2;
67 $contributionParams['is_pay_later'] = 1;
68 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
69
70 $contribution3 = $this->callAPISuccess('Contribution', 'create', $contributionParams);
71 $this->callAPISuccess('Payment', 'create', ['total_amount' => 8, 'contribution_id' => $contribution3['id']]);
72
73 $this->callAPISuccess('Contribution', 'create', ['id' => $contribution3['id'], 'is_pay_later' => 0]);
74
75 $contributionIDs = [
76 [$result['id']],
77 [$contribution['id']],
78 [$contribution3['id']],
79 ];
80
81 $contactIds[] = $this->_individualId;
82 foreach ($contributionIDs as $contributionID) {
83 $invoiceHTML[current($contributionID)] = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, $params, $contactIds);
84 }
85
86 $this->assertNotContains('Due Date', $invoiceHTML[$result['id']]);
87 $this->assertNotContains('PAYMENT ADVICE', $invoiceHTML[$result['id']]);
88
89 $this->assertContains('Due Date', $invoiceHTML[$contribution['id']]);
90 $this->assertContains('PAYMENT ADVICE', $invoiceHTML[$contribution['id']]);
91
92 $this->assertContains('AMOUNT DUE: </font></b></td>
93 <td style = "padding-left:34px;text-align:right;"><b><font size = "1">$ 92.00</font></b></td>', $invoiceHTML[$contribution3['id']]);
94
95 }
96
97 /**
98 * PR 13477 - Fix incorrect display of Line Items created via API
99 * when printing invoice (for Participants).
100 *
101 * @throws \CRM_Core_Exception
102 */
103 public function testInvoiceForLineItems() {
104
105 $this->enableTaxAndInvoicing();
106
107 $event = $this->eventCreatePaid([]);
108
109 $individualOneId = $this->individualCreate();
110 $individualTwoId = $this->individualCreate();
111 $contactIds = [$individualOneId, $individualTwoId];
112
113 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $event['id']);
114 $priceField = $this->callAPISuccess('PriceField', 'get', ['price_set_id' => $priceSetId]);
115 $priceFieldValues = $this->callAPISuccess('PriceFieldValue', 'get', [
116 'sequential' => 1,
117 'price_field_id' => $priceField['id'],
118 ]);
119
120 $lineItemParams = [];
121 foreach ($priceFieldValues['values'] as $key => $priceFieldValue) {
122 $lineItemParams[] = [
123 'line_item' => [
124 $priceFieldValue['id'] => [
125 'price_field_id' => $priceField['id'],
126 'label' => $priceFieldValue['label'],
127 'financial_type_id' => $priceFieldValue['financial_type_id'],
128 'price_field_value_id' => $priceFieldValue['id'],
129 'qty' => 1,
130 'field_title' => $priceFieldValue['label'],
131 'unit_price' => $priceFieldValue['amount'],
132 'line_total' => $priceFieldValue['amount'],
133 'entity_table' => 'civicrm_participant',
134 ],
135 ],
136 // participant params
137 'params' => [
138 'contact_id' => $contactIds[$key],
139 'event_id' => $event['id'],
140 'status_id' => 1,
141 'price_set_id' => $priceSetId,
142 'participant_fee_amount' => $priceFieldValue['amount'],
143 'participant_fee_level' => $priceFieldValue['label'],
144 ],
145 ];
146 }
147
148 $orderParams = [
149 'contact_id' => $individualOneId,
150 'total_amount' => array_reduce($priceFieldValues['values'], function($total, $priceFieldValue) {
151 $total += $priceFieldValue['amount'];
152 return $total;
153 }),
154 'financial_type_id' => $priceFieldValues['values'][0]['financial_type_id'],
155 'contribution_status_id' => 'Pending',
156 'currency' => 'USD',
157 'line_items' => $lineItemParams,
158 ];
159
160 $order = $this->callAPISuccess('Order', 'create', $orderParams);
161
162 $pdfParams = [
163 'output' => 'pdf_invoice',
164 'forPage' => 1,
165 ];
166
167 $invoiceHTML = CRM_Contribute_Form_Task_Invoice::printPDF([$order['id']], $pdfParams, [$individualOneId]);
168
169 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $order['id']]);
170
171 foreach ($lineItems['values'] as $lineItem) {
172 $this->assertContains("<font size = \"1\">$ {$lineItem['line_total']}</font>", $invoiceHTML);
173 }
174
175 $totalAmount = $this->formatMoneyInput($order['values'][$order['id']]['total_amount']);
176 $this->assertContains("TOTAL USD</font></b></td>
177 <td style = \"padding-left:34px;text-align:right;\"><font size = \"1\">$ $totalAmount</font>", $invoiceHTML);
178
179 }
180
181 }