Uskutecneni nakupu.
This commit is contained in:
parent
f2cbdeae49
commit
b950374236
32
app.php
32
app.php
|
@ -332,7 +332,8 @@ route('POST', '/bar/userinfo/', function ($req){
|
|||
});
|
||||
|
||||
route('POST', '/bar/buy/', function ($req){
|
||||
if (!user()) return jsonResponse(['error'=>'Unauthorized.'], 403);
|
||||
if (!user()) return jsonResponse(['error'=>'Vypršelo přihlášení.']);
|
||||
$user = user();
|
||||
|
||||
/** @var Psr\Http\Message\ServerRequestInterface $req */
|
||||
/** @var severak\database\rows $rows */
|
||||
|
@ -342,17 +343,40 @@ route('POST', '/bar/buy/', function ($req){
|
|||
|
||||
$card = $rows->one('cards', $Q['card_id']);
|
||||
|
||||
if ($card && $card['is_blocked']) {
|
||||
return jsonResponse(['error'=>'Karta je zablokovaná.']);
|
||||
}
|
||||
|
||||
if (!$card || !$card['is_active']) {
|
||||
return jsonResponse(['error'=>'Karta není aktivní.']);
|
||||
}
|
||||
|
||||
$member = $rows->one('members', $card['member_id']);
|
||||
|
||||
if ($member['balance']==0) {
|
||||
if ($member['balance']<1) {
|
||||
return jsonResponse(['error'=>'Karta není nabitá.']);
|
||||
}
|
||||
|
||||
// BIG TODO
|
||||
$totalSum = 0;
|
||||
foreach ($Q['items'] as $item) {
|
||||
$totalSum = $totalSum + ($item['price'] * $item['amount']);
|
||||
}
|
||||
|
||||
return jsonResponse(['error'=>'Zpracování nákupu zatím neimplemetováno.']);
|
||||
if ($member['balance']<$totalSum) {
|
||||
return jsonResponse(['error'=>'Na kartě není dostatek peněz.', 'balance'=>$member['balance']]);
|
||||
}
|
||||
|
||||
$rows->insert('transactions', [
|
||||
'member_id' => $member['id'],
|
||||
'card_id' => $card['id'],
|
||||
'issued_by'=>$user['id'],
|
||||
'issued_at'=>time(),
|
||||
'amount'=>$totalSum * -1,
|
||||
'items' => json_encode($Q['items']),
|
||||
'is_cash'=>0
|
||||
]);
|
||||
|
||||
$rows->execute($rows->query('UPDATE members SET balance = balance - ? WHERE id=?', [$totalSum, $member['id']]));
|
||||
|
||||
return jsonResponse(['success'=>true]);
|
||||
});
|
||||
|
|
29
tpl/bar.php
29
tpl/bar.php
|
@ -3,6 +3,12 @@
|
|||
|
||||
<!-- nejdříve se přihlásíme přes https://cozmo.github.io/jsQR/ -->
|
||||
|
||||
<div class="message is-success" v-show="success && !name">
|
||||
<div class="message-body">
|
||||
Nákup proběhl úspěšně. Můžete zadat další.
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="field has-addons" v-show="!name">
|
||||
<div class="control is-expanded">
|
||||
<input name="searchFor" class="input" placeholder="číslo karty" v-model="card_id" @change="findUser">
|
||||
|
@ -21,9 +27,11 @@
|
|||
<div v-show="name">
|
||||
<div class="message" >
|
||||
<div class="message-body">
|
||||
<span class="fas fa-user"></span>
|
||||
{{ name }} |
|
||||
k dispozici: {{ balance }},-
|
||||
<div class="columns is-mobile">
|
||||
<div class="column"><span class="fas fa-user"></span>
|
||||
{{ name }}</div>
|
||||
<div class="column has-text-right">{{ balance }},-</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
@ -53,7 +61,7 @@
|
|||
|
||||
<tr>
|
||||
<th colspan="3">celkem</th>
|
||||
<th class="has-text-right">{{ totalAmount }}</th>
|
||||
<th v-bind:class="totalClass" class="has-text-right">{{ totalAmount }}</th>
|
||||
</tr>
|
||||
|
||||
</table>
|
||||
|
@ -111,7 +119,8 @@
|
|||
balance: -1,
|
||||
can_buy_alcohol: false,
|
||||
items : [],
|
||||
error: false
|
||||
error: false,
|
||||
success: false
|
||||
},
|
||||
|
||||
computed: {
|
||||
|
@ -122,6 +131,13 @@
|
|||
total += item.price * item.amount;
|
||||
});
|
||||
return total;
|
||||
},
|
||||
|
||||
totalClass: function () {
|
||||
console.log(this);
|
||||
return {
|
||||
'has-text-danger': this.totalAmount > this.balance
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -202,7 +218,8 @@
|
|||
that.balance = response.balance;
|
||||
}
|
||||
} else if (response.success) {
|
||||
this.reset();
|
||||
that.success = true;
|
||||
that.reset();
|
||||
}
|
||||
}, function () {
|
||||
that.error = 'Došlo k nečekané chybě.';
|
||||
|
|
Loading…
Reference in New Issue