]> _ Git - pmi.git/commitdiff
wip #5788 @4:00 affichage dynamique des tarifs selon le changement de remise, réactiv...
authorsoufiane <soufiane@cubedesigners.com>
Fri, 9 Jun 2023 15:53:17 +0000 (17:53 +0200)
committersoufiane <soufiane@cubedesigners.com>
Fri, 9 Jun 2023 15:53:17 +0000 (17:53 +0200)
app/Http/Controllers/AjaxController.php
app/Models/CommandPanierSchema.php
app/Models/Panier.php
app/Models/Product.php
app/Providers/AppServiceProvider.php
resources/js/app.js
resources/js/components/CartItem.vue
resources/views/components/item-cart-order.blade.php [new file with mode: 0644]
resources/views/components/item-cart.blade.php
resources/views/pages/my_order.blade.php

index ed7eb055b14c481eaca81bc76981d028d072b7b1..2fce0c447adc0011e669038c4009a2388af08a83 100644 (file)
@@ -3,6 +3,7 @@
 namespace App\Http\Controllers;
 
 use App\Models\Command;
+use App\Models\CommandPanierSchema;
 use App\Models\Panier;
 use Cubist\Backpack\Facades\App;
 use App\Models\Page;
@@ -587,10 +588,16 @@ class AjaxController extends CubistFrontController
         $currentCartRef = Panier::getRefs($data['products']);
         $intersect = $lastCartRefs ? array_intersect($currentCartRef,$lastCartRefs) : [];
 
+        $data['products'] = json_encode($data['products']);
+        $ids = Product::getProductIds($data);
+        $productsInfosBase = Product::getProductsByIds($ids);
+        $currentCartRef = Panier::getRefs(json_decode($data['products'],true));
+        $data['products'] = json_decode($data['products'], true);
+
         /**
          *
          */
-        $data = Panier::fillData($currentCartRef, $data, false);
+        $data = Panier::fillData($currentCartRef, $data, $productsInfosBase,false);
 
         if(sizeof($lastCartRefs) !== sizeof($intersect) ||
             (sizeof($lastCartRefs) === sizeof($intersect) && sizeof($currentCartRef) != sizeof($lastCartRefs)) ) {
@@ -641,12 +648,20 @@ class AjaxController extends CubistFrontController
         $cartId = $request->input('id');
 
         $cart = Panier::find($cartId)->toArray();
+        $ids = Product::getProductIds($cart);
+        $productsInfosBase = Product::getProductsByIds($ids);
 
         $clientIdInRequest = intval($cart['user_id']);
         $clientId = Client::getClientConnected()->id;
 
+        $categoryClient = Client::getClientConnected()->discount ?? 'A';
+        $currentCartRef = Panier::getRefs(json_decode($cart['products'],true));
+
+        $cart['products'] = json_decode($cart['products'], true);
+        $data = Panier::test($currentCartRef,$categoryClient,$cart,$productsInfosBase);
+
         if($clientIdInRequest === $clientId) {
-            $cartData = json_decode($cart['products'],true);
+            $cartData = $data['products'];
 
             // Clear cart session
             $request->session()->forget('cart_items');
@@ -666,8 +681,12 @@ class AjaxController extends CubistFrontController
         ];
         $data = $this->validation_form($request, $validation);
 
-        $currentCartRef = Panier::getRefs($data['products']);
-        $data = Command::fillData($currentCartRef, $data);
+        $data['products'] = json_encode($data['products']);
+        $ids = Product::getProductIds($data);
+        $productsInfosBase = Product::getProductsByIds($ids);
+        $currentCartRef = Panier::getRefs(json_decode($data['products'],true));
+        $data['products'] = json_decode($data['products'], true);
+        $data = Command::fillData($currentCartRef, $data,$productsInfosBase);
 
 //        dd($data);
 
@@ -684,26 +703,23 @@ class AjaxController extends CubistFrontController
         return __('Votre commande a bien été validée !');
     }
 
