Merge pull request #17981 from eileenmcnaughton/merge_form
[civicrm-core.git] / tests / phpunit / CRM / Contribute / Form / Task / InvoiceTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * Test APIv3 civicrm_contribute_* functions
14 *
15 * @package CiviCRM_APIv3
16 * @subpackage API_Contribution
17 * @group headless
18 */
19 class CRM_Contribute_Form_Task_InvoiceTest extends CiviUnitTestCase {
20
21 protected $_individualId;
22
23 /**
24 * Clean up after each test.
25 */
26 public function tearDown() {
27 $this->quickCleanUpFinancialEntities();
28 CRM_Utils_Hook::singleton()->reset();
29 }
30
31 /**
32 * CRM-17815 - Test due date and payment advice block in generated
33 * invoice pdf for pending and completed contributions
34 */
35 public function testInvoiceForDueDate() {
36 $contactIds = [];
37 $params = [
38 'output' => 'pdf_invoice',
39 'forPage' => 1,
40 ];
41
42 $this->_individualId = $this->individualCreate();
43 $contributionParams = [
44 'contact_id' => $this->_individualId,
45 'total_amount' => 100,
46 'financial_type_id' => 'Donation',
47 ];
48 $result = $this->callAPISuccess('Contribution', 'create', $contributionParams);
49
50 $contributionParams['contribution_status_id'] = 2;
51 $contributionParams['is_pay_later'] = 1;
52 $contribution = $this->callAPISuccess('Contribution', 'create', $contributionParams);
53
54 $contribution3 = $this->callAPISuccess('Contribution', 'create', $contributionParams);
55 $this->callAPISuccess('Payment', 'create', ['total_amount' => 8, 'contribution_id' => $contribution3['id']]);
56
57 $this->callAPISuccess('Contribution', 'create', ['id' => $contribution3['id'], 'is_pay_later' => 0]);
58
59 $contributionIDs = [
60 [$result['id']],
61 [$contribution['id']],
62 [$contribution3['id']],
63 ];
64
65 $contactIds[] = $this->_individualId;
66 foreach ($contributionIDs as $contributionID) {
67 $invoiceHTML[current($contributionID)] = CRM_Contribute_Form_Task_Invoice::printPDF($contributionID, $params, $contactIds);
68 }
69
70 $this->assertNotContains('Due Date', $invoiceHTML[$result['id']]);
71 $this->assertNotContains('PAYMENT ADVICE', $invoiceHTML[$result['id']]);
72
73 $this->assertContains('Due Date', $invoiceHTML[$contribution['id']]);
74 $this->assertContains('PAYMENT ADVICE', $invoiceHTML[$contribution['id']]);
75
76 $this->assertContains('AMOUNT DUE:</font></b></td>
77 <td style="text-align:right;"><b><font size="1">$ 92.00</font></b></td>', $invoiceHTML[$contribution3['id']]);
78
79 }
80
81 /**
82 * PR 13477 - Fix incorrect display of Line Items created via API
83 * when printing invoice (for Participants).
84 *
85 * @throws \CRM_Core_Exception
86 */
87 public function testInvoiceForLineItems() {
88
89 $this->enableTaxAndInvoicing();
90
91 $event = $this->eventCreatePaid([]);
92
93 $individualOneId = $this->individualCreate();
94 $individualTwoId = $this->individualCreate();
95 $contactIds = [$individualOneId, $individualTwoId];
96
97 $priceSetId = CRM_Price_BAO_PriceSet::getFor('civicrm_event', $event['id']);
98 $priceField = $this->callAPISuccess('PriceField', 'get', ['price_set_id' => $priceSetId]);
99 $priceFieldValues = $this->callAPISuccess('PriceFieldValue', 'get', [
100 'sequential' => 1,
101 'price_field_id' => $priceField['id'],
102 ]);
103
104 $lineItemParams = [];
105 foreach ($priceFieldValues['values'] as $key => $priceFieldValue) {
106 $lineItemParams[] = [
107 'line_item' => [
108 $priceFieldValue['id'] => [
109 'price_field_id' => $priceField['id'],
110 'label' => $priceFieldValue['label'],
111 'financial_type_id' => $priceFieldValue['financial_type_id'],
112 'price_field_value_id' => $priceFieldValue['id'],
113 'qty' => 1,
114 'field_title' => $priceFieldValue['label'],
115 'unit_price' => $priceFieldValue['amount'],
116 'line_total' => $priceFieldValue['amount'],
117 'entity_table' => 'civicrm_participant',
118 ],
119 ],
120 // participant params
121 'params' => [
122 'contact_id' => $contactIds[$key],
123 'event_id' => $event['id'],
124 'status_id' => 1,
125 'price_set_id' => $priceSetId,
126 'participant_fee_amount' => $priceFieldValue['amount'],
127 'participant_fee_level' => $priceFieldValue['label'],
128 ],
129 ];
130 }
131
132 $orderParams = [
133 'contact_id' => $individualOneId,
134 'total_amount' => array_reduce($priceFieldValues['values'], function($total, $priceFieldValue) {
135 $total += $priceFieldValue['amount'];
136 return $total;
137 }),
138 'financial_type_id' => $priceFieldValues['values'][0]['financial_type_id'],
139 'currency' => 'USD',
140 'line_items' => $lineItemParams,
141 ];
142
143 $order = $this->callAPISuccess('Order', 'create', $orderParams);
144
145 $pdfParams = [
146 'output' => 'pdf_invoice',
147 'forPage' => 1,
148 ];
149
150 $invoiceHTML = CRM_Contribute_Form_Task_Invoice::printPDF([$order['id']], $pdfParams, [$individualOneId]);
151
152 $lineItems = $this->callAPISuccess('LineItem', 'get', ['contribution_id' => $order['id']]);
153
154 foreach ($lineItems['values'] as $lineItem) {
155 $this->assertContains("<font size=\"1\">$ {$lineItem['line_total']}</font>", $invoiceHTML);
156 }
157
158 $totalAmount = $this->formatMoneyInput($order['values'][$order['id']]['total_amount']);
159 $this->assertContains("TOTAL USD</font></b></td>
160 <td style=\"text-align:right;\"><font size=\"1\">$ $totalAmount</font>", $invoiceHTML);
161
162 }
163
164 }