From cc81fe249cbe5c633c20c63b7048c8b400b9f4ed Mon Sep 17 00:00:00 2001
From: soufiane
Date: Fri, 9 Jun 2023 17:53:17 +0200
Subject: [PATCH] =?utf8?q?wip=20#5788=20@4:00=20affichage=20dynamique=20de?=
=?utf8?q?s=20tarifs=20selon=20le=20changement=20de=20remise,=20r=C3=A9act?=
=?utf8?q?ivit=C3=A9=20du=20changement=20de=20donn=C3=A9es?=
MIME-Version: 1.0
Content-Type: text/plain; charset=utf8
Content-Transfer-Encoding: 8bit
---
app/Http/Controllers/AjaxController.php | 58 +++++++++-----
app/Models/CommandPanierSchema.php | 29 ++-----
app/Models/Panier.php | 19 +++++
app/Models/Product.php | 14 +++-
app/Providers/AppServiceProvider.php | 1 +
resources/js/app.js | 8 +-
resources/js/components/CartItem.vue | 1 -
.../components/item-cart-order.blade.php | 80 +++++++++++++++++++
.../views/components/item-cart.blade.php | 5 +-
resources/views/pages/my_order.blade.php | 2 +-
10 files changed, 165 insertions(+), 52 deletions(-)
create mode 100644 resources/views/components/item-cart-order.blade.php
diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php
index ed7eb05..2fce0c4 100644
--- a/app/Http/Controllers/AjaxController.php
+++ b/app/Http/Controllers/AjaxController.php
@@ -3,6 +3,7 @@
namespace App\Http\Controllers;
use App\Models\Command;
+use App\Models\CommandPanierSchema;
use App\Models\Panier;
use Cubist\Backpack\Facades\App;
use App\Models\Page;
@@ -587,10 +588,16 @@ class AjaxController extends CubistFrontController
$currentCartRef = Panier::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));
+ $data['products'] = json_decode($data['products'], true);
+
/**
*
*/
- $data = Panier::fillData($currentCartRef, $data, false);
+ $data = Panier::fillData($currentCartRef, $data, $productsInfosBase,false);
if(sizeof($lastCartRefs) !== sizeof($intersect) ||
(sizeof($lastCartRefs) === sizeof($intersect) && sizeof($currentCartRef) != sizeof($lastCartRefs)) ) {
@@ -641,12 +648,20 @@ class AjaxController extends CubistFrontController
$cartId = $request->input('id');
$cart = Panier::find($cartId)->toArray();
+ $ids = Product::getProductIds($cart);
+ $productsInfosBase = Product::getProductsByIds($ids);
$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);
+
if($clientIdInRequest === $clientId) {
- $cartData = json_decode($cart['products'],true);
+ $cartData = $data['products'];
// Clear cart session
$request->session()->forget('cart_items');
@@ -666,8 +681,12 @@ class AjaxController extends CubistFrontController
];
$data = $this->validation_form($request, $validation);
- $currentCartRef = Panier::getRefs($data['products']);
- $data = Command::fillData($currentCartRef, $data);
+ $data['products'] = json_encode($data['products']);
+ $ids = Product::getProductIds($data);
+ $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);
@@ -684,26 +703,23 @@ class AjaxController extends CubistFrontController
return __('Votre commande a bien été validée !');
}
- public function getdiscount($request) {
- /*$id = $request->id;
+ public function updateCart($request) {
+ $id = $request->id;
- $product = Product::find($id);
- $products = $request->session()->get('cart_items');
- $discount = Client::discountArray($product->category);
- $needs_update = true;
-
- $cartItems_ = '';
+ $cart = Product::getCartData();
+ $data['products'] = json_encode($cart);
+ $ids = Product::getProductIds($data);
+ $productsInfosBase = Product::getProductsByIds($ids);
+ $currentCartRef = Panier::getRefs(json_decode($data['products'],true));
+ $data['products'] = json_decode($data['products'], true);
+ $data = Panier::fillData($currentCartRef, $data, $productsInfosBase,false);
+ $update = json_decode($data['products'],true);
- if($products) {
- $cartItems_ = array_map(function($value) use ($discount) {
- $value['discount'] = $discount;
- return $value;
- }, $products);
- }
+ $request->session()->forget('cart_items');
+ $request->session()->put('cart_items', $update);
- $request->session()->flash('cart_items', $cartItems_);
- $request->session()->put('cart_items', $cartItems_);
+ $needs_update = true;
- return ['needs_update' => $needs_update, 'cart_data' => $cartItems_];*/
+ return ['needs_update' => $needs_update, 'cart_data' => $update];
}
}
diff --git a/app/Models/CommandPanierSchema.php b/app/Models/CommandPanierSchema.php
index 4d9a9fb..8471757 100644
--- a/app/Models/CommandPanierSchema.php
+++ b/app/Models/CommandPanierSchema.php
@@ -31,28 +31,13 @@ class CommandPanierSchema extends CubistMagicAbstractModel
]
];
+
protected function getAllByUserId() {
$userID = Client::getClientConnected()->id;
- $items = self::all()->where('user_id', $userID)->toArray();
- $allProducts = array_map(function($n){ return json_decode($n['products'], true); }, $items);
- $idsProducts = array_column(call_user_func_array('array_merge', $allProducts),'id');
-
- $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);
- $items[$key]['products'] = json_encode($data['products']);
- $items[$key]['total'] = $data['total'];
- }
-
- return $items;
+ return self::all()->where('user_id', $userID)->toArray();
}
- protected static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){
+ public static function test($currentCartRef,$categoryClient,$data = [], $productsInfosBase = []){
$opt = [];
$total = [];
@@ -87,8 +72,8 @@ class CommandPanierSchema extends CubistMagicAbstractModel
$data['products'][$keyRefs]["basic_selling_price"] = $basicSellingPrice;
$discount = floatval($data['products'][$keyRefs]["discount"]) ?? 0;
$price = floatval($basicSellingPrice + $totalOptionsPrice);
- $price = ($price - (($price * $discount) / 100));
- $total[] = str_replace(',','.',$price * $quantity);
+ $priceDiscount = ($price - (($price * $discount) / 100));
+ $total[] = str_replace(',','.',$priceDiscount * $quantity);
$data['products'][$keyRefs]["price"] = $price;
}
@@ -105,13 +90,13 @@ class CommandPanierSchema extends CubistMagicAbstractModel
return $data;
}
- public static function fillData($currentCartRef, $data, $haveStatus = true) {
+ public static function fillData($currentCartRef, $data, $productsInfosBase, $haveStatus = true) {
/**
*
*/
$categoryClient = Client::getClientConnected()->discount ?? 'A';
- $data = self::test($currentCartRef,$categoryClient,$data);
+ $data = self::test($currentCartRef,$categoryClient,$data,$productsInfosBase);
//
if(isset($data['addresses']))
diff --git a/app/Models/Panier.php b/app/Models/Panier.php
index 80e5272..911ed5a 100644
--- a/app/Models/Panier.php
+++ b/app/Models/Panier.php
@@ -26,6 +26,25 @@ class Panier extends CommandPanierSchema
]);
}
+ protected function getAllByUserId()
+ {
+ $items = parent::getAllByUserId();
+ $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);
+ $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"];
diff --git a/app/Models/Product.php b/app/Models/Product.php
index 2eff188..8262561 100644
--- a/app/Models/Product.php
+++ b/app/Models/Product.php
@@ -485,7 +485,6 @@ class Product extends CubistMagicPageModel
$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')
@@ -512,6 +511,19 @@ class Product extends CubistMagicPageModel
->toArray();
}
+ public static function getProductIds($data) {
+ if(isset($data['products'])){
+ $allProducts = json_decode($data['products'], true);
+ $ids = array_column($allProducts, 'id');
+ }else{
+ $allProducts = array_map(function ($n) {
+ return json_decode($n['products'], true);
+ }, $data);
+ $ids = array_column(call_user_func_array('array_merge', $allProducts),'id');
+ }
+ return $ids;
+ }
+
public static function getFilteredProducts($product_type, $filter_values = [])
{
diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php
index 114551d..c05ccd3 100644
--- a/app/Providers/AppServiceProvider.php
+++ b/app/Providers/AppServiceProvider.php
@@ -61,6 +61,7 @@ class AppServiceProvider extends ServiceProvider
BladeX::component('components.modal-confirm'); // ...
BladeX::component('components.btn-delete'); // ...
BladeX::component('components.item-cart'); // ...
+ BladeX::component('components.item-cart-order'); // ...
} catch (\Exception $e) {
}
diff --git a/resources/js/app.js b/resources/js/app.js
index 82f99a5..6447003 100644
--- a/resources/js/app.js
+++ b/resources/js/app.js
@@ -506,8 +506,8 @@ const app = new Vue({
axios.post('/ajax/storecart', data)
.then(function (response) {
- //root.validateAction = true
- //root.confirmMessageCart = response.data;
+ root.validateAction = true
+ root.confirmMessageCart = response.data;
})
.catch(function (error) {
console.log(error)
@@ -577,10 +577,10 @@ const app = new Vue({
.catch(function (error) {
})
},
- getDiscount(id) {
+ updateCart(id) {
let root = this
- axios.post('/ajax/getdiscount', id)
+ axios.post('/ajax/updateCart', id)
.then(function (response) {
if (response.data.needs_update) {
root.items = response.data.cart_data;
diff --git a/resources/js/components/CartItem.vue b/resources/js/components/CartItem.vue
index 131d257..eda3b2b 100644
--- a/resources/js/components/CartItem.vue
+++ b/resources/js/components/CartItem.vue
@@ -49,7 +49,6 @@
},
mounted() {
- console.log(this.item)
eventBus.$emit('send-id', {
id: this.item.id
});
diff --git a/resources/views/components/item-cart-order.blade.php b/resources/views/components/item-cart-order.blade.php
new file mode 100644
index 0000000..bfe65e4
--- /dev/null
+++ b/resources/views/components/item-cart-order.blade.php
@@ -0,0 +1,80 @@
+@foreach($items as $item)
+
+
+
+
+
{{ __('Date de commande') }}
+
{{ \Carbon\Carbon::parse($item['created_at'])->isoFormat("D/MM/Y") }}
+
+
+
{{ __('Numéro de commande') }}
+
N°{{ $item['id'] }}
+
+
+
+
+ @foreach(json_decode($item['products'], true) as $key => $product)
+
+
+
+
+
+
{{ $product['ref'] }}
+
{{ $product['name'] }}
+ @if(isset($product['discount']) && $product['discount'] !== 0)
+ @if(floatval($product['basic_selling_price']))
+
{{ __('Prix catalogue') }} : {{
+ $product['basic_selling_price'] }} ⬠HT
+
+
{{ __('Votre prix') }} : {{
+ number_format($product['basic_selling_price'] - (($product['basic_selling_price'] * $product['discount']) / 100), 2) }} ⬠HT
+
+ @endif
+ @else
+ @isset($product['basic_selling_price'])
+ @if(floatval($product['basic_selling_price']))
+
{{ __('Prix unitaire') }} : {{
+ $product['basic_selling_price'] }} ⬠HT
+
+ @endif
+ @endisset
+ @endif
+
{{ __('Quantité') }} : {{$product['quantity']}}
+
+
+ @if(floatval($product['price']))
+
{{ number_format($product['price'] * $product['quantity'], 2) }}⬠HT
+ @else
+
+ N/A
+ ?
+
+ @endif
+
+
+
+ @endforeach
+
+
+
+@endforeach
diff --git a/resources/views/components/item-cart.blade.php b/resources/views/components/item-cart.blade.php
index 65d4f61..510fbbc 100644
--- a/resources/views/components/item-cart.blade.php
+++ b/resources/views/components/item-cart.blade.php
@@ -36,7 +36,7 @@
$product['basic_selling_price'] }} ⬠HT
{{ __('Votre prix') }} : {{
- $product['basic_selling_price'] - (($product['basic_selling_price'] * $product['discount']) / 100) }} ⬠HT
+ number_format($product['basic_selling_price'] - (($product['basic_selling_price'] * $product['discount']) / 100), 2) }} ⬠HT
@endif
@else
@@ -52,7 +52,8 @@
@if(floatval($product['price']))
-
{{ number_format(($product['price'] * $product['quantity']), 2) }}⬠HT
+ @php($price = $product['price'] - (($product['price'] * $product['discount']) / 100))
+
{{ number_format($product['price'] * $product['quantity'], 2) }}⬠HT
@else
N/A
diff --git a/resources/views/pages/my_order.blade.php b/resources/views/pages/my_order.blade.php
index 22192ce..b44dc47 100644
--- a/resources/views/pages/my_order.blade.php
+++ b/resources/views/pages/my_order.blade.php
@@ -4,7 +4,7 @@
-
+
@endsection
--
2.39.5