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;
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;
$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 !');
]);
$id = $request->input('id');
- $cart = Panier::find($id);
+ $cart = Cart::find($id);
$cart->delete();
}
$id = $request->input('id');
$newName = $request->input('text');
- $cart = Panier::find($id);
+ $cart = Cart::find($id);
$cart->name = $newName;
$cart->save();
}
$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'];
$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);
$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');
--- /dev/null
+<?php
+namespace App\Http\Controllers\Client;
+
+use App\Models\Client;
+use Illuminate\Http\Request;
+use Illuminate\Http\RedirectResponse;
+use Illuminate\Support\Facades\Auth;
+use Cubist\Backpack\app\Http\Controllers\CubistPageController;
+use Illuminate\Support\Facades\DB;
+use Illuminate\Support\Facades\Hash;
+use Illuminate\Support\Facades\Password;
+
+class ClientController extends CubistPageController
+{
+ public function logout(Request $request): RedirectResponse
+ {
+ Auth::guard('web-clients')->logout();
+ $request->session()->invalidate();
+ $request->session()->regenerateToken();
+ return redirect('/se-connecter');
+ }
+}
--- /dev/null
+<?php
+
+namespace App\Models;
+
+class Cart extends ECommerceCommon
+{
+ protected $table = 'cart';
+
+ protected $_options = ['name' => '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;
+ }
+}
+++ /dev/null
-<?php
-
-namespace App\Models;
-
-use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
-
-class CommandPanierSchema extends CubistMagicAbstractModel
-{
- protected $fields = [
- [
- 'name' => '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;
- }
-}
--- /dev/null
+<?php
+
+namespace App\Models;
+
+use Cubist\Backpack\app\Magic\Models\CubistMagicAbstractModel;
+
+class ECommerceCommon extends CubistMagicAbstractModel
+{
+ protected $fields = [
+ [
+ 'name' => '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;
+ }
+}
<?php
namespace App\Models;
-class Order extends CommandPanierSchema
+class Order extends ECommerceCommon
{
protected $table = 'order';
+++ /dev/null
-<?php
-
-namespace App\Models;
-
-class Panier extends CommandPanierSchema
-{
- protected $table = 'selection';
-
- protected $_options = ['name' => '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;
- }
-}
namespace App\Templates;
-use App\Models\Panier;
+use App\Models\Cart;
class PaniersEnregistres extends Base
{
public function setData(&$data)
{
- $data["items"] = Panier::getAllByUserId();
+ $data["items"] = Cart::getAllByUserId();
}
public function init()
</li>
<li class="{{ Request::is('paniers-enregistres') ? "active" : '' }}">
<a href="/paniers-enregistres">
- <span>{{ __('Panier enregistrés') }}</span>
+ <span>{{ __('Paniers enregistrés') }}</span>
</a>
</li>
<li>
@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'),