Skip to content

Commit

Permalink
feat: add real time pending orders number to admin
Browse files Browse the repository at this point in the history
  • Loading branch information
phojie committed Jan 11, 2023
1 parent 2595f2e commit 9a99e4c
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 33 deletions.
46 changes: 46 additions & 0 deletions _ide_helper_models.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@
*/


namespace App\Models{
/**
* App\Models\Address
*
* @property string $id
* @property string $address1
* @property string|null $address2
* @property string $city
* @property string $province
* @property string $postal_code
* @property string $country
* @property string|null $user_id
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read string $full_address
* @property-read \App\Models\User|null $user
* @method static \Illuminate\Database\Eloquent\Builder|Address newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Address newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Address query()
* @method static \Illuminate\Database\Eloquent\Builder|Address whereAddress1($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereAddress2($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereCity($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereCountry($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address wherePostalCode($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereProvince($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Address whereUserId($value)
*/
class Address extends \Eloquent {}
}

namespace App\Models{
/**
* App\Models\Cart
Expand Down Expand Up @@ -53,6 +86,10 @@ class Cart extends \Eloquent {}
* @property-read string $status
* @property-read \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection|\Spatie\MediaLibrary\MediaCollections\Models\Media[] $media
* @property-read int|null $media_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\OrderVariant[] $orderVariants
* @property-read int|null $order_variants_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Order[] $orders
* @property-read int|null $orders_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Product[] $products
* @property-read int|null $products_count
* @method static \Database\Factories\CategoryFactory factory(...$parameters)
Expand Down Expand Up @@ -94,6 +131,8 @@ class Category extends \Eloquent implements \Spatie\MediaLibrary\HasMedia {}
* @property float $shipping_amount
* @property float $total_amount
* @property string $user_id
* @property string|null $delivered_at
* @property string|null $delivered_by
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\OrderVariant[] $orderVariants
Expand All @@ -108,6 +147,8 @@ class Category extends \Eloquent implements \Spatie\MediaLibrary\HasMedia {}
* @method static \Illuminate\Database\Eloquent\Builder|Order whereCity($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereCountry($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereDeliveredAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereDeliveredBy($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereEmail($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Order whereName($value)
Expand Down Expand Up @@ -208,6 +249,8 @@ class Permission extends \Eloquent {}
* @property-read string $image
* @property-read \Spatie\MediaLibrary\MediaCollections\Models\Collections\MediaCollection|\Spatie\MediaLibrary\MediaCollections\Models\Media[] $media
* @property-read int|null $media_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\OrderVariant[] $orderVariants
* @property-read int|null $order_variants_count
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Variant[] $variants
* @property-read int|null $variants_count
* @method static \Illuminate\Database\Eloquent\Builder|Product available()
Expand Down Expand Up @@ -301,12 +344,14 @@ class TemporaryFile extends \Eloquent {}
* @property string $last_name
* @property string $full_name
* @property string $password
* @property string|null $phone
* @property string $status
* @property \Illuminate\Support\Carbon|null $email_verified_at
* @property string|null $remember_token
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read \App\Models\Address|null $address
* @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Cart[] $carts
* @property-read int|null $carts_count
* @property-read string $avatar
Expand Down Expand Up @@ -340,6 +385,7 @@ class TemporaryFile extends \Eloquent {}
* @method static \Illuminate\Database\Eloquent\Builder|User whereLastName($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereMiddleName($value)
* @method static \Illuminate\Database\Eloquent\Builder|User wherePassword($value)
* @method static \Illuminate\Database\Eloquent\Builder|User wherePhone($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereRememberToken($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereStatus($value)
* @method static \Illuminate\Database\Eloquent\Builder|User whereUpdatedAt($value)
Expand Down
37 changes: 37 additions & 0 deletions app/Events/PendingOrdersNumber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace App\Events;

use App\Models\Order;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class PendingOrdersNumber implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;

public function __construct()
{
//
}

public function broadcastOn(): Channel|array
{
return new Channel('pending-order-number');
}

public function broadcastAs(): string
{
return 'pending.order.number';
}

public function broadcastWith(): array
{
return [
'pendingOrderNumber' => (new Order())->countPending(),
];
}
}
2 changes: 2 additions & 0 deletions app/Http/Controllers/Customer/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers\Customer;

use App\Events\NewOrder;
use App\Events\PendingOrdersNumber;
use App\Http\Requests\OrderRequest;
use App\Http\Resources\OrderResource;
use App\Models\Order;
Expand Down Expand Up @@ -72,6 +73,7 @@ public function store(OrderRequest $request)
});

NewOrder::dispatch($order);
broadcast(new PendingOrdersNumber());

session()->flash('success', [
'title' => 'Order',
Expand Down
7 changes: 5 additions & 2 deletions app/Http/Middleware/HandleInertiaRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Middleware;

use App\Http\Resources\UserResource;
use App\Models\Order;
use Auth;
use Illuminate\Http\Request;
use Inertia\Middleware;
Expand Down Expand Up @@ -37,6 +38,7 @@ public function version(Request $request)
public function share(Request $request)
{
$isAuth = Auth::check();
$isAuthHasAdminRole = $isAuth ? Auth::user()->hasRole('admin') : false;

return array_merge(parent::share($request), [
'auth' => [
Expand All @@ -47,10 +49,11 @@ public function share(Request $request)
],
'flash' => [
'notification' => session('notification'),
'success' => session('success'),
'success' => session('success'),
],
'csrfToken' => csrf_token(),
'cartCount' => $isAuth? Auth::user()->carts->count() : 0,
'cartCount' => $isAuth ? Auth::user()->carts->count() : 0,
'pendingOrderCount' => $isAuthHasAdminRole ? (new Order())->countPending(): 0,

// 'ziggy' => function () use ($request) {1
// return array_merge((new Ziggy)->toArray(), [
Expand Down
21 changes: 13 additions & 8 deletions app/Models/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,24 @@ public function user(): BelongsTo
return $this->belongsTo(User::class);
}

public function countPending(): int
{
return $this->where('status', 'pending')->count();
}

public function scopeSearch($query, $search): object
{
return $query->when(
$search,
fn ($q) => $q->where('order_number', 'ilike', "%{$search}%")
->orWhere('name', 'ilike', "%{$search}%")
->orWhere('email', 'ilike', "%{$search}%")
->orWhere('phone', 'ilike', "%{$search}%")
->orWhere('address', 'ilike', "%{$search}%")
->orWhere('city', 'ilike', "%{$search}%")
->orWhere('province', 'ilike', "%{$search}%")
->orWhere('postal_code', 'ilike', "%{$search}%")
->orWhere('status', 'ilike', "%{$search}%")
->orWhere('name', 'ilike', "%{$search}%")
->orWhere('email', 'ilike', "%{$search}%")
->orWhere('phone', 'ilike', "%{$search}%")
->orWhere('address', 'ilike', "%{$search}%")
->orWhere('city', 'ilike', "%{$search}%")
->orWhere('province', 'ilike', "%{$search}%")
->orWhere('postal_code', 'ilike', "%{$search}%")
->orWhere('status', 'ilike', "%{$search}%")
);
}

Expand Down
47 changes: 25 additions & 22 deletions app/Services/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Services;

use App\Events\OrderStatusUpdated;
use App\Events\PendingOrdersNumber;
use App\Http\Requests\OrderRequest;
use App\Models\Order;
use App\Services\Interfaces\OrderServiceInterface;
Expand Down Expand Up @@ -52,19 +53,19 @@ public function update(OrderRequest $request, Order $order): void
{
try {
\DB::transaction(function () use ($request, $order) {
// if status is cancelled before, and updated to paid
if ($order->getOriginal('status') === 'cancelled' && ($request->status === 'delivered' ||$request->status === 'shipped' || $request->status === 'pending')) {
// set order variants stock
$order->orderVariants()->each(function ($orderVariant) {
$orderVariant->variant->decrement('stock', $orderVariant->quantity);
});
}

// if status is changed
if ($order->getOriginal('status') !== $request->status) {
// broadcast order status updated
broadcast(new OrderStatusUpdated($order));
}
// if status is cancelled before, and updated to paid
if ($order->getOriginal('status') === 'cancelled' && ($request->status === 'delivered' || $request->status === 'shipped' || $request->status === 'pending')) {
// set order variants stock
$order->orderVariants()->each(function ($orderVariant) {
$orderVariant->variant->decrement('stock', $orderVariant->quantity);
});
}

// if status is changed
if ($order->getOriginal('status') !== $request->status) {
// broadcast order status updated
broadcast(new OrderStatusUpdated($order));
}

$order->update([
'name' => $request->name,
Expand All @@ -84,15 +85,17 @@ public function update(OrderRequest $request, Order $order): void
// set order variants status
$order->orderVariants()->update(['status' => $request->status]);

// if status is cancelled
if ($request->status === 'cancelled') {
// set order variants stock
$order->orderVariants()->each(function ($orderVariant) {
$orderVariant->variant->increment('stock', $orderVariant->quantity);
});
}


// if status is cancelled
if ($request->status === 'cancelled') {
// set order variants stock
$order->orderVariants()->each(function ($orderVariant) {
$orderVariant->variant->increment('stock', $orderVariant->quantity);
});
}

if ($request->status === 'pending') {
broadcast(new PendingOrdersNumber());
}
});
} catch (\Exception $e) {
throw $e;
Expand Down
10 changes: 9 additions & 1 deletion resources/js/components/Admin/AdminHeader.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
<script setup lang="ts">
const sidebar = useSidebarStore()
let count = $ref(usePage().props.value.pendingOrderCount)
onMounted(() => {
window.Echo.channel('pending-order-number')
.listen('.pending.order.number', (e: any) => {
count = e.pendingOrderNumber
})
})
</script>

<template>
Expand Down Expand Up @@ -37,7 +45,7 @@ const sidebar = useSidebarStore()
<span class="flex">
<heroicons-clipboard-document-list class="w-6 h-6" aria-hidden="true" />
<span class="ml-1">
5
{{ count }}
</span>
</span>
</button>
Expand Down
1 change: 1 addition & 0 deletions resources/js/pages/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const perks = [
</script>

<template>
<Head title="Ronalds Catering" />
<main>
<!-- Hero section -->
<div class="relative">
Expand Down

0 comments on commit 9a99e4c

Please sign in to comment.