forked from Wunderbyte-GmbH/moodle-local_shopping_cart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceipt.php
199 lines (166 loc) · 6.72 KB
/
receipt.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
// This file is part of Moodle - https://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <https://www.gnu.org/licenses/>.
/**
* Generate receipt after cashier has confirmed payment.
*
* @package local_shopping_cart
* @copyright 2021 Wunderbyte GmbH <[email protected]>
* @license https://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require_once(__DIR__ . '/../../config.php');
require_once($CFG->libdir . '/pdflib.php');
require_login();
// Include the main TCPDF library (search for installation path).
$id = required_param('id', PARAM_INT);
$userid = required_param('userid', PARAM_INT);
$context = context_system::instance();
$commaseparator = current_language() == 'de' ? ',' : '.';
ob_start();
// Create new PDF document.
$pdf = new TCPDF('p', 'pt', 'A4', true, 'UTF-8', false);
// Set some content to print.
$PAGE->set_context($context);
$PAGE->set_url('/receipt.php');
$PAGE->set_title('Receipt');
$PAGE->set_heading('Receipt');
$filename = get_config('local_shopping_cart' , 'receiptimage');
$cfghtml = get_config('local_shopping_cart', 'receipthtml');
$context = \context_system::instance();
$fs = get_file_storage();
$files = $fs->get_area_files($context->id, 'local_shopping_cart', 'local_shopping_cart_receiptimage');
foreach ($files as $file) {
if ($file->get_filesize() > 0) {
$filename = $file->get_filename();
$imgurl = moodle_url::make_pluginfile_url($file->get_contextid(), $file->get_component(), $file->get_filearea(),
$file->get_itemid(), $file->get_filepath(), $file->get_filename(), true);
}
}
$items = local_shopping_cart\shopping_cart_history::return_data_from_ledger_via_identifier($id);
$timecreated = $items[array_key_first($items)]->timecreated;
$date = date("Y-m-d", $timecreated);
$userid = $items[array_key_first($items)]->userid;
global $DB;
$userdetails = $DB->get_record('user', ['id' => $userid]);
/*
* TODO: We'll have to add the user profile picture in the future.
* But it does not work with HTML. So we'll need a config setting
* in shopping cart and have to add it using TCPDF.
*/
// phpcs:ignore Squiz.PHP.CommentedOutCode.Found
/*$picturefile = null;
if ($usercontext = context_user::instance($userid, IGNORE_MISSING)) {
$fs = get_file_storage();
$files = $fs->get_area_files($usercontext->id, 'user', 'icon');
foreach ($files as $file) {
$filenamewithoutextension = explode('.', $file->get_filename())[0];
if ($filenamewithoutextension === 'f1') {
$picturefile = $file;
// We found it, so break the loop.
break;
}
}
}
if (!empty($picturefile)) {
// Retrieve the image contents and encode them as base64.
$picturedata = $picturefile->get_content();
$picturebase64 = base64_encode($picturedata);
// Now load the HTML of the image into the profilepicture param.
$userpic = '<img src="data:image/image;base64,' . $picturebase64 . '" />';
} else {
$userpic = '';
}*/
$cfghtml = str_replace("[[date]]", $date, $cfghtml);
$cfghtml = str_replace("[[firstname]]", $userdetails->firstname, $cfghtml);
$cfghtml = str_replace("[[lastname]]", $userdetails->lastname, $cfghtml);
$cfghtml = str_replace("[[mail]]", $userdetails->email, $cfghtml);
$cfghtml = str_replace("[[id]]", $id, $cfghtml);
$prehtml = explode('[[items]]', $cfghtml);
$repeathtml = explode('[[/items]]', $prehtml[1]);
$posthtml = $repeathtml[1];
$pos = 1;
$sum = 0.0;
$itemhtml = '';
foreach ($items as $item) {
$tmp = str_replace("[[price]]", number_format((float) $item->price, 2, $commaseparator, ''),
$repeathtml[0]);
$tmp = str_replace("[[name]]", $item->itemname, $tmp);
$tmp = str_replace("[[pos]]", $pos, $tmp);
// If it's a booking option, we add option-specific data.
if ($item->area == "option" && class_exists('mod_booking\singleton_service')) {
$optionid = $item->itemid;
$optionsettings = \mod_booking\singleton_service::get_instance_of_booking_option_settings($optionid);
$tmp = str_replace("[[location]]", $optionsettings->location ?? '', $tmp); // Add location.
$tmp = str_replace("[[dayofweektime]]", $optionsettings->dayofweektime ?? '', $tmp); // E.g. "Mo, 10:00 - 12:00".
} else {
// It should still be replaced with an empty string in case it's no booking option.
$tmp = str_replace("[[location]]", '', $tmp);
$tmp = str_replace("[[dayofweektime]]", '', $tmp);
}
$sum += $item->price;
$itemhtml .= $tmp;
$pos++;
}
$sumstring = number_format((float) $sum, 2, $commaseparator, '');
$posthtml = str_replace("[[sum]]", $sumstring, $posthtml);
$html = '
<style>
h1 {
color: black;
font-family: times;
font-size: 24pt;
}
td {
border-bottom: 1px solid #c3c3c3;
}
tr {
border: 1px solid #c3c3c3;
}
</style>
'. $prehtml[0] . $itemhtml . $posthtml;
// Print text using writeHTMLCell().
// Set document information.
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor($userdetails->email);
$pdf->SetTitle('bookingreceipt_' . $id . '_' . $userid . '_' . $date);
$pdf->SetSubject('');
$pdf->SetKeywords('');
// Set header and footer fonts.
$pdf->setHeaderFont([PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN]);
$pdf->setFooterFont([PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA]);
// Set default monospaced font.
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// Set auto page breaks.
$pdf->SetAutoPageBreak(false, PDF_MARGIN_BOTTOM);
// Set image scale factor.
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// Set default font subsetting mode.
$pdf->setFontSubsetting(true);
$pdf->SetHeaderMargin(0);
$pdf->SetFooterMargin(0);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
// Remove default footer.
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->AddPage();
if (isset($imgurl)) {
$pdf->Image($imgurl->out(false), 0, 0, $pdf->getPageWidth(), $pdf->getPageHeight(), "", "", "",
true, "300", "", false, false, 0);
}
$pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
ob_end_clean();
// Close and output PDF document.
// This method has several options, check the source code documentation for more information.
$pdf->Output($userdetails->firstname . '_' . $userdetails->lastname . '_' . $date . '.pdf', 'I');