/
voucher.php
executable file
·119 lines (99 loc) · 3 KB
/
voucher.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
<?php
// Christian Egger, zone11@mac.com
// 2021-09-12
//
// - New UniFi API
// - Printer Exception
// - PHP8 Testing and CLI
// Composer
require __DIR__ . '/vendor/autoload.php';
// Config
require("./config.php");
// POST Data
if(!isset($_POST['duration'])) {
echo ("POST: The duration is missing!\n");
if(defined('STDIN')) {
echo("Running from CLI, so lets take a duration from 1 day\n");
$voucherDuration = 24;
} else {
echo("Sorry, we're leaving here...");
exit();
}
} else {
$voucherDuration = intval($_POST['duration']); // in hours
}
// Printer
use Mike42\Escpos\PrintConnectors\NetworkPrintConnector;
use Mike42\Escpos\EscposImage;
use Mike42\Escpos\Printer;
// Connect Printer Socket
try {
$printerConnector = new NetworkPrintConnector($cfgPrinterNetIP, $cfgPrinterNetPort,5);
} catch (Exception $e) {
echo('Printer Exception: '.$e->getMessage()."\n");
exit();
}
// Prepare Printer and Logo
$printer = new Printer($printerConnector);
$logo = EscposImage::load($cfgPrintLogoFile, false);
// unifiapi
$unifi = new UniFi_API\Client($cfgUnifiUsername, $cfgUnifiPassword, $cfgUnifiBaseurl, $cfgUnifiSiteid, false);
$unifi->set_debug(false);
$login = $unifi->login();
if ($login == 1) {
// Debug
echo("Login: OK!<br>\n");
//Create Voucher
$voucher_timestamp = $unifi->create_voucher(($voucherDuration*60),1,0,$cfgUnifiVoucherNote);
$voucher_timestamp_int = $voucher_timestamp[0]->create_time;
echo "Voucher Timestamp: ".$voucher_timestamp_int."<br>\n";
$voucher = $unifi->stat_voucher($voucher_timestamp_int);
// We got a voucher as array item 0
if(sizeof($voucher) > 0) {
// Form strings for printing
$voucherCodePrint = substr($voucher[0]->code,0,5)."-".substr($voucher[0]->code,5,5);
$voucherDurationPrint = floor($voucherDuration/24);
// Add day/days to Duration
if ($voucherDurationPrint > 1) {
$voucherDurationPrint .= " ".$cfgPrintTextDays;
} else {
$voucherDurationPrint .= " ".$cfgPrintTextDay;
}
// Debug
echo "Code: ".$voucherCodePrint."<br>\n";
echo "Duration: ".$voucherDurationPrint."<br>\n";
// Init printer
$printer -> initialize();
// Logo centered
$printer -> setJustification(Printer::JUSTIFY_CENTER);
$printer -> graphics($logo);
$printer -> feed();
// Name and title
$printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
$printer -> text($cfgPrintStrWifiName);
$printer -> selectPrintMode();
$printer -> feed();
$printer -> text($cfgPrintStrWifiTitle);
$printer -> feed(2);
// Voucher code
$printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
$printer -> text($cfgPrintTitleVoucher);
$printer -> feed();
$printer -> text($voucherCodePrint);
$printer -> feed(2);
// Duration
$printer -> selectPrintMode(Printer::MODE_DOUBLE_WIDTH);
$printer -> text($cfgPrintTitleDuration);
$printer -> feed();
$printer -> text($voucherDurationPrint);
$printer -> feed(5);
// Cut and finish
$printer -> cut();
$printer -> pulse();
}
} else {
echo "Login: FAILED!\n";
}
// Close printer
$printer -> close();
?>