From: soufiane Date: Wed, 19 Apr 2023 08:12:49 +0000 (+0200) Subject: wip #5858 @14:00 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=a4c8c9065d772a97da6e8dafd44dcc2b5d8df20c;p=pmi.git wip #5858 @14:00 --- diff --git a/app/Http/Controllers/Admin/SelectionCrudController.php b/app/Http/Controllers/Admin/SelectionCrudController.php index 88745be..12b23ca 100644 --- a/app/Http/Controllers/Admin/SelectionCrudController.php +++ b/app/Http/Controllers/Admin/SelectionCrudController.php @@ -7,7 +7,7 @@ class SelectionCrudController extends \Cubist\Backpack\app\Magic\Controllers\Cub protected $_modelNamespace = 'App\Models\Panier'; protected $_routeURL = 'selection'; protected $_singular = 'Panier'; - protected $_plural = 'Paniers'; + protected $_plural = 'PaniersEnregistres'; protected $_clonable = true; protected $_bulk = true; protected $_oneInstance= false; diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 4ae3f73..9c71249 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -21,6 +21,7 @@ use Illuminate\Support\Facades\Mail; use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Hash; use Illuminate\Support\Facades\Auth; +use Illuminate\Support\Arr; class AjaxController extends CubistFrontController { @@ -566,17 +567,106 @@ class AjaxController extends CubistFrontController return false; } - public function storepanier(Request $request) { + public function storecart(Request $request) { $validation = [ 'name' => 'required|string|max:255', 'addresses' => 'required', 'products' => 'required', - 'userId' => 'required|numeric' + 'user_id' => 'required|numeric' ]; $data = $this->validation_form($request, $validation); - $panier = new Panier($data); - $panier->save(); - return 'ok'; + $lastCartId = $request->session()->get('last_selection'); + + // + $lastCartRefs = $lastCartId ? Panier::find($lastCartId)->getReferences() : []; + $currentCartRef = Panier::getRefs($data['products']); + $intersect = $lastCartRefs ? array_intersect($currentCartRef,$lastCartRefs) : []; + + /** + * + */ + $options = Product::getOptionsByProductsId($data['products']); + + $opt = []; + $total = []; + + foreach ($currentCartRef as $keyRefs => $refs) { + foreach (explode("/", $refs) as $key => $ref) { + $key -= 1; + if ($key > -1) { + $opt_ = array_filter($options[$keyRefs][$key]["options"], function ($n) use ($ref) { + return $n["ref"] === $ref; + }); + $opt_ = array_values($opt_); + $opt[$refs][] = $opt_[0]["sale_price"]; + } + } + + $quantity = intval($data['products'][$keyRefs]["quantity"]); + $basicSellingPrice = $data['products'][$keyRefs]["basic_selling_price"]; + + $totalOptionsPrice = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { + return $carry + $item; + }) : 0; + + $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"]; + $price = floatval(($basicPriceByProductId + $totalOptionsPrice) * $quantity); + $data['products'][$keyRefs]["price"] = $total[] = $price; + } + + $ht = array_reduce($total, function($carry, $item) { return $carry + $item; }); + $ht += ($total > 1000) ? 20 : 0; + $tva = $ht * 0.2; + $ttc = $ht + $tva; + $data['total'] = $ttc; + + // + $data['addresses'] = json_encode($data['addresses']); + $data['products'] = json_encode($data['products']); + + if(sizeof($lastCartRefs) !== sizeof($intersect) || + (sizeof($lastCartRefs) === sizeof($intersect) && sizeof($currentCartRef) != sizeof($lastCartRefs)) ) { + $panier = Panier::firstOrCreate($data); + $panier->save(); + // Save back to the session with the latest cart id + $id = $panier->getOriginal('id'); + $request->session()->put('last_selection', $id); + } else { + Panier::where('id', $lastCartRefs)->update($data); + } + + return __('Le panier a été enregistré avec succès !'); + } + + public function deleteSavedcart(Request $request) { + $request->validate([ + 'id' => 'required|numeric', + ]); + $id = $request->input('id'); + + $cart = Panier::find($id); + + $cart->delete(); + } + + public function updateNameSavedcart(Request $request) { + $request->validate([ + 'id' => 'required|numeric', + 'text' => 'required|max:255' + ]); + $id = $request->input('id'); + $text = $request->input('text'); + + $cart = Panier::find($id); + + $cart->name = $text; + + $cart->save(); + } + + public function savedCartToCurrent(Request $request) { + + //$this->cart() } } diff --git a/app/Http/Middleware/RedirectClientIfAuthenticated.php b/app/Http/Middleware/RedirectClientIfAuthenticated.php index fb9e042..ed3c1c3 100644 --- a/app/Http/Middleware/RedirectClientIfAuthenticated.php +++ b/app/Http/Middleware/RedirectClientIfAuthenticated.php @@ -18,9 +18,7 @@ class RedirectClientIfAuthenticated { if (($request->path() === "se-connecter") && Auth::guard('web-clients')->check()) { return redirect('/'); - } - - if (($request->path() === "mon-compte") && !Auth::guard('web-clients')->check()) { + }elseif($request->path() !== "se-connecter" && !Auth::guard('web-clients')->check()) { return redirect('/se-connecter'); } diff --git a/app/Models/CommandPanierSchema.php b/app/Models/CommandPanierSchema.php index 8299a22..de235d6 100644 --- a/app/Models/CommandPanierSchema.php +++ b/app/Models/CommandPanierSchema.php @@ -24,6 +24,10 @@ class CommandPanierSchema extends CubistMagicAbstractModel 'label' => 'Produits', 'type' => 'Text', 'column' => true + ], + [ + 'name' => 'total', + 'type' => 'Text' ] ]; } diff --git a/app/Models/Panier.php b/app/Models/Panier.php index 80d1dc1..76658ba 100644 --- a/app/Models/Panier.php +++ b/app/Models/Panier.php @@ -8,7 +8,7 @@ class Panier extends CommandPanierSchema protected $_options = ['name' => 'selection', 'singular' => 'Panier', - 'plural' => 'Paniers']; + 'plural' => 'PaniersEnregistres']; public function setFields() { @@ -24,4 +24,25 @@ class Panier extends CommandPanierSchema $this->addField($fields); } } + + public static function getRefs($products) { + $refs = array_map(function ($n) { + return $n["ref"] ?? $n["reference"]; + }, $products); + + return $refs; + } + + public function getReferences(){ + $panier = $this->toArray(); + $products = json_decode($panier['products'], true); + + $refs = self::getRefs($products); + + return $refs; + } + + public static function getAllCart() { + return Panier::all()->toArray(); + } } diff --git a/app/Models/Product.php b/app/Models/Product.php index c5f05f2..bde024d 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -480,14 +480,46 @@ class Product extends CubistMagicPageModel $cart_items = session('cart_items', []); self::$_cart_data = []; + $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') + ->get() + ->toArray(); + + foreach ($cart_items as $key => &$values) { + $id = $values["id"]; + $getCartItem = array_values(array_filter($productsSellingBasicPrice, function($n) use($id) { return $n['id'] === $id; })); + $values["basic_selling_price"] = floatval($getCartItem[0]["basic_selling_price"]) ?? 0; + } + self::$_cart_data = $cart_items; } } - return self::$_cart_data; } + public function getOptions() { + return json_decode($this['json'], true); + } + + public static function getOptionsByProductsId($products) { + $options = Product::whereIn('id', array_column($products, 'id')) + ->select('id','json') + ->get() + ->groupBy('id') + ->map(function ($group) { + return $group->pluck('json')->toArray(); + }) + ->toArray(); + + return array_map(function($n) use($options) { + return json_decode($options[$n['id']][0],true); + }, $products + ); + } public static function getFilteredProducts($product_type, $filter_values = []) { diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index b5cf465..be654ed 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -59,6 +59,7 @@ class AppServiceProvider extends ServiceProvider BladeX::component('components.news-item'); // ... BladeX::component('components.address-form'); // ... BladeX::component('components.modal-confirm'); // ... + BladeX::component('components.btn-delete'); // ... } catch (\Exception $e) { } diff --git a/app/Templates/PaniersEnregistres.php b/app/Templates/PaniersEnregistres.php new file mode 100644 index 0000000..1f21efb --- /dev/null +++ b/app/Templates/PaniersEnregistres.php @@ -0,0 +1,18 @@ + { data.action = 'add'; - data.ref = this.ref; + data.ref = this.ref ?? data.ref; data.price = this.price; this.saveCart(data); }); @@ -260,6 +262,19 @@ const app = new Vue({ } } }, + animateDelete(el, parent = null) { + el.animate([ + { opacity: 1 }, + { opacity: 0 } + ], + { + duration: 1000 + } + ); + setTimeout(() => { + parent ? el.parentElement.remove() : el.remove() + }, 1000) + }, checkEmailExist() { let root = this, data = { @@ -422,17 +437,7 @@ const app = new Vue({ if(response.data) root.addresses = response.data - form_.animate([ - { opacity: 1 }, - { opacity: 0 } - ], - { - duration: 1000 - } - ); - setTimeout(() => { - form_.parentElement.remove() - }, 1000) + root.animateDelete(form_, true) }) .catch(function (error) { } @@ -472,22 +477,62 @@ const app = new Vue({ /** * */ - storePanier() { + storeCart() { let root = this, data = { name: "Panier du "+document.querySelector('[name="now"]').dataset.content, - userId: this.user.id, + user_id: this.user.id, addresses: this.addresses, products: this.items } - axios.post('/ajax/storepanier', data) + axios.post('/ajax/storecart', data) .then(function (response) { console.log(response) }) .catch(function (error) { console.log(error) }) + }, + toggleName(event) { + const id = event.target.dataset.input, + editText = event.target.dataset.edittext, + defaultText = event.target.dataset.defaulttext, + el = document.getElementById('cart-name-'+id) + + if(el.value.length > 0) + var state = el.classList.toggle("readonly") + + if(!state) { + el.focus() + el.removeAttribute('readonly') + event.target.innerText = editText + } else { + axios.post('/ajax/updateNameSavedcart', {id: id, text: el.value}) + .then(function (response) { + event.target.innerText = defaultText + el.setAttribute('readonly', 'readonly') + }) + .catch(function (error) { + }) + } + }, + removeSavedCart(event) { + const id = event.target.dataset.id ?? event.target.parentElement.dataset.id, + el = document.getElementById('cart-saved-'+id), + root = this + + axios.post('/ajax/deleteSavedcart', {id: id}) + .then(function (response) { + root.animateDelete(el) + }) + .catch(function (error) { + }) + }, + savedCartToCurrent() { + const data = { + + } } }, /** @@ -568,10 +613,13 @@ $(document).on('click', 'button.cart-add', function () { $(this).addClass('btn-no-hover').addClass('bg-navy'); $(this).find('.add').addClass('hidden'); $(this).find('.added').removeClass('hidden').addClass('inline-flex'); - var id = parseInt($(this).attr('data-product-id')); + var id = parseInt($(this).attr('data-product-id')), + ref = $(this).attr('data-ref') + eventBus.$emit('add-item', { id: id, quantity: 1, + ref: ref }); clearTimeout(time); diff --git a/resources/styles/common/global.styl b/resources/styles/common/global.styl index cc8e266..c9187e8 100644 --- a/resources/styles/common/global.styl +++ b/resources/styles/common/global.styl @@ -6,7 +6,7 @@ body min-width: 320px -p:not(:last-child) +p:not(:last-child):not(.no-m) margin-bottom: 1.5em // Layout containers diff --git a/resources/styles/components/cart.styl b/resources/styles/components/cart.styl index 71ed0b5..7218958 100644 --- a/resources/styles/components/cart.styl +++ b/resources/styles/components/cart.styl @@ -138,3 +138,37 @@ +below(850px) grid-fallback(1) + +// +.cartsave + &:not(:last-child) + margin-bottom: 24px + &-name + padding: 46px 0 26px + input + padding: 10px + width: 100% + max-width: 100% + &.readonly + background: none + cursor: default + outline: none + padding: 0 + + &-products + padding: 24px 0 + border-top: 1px solid theme('colors.light-b') + border-bottom: 1px solid theme('colors.light-b') + + .grid:not(:last-child) + margin-bottom: 24px + + &-grid + grid-template-columns: 96px 1fr !important + + &-footer + padding: 48px 0 + + .product + &-thumbnail + height: 96px diff --git a/resources/views/components/btn-delete.blade.php b/resources/views/components/btn-delete.blade.php new file mode 100644 index 0000000..2fd9de4 --- /dev/null +++ b/resources/views/components/btn-delete.blade.php @@ -0,0 +1,4 @@ + diff --git a/resources/views/components/cart-add.blade.php b/resources/views/components/cart-add.blade.php index ec5c83e..985fc04 100644 --- a/resources/views/components/cart-add.blade.php +++ b/resources/views/components/cart-add.blade.php @@ -1,4 +1,4 @@ - -
+
En nous transmettant votre demande, vous acceptez que PM Instrumentation traite vos données personnelles dans le but de vous offrir un service de qualité. Pour plus d’information sur la protection de vos données à caractère personnel, vous pouvez consulter la page diff --git a/resources/views/pages/paniers_enregistres.blade.php b/resources/views/pages/paniers_enregistres.blade.php new file mode 100644 index 0000000..1762641 --- /dev/null +++ b/resources/views/pages/paniers_enregistres.blade.php @@ -0,0 +1,79 @@ +@extends('layouts/app') + +@section('content') + + + + + @foreach(\App\Models\Panier::getAllCart() as $cart) +
+
+
+