EN COURS - Ajout des commandes

This commit is contained in:
Romain Bonhomme 2020-11-17 13:41:31 +01:00
parent 53002ef402
commit 272ecd0ced
24 changed files with 464 additions and 15 deletions

View file

@ -3,7 +3,6 @@
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class DashboardController extends Controller
{

View file

@ -0,0 +1,47 @@
<?php
namespace App\Http\Controllers\Bo;
use App\Http\Controllers\Controller;
use App\Models\Orders;
use App\Models\OrdersProducts;
use App\Models\Products;
class OrdersController extends Controller
{
public function index() {
$orders = Orders::orderBy('date', 'DESC')->get();
return view('bo.orders', compact('orders'));
}
public function changeStatus($reference) {
$order = Orders::where('reference', $reference)->first();
$order->status_id = $order->status_id + 1;
$order->save();
return redirect()->route('bo.orders')->with('success', "Changement de status enregistré pour la commande " . $order->reference . ".");
}
public function cancelOrder($reference) {
$order = Orders::where('reference', $reference)->first();
$order->status_id = 7;
$order->save();
return redirect()->route('bo.orders')->with('success', "La commande à été annulée.");
}
public function addProduct($orderReference, $productId, $quantity) {
$product = Products::findOrFail($productId);
$order = Orders::where('reference', $orderReference)->first();
$order->total = $order->total + (($product->price - $product->price_buy) * $quantity);
$orderProduct = new OrdersProducts();
$orderProduct->product_id = $product->id;
$orderProduct->order_id = $order->id;
$orderProduct->quantity = $quantity;
$orderProduct->save();
return redirect()->route('bo.orders')->with('success', "Le produit à été ajouté.");
}
}

View file

@ -22,18 +22,24 @@ class RedirectOldSlugs
$rangeSlug = $request->route('rangeSlug');
if ($productSlug !== null) {
$productOldSlug = OldSlug::where('slug', $productSlug)->first();
if ($productOldSlug) {
$product = Products::where('id', $productOldSlug->product_id)->first();
return redirect()->route('product', $product->slug);
$product = Products::where('slug', $productSlug)->first();
if (!$product) {
$productOldSlug = OldSlug::where('slug', $productSlug)->first();
if ($productOldSlug) {
$product = Products::where('id', $productOldSlug->product_id)->first();
return redirect()->route('product', $product->slug);
}
}
}
if ($rangeSlug !== null) {
$rangeOldSlug = OldSlug::where('slug', $rangeSlug)->first();
if ($rangeOldSlug) {
$range = Ranges::where('id', $rangeOldSlug->range_id)->first();
return redirect()->route('range', $range->slug);
$range = Ranges::where('slug', $rangeSlug)->first();
if (!$range) {
$rangeOldSlug = OldSlug::where('slug', $rangeSlug)->first();
if ($rangeOldSlug) {
$range = Ranges::where('id', $rangeOldSlug->range_id)->first();
return redirect()->route('range', $range->slug);
}
}
}

23
app/Models/Orders.php Normal file
View file

@ -0,0 +1,23 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Orders extends Model
{
use HasFactory;
public function products() {
return $this->belongsToMany('App\Models\Products', 'orders_products', 'order_id', 'product_id')->withPivot('quantity');
}
public function status() {
return $this->belongsTo('App\Models\OrdersStatus', 'status_id');
}
public function user() {
return $this->belongsTo('App\Models\User', 'user_id');
}
}

View file

@ -0,0 +1,11 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\Pivot;
class OrdersProducts extends Pivot
{
use HasFactory;
}

View file

@ -0,0 +1,18 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class OrdersStatus extends Model
{
use HasFactory;
protected $table = "orders_status";
public $timestamps = false;
public function orders() {
return $this->hasMany('App\Models\Orders', 'status_id');
}
}

View file

@ -4,10 +4,12 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Products extends Model
{
use HasFactory;
use SoftDeletes;
protected $table = "products";
@ -18,4 +20,12 @@ class Products extends Model
public function old_slugs() {
return $this->hasMany('App\Model\OldSlug', 'product_id');
}
public function stats() {
return $this->hasMany('App\Models\Stats', 'product_id');
}
public function orders() {
return $this->belongsToMany('App\Models\Orders', 'orders_products', 'product_id', 'order_id')->withPivot('quantity');
}
}

17
app/Models/Stats.php Normal file
View file

@ -0,0 +1,17 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Stats extends Model
{
use HasFactory;
protected $table = "stats";
public function product() {
return $this->belongsTo('App\Models\Products', 'product_id');
}
}

View file

@ -43,4 +43,8 @@ class User extends Authenticatable
protected $casts = [
'email_verified_at' => 'datetime',
];
public function orders() {
return $this->hasMany('App\Models\Orders', 'user_id');
}
}

View file

@ -19,13 +19,15 @@ class CreateProducts extends Migration
$table->text('description');
$table->string('picture')->nullable();
$table->float('price');
$table->float('price_buy');
$table->float('price_buy')->nullable();
$table->unsignedBigInteger('stock');
$table->string('reference');
$table->string('slug');
$table->boolean('status');
$table->foreignId('range_id')->constrained('ranges');
$table->float('margin')->default(0);
$table->timestamps();
$table->softDeletes();
});
}

