]> _ Git - pmi.git/commitdiff
wip #5858 @2:00
authorsoufiane <soufiane@cubedesigners.com>
Thu, 20 Apr 2023 14:08:35 +0000 (16:08 +0200)
committersoufiane <soufiane@cubedesigners.com>
Thu, 20 Apr 2023 14:08:35 +0000 (16:08 +0200)
app/Http/Controllers/AjaxController.php
app/Models/Command.php
app/Models/CommandPanierSchema.php
app/Models/Panier.php
resources/js/app.js
resources/views/components/item-cart.blade.php
resources/views/partials/nav-account.blade.php

index a66268f169bf6ab8eafbedd0ebea57f9780dc8a3..691b89caa6b4d26161ac403e15ae64228e152cf0 100644 (file)
@@ -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.');
     }
 }
index 1c4388310a89562c0c6436a4bed302028bb79997..58795b645ac877397e65a853d3321785cde895c7 100644 (file)
@@ -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;
     }
 }
index fd11cb3aa5d7287893c2f1d22cabce84af9562d5..4f7404861fd7f0d380e96918747cb87c625e219c 100644 (file)
@@ -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;
+    }
 }
index b701bb79de38f304d0a953c73fbb8128b3df4ed9..80e527284c1ab2007bc1fc3c8f1ead91941fc680 100644 (file)
@@ -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) {
index d18faf3df3844a751a5df942962236dbe03aae49..5f4d363e1a77fdd507fabc5abda39eb7e0e6926f 100644 (file)
@@ -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) {
                 })
index 54e7cab99f10f7eed9f99664f75a9c103e76611e..cda2bf19d534161f3715169a83de7fe24aedc720 100644 (file)
@@ -14,9 +14,9 @@
                         <p class="no-m">{{ __('Date de commande') }}</p>
                         <p class="no-m text-navy">{{ \Carbon\Carbon::parse($item['created_at'])->isoFormat("D/MM/Y") }}</p>
                     </div>
-                    <div>
+                    <div class="text-right">
                         <p class="no-m">{{ __('Numéro de commande') }}</p>
-                        <p class="no-m text-navy">{{ $item['id'] }}</p>
+                        <p class="no-m text-navy">{{ $item['id'] }}</p>
                     </div>
                 </div>
             @endif
@@ -33,7 +33,7 @@
                             @isset($product['basic_selling_price'])
                                 @if(floatval($product['basic_selling_price']))
                                     <p class="no-m">{{ __('Prix unitaire') }} <span class="text-navy">{{
-                                                        $product['basic_selling_price'] }} € HT</span>
+                                        $product['basic_selling_price'] }} € HT</span>
                                     </p>
                                 @endif
                             @endisset
             @endforeach
         </div>
         <div class="cartsave-footer <?php echo $item['total'] ? 'pt-6' : 'pt-12' ?>">
-            <div class=<?php echo $page !== "order" ?: "flex justify-between" ?>>
+            <div class="<?php echo $page !== 'order' ?: 'flex justify-between' ?>">
                 @if($page === "order")
                     <div>
                         <p class="no-m">{{ __('Statut') }}</p>
-                        <p class="no-m text-navy">{{ $item['status'] }}</p>
+                        <p class="no-m text-navy">
+                            {{ \App\Models\Command::getOptionValue($item['status']) }}
+                            @if($item['complete_status'])
+                                &#183;&nbsp;{{ $item['complete_status'] }}
+                            @endif
+                        </p>
                     </div>
                 @endif
                 @if($item['total'])
index b86c14d84b379feba2ec5c3b0d9bf9bc38aab930..074871ebf284e6806c770b8f9738abc1f443bb41 100644 (file)
@@ -17,7 +17,7 @@
                 <span>{{ __('Mes commandes') }}</span>
             </a>
         </li>
-        <li class="{{ Request::is('panier-enregistré') ? "active" : '' }}">
+        <li class="{{ Request::is('paniers-enregistres') ? "active" : '' }}">
             <a href="/paniers-enregistres">
                 <span>{{ __('Panier enregistrés') }}</span>
             </a>