Skip to content

Commit

Permalink
wip: add checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiritin committed Sep 14, 2024
1 parent a88b78b commit 35ad502
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 35 deletions.
69 changes: 65 additions & 4 deletions app/Http/Controllers/POS/CheckoutController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
namespace App\Http\Controllers\POS;

use App\Http\Controllers\Controller;
use App\Models\Badge\Badge;
use App\Models\Checkout\Checkout;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Inertia\Inertia;
use Inertia\Response;

class CheckoutController extends Controller
{
public function show(): Response
public function show(Checkout $checkout): Response
{
return Inertia::render('POS/Checkout/Show', []);
return Inertia::render('POS/Checkout/Show', [
'checkout' => $checkout->load('items'),
]);
}

/**
Expand All @@ -23,9 +28,65 @@ public function store(Request $request)
{
$data = $request->validate([
'badge_ids.*' => 'nullable|int',
'user_id' => 'nullable|int',
'user_id' => 'required|int|exists:users,id',
]);
dd($data);

if(empty($data['badge_ids'])) {
$data['badge_ids'] = Badge::whereHas('fursuit.user', function($query) use ($data) {
$query->where('id', $data['user_id']);
})->where('status', 'unpaid')->pluck('id')->toArray();
}

$checkout = DB::transaction(function() use ($data) {
$badges = Badge::whereIn('id', $data['badge_ids'])->get();
$total = $badges->sum('total');
$subtotal = $badges->sum('subtotal');
$tax = $badges->sum('tax');

// Create Checkout
$checkout = Checkout::create([
'status' => 'open',
'user_id' => $data['user_id'],
'cashier_id' => auth('machine-user')->id(),
'total' => $total,
'tax' => $tax,
'subtotal' => $subtotal,
'fiskaly_data' => [],
]);
foreach($badges as $badge) {
$checkout->items()->create([
'payable_type' => Badge::class,
'payable_id' => $badge->id,
//
'name' => $this->generateName($badge),
'description' => $this->generateDescription($badge),
//
'total' => $badge->total,
'tax' => $badge->tax,
'subtotal' => $badge->subtotal,
]);
}
return $checkout;
});

return redirect()->route('pos.checkout.show', ['checkout' => $checkout->id]);

}

private function generateDescription(Badge $badge): array
{
$features = [];
if($badge->dual_side_print) {
$features[] = 'Double Sided Print';
}
if($badge->extra_copy_of) {
$features[] = 'Extra Copy';
}
return $features;
}

private function generateName(Badge $badge): string
{
return 'Fursuit Badge';
}
}
2 changes: 1 addition & 1 deletion app/Http/Middleware/InactivityLogoutMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class InactivityLogoutMiddleware
{
public function handle(Request $request, Closure $next)
{
if ($request->session()->has('lastActivityTime') && time() - $request->session()->get('lastActivityTime') > 30) {
if ($request->session()->has('lastActivityTime') && time() - $request->session()->get('lastActivityTime') > (60*30)) {
$request->session()->forget('lastActivityTime');
$request->session()->flush();
$user = auth()->user();
Expand Down
7 changes: 7 additions & 0 deletions app/Models/Checkout/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
class Checkout extends Model
{
protected $guarded = [];

protected function casts(): array
{
return [
'fiskaly_data' => 'array',
];
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
Expand Down
7 changes: 7 additions & 0 deletions app/Models/Checkout/CheckoutItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ class CheckoutItem extends Model
{
protected $guarded = [];

protected function casts(): array
{
return [
'description' => 'array',
];
}

public function checkout()
{
return $this->belongsTo(Checkout::class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration {
public function up(): void
{
Schema::table('checkout_items', function (Blueprint $table) {
$table->after('checkout_id', function ($table) {
$table->morphs('payable');
});
});
}
};
48 changes: 18 additions & 30 deletions resources/js/Pages/POS/Checkout/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import POSLayout from "@/Layouts/POSLayout.vue";
import Button from 'primevue/button';
import Cash from "@/Components/POS/Checkout/Cash.vue";
import SimpleKeyboard from "@/Components/SimpleKeyboard.vue";
import {formatEuroFromCents} from "@/helpers.js";
defineOptions({
layout: POSLayout,
Expand All @@ -14,7 +15,7 @@ let givenBills = ref([]);
let currentChange = ref([]);
const props = defineProps({
positions: Array,
checkout: Object,
total: Number,
});
Expand Down Expand Up @@ -47,27 +48,7 @@ const keyboardOptions = {
theme: "hg-theme-default hg-layout-numeric numeric-theme"
};
// todo: remove
const demoPositions = [
{
id: 0,
checkout_id: 69,
name: 'Luna',
description: 'Fursuit Badge',
subtotal: 0.89,
tax: 0.19,
total: 1
},
{
id: 1,
checkout_id: 70,
name: 'Fenya',
description: 'Fursuit Badge',
subtotal: 2.53,
tax: 0.47,
total: 3
},
];
const positions = props.checkout.items;
function denomToValue(denom) {
return Number(denom.replace(/[^\d]/g, '')) / (denom.endsWith('¢') ? 100 : 1);
Expand Down Expand Up @@ -154,14 +135,17 @@ function keyPress(event) {
<div class="flex flex-col grow p-4 gap-4 h-[100%]">
<div class="flex flex-col grow">
<span class="text-2xl">Positions:</span>
<!-- todo: remove: vvvvvvvvvvvvvvvv -->
<div v-if="positions || demoPositions" class="flex flex-col">
<!-- todo: remove: v..........vvvvvvvvvvvvvvvvv -->
<div v-for="pos in (positions || demoPositions)" :key="pos" class="flex p-2 rounded-lg gap-2 items-center">
<span><strong class="bg-white rounded-lg p-1">#{{pos.id}}</strong></span>
<div v-if="positions" class="flex flex-col">
<div v-for="pos in (positions)" :key="pos" class="flex p-2 rounded-lg gap-2 items-center">
<span><strong class="bg-white rounded-lg p-1">#{{pos.payable_id}}</strong></span>
<div class="flex bg-gray-200 grow justify-between p-1 rounded-lg">
<span><strong>{{ pos.name }}</strong> {{ pos.description }}</span>
<span class="ml-[auto]"><strong>{{ pos.total }}€</strong> ({{ pos.subtotal }}€ gross)</span>
<div>
<strong>{{ pos.name }}</strong>
<ul>
<li v-for="item in pos.description" :key="item">{{ item }}</li>
</ul>
</div>
<span class="ml-[auto]"><strong>{{ formatEuroFromCents(pos.total) }}</strong></span>
</div>
</div>
</div>
Expand All @@ -171,9 +155,13 @@ function keyPress(event) {
</div>
<!-- todo: implement -->
<div>
<div class="text-1xl text-gray-600 border-b-gray-400 flex justify-between items-end border-b-2 border-dotted border-black pb-2 mb-2">
<div>Tax</div>
<div>{{ formatEuroFromCents(checkout.tax) }}</div>
</div>
<div class="text-2xl flex justify-between items-end border-b-2 border-double border-black pb-2">
<div>Total</div>
<div>{{ total || 4 }}€</div>
<div>{{ formatEuroFromCents(checkout.total) }}</div>
</div>
</div>
<div class="flex rounded-lg bg-cyan-200 p-3 shrink">card status here</div>
Expand Down

0 comments on commit 35ad502

Please sign in to comment.