View file

@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateStats extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('stats', function (Blueprint $table) {
$table->id();
$table->foreignId('product_id')->constrained('products');
$table->date('date');
$table->float('margin')->default(0);
$table->unsignedBigInteger('nb_sale')->default(0);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('stats');
}
}

View file

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersStatus extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders_status', function (Blueprint $table) {
$table->id();
$table->string('label');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders_status');
}
}

View file

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrders extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users');
$table->string('reference')->unique();
$table->date('date');
$table->string('commentaire')->nullable();
$table->float('total');
$table->foreignId('status_id')->constrained('orders_status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders');
}
}

View file

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateOrdersProducts extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('orders_products', function (Blueprint $table) {
$table->foreignId('product_id')->constrained('products');
$table->foreignId('order_id')->constrained('orders');
$table->unsignedInteger('quantity')->default(1);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('orders_products');
}
}

View file

@ -15,5 +15,6 @@ class DatabaseSeeder extends Seeder
{
$this->call(ContentSeeder::class);
$this->call(SettingsSeeder::class);
$this->call(OrdersStatusSeeder::class);
}
}

View file

@ -0,0 +1,47 @@
<?php
namespace Database\Seeders;
use App\Models\OrdersStatus;
use Illuminate\Database\Seeder;
class OrdersStatusSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$attente = new OrdersStatus();
$attente->label = "En attente";
$payee = new OrdersStatus();
$payee->label ="Payée";
$validee = new OrdersStatus();
$validee->label = "Validée";
$preparation = new OrdersStatus();
$preparation->label = "En cours de préparation";
$prete = new OrdersStatus();
$prete->label = "Prête";
$retiree = new OrdersStatus();
$retiree->label = "Retirée";
$annulee = new OrdersStatus();
$annulee->label = "Annulée";
// Ordre
$attente->save();
$payee->save();
$validee->save();
$preparation->save();
$prete->save();
$retiree->save();
$annulée->save();
}
}

View file

@ -33,7 +33,22 @@
@section('scripts')
<script type="text/javascript">
ClassicEditor
.create( document.querySelector( '#value' ) )
.create( document.querySelector( '#value' ), {
removePlugins: ["Image", "ImageCaption", "ImageStyle", "ImageToolbar", "ImageUpload"],
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'blockQuote', 'insertTable', '|', 'undo', 'redo' ],
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' },
{ model: 'heading4', view: 'h4', title: 'Heading 4', class: 'ck-heading_heading4' }
]
},
table: {
contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
}
} )
.catch( error => {
console.error( error );
} );

View file

@ -0,0 +1,39 @@
@extends('layouts.bo-layout')
@section('content')
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
<h1 class="h2">Commandes</h1>
</div>
<table class="table">
<thead>
<th scope="col">Utilisateur</th>
<th scope="col">Référence</th>
<th scope="col">Date</th>
<th scope="col">Total en €</th>
<th scope="col">Status</th>
<th></th>
</thead>
<tbody>
@foreach ($orders as $order)
<tr>
<td>{{$order->user->email}}</td>
<td>{{$order->reference}}</td>
<td>{{\Carbon\Carbon::parse($order->date)->format('d/m/Y')}}</td>
<td>{{number_format($order->total, 2, ',', ' ')}}€</td>
<td>{{$order->status->label}}</td>
<td class="text-center">
{{-- L'id du status 6 est le status "Retirée", la dernière étape d'une commande --}}
@if ($order->status_id < 6)
<a href="{{route('bo.change-order-status', $order->reference)}}" class="btn btn-sm btn-primary"><i class="fas fa-forward"></i></a>
<a href="{{route('bo.cancel-order', $order->id)}}" class="btn btn-sm btn-danger"><i class="fas fa-times"></i></a>
@endif
</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
@section('scripts')
<script src="https://kit.fontawesome.com/2327d60f96.js" crossorigin="anonymous"></script>
@endsection

