]> _ Git - pmi.git/commitdiff
wip #5788 @3:00 refacto, responsive
authorsoufiane <soufiane@cubedesigners.com>
Mon, 12 Jun 2023 12:36:45 +0000 (14:36 +0200)
committersoufiane <soufiane@cubedesigners.com>
Mon, 12 Jun 2023 12:36:45 +0000 (14:36 +0200)
app/Http/Controllers/AjaxController.php
app/Models/Client.php
app/Models/CommandPanierSchema.php
app/Models/Panier.php
app/Models/Product.php
app/Templates/MyOrder.php
resources/styles/components/my-account.styl
resources/views/layouts/app.blade.php
resources/views/pages/my_account.blade.php
resources/views/pages/product-detail.blade.php
resources/views/partials/product-link.blade.php

index 2fce0c447adc0011e669038c4009a2388af08a83..14e164bd2f6b30f188e376df5ba479f6915e7e6a 100644 (file)
@@ -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);
index 804c10d20f267c37f7d367b9748c02b45941242c..34b59600c183f5628d769cc720e9a2a518affcff 100644 (file)
@@ -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,
index 8471757d77d9f9d69231f9c7b501d8020f96bf9c..e9455a4b9cfa621b8600b6905162d97143c4f24a 100644 (file)
@@ -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;
     }
 }
index 911ed5a3871e087c5f946fe87b9b8a0462573304..abe060e7ac954a8cfaa5ecae3a5f123814ee5917 100644 (file)
@@ -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'];
         }
index 8262561770d4a7c0e50daf2e94fa85ade0b7305e..66f54b2be64551bd7fdc9cea4e227fcefa625063 100644 (file)
@@ -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{
index c045c9633fa6434ae763c128e5bf86caa45d9a9b..99a4efa742b1fa4414c624ce8f83d7faccfa727e 100644 (file)
@@ -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
 {
index 8b381407915b9e8b3cf0fe1aef4e7a58d34579ae..933cca32f3fbececeb855e3108f525ea5be568ff 100644 (file)
@@ -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
index da989a7d886677e76ed6c7dbe7083ff604b765aa..c66cbe341fc8e9b3d6f9620ca40e59c34b502ec8 100644 (file)
@@ -71,7 +71,9 @@
     @section('breadcrumbs')
         <full-width padding="pt-1v pb-1v">
             <content>
-                {!! CubistMenu::breadcrumbs() !!}
+                <div class="xs:hidden">
+                    {!! CubistMenu::breadcrumbs() !!}
+                </div>
             </content>
         </full-width>
     @show
index 24f52b7f2a40d7d83b71b15725d38106086c6783..5790395410a21c94a7a1393b373a3a8c377c1c37 100644 (file)
@@ -9,12 +9,13 @@
         <content>
             <div class="account">
                 <text-block :title="$page->title" title-tag="h1" />
-
-                <ul class="account-nav flex items-center pl-2v">
-                    <li @click="tab = 'infos'; activeTab('infos')" class="text-lg cursor-pointer" :class="{ active: tab === 'infos' }">{{ __('Mes coordonnées') }}</li>
-                    <li @click="tab = 'address'; activeTab('address')" class="text-lg cursor-pointer" :class="{ active: tab === 'address' }">{{ __('Mes adresses') }}</li>
-                    <li @click="tab = 'pw'; activeTab('pw')" class="text-lg cursor-pointer" :class="{ active: tab === 'pw' }">{{ __('Modifier mon mot de passe') }}</li>
-                </ul>
+                <div class="account-nav-container">
+                    <ul class="account-nav flex items-center pl-2v">
+                        <li @click="tab = 'infos'; activeTab('infos')" class="text-lg cursor-pointer" :class="{ active: tab === 'infos' }">{{ __('Mes coordonnées') }}</li>
+                        <li @click="tab = 'address'; activeTab('address')" class="text-lg cursor-pointer" :class="{ active: tab === 'address' }">{{ __('Mes adresses') }}</li>
+                        <li @click="tab = 'pw'; activeTab('pw')" class="text-lg cursor-pointer" :class="{ active: tab === 'pw' }">{{ __('Modifier mon mot de passe') }}</li>
+                    </ul>
+                </div>
 
                 <div class="account-container-content bg-grey-200">
                     <div id="account-infos" v-cloak v-if="tab === 'infos'">
@@ -73,9 +74,9 @@
                     </div>
 
                     <div id="account-address" class="account-address" v-cloak v-if="tab === 'address'">
-                        <div class="account-address-header flex items-center justify-between border-b border-b-light-b relative">
-                            <h2 class="text-2xl m-0">{{ __('Mes adresses') }}</h2>
-                            <button class="btn btn-custom xs:w-full flex items-center absolute right-0" @click.prevent="addAddressToForm">
+                        <div class="account-address-header sm:flex-wrap flex items-center justify-between border-b border-b-light-b relative">
+                            <h2 class="text-2xl m-0 sm:mb-6">{{ __('Mes adresses') }}</h2>
+                            <button class="btn btn-custom xs:w-full flex items-center absolute right-0 sm:relative" @click.prevent="addAddressToForm">
                                 <span class="pr-2">{{ __('Ajouter une adresse') }}</span>
                                 @svg('icon-white-add')
                             </button>
index 1a4d2b06e3d426ef6391688bc87f56810607cc6c..5aa24245f8e0a2f0061cb5e04bdb591b0263cb01 100644 (file)
@@ -8,7 +8,7 @@
         $options = true;
     }
 
-    $discount = \App\Models\Client::discountArray($categoryProduct);
+    $discount = \App\Models\Client::getDiscount($categoryProduct);
 @endphp
 
 @extends('layouts/app')
index f8c3c15c3579b8b0feafefdb4977e3e78eabeef7..d9b48cb3495c7f1b1f5fe56748bbc1167704dc5a 100644 (file)
@@ -4,7 +4,7 @@
     $options = false;
 
     $categoryProduct = $product->category;
-    $discount = \App\Models\Client::discountArray($categoryProduct);
+    $discount = \App\Models\Client::getDiscount($categoryProduct);
 
     $price = floatval(number_format($product->basic_selling_price - (($product->basic_selling_price * $discount) / 100), 2));
     if($price < 1)