]> _ Git - pmi.git/commitdiff
wip #5788 @2:00 affichage dynamique des tarifs selon le changement de remise, réactiv...
authorsoufiane <soufiane@cubedesigners.com>
Fri, 9 Jun 2023 10:37:14 +0000 (12:37 +0200)
committersoufiane <soufiane@cubedesigners.com>
Fri, 9 Jun 2023 10:37:14 +0000 (12:37 +0200)
app/Models/CommandPanierSchema.php
app/Models/Product.php
resources/views/components/item-cart.blade.php

index a127cc7da8bf70a5836ec746697b8205bb4a3dda..4d9a9fb4c10492c299083b87b387b2c77f37a4a0 100644 (file)
@@ -35,40 +35,44 @@ class CommandPanierSchema extends CubistMagicAbstractModel
         $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');
 
-        $categories = Product::getCategoriesByProductsId($allProducts);
-        $options = Product::getOptionsByProductsId($allProducts);
         $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,$options,$categories,$categoryClient,$data);
-            dd($data);
+            $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase);
             $items[$key]['products'] = json_encode($data['products']);
+            $items[$key]['total'] = $data['total'];
         }
 
         return $items;
     }
 
-    protected static function test($currentCartRef,$options,$categories,$categoryClient,$data = []){
+    protected static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){
         $opt = [];
         $total = [];
+
         foreach ($currentCartRef as $keyRefs => $refs) {
-            foreach (explode("/", $refs) as $key => $ref) {
-                $key -= 1;
-                if ($key > -1) {
-                    $optByProductId = array_column($options, $data['products'][$keyRefs]['id'])[0][0];
-                    $optByProductId = json_decode($optByProductId, true);
-                    $opt_ = array_filter($optByProductId[$keyRefs]["options"], function ($n) use ($ref) {
-                        return $n["ref"] === $ref;
-                    });
-                    $opt_ = array_values($opt_);
-                    $opt[$refs][] = $opt_[0]["sale_price"] ?? 0;
+            if($productsInfosBase[$data['products'][$keyRefs]['id']][0]['json']) {
+                $options = json_decode($productsInfosBase[$data['products'][$keyRefs]['id']][0]['json'], true);
+                foreach (explode("/", $refs) as $key => $ref) {
+                    $key -= 1;
+                    if ($key > -1) {
+                        $options_ = $options[$key]['options'];
+                        foreach ($options_ as $opt_) {
+                            if ($opt_["ref"] === $ref) {
+                                $opt[$refs][] = $opt_["sale_price"] ?? 0;
+                            }
+                        }
+                    }
                 }
             }
 
-            $cat = array_column($categories, $data['products'][$keyRefs]['id'])['category'] ?? 'A';
+            $cat = $productsInfosBase[$data['products'][$keyRefs]['id']][0]['category'];
             $discount = Client::discountArray($cat,$categoryClient);
             $data['products'][$keyRefs]['discount'] = $discount;
 
@@ -79,9 +83,10 @@ class CommandPanierSchema extends CubistMagicAbstractModel
                 return $carry + $item;
             }) : 0;
 
-            $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"];
+            $basicSellingPrice = $productsInfosBase[$data['products'][$keyRefs]['id']][0]['basic_selling_price'] ?? 0;
+            $data['products'][$keyRefs]["basic_selling_price"] = $basicSellingPrice;
             $discount = floatval($data['products'][$keyRefs]["discount"]) ?? 0;
-            $price = floatval($basicPriceByProductId + $totalOptionsPrice);
+            $price = floatval($basicSellingPrice + $totalOptionsPrice);
             $price = ($price - (($price * $discount) / 100));
             $total[] = str_replace(',','.',$price * $quantity);
             $data['products'][$keyRefs]["price"] = $price;
@@ -104,11 +109,9 @@ class CommandPanierSchema extends CubistMagicAbstractModel
         /**
          *
          */
-        $categories = Product::getCategoriesByProductsId($data['products']);
-        $options = Product::getOptionsByProductsId($data['products']);
         $categoryClient = Client::getClientConnected()->discount ?? 'A';
 
-        $data = self::test($currentCartRef, $options,$categories,$categoryClient,$data);
+        $data = self::test($currentCartRef,$categoryClient,$data);
 
         //
         if(isset($data['addresses']))
index e15fd0991ecf9fea6ad02a5e2e657690cb4d2c0c..2eff1880c149b34e2831cc83ce3784b562d15432 100644 (file)
@@ -474,12 +474,14 @@ class Product extends CubistMagicPageModel
      * Fetch selected product data for use in cart Vue component
      * @return array
      */
-    public static function getCartData()
+    public static function getCartData($cart_items = [])
     {
-        if (!self::$_cart_data) {
+        if (!self::$_cart_data && isset($cart_items)) {
 
-            $cart_items = session('cart_items', []);
-            self::$_cart_data = [];
+            if(sizeof($cart_items) < 1) {
+                $cart_items = session('cart_items', []);
+                self::$_cart_data = [];
+            }
 
             $cart_items_id = array_map(function($n){ return $n['id']; }, $cart_items);
 
@@ -502,54 +504,12 @@ class Product extends CubistMagicPageModel
         return self::$_cart_data;
     }
 
-    public static function getOptionsByProductsId($products) {
-        $options = collect($products)->map(function ($product) {
-            return collect($product)->map(function ($pro) {
-                return Product::where('id', $pro['id'])
-                    ->select('id', 'json')
-                    ->get()
-                    ->groupBy('id')
-                    ->map(function ($group) {
-                        return $group->pluck('json')->toArray();
-                    })
-                    ->toArray();
-            });
-        })->toArray();
-
-        $_arr = call_user_func_array('array_merge',$options);
-
-        return $_arr;
-
-        /*return array_map(function($n) {
-                $key = array_keys($n)[0];
-                return json_decode($n[$key][0], true);
-            }, $_arr
-        );*/
-
-        /*return array_map(function($n) use($options) {
-            return json_decode($options[$n['id']][0],true);
-            }, $products
-        );*/
-    }
-
-    public static function getCategoriesByProductsId($products) {
-        $cat = collect($products)->map(function ($product) {
-            return collect($product)->map(function ($pro) {
-                return Product::where('id', $pro['id'])
-                    ->select('id', 'category')
-                    ->get()
-                    ->groupBy('id')
-                    ->toArray();
-            });
-        })->toArray();
-
-        $_arr = call_user_func_array('array_merge',$cat);
-
-        return array_map(function($n) {
-                $key = array_keys($n)[0];
-                return $n[$key][0];
-            }, $_arr
-        );
+    public static function getProductsByIds($ids) {
+        return self::whereIn('id', $ids)
+            ->select('id','basic_selling_price','category','json')
+            ->get()
+            ->groupBy('id')
+            ->toArray();
     }
 
     public static function getFilteredProducts($product_type, $filter_values = [])
index 804d786121f1318f78823c9ab2aa4868a4f60891..65d4f6160e123222c98ab23311bc322107ae36d6 100644 (file)
@@ -52,7 +52,7 @@
                         </div>
                         <div class="product-price">
                             @if(floatval($product['price']))
-                                <p class="no-m">{{ number_format($product['price'] * $product['quantity'], 2) }}€ HT</p>
+                                <p class="no-m">{{ number_format(($product['price'] * $product['quantity']), 2) }}€ HT</p>
                             @else
                                 <p class="text-navy">
                                     N/A