View file

@ -68,7 +68,22 @@
@section('scripts')
<script type="text/javascript">
ClassicEditor
.create( document.querySelector( '#description' ) )
.create( document.querySelector( '#value' ), {
removePlugins: ["Image", "ImageCaption", "ImageStyle", "ImageToolbar", "ImageUpload"],
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'blockQuote', 'insertTable', '|', 'undo', 'redo' ],
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' },
{ model: 'heading4', view: 'h4', title: 'Heading 4', class: 'ck-heading_heading4' }
]
},
table: {
contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
}
} )
.catch( error => {
console.error( error );
} );

View file

@ -77,7 +77,22 @@
@section('scripts')
<script type="text/javascript">
ClassicEditor
.create( document.querySelector( '#description' ) )
.create( document.querySelector( '#value' ), {
removePlugins: ["Image", "ImageCaption", "ImageStyle", "ImageToolbar", "ImageUpload"],
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'blockQuote', 'insertTable', '|', 'undo', 'redo' ],
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' },
{ model: 'heading4', view: 'h4', title: 'Heading 4', class: 'ck-heading_heading4' }
]
},
table: {
contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
}
} )
.catch( error => {
console.error( error );
} );

View file

@ -42,7 +42,22 @@
@section('scripts')
<script type="text/javascript">
ClassicEditor
.create( document.querySelector( '#description' ) )
.create( document.querySelector( '#value' ), {
removePlugins: ["Image", "ImageCaption", "ImageStyle", "ImageToolbar", "ImageUpload"],
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'blockQuote', 'insertTable', '|', 'undo', 'redo' ],
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' },
{ model: 'heading4', view: 'h4', title: 'Heading 4', class: 'ck-heading_heading4' }
]
},
table: {
contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
}
} )
.catch( error => {
console.error( error );
} );

View file

@ -51,7 +51,22 @@
@section('scripts')
<script type="text/javascript">
ClassicEditor
.create( document.querySelector( '#description' ) )
.create( document.querySelector( '#value' ), {
removePlugins: ["Image", "ImageCaption", "ImageStyle", "ImageToolbar", "ImageUpload"],
toolbar: [ 'heading', '|', 'bold', 'italic', 'link', 'bulletedList', 'numberedList', '|', 'blockQuote', 'insertTable', '|', 'undo', 'redo' ],
heading: {
options: [
{ model: 'paragraph', title: 'Paragraph', class: 'ck-heading_paragraph' },
{ model: 'heading1', view: 'h1', title: 'Heading 1', class: 'ck-heading_heading1' },
{ model: 'heading2', view: 'h2', title: 'Heading 2', class: 'ck-heading_heading2' },
{ model: 'heading3', view: 'h3', title: 'Heading 3', class: 'ck-heading_heading3' },
{ model: 'heading4', view: 'h4', title: 'Heading 4', class: 'ck-heading_heading4' }
]
},
table: {
contentToolbar: [ 'tableColumn', 'tableRow', 'mergeTableCells' ]
}
} )
.catch( error => {
console.error( error );
} );

View file

@ -61,6 +61,15 @@
</li>
</ul>
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>COMMANDES</span>
</h6>
<ul class="nav flex-column mb-2">
<li class="nav-item">
<a class="nav-link" href="{{route('bo.orders')}}">Commandes</a>
</li>
</ul>
<h6 class="sidebar-heading d-flex justify-content-between align-items-center px-3 mt-4 mb-1 text-muted">
<span>SERVICE WEB</span>
</h6>

View file

@ -14,6 +14,7 @@ use App\Http\Controllers\Bo\SettingsController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\Bo\OrdersController;
/*
|--------------------------------------------------------------------------
@ -52,6 +53,11 @@ Route::namespace('Bo')
// Produits
Route::resource('products', '\App\Http\Controllers\Bo\ProductsController');
Route::resource('ranges', '\App\Http\Controllers\Bo\RangesController');
// Commandes
Route::get('/orders', [OrdersController::class, 'index'])->name('orders');
Route::get('/orders/{reference}', [OrdersController::class, 'changeStatus'])->name('change-order-status');
Route::get('/orders/{reference}/cancel', [OrdersController::class, 'cancelOrder'])->name('cancel-order');
Route::post('/orders/add', [OrdersController::class, 'addProduct'])->name('order-add-product');
});
Route::namespace('Auth')