From 5f390f3d25ba039c044890d927be5a80a7cd1a29 Mon Sep 17 00:00:00 2001 From: soufiane Date: Mon, 12 Jun 2023 14:36:45 +0200 Subject: [PATCH] wip #5788 @3:00 refacto, responsive --- app/Http/Controllers/AjaxController.php | 19 ++++--- app/Models/Client.php | 10 ++-- app/Models/CommandPanierSchema.php | 52 +++++++++---------- app/Models/Panier.php | 5 +- app/Models/Product.php | 2 +- app/Templates/MyOrder.php | 2 - resources/styles/components/my-account.styl | 15 +++++- resources/views/layouts/app.blade.php | 4 +- resources/views/pages/my_account.blade.php | 19 +++---- .../views/pages/product-detail.blade.php | 2 +- .../views/partials/product-link.blade.php | 2 +- 11 files changed, 73 insertions(+), 59 deletions(-) diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 2fce0c4..14e164b 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -176,7 +176,7 @@ class AjaxController extends CubistFrontController $currentItem = false; if($product) { $categoryProduct = $product->category; - $discount = \App\Models\Client::discountArray($categoryProduct); + $discount = \App\Models\Client::getDiscount($categoryProduct); $currentItem = array_filter($cart_items, function ($n) use ($ref) { return $n['ref'] === $ref; }); @@ -653,12 +653,10 @@ class AjaxController extends CubistFrontController $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); + $data = Panier::updateCartData($currentCartRef,$cart,$productsInfosBase); if($clientIdInRequest === $clientId) { $cartData = $data['products']; @@ -671,6 +669,8 @@ class AjaxController extends CubistFrontController $request->session()->put('last_selection', intval($cartId)); return "success"; } + + return false; } public function order(Request $request) { @@ -683,18 +683,18 @@ class AjaxController extends CubistFrontController $data['products'] = json_encode($data['products']); $ids = Product::getProductIds($data); + $data['products'] = json_decode($data['products'], true); + $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); + $data = Command::fillData($currentCartRef,$data,$productsInfosBase); if($data) { $order = Command::create($data); $order->save(); }else{ - abort('422', 'Data are empty'); + abort('422', 'Datas are empty'); } // Clear cart session @@ -704,9 +704,8 @@ class AjaxController extends CubistFrontController } public function updateCart($request) { - $id = $request->id; - $cart = Product::getCartData(); + $data['products'] = json_encode($cart); $ids = Product::getProductIds($data); $productsInfosBase = Product::getProductsByIds($ids); diff --git a/app/Models/Client.php b/app/Models/Client.php index 804c10d..34b5960 100644 --- a/app/Models/Client.php +++ b/app/Models/Client.php @@ -105,10 +105,14 @@ class Client extends CubistMagicAbstractModel return Auth::guard('web-clients')->user(); } - public static function discountArray($categoryProduct, $categoryClient = false) { + public static function getCategory() { + return self::getClientConnected()->discount ?? 'A'; + } + + public static function getDiscount($categoryProduct, $categoryClient = false) { if(!$categoryClient) - $categoryClient = self::getClientConnected()->discount ?? 'A'; - //dd(self::getClientConnected()); + $categoryClient = self::getCategory(); + $category_array = [ 'A' => 0, 'B' => 5, diff --git a/app/Models/CommandPanierSchema.php b/app/Models/CommandPanierSchema.php index 8471757..e9455a4 100644 --- a/app/Models/CommandPanierSchema.php +++ b/app/Models/CommandPanierSchema.php @@ -37,13 +37,21 @@ class CommandPanierSchema extends CubistMagicAbstractModel return self::all()->where('user_id', $userID)->toArray(); } - public static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){ + public static function updateCartData($currentCartRef,$data = [], $productsInfosBase = []) { $opt = []; $total = []; + /* + * + * Recalculate option prices + * and store the result in an array like + * ["first_ref" => [5,30,45,...], "second_ref" => [...], ... ] + * + */ foreach ($currentCartRef as $keyRefs => $refs) { - if($productsInfosBase[$data['products'][$keyRefs]['id']][0]['json']) { - $options = json_decode($productsInfosBase[$data['products'][$keyRefs]['id']][0]['json'], true); + $index = $data['products'][$keyRefs]['id']; + if($productsInfosBase[$index][0]['json']) { + $options = json_decode($productsInfosBase[$index][0]['json'], true); foreach (explode("/", $refs) as $key => $ref) { $key -= 1; if ($key > -1) { @@ -57,56 +65,48 @@ class CommandPanierSchema extends CubistMagicAbstractModel } } - $cat = $productsInfosBase[$data['products'][$keyRefs]['id']][0]['category']; - $discount = Client::discountArray($cat,$categoryClient); - $data['products'][$keyRefs]['discount'] = $discount; - + $categoryProduct = $productsInfosBase[$index][0]['category']; + $discount = intval(Client::getDiscount($categoryProduct)); $quantity = intval($data['products'][$keyRefs]["quantity"]); - $basicSellingPrice = $data['products'][$keyRefs]["basic_selling_price"]; + $basicSellingPrice = $productsInfosBase[$index][0]['basic_selling_price'] ?? 0; - $totalOptionsPrice = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { + $totalOptionPrices = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { return $carry + $item; }) : 0; - $basicSellingPrice = $productsInfosBase[$data['products'][$keyRefs]['id']][0]['basic_selling_price'] ?? 0; + $price = floatval($basicSellingPrice + $totalOptionPrices); + $priceWithDiscount = ($price - (($price * $discount) / 100)); + $total[] = str_replace(',','.',$priceWithDiscount * $quantity); + + $data['products'][$keyRefs]['discount'] = $discount; $data['products'][$keyRefs]["basic_selling_price"] = $basicSellingPrice; - $discount = floatval($data['products'][$keyRefs]["discount"]) ?? 0; - $price = floatval($basicSellingPrice + $totalOptionsPrice); - $priceDiscount = ($price - (($price * $discount) / 100)); - $total[] = str_replace(',','.',$priceDiscount * $quantity); $data['products'][$keyRefs]["price"] = $price; } - $checkUnavailableEcommerceProduct = array_filter($data['products'], function($n) { - return !$n['price']; + $unavailableEcommerceProduct = array_filter($data['products'], function($n) { + return !$n['price'] || $n['price'] <= 0; }); $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 ? number_format($ttc,2) : null; + $data['total'] = !$unavailableEcommerceProduct ? number_format($ttc,2) : null; return $data; } public static function fillData($currentCartRef, $data, $productsInfosBase, $haveStatus = true) { - /** - * - */ - $categoryClient = Client::getClientConnected()->discount ?? 'A'; + $data = self::updateCartData($currentCartRef,$data,$productsInfosBase); - $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase); - - // if(isset($data['addresses'])) $data['addresses'] = json_encode($data['addresses']); - $data['products'] = json_encode($data['products']); - if($haveStatus) $data['status'] = 'new'; + $data['products'] = json_encode($data['products']); + return $data; } } diff --git a/app/Models/Panier.php b/app/Models/Panier.php index 911ed5a..abe060e 100644 --- a/app/Models/Panier.php +++ b/app/Models/Panier.php @@ -32,13 +32,12 @@ class Panier extends CommandPanierSchema $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); + $currentCartRef = self::getRefs($data['products']); + $data = self::updateCartData($currentCartRef,$data,$productsInfosBase); $items[$key]['products'] = json_encode($data['products']); $items[$key]['total'] = $data['total']; } diff --git a/app/Models/Product.php b/app/Models/Product.php index 8262561..66f54b2 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -512,7 +512,7 @@ class Product extends CubistMagicPageModel } public static function getProductIds($data) { - if(isset($data['products'])){ + if(isset($data['products'])){ //way to check if is multidimensional array $allProducts = json_decode($data['products'], true); $ids = array_column($allProducts, 'id'); }else{ diff --git a/app/Templates/MyOrder.php b/app/Templates/MyOrder.php index c045c96..99a4efa 100644 --- a/app/Templates/MyOrder.php +++ b/app/Templates/MyOrder.php @@ -4,8 +4,6 @@ namespace App\Templates; use App\Models\Command; -use App\Models\Panier; -use Cubist\Backpack\app\Magic\Menu\PageItem; class MyOrder extends Base { diff --git a/resources/styles/components/my-account.styl b/resources/styles/components/my-account.styl index 8b38140..933cca3 100644 --- a/resources/styles/components/my-account.styl +++ b/resources/styles/components/my-account.styl @@ -3,7 +3,15 @@ min-width: 281px !important right: 0 + &-nav-container + overflow-x: auto + -ms-overflow-style: none + scrollbar-width: none + &::-webkit-scrollbar + display: none + &-nav + width max-content li position: relative padding-bottom: 21px @@ -28,7 +36,9 @@ background-color: currentColor &-container-content - padding: 46px 96px + padding: 2.5rem + +above($breakpoint-columns) + padding: 46px 96px &-address, &-password @@ -40,7 +50,8 @@ padding-top: 0 !important &-address-header - padding-bottom: 46px + +above($breakpoint-columns) + padding-bottom 46px form label diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index da989a7..c66cbe3 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -71,7 +71,9 @@ @section('breadcrumbs') - {!! CubistMenu::breadcrumbs() !!} +
+ {!! CubistMenu::breadcrumbs() !!} +
@show diff --git a/resources/views/pages/my_account.blade.php b/resources/views/pages/my_account.blade.php index 24f52b7..5790395 100644 --- a/resources/views/pages/my_account.blade.php +++ b/resources/views/pages/my_account.blade.php @@ -9,12 +9,13 @@