-    public function getdiscount($request) {
-        /*$id = $request->id;
+    public function updateCart($request) {
+        $id = $request->id;
 
-        $product = Product::find($id);
-        $products = $request->session()->get('cart_items');
-        $discount = Client::discountArray($product->category);
-        $needs_update = true;
-
-        $cartItems_ = '';
+        $cart = Product::getCartData();
+        $data['products'] = json_encode($cart);
+        $ids = Product::getProductIds($data);
+        $productsInfosBase = Product::getProductsByIds($ids);
+        $currentCartRef = Panier::getRefs(json_decode($data['products'],true));
+        $data['products'] = json_decode($data['products'], true);
+        $data = Panier::fillData($currentCartRef, $data, $productsInfosBase,false);
+        $update = json_decode($data['products'],true);
 
-        if($products) {
-            $cartItems_ = array_map(function($value) use ($discount) {
-                $value['discount'] = $discount;
-                return $value;
-            }, $products);
-        }
+        $request->session()->forget('cart_items');
+        $request->session()->put('cart_items', $update);
 
-        $request->session()->flash('cart_items', $cartItems_);
-        $request->session()->put('cart_items', $cartItems_);
+        $needs_update = true;
 
-        return ['needs_update' => $needs_update, 'cart_data' => $cartItems_];*/
+        return ['needs_update' => $needs_update, 'cart_data' => $update];
     }
 }
index 4d9a9fb4c10492c299083b87b387b2c77f37a4a0..8471757d77d9f9d69231f9c7b501d8020f96bf9c 100644 (file)
@@ -31,28 +31,13 @@ class CommandPanierSchema extends CubistMagicAbstractModel
         ]
     ];
 
+
     protected function getAllByUserId() {
         $userID = Client::getClientConnected()->id;
-        $items = self::all()->where('user_id', $userID)->toArray();
-        $allProducts = array_map(function($n){ return json_decode($n['products'], true); }, $items);
-        $idsProducts = array_column(call_user_func_array('array_merge', $allProducts),'id');
-
-        $categoryClient = Client::getClientConnected()->discount ?? 'A';
-
-        $productsInfosBase = Product::getProductsByIds($idsProducts);
-
-        foreach ($allProducts as $key => $products){
-            $data['products'] = $products;
-            $currentCartRef = Panier::getRefs($data['products']);
-            $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase);
-            $items[$key]['products'] = json_encode($data['products']);
-            $items[$key]['total'] = $data['total'];
-        }
-
-        return $items;
+        return self::all()->where('user_id', $userID)->toArray();
     }
 
-    protected static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){
+    public static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){
         $opt = [];
         $total = [];
 
@@ -87,8 +72,8 @@ class CommandPanierSchema extends CubistMagicAbstractModel
             $data['products'][$keyRefs]["basic_selling_price"] = $basicSellingPrice;
             $discount = floatval($data['products'][$keyRefs]["discount"]) ?? 0;
             $price = floatval($basicSellingPrice + $totalOptionsPrice);
-            $price = ($price - (($price * $discount) / 100));
-            $total[] = str_replace(',','.',$price * $quantity);
+            $priceDiscount = ($price - (($price * $discount) / 100));
+            $total[] = str_replace(',','.',$priceDiscount * $quantity);
             $data['products'][$keyRefs]["price"] = $price;
         }
 
@@ -105,13 +90,13 @@ class CommandPanierSchema extends CubistMagicAbstractModel
         return $data;
     }
 
-    public static function fillData($currentCartRef, $data, $haveStatus = true) {
+    public static function fillData($currentCartRef, $data, $productsInfosBase, $haveStatus = true) {
         /**
          *
          */
         $categoryClient = Client::getClientConnected()->discount ?? 'A';
 
-        $data = self::test($currentCartRef,$categoryClient,$data);
+        $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase);
 
         //
         if(isset($data['addresses']))
index 80e527284c1ab2007bc1fc3c8f1ead91941fc680..911ed5a3871e087c5f946fe87b9b8a0462573304 100644 (file)
@@ -26,6 +26,25 @@ class Panier extends CommandPanierSchema
         ]);
     }
 
