From bf352df609b72cbc7e1a8c80bc1d3472f2591ea6 Mon Sep 17 00:00:00 2001 From: soufiane Date: Fri, 9 Jun 2023 12:37:14 +0200 Subject: [PATCH] =?utf8?q?wip=20#5788=20@2:00=20affichage=20dynamique=20de?= =?utf8?q?s=20tarifs=20selon=20le=20changement=20de=20remise,=20r=C3=A9act?= =?utf8?q?ivit=C3=A9=20du=20changement=20de=20donn=C3=A9es?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/Models/CommandPanierSchema.php | 45 +++++++------ app/Models/Product.php | 64 ++++--------------- .../views/components/item-cart.blade.php | 2 +- 3 files changed, 37 insertions(+), 74 deletions(-) diff --git a/app/Models/CommandPanierSchema.php b/app/Models/CommandPanierSchema.php index a127cc7..4d9a9fb 100644 --- a/app/Models/CommandPanierSchema.php +++ b/app/Models/CommandPanierSchema.php @@ -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'])) diff --git a/app/Models/Product.php b/app/Models/Product.php index e15fd09..2eff188 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -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 = []) diff --git a/resources/views/components/item-cart.blade.php b/resources/views/components/item-cart.blade.php index 804d786..65d4f61 100644 --- a/resources/views/components/item-cart.blade.php +++ b/resources/views/components/item-cart.blade.php @@ -52,7 +52,7 @@
@if(floatval($product['price'])) -

{{ number_format($product['price'] * $product['quantity'], 2) }}€ HT

+

{{ number_format(($product['price'] * $product['quantity']), 2) }}€ HT

@else

N/A -- 2.39.5