From e5e0130bf82ec66650cf49a8f2e9cf024f9c8132 Mon Sep 17 00:00:00 2001 From: soufiane Date: Thu, 20 Apr 2023 16:08:35 +0200 Subject: [PATCH] wip #5858 @2:00 --- app/Http/Controllers/AjaxController.php | 69 +++++++------------ app/Models/Command.php | 31 +++++++-- app/Models/CommandPanierSchema.php | 51 ++++++++++++++ app/Models/Panier.php | 9 +-- resources/js/app.js | 13 ++-- .../views/components/item-cart.blade.php | 15 ++-- .../views/partials/nav-account.blade.php | 2 +- 7 files changed, 126 insertions(+), 64 deletions(-) diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index a66268f..691b89c 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers; +use App\Models\Command; use App\Models\Panier; use Cubist\Backpack\Facades\App; use App\Models\Page; @@ -586,49 +587,7 @@ class AjaxController extends CubistFrontController /** * */ - $options = Product::getOptionsByProductsId($data['products']); - - $opt = []; - $total = []; - - foreach ($currentCartRef as $keyRefs => $refs) { - foreach (explode("/", $refs) as $key => $ref) { - $key -= 1; - if ($key > -1) { - $opt_ = array_filter($options[$keyRefs][$key]["options"], function ($n) use ($ref) { - return $n["ref"] === $ref; - }); - $opt_ = array_values($opt_); - $opt[$refs][] = $opt_[0]["sale_price"]; - } - } - - $quantity = intval($data['products'][$keyRefs]["quantity"]); - $basicSellingPrice = $data['products'][$keyRefs]["basic_selling_price"]; - - $totalOptionsPrice = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { - return $carry + $item; - }) : 0; - - $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"]; - $price = floatval($basicPriceByProductId + $totalOptionsPrice); - $total[] = floatval($price * $quantity); - $data['products'][$keyRefs]["price"] = $price; - } - - $checkUnavailableEcommerceProduct = array_filter($data['products'], function($n) { - return !$n['price']; - }); - - $ht = array_reduce($total, function($carry, $item) { return $carry + $item; }); - $ht += ($ht > 1000) ? 20 : 0; - $tva = $ht * 0.2; - $ttc = $ht + $tva; - $data['total'] = !$checkUnavailableEcommerceProduct ? $ttc : null; - - // - $data['addresses'] = json_encode($data['addresses']); - $data['products'] = json_encode($data['products']); + $data = Panier::fillData($currentCartRef, $data); if(sizeof($lastCartRefs) !== sizeof($intersect) || (sizeof($lastCartRefs) === sizeof($intersect) && sizeof($currentCartRef) != sizeof($lastCartRefs)) ) { @@ -698,8 +657,26 @@ class AjaxController extends CubistFrontController } public function order(Request $request) { - $request->validate([ - 'id' => 'required|numeric' - ]); + $validation = [ + 'addresses' => 'required', + 'products' => 'required', + 'user_id' => 'required|numeric' + ]; + $data = $this->validation_form($request, $validation); + + $currentCartRef = Panier::getRefs($data['products']); + $data = Command::fillData($currentCartRef, $data); + + if($data) { + $order = Command::create($data); + $order->save(); + }else{ + abort('422', 'Data are empty'); + } + + // Clear cart session + $request->session()->forget('cart_items'); + + return __('Votre commande a bien été enregistrée.'); } } diff --git a/app/Models/Command.php b/app/Models/Command.php index 1c43883..58795b6 100644 --- a/app/Models/Command.php +++ b/app/Models/Command.php @@ -9,19 +9,42 @@ class Command extends CommandPanierSchema 'singular' => 'Commande', 'plural' => 'Commandes']; + private static $_optionsForSelect = [ + 'new' => 'Nouvelle', + 'pending' => 'En attente de règlement', + 'in_progress' => 'En cours', + 'done' => 'Clôturée', + ]; + public function setFields() { parent::setFields(); + foreach ($this->fields as $fields) { + $fields['tab'] = 'Informations'; + $this->addField($fields); + } + $this->addField(['name' => 'status', - 'type' => 'Text', 'label' => 'Statut', + 'type' => 'SelectFromArray', 'column' => true, + 'options' => self::$_optionsForSelect, + 'default' => 'new', 'tab' => 'Informations' ]); - foreach ($this->fields as $fields) { - $this->addField($fields); - } + $this->addField(['name' => 'complete_status', + 'label' => 'Indication du statut', + 'type' => 'Text', + 'tab' => 'Informations' + ]); + } + + public static function getOptionValue($key) { + if($key) + return self::$_optionsForSelect[$key]; + + return false; } } diff --git a/app/Models/CommandPanierSchema.php b/app/Models/CommandPanierSchema.php index fd11cb3..4f74048 100644 --- a/app/Models/CommandPanierSchema.php +++ b/app/Models/CommandPanierSchema.php @@ -35,4 +35,55 @@ class CommandPanierSchema extends CubistMagicAbstractModel $userID = Client::getClientConnected()->id; return self::all()->where('user_id', $userID)->toArray(); } + + public static function fillData($currentCartRef, $data) { + /** + * + */ + $options = Product::getOptionsByProductsId($data['products']); + + $opt = []; + $total = []; + + foreach ($currentCartRef as $keyRefs => $refs) { + foreach (explode("/", $refs) as $key => $ref) { + $key -= 1; + if ($key > -1) { + $opt_ = array_filter($options[$keyRefs][$key]["options"], function ($n) use ($ref) { + return $n["ref"] === $ref; + }); + $opt_ = array_values($opt_); + $opt[$refs][] = $opt_[0]["sale_price"]; + } + } + + $quantity = intval($data['products'][$keyRefs]["quantity"]); + $basicSellingPrice = $data['products'][$keyRefs]["basic_selling_price"]; + + $totalOptionsPrice = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { + return $carry + $item; + }) : 0; + + $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"]; + $price = floatval($basicPriceByProductId + $totalOptionsPrice); + $total[] = floatval($price * $quantity); + $data['products'][$keyRefs]["price"] = $price; + } + + $checkUnavailableEcommerceProduct = array_filter($data['products'], function($n) { + return !$n['price']; + }); + + $ht = array_reduce($total, function($carry, $item) { return $carry + $item; }); + $ht += ($ht > 1000) ? 20 : 0; + $tva = $ht * 0.2; + $ttc = $ht + $tva; + $data['total'] = !$checkUnavailableEcommerceProduct ? $ttc : null; + + // + $data['addresses'] = json_encode($data['addresses']); + $data['products'] = json_encode($data['products']); + + return $data; + } } diff --git a/app/Models/Panier.php b/app/Models/Panier.php index b701bb7..80e5272 100644 --- a/app/Models/Panier.php +++ b/app/Models/Panier.php @@ -14,15 +14,16 @@ class Panier extends CommandPanierSchema { parent::setFields(); + foreach ($this->fields as $fields) { + $fields['tab'] = 'Informations'; + $this->addField($fields); + } + $this->addField(['name' => 'name', 'type' => 'Text', 'label' => 'Nom', 'column' => true ]); - - foreach ($this->fields as $fields) { - $this->addField($fields); - } } public static function getRefs($products) { diff --git a/resources/js/app.js b/resources/js/app.js index d18faf3..5f4d363 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -543,14 +543,19 @@ const app = new Vue({ .catch(function (error) { }) }, - order(event) { - const root = this, - id = event.target.dataset.id + order() { + let root = this, + data = { + user_id: this.user.id, + addresses: this.addresses, + products: this.items + } - axios.post('/ajax/order', {id: id}) + axios.post('/ajax/order', data) .then(function (response) { root.validateAction = true root.confirmMessageCart = response.data; + root.items = {} }) .catch(function (error) { }) diff --git a/resources/views/components/item-cart.blade.php b/resources/views/components/item-cart.blade.php index 54e7cab..cda2bf1 100644 --- a/resources/views/components/item-cart.blade.php +++ b/resources/views/components/item-cart.blade.php @@ -14,9 +14,9 @@

{{ __('Date de commande') }}

{{ \Carbon\Carbon::parse($item['created_at'])->isoFormat("D/MM/Y") }}

-
+

{{ __('Numéro de commande') }}

-

{{ $item['id'] }}

+

N°{{ $item['id'] }}

@endif @@ -33,7 +33,7 @@ @isset($product['basic_selling_price']) @if(floatval($product['basic_selling_price']))

{{ __('Prix unitaire') }} {{ - $product['basic_selling_price'] }} € HT + $product['basic_selling_price'] }} € HT

@endif @endisset @@ -56,11 +56,16 @@ @endforeach