-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.inc.php
138 lines (124 loc) · 3.3 KB
/
functions.inc.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
<?php
function price($diff) {
global $deposit;
$fullDays = floor($diff / 86400);
$leftMinutes = floor($diff / 60) - ($fullDays * 1440);
// TAGESPREIS: 3,00 € pro vollem Tag
$daysPrice = $fullDays*3;
// STUNDENPREIS: 0,39 € pro Stunde + ggf. 0,90 € Grundgebühr
$minutesPrice = $leftMinutes * (.39 / 60);
if($fullDays == 0) $minutesPrice += .9;
if($minutesPrice > 3) $minutesPrice = 3;
// GESAMTPREIS
$price = $daysPrice + $minutesPrice;
// KAUTION prüfen
if ($price > $deposit) $price = $deposit;
return round($price, 2);
}
function loadData() {
$arr = @json_decode(file_get_contents('data/'.USER.'.json'), true);
if (!$arr) $arr = [
'lend' => [],
'history' => []
];
return $arr;
}
function cronMove2History(&$myData) {
$myDataChanged = false;
$maxAge = time() - 3600 * 24 * 7;
foreach ($myData['lend'] as $i => $v) {
if ($v['ts'] < $maxAge) {
// move to history
$myData['history'][] = $v + [
'rdate' => 'Nie',
'rtime' => '',
'price' => $deposit,
'cable' => 'no',
'adapter' => 'no'
];
unset($myData['lend'][$i]);
$myDataChanged = true;
}
}
if ($myDataChanged) saveData($myData);
}
function saveData($myData) {
file_put_contents('data/'.USER.'.json', json_encode($myData, JSON_PRETTY_PRINT));
}
### AJAX
function addLend($myData, $id, $device) {
global $deposit;
if ($id) {
$json = [
'id' => $id,
'device' => $device,
'date' => date('d.m.y'),
'time' => date('H:i'),
'ts' => time()
];
$myData['lend'][] = $json;
$myDataChanged = true;
$json['success'] = true;
saveData($myData);
} else {
$json = ['error' => 'Fehlerhafte/Fehlende Formularinformationen erhalten'];
}
return $json;
}
function calculatePrice($myData, $id) {
foreach ($myData['lend'] as $k => $lend) {
if ($lend['id'] == $id) {
$to = time();
$price = price($to - $lend['ts']);
$json = [
'price' => floor($price * 100) / 100,
'time' => $to,
'device' => $lend['device']
];
$found = true;
break;
}
}
if ($found) $json['success'] = true;
else $json['error'] = 'Akku-Nr. konnte nicht ermittelt werden.';
return $json;
}
function returned($myData, $id, $cable, $adapter, $toReceived) {
global $deposit;
$error = 'AkkuNr konnte nicht gefunden werden. Bitte aktualisieren Sie die Seite und versuchen Sie es erneut.';
if ($id) {
$found = false;
foreach ($myData['lend'] as $k => $lend) {
if ($lend['id'] == $id) {
if (time() - $toReceived > 60 * 12) {
$error = 'Zeit abgelaufen, versuchen Sie es erneut.';
break;
}
$price = price($toReceived - $lend['ts']);
if ($lend['device']) {
if (!$cable) $price += 2;
if ($lend['device'] > 1 && !$adapter) $price += 1.5;
}
if ($price > $deposit) $price = $deposit;
$d = $lend + [
'rdate' => date('d.m.y', $toReceived), 'rtime' => date('H:i', $toReceived),
'rts' => $toReceived,
'price' => number_format(floor($price * 100) / 100, 2, ',', ' '),
'cable' => $cable ? 'yes' : 'no',
'adapter' => $adapter ? 'yes' : 'no'
];
$myData['history'][] = $d;
$json = $d;
unset($myData['lend'][$k]);
saveData($myData);
$found = true;
break;
}
}
if ($found) $json['success'] = true;
else $json = ['error' => $error];
} else {
$json = ['error' => 'AkkuNr konnte nicht ermittelt werden'];
}
return $json;
}