From: soufiane Date: Tue, 20 Jun 2023 09:23:44 +0000 (+0200) Subject: wip #5788 @0:30 X-Git-Url: http://git.cubedesigners.com/?a=commitdiff_plain;h=15c8ffe1dbf5bacffecbae9fd56f2d5d8df33cbc;p=pmi.git wip #5788 @0:30 --- diff --git a/app/Http/Controllers/Admin/SelectionCrudController.php b/app/Http/Controllers/Admin/SelectionCrudController.php index 12b23ca..6ba5621 100644 --- a/app/Http/Controllers/Admin/SelectionCrudController.php +++ b/app/Http/Controllers/Admin/SelectionCrudController.php @@ -4,9 +4,9 @@ namespace App\Http\Controllers\Admin; class SelectionCrudController extends \Cubist\Backpack\app\Magic\Controllers\CubistMagicController { - protected $_modelNamespace = 'App\Models\Panier'; + protected $_modelNamespace = 'App\Models\Cart'; protected $_routeURL = 'selection'; - protected $_singular = 'Panier'; + protected $_singular = 'Cart'; protected $_plural = 'PaniersEnregistres'; protected $_clonable = true; protected $_bulk = true; diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index 510aa02..4bcc69f 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Models\Order; -use App\Models\Panier; +use App\Models\Cart; use Cubist\Backpack\Facades\App; use App\Models\Page; use App\Models\Product; @@ -598,31 +598,31 @@ class AjaxController extends CubistFrontController $lastCartId = $request->session()->get('last_selection'); // - $lastCartRefs = $lastCartId ? Panier::find($lastCartId)->getReferences() : []; - $currentCartRef = Panier::getRefs($data['products']); + $lastCartRefs = $lastCartId ? Cart::find($lastCartId)->getReferences() : []; + $currentCartRef = Cart::getRefs($data['products']); $intersect = $lastCartRefs ? array_intersect($currentCartRef,$lastCartRefs) : []; $data['products'] = json_encode($data['products']); $ids = Product::getProductIds($data); $productsInfosBase = Product::getProductsByIds($ids); - $currentCartRef = Panier::getRefs(json_decode($data['products'],true)); + $currentCartRef = Cart::getRefs(json_decode($data['products'],true)); $data['products'] = json_decode($data['products'], true); /** * */ - $data = Panier::fillData($currentCartRef, $data, $productsInfosBase,false); + $data = Cart::fillData($currentCartRef, $data, $productsInfosBase,false); if(sizeof($lastCartRefs) !== sizeof($intersect) || (sizeof($lastCartRefs) === sizeof($intersect) && sizeof($currentCartRef) != sizeof($lastCartRefs)) ) { - $panier = Panier::firstOrCreate($data); + $panier = Cart::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', $lastCartId)->update($data); + Cart::where('id', $lastCartId)->update($data); } return __('Le panier a été enregistré avec succès !'); @@ -634,7 +634,7 @@ class AjaxController extends CubistFrontController ]); $id = $request->input('id'); - $cart = Panier::find($id); + $cart = Cart::find($id); $cart->delete(); } @@ -647,7 +647,7 @@ class AjaxController extends CubistFrontController $id = $request->input('id'); $newName = $request->input('text'); - $cart = Panier::find($id); + $cart = Cart::find($id); $cart->name = $newName; $cart->save(); } @@ -659,16 +659,16 @@ class AjaxController extends CubistFrontController $cartId = $request->input('id'); - $cart = Panier::find($cartId)->toArray(); + $cart = Cart::find($cartId)->toArray(); $ids = Product::getProductIds($cart); $productsInfosBase = Product::getProductsByIds($ids); $clientIdInRequest = intval($cart['user_id']); $clientId = Client::getClientConnected()->id; - $currentCartRef = Panier::getRefs(json_decode($cart['products'],true)); + $currentCartRef = Cart::getRefs(json_decode($cart['products'],true)); $cart['products'] = json_decode($cart['products'], true); - $data = Panier::updateCartData($currentCartRef,$cart,$productsInfosBase); + $data = Cart::updateCartData($currentCartRef,$cart,$productsInfosBase); if($clientIdInRequest === $clientId) { $cartData = $data['products']; @@ -698,7 +698,7 @@ class AjaxController extends CubistFrontController $data['products'] = json_decode($data['products'], true); $productsInfosBase = Product::getProductsByIds($ids); - $currentCartRef = Panier::getRefs($data['products']); + $currentCartRef = Cart::getRefs($data['products']); $data = Order::fillData($currentCartRef,$data,$productsInfosBase); @@ -721,9 +721,9 @@ class AjaxController extends CubistFrontController $data['products'] = json_encode($cart); $ids = Product::getProductIds($data); $productsInfosBase = Product::getProductsByIds($ids); - $currentCartRef = Panier::getRefs(json_decode($data['products'],true)); + $currentCartRef = Cart::getRefs(json_decode($data['products'],true)); $data['products'] = json_decode($data['products'], true); - $data = Panier::fillData($currentCartRef, $data, $productsInfosBase,false); + $data = Cart::fillData($currentCartRef, $data, $productsInfosBase,false); $update = json_decode($data['products'],true); $request->session()->forget('cart_items'); diff --git a/app/Http/Controllers/Client/ClientController.php b/app/Http/Controllers/Client/ClientController.php new file mode 100644 index 0000000..905be08 --- /dev/null +++ b/app/Http/Controllers/Client/ClientController.php @@ -0,0 +1,22 @@ +logout(); + $request->session()->invalidate(); + $request->session()->regenerateToken(); + return redirect('/se-connecter'); + } +} diff --git a/app/Models/Cart.php b/app/Models/Cart.php new file mode 100644 index 0000000..0e5fc37 --- /dev/null +++ b/app/Models/Cart.php @@ -0,0 +1,63 @@ + 'cart', + 'singular' => 'Cart', + 'plural' => 'Carts']; + + public function setFields() + { + parent::setFields(); + + foreach ($this->fields as $fields) { + $fields['tab'] = 'Informations'; + $this->addField($fields); + } + + $this->addField(['name' => 'name', + 'type' => 'Text', + 'label' => 'Nom', + 'column' => true + ]); + } + + protected function getAllByUserId() + { + $items = parent::getAllByUserId(); + $allProducts = array_map(function($n){ return json_decode($n['products'], true); }, $items); + + $idsProducts = Product::getProductIds($items); + $productsInfosBase = Product::getProductsByIds($idsProducts); + + foreach ($allProducts as $key => $products){ + $data['products'] = $products; + $currentCartRef = self::getRefs($data['products']); + $data = self::updateCartData($currentCartRef,$data,$productsInfosBase); + $items[$key]['products'] = json_encode($data['products']); + $items[$key]['total'] = $data['total']; + } + return $items; + } + + 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; + } +} diff --git a/app/Models/CommandPanierSchema.php b/app/Models/CommandPanierSchema.php deleted file mode 100644 index e9455a4..0000000 --- a/app/Models/CommandPanierSchema.php +++ /dev/null @@ -1,112 +0,0 @@ - 'user_id', - 'label' => 'user_id', - 'type' => 'Text', - 'column' => true - ], - [ - 'name' => 'addresses', - 'label' => 'Adresses', - 'type' => 'Text', - 'column' => true - ], - [ - 'name' => 'products', - 'label' => 'Produits', - 'type' => 'Text', - 'column' => true - ], - [ - 'name' => 'total', - 'type' => 'Text' - ] - ]; - - - protected function getAllByUserId() { - $userID = Client::getClientConnected()->id; - return self::all()->where('user_id', $userID)->toArray(); - } - - 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) { - $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) { - $options_ = $options[$key]['options']; - foreach ($options_ as $opt_) { - if ($opt_["ref"] === $ref) { - $opt[$refs][] = $opt_["sale_price"] ?? 0; - } - } - } - } - } - - $categoryProduct = $productsInfosBase[$index][0]['category']; - $discount = intval(Client::getDiscount($categoryProduct)); - $quantity = intval($data['products'][$keyRefs]["quantity"]); - $basicSellingPrice = $productsInfosBase[$index][0]['basic_selling_price'] ?? 0; - - $totalOptionPrices = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { - return $carry + $item; - }) : 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; - $data['products'][$keyRefs]["price"] = $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'] = !$unavailableEcommerceProduct ? number_format($ttc,2) : null; - - return $data; - } - - public static function fillData($currentCartRef, $data, $productsInfosBase, $haveStatus = true) { - $data = self::updateCartData($currentCartRef,$data,$productsInfosBase); - - if(isset($data['addresses'])) - $data['addresses'] = json_encode($data['addresses']); - - if($haveStatus) - $data['status'] = 'new'; - - $data['products'] = json_encode($data['products']); - - return $data; - } -} diff --git a/app/Models/ECommerceCommon.php b/app/Models/ECommerceCommon.php new file mode 100644 index 0000000..a3c5fb8 --- /dev/null +++ b/app/Models/ECommerceCommon.php @@ -0,0 +1,112 @@ + 'user_id', + 'label' => 'user_id', + 'type' => 'Text', + 'column' => true + ], + [ + 'name' => 'addresses', + 'label' => 'Adresses', + 'type' => 'Text', + 'column' => true + ], + [ + 'name' => 'products', + 'label' => 'Produits', + 'type' => 'Text', + 'column' => true + ], + [ + 'name' => 'total', + 'type' => 'Text' + ] + ]; + + + protected function getAllByUserId() { + $userID = Client::getClientConnected()->id; + return self::all()->where('user_id', $userID)->toArray(); + } + + 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) { + $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) { + $options_ = $options[$key]['options']; + foreach ($options_ as $opt_) { + if ($opt_["ref"] === $ref) { + $opt[$refs][] = $opt_["sale_price"] ?? 0; + } + } + } + } + } + + $categoryProduct = $productsInfosBase[$index][0]['category']; + $discount = intval(Client::getDiscount($categoryProduct)); + $quantity = intval($data['products'][$keyRefs]["quantity"]); + $basicSellingPrice = $productsInfosBase[$index][0]['basic_selling_price'] ?? 0; + + $totalOptionPrices = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) { + return $carry + $item; + }) : 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; + $data['products'][$keyRefs]["price"] = $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'] = !$unavailableEcommerceProduct ? number_format($ttc,2) : null; + + return $data; + } + + public static function fillData($currentCartRef, $data, $productsInfosBase, $haveStatus = true) { + $data = self::updateCartData($currentCartRef,$data,$productsInfosBase); + + if(isset($data['addresses'])) + $data['addresses'] = json_encode($data['addresses']); + + if($haveStatus) + $data['status'] = 'new'; + + $data['products'] = json_encode($data['products']); + + return $data; + } +} diff --git a/app/Models/Order.php b/app/Models/Order.php index 7f9afb2..e13d09f 100644 --- a/app/Models/Order.php +++ b/app/Models/Order.php @@ -1,7 +1,7 @@ 'selection', - 'singular' => 'Panier', - 'plural' => 'PaniersEnregistres']; - - public function setFields() - { - parent::setFields(); - - foreach ($this->fields as $fields) { - $fields['tab'] = 'Informations'; - $this->addField($fields); - } - - $this->addField(['name' => 'name', - 'type' => 'Text', - 'label' => 'Nom', - 'column' => true - ]); - } - - protected function getAllByUserId() - { - $items = parent::getAllByUserId(); - $allProducts = array_map(function($n){ return json_decode($n['products'], true); }, $items); - - $idsProducts = Product::getProductIds($items); - $productsInfosBase = Product::getProductsByIds($idsProducts); - - foreach ($allProducts as $key => $products){ - $data['products'] = $products; - $currentCartRef = self::getRefs($data['products']); - $data = self::updateCartData($currentCartRef,$data,$productsInfosBase); - $items[$key]['products'] = json_encode($data['products']); - $items[$key]['total'] = $data['total']; - } - return $items; - } - - 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; - } -} diff --git a/app/Templates/PaniersEnregistres.php b/app/Templates/PaniersEnregistres.php index 2687c08..b592719 100644 --- a/app/Templates/PaniersEnregistres.php +++ b/app/Templates/PaniersEnregistres.php @@ -4,7 +4,7 @@ namespace App\Templates; -use App\Models\Panier; +use App\Models\Cart; class PaniersEnregistres extends Base { @@ -15,7 +15,7 @@ class PaniersEnregistres extends Base public function setData(&$data) { - $data["items"] = Panier::getAllByUserId(); + $data["items"] = Cart::getAllByUserId(); } public function init() diff --git a/resources/views/partials/nav-account.blade.php b/resources/views/partials/nav-account.blade.php index 074871e..15bee73 100644 --- a/resources/views/partials/nav-account.blade.php +++ b/resources/views/partials/nav-account.blade.php @@ -19,7 +19,7 @@
  • - {{ __('Panier enregistrés') }} + {{ __('Paniers enregistrés') }}
  • diff --git a/resources/views/vendor/notifications/email.blade.php b/resources/views/vendor/notifications/email.blade.php index 05be6cd..01671da 100644 --- a/resources/views/vendor/notifications/email.blade.php +++ b/resources/views/vendor/notifications/email.blade.php @@ -53,8 +53,8 @@ @slot('subcopy') @lang( - ":text1 \":actionText\" :text2". - ':text3 [:actionURL](:actionURL)', + ":text1 \":actionText\" :text2 ". + ":text3 [:actionURL](:actionURL)", [ 'text1' => trans('mail.footer_part1'), 'text2' => trans('mail.footer_part2'),