+    protected function getAllByUserId()
+    {
+        $items = parent::getAllByUserId();
+        $allProducts = array_map(function($n){ return json_decode($n['products'], true); }, $items);
+
+        $idsProducts = Product::getProductIds($items);
+        $categoryClient = Client::getClientConnected()->discount ?? 'A';
+        $productsInfosBase = Product::getProductsByIds($idsProducts);
+
+        foreach ($allProducts as $key => $products){
+            $data['products'] = $products;
+            $currentCartRef = Panier::getRefs($data['products']);
+            $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase);
+            $items[$key]['products'] = json_encode($data['products']);
+            $items[$key]['total'] = $data['total'];
+        }
+        return $items;
+    }
+
     public static function getRefs($products) {
         $refs = array_map(function ($n) {
             return $n["ref"] ?? $n["reference"];
index 2eff1880c149b34e2831cc83ce3784b562d15432..8262561770d4a7c0e50daf2e94fa85ade0b7305e 100644 (file)
@@ -485,7 +485,6 @@ class Product extends CubistMagicPageModel
 
             $cart_items_id = array_map(function($n){ return $n['id']; }, $cart_items);
 
-
             if (count($cart_items) > 0) {
                 $productsSellingBasicPrice = self::whereIn('id', $cart_items_id)
                     ->select('id','basic_selling_price')
@@ -512,6 +511,19 @@ class Product extends CubistMagicPageModel
             ->toArray();
     }
 
+    public static function getProductIds($data) {
+        if(isset($data['products'])){
+            $allProducts = json_decode($data['products'], true);
+            $ids = array_column($allProducts, 'id');
+        }else{
+            $allProducts = array_map(function ($n) {
+                return json_decode($n['products'], true);
+            }, $data);
+            $ids = array_column(call_user_func_array('array_merge', $allProducts),'id');
+        }
+        return $ids;
+    }
+
     public static function getFilteredProducts($product_type, $filter_values = [])
     {
 
index 114551df77aa16687f9e92e2581d23c8ed7c7d2b..c05ccd344f4514863febb9a47efbe4794f94c79a 100644 (file)
@@ -61,6 +61,7 @@ class AppServiceProvider extends ServiceProvider
             BladeX::component('components.modal-confirm'); // <modal-confirm> ... </modal-confirm>
             BladeX::component('components.btn-delete'); // <btn-delete> ... </btn-delete>
             BladeX::component('components.item-cart'); // <item-cart> ... </item-cart>
+            BladeX::component('components.item-cart-order'); // <item-cart-order> ... </item-cart-order>
         } catch (\Exception $e) {
 
         }
index 82f99a5639416967163faa8754b04781c3755622..64470036ea133a09e2bad94611ed52489cdc7883 100644 (file)
@@ -506,8 +506,8 @@ const app = new Vue({
 
             axios.post('/ajax/storecart', data)
                 .then(function (response) {
-                    //root.validateAction = true
-                    //root.confirmMessageCart = response.data;
+                    root.validateAction = true
+                    root.confirmMessageCart = response.data;
                 })
                 .catch(function (error) {
                     console.log(error)
@@ -577,10 +577,10 @@ const app = new Vue({
                 .catch(function (error) {
                 })
         },
-        getDiscount(id) {
+        updateCart(id) {
             let root = this
 
-            axios.post('/ajax/getdiscount', id)
+            axios.post('/ajax/updateCart', id)
                 .then(function (response) {
                     if (response.data.needs_update) {
                         root.items = response.data.cart_data;
index 131d257b4099bd92cd9e716002c8429651ea8863..eda3b2b99bbf7697152c9e38614efba8e2ff33ed 100644 (file)
@@ -49,7 +49,6 @@
         },
 
         mounted() {
-            console.log(this.item)
             eventBus.$emit('send-id', {
                 id: this.item.id
             });
diff --git a/resources/views/components/item-cart-order.blade.php b/resources/views/components/item-cart-order.blade.php
new file mode 100644 (file)
index 0000000..bfe65e4
--- /dev/null
@@ -0,0 +1,80 @@
+@foreach($items as $item)
+    <div id="cart-saved-{{ $item['id'] }}" class="cartsave bg-grey-200 pr-2v pl-2v">
+        <div class="cartsave-name">
+            <div class="flex justify-between">
+                <div>
+                    <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 class="text-right">
+                    <p class="no-m">{{ __('Numéro de commande') }}</p>
+                    <p class="no-m text-navy">N°{{ $item['id'] }}</p>
+                </div>
+            </div>
+        </div>
+        <div class="cartsave-products">
+            @foreach(json_decode($item['products'], true) as $key => $product)
+                <grid gap="md" class="sm:grid-cols-1 cartsave-grid">
+                    <div class="product-thumbnail bg-center bg-contain bg-no-repeat" style='background-image : url("{{ $product['image'] }}")'>
+                    </div>
+                    <div class="product-infos flex justify-between items-center">
+                        <div>
+                            <p class="no-m">{{ $product['ref'] }}</p>
+                            <p class="no-m text-navy">{{ $product['name'] }}</p>
+                            @if(isset($product['discount']) && $product['discount'] !== 0)
+                                @if(floatval($product['basic_selling_price']))
+                                    <p class="no-m">{{ __('Prix catalogue') }} : <span class="text-navy line-through">{{
+                                        $product['basic_selling_price'] }} € HT</span>
+                                    </p>
+                                    <p class="no-m">{{ __('Votre prix') }} : <span class="text-navy">{{
+                                        number_format($product['basic_selling_price'] - (($product['basic_selling_price'] * $product['discount']) / 100), 2) }} € HT</span>
+                                    </p>
+                                @endif
+                            @else
+                                @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>
+                                        </p>
+                                    @endif
+                                @endisset
+                            @endif
+                            <p class="no-m">{{ __('Quantité') }} : <span class="text-navy">{{$product['quantity']}}</span></p>
+                        </div>
+                        <div class="product-price">
+                            @if(floatval($product['price']))
+                                <p class="no-m">{{ number_format($product['price'] * $product['quantity'], 2) }}€ HT</p>
+                            @else
+                                <p class="text-navy">
+                                    N/A
+                                    <span
+                                        data-tooltip="{{__('Prix sur devis.')}}"
+                                        class="font-display text-lg inline-block align-middle rounded-full border-grey-dark border-2 h-8 w-8 text-center ml-3">?</span>
+                                </p>
+                            @endif
+                        </div>
+                    </div>
+                </grid>
+            @endforeach
+        </div>
+        <div class="cartsave-footer <?php echo $item['total'] ? 'pt-6' : 'pt-12' ?>">
+            <div class="flex justify-between">
+                <div>
+                    <p class="no-m">{{ __('Statut') }}</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>
+                @if($item['total'])
+                    <div class="text-right pb-6">
+                        <p class="no-m">{{ __('Montant Total') }}</p>
+                        <p class="no-m">{{ $item['total'] }}€ HT</p>
+                    </div>
+                @endif
+            </div>
+        </div>
+    </div>
+@endforeach
index 65d4f6160e123222c98ab23311bc322107ae36d6..510fbbce4758faf2c802f5927d1d15aa6726ee08 100644 (file)
@@ -36,7 +36,7 @@
                                         $product['basic_selling_price'] }} € HT</span>
                                     </p>
                                     <p class="no-m">{{ __('Votre prix') }} : <span class="text-navy">{{
-                                        $product['basic_selling_price'] - (($product['basic_selling_price'] * $product['discount']) / 100) }} € HT</span>
+                                        number_format($product['basic_selling_price'] - (($product['basic_selling_price'] * $product['discount']) / 100), 2) }} € HT</span>
                                     </p>
                                 @endif
                             @else
@@ -52,7 +52,8 @@
                         </div>
                         <div class="product-price">
                             @if(floatval($product['price']))
-                                <p class="no-m">{{ number_format(($product['price'] * $product['quantity']), 2) }}€ HT</p>
+                                @php($price = $product['price'] - (($product['price'] * $product['discount']) / 100))
+                                <p class="no-m">{{ number_format($product['price'] * $product['quantity'], 2) }}€ HT</p>
                             @else
                                 <p class="text-navy">
                                     N/A
index 22192ceadf475a10a9384b27028b0dae73e44a29..b44dc47c067d54e4fa5d1e155634ba869e4544f0 100644 (file)
@@ -4,7 +4,7 @@
     <full-width padding="pb-3v">
         <content>
             <text-block :title="$page->title" title-tag="h1" />
-            <item-cart :items="$items" page="order"></item-cart>
+            <item-cart-order :items="$items" page="order"></item-cart-order>
         </content>
     </full-width>
 @endsection