]> _ Git - pmi.git/commitdiff
wip #6106 @9:00
authorsoufiane <soufiane@cubedesigners.com>
Fri, 7 Jul 2023 22:47:53 +0000 (00:47 +0200)
committersoufiane <soufiane@cubedesigners.com>
Fri, 7 Jul 2023 22:47:53 +0000 (00:47 +0200)
app/Events/OrderSaved.php [new file with mode: 0644]
app/Http/Controllers/AjaxController.php
app/Models/Client.php
app/Models/Order.php
app/Models/Product.php
resources/views/components/item-cart-order.blade.php
resources/views/pages/my_order.blade.php

diff --git a/app/Events/OrderSaved.php b/app/Events/OrderSaved.php
new file mode 100644 (file)
index 0000000..a377159
--- /dev/null
@@ -0,0 +1,26 @@
+<?php
+
+namespace App\Events;
+
+use Illuminate\Queue\SerializesModels;
+use App\Models\Order;
+use App\Models\Client;
+
+class OrderSaved
+{
+    use SerializesModels;
+    public function __construct(Order $order)
+    {
+        $clientById = Client::find($order->user_id);
+        $company = $clientById->company;
+        $addresses = $clientById->address;
+
+        $newUserId = $order->getAttribute('user_id');
+        $originalUserID = $order->getOriginal('user_id');
+
+        if($newUserId != $originalUserID)
+            $order->addresses = $addresses;
+
+        $order->company = $company;
+    }
+}
index b1bddb9630209ed15d8327b3c53a92bb20ded320..c4835856c50776b0e37e8670054dacda2c1b0784 100644 (file)
@@ -683,9 +683,7 @@ class AjaxController extends CubistFrontController
         $company = array_values(array_filter(json_decode($data['addresses'], true), function($n) {
             return intval($n['delivery_address']);
         }))[0]['company'];
-
         $data['company'] = $company;
-        $data['email'] = Client::getClientConnected()->email;
         $data['request_date'] = Carbon::now('Europe/Paris');
         $data['products'] = json_encode(array_map(function($n){
             $exp = explode('|',$n['ref']);
@@ -693,7 +691,6 @@ class AjaxController extends CubistFrontController
             $n['ref'] = sizeof($exp) > 1 ? implode('|', $exp) : $exp;
             return $n;
         }, json_decode($data['products'],true)));
-        $data['user'] = Client::getClientConnected()->email;
 
         if($data) {
             $order = Order::create($data);
index b1b7e4b60bf5bab6da03dfbe14f8dd43b7b187b3..d693c92798eedb4d0ef02b36f5383a3a466ccab8 100644 (file)
@@ -105,8 +105,8 @@ class Client extends CubistMagicAbstractModel
         return Auth::guard('web-clients')->user();
     }
 
-    public static function getEmailsClientsApproved() {
-        return self::all()->where('status',1)->pluck('email');
+    public static function getClientsApproved() {
+        return self::select('*')->where('status',1)->get()->toArray();
     }
 
     public static function getCategory() {
index 7be9d27ee12be8c88161dfc70f9ec579824c767d..dfd156dae867fc6ac1302764c083721cf1f6d369 100644 (file)
@@ -1,6 +1,9 @@
 <?php
 namespace App\Models;
 
+use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
+use App\Events\OrderSaved;
+
 class Order extends ECommerceCommon
 {
     protected $table = 'order';
@@ -16,39 +19,53 @@ class Order extends ECommerceCommon
         'done' => 'Clôturée',
     ];
 
+    protected $fillable = ['company'];
+
+    protected $_optionsForUserInfo = [];
+
+    protected $dispatchesEvents = [
+        'saving' => OrderSaved::class
+    ];
+
     public function setFields()
     {
-        parent::setFields();
 
         $tabOrder = 'Commande';
 
-        $this->addField(['name' => 'user',
-            'label' => 'Utilisateur',
-            'type' => 'SelectFromArray',
-            'options' => Client::getEmailsClientsApproved(),
-            'tab' => 'Informations'
-        ]);
-
         $this->addField(['name' => 'company',
             'label' => 'Société',
-            'type' => 'Text',
+            'type' => 'Hidden',
             'column' => true,
-            'tab' => 'Informations'
         ]);
 
-        $this->addField(['name' => 'email',
-            'label' => 'Email',
-            'type' => 'Text',
-            'column' => true,
-            'tab' => 'Informations'
-        ]);
+        foreach ($this->fields as $fields) {
+            if ($fields['name'] === 'products') {
+                $fields = ['name' => 'products',
+                    'label' => 'Produits',
+                    'type' => 'Table',
+                    'columns' => ['id' => '#', 'ref' => 'Référence options', 'reference' => 'Référence produit', 'name' => 'Nom de produit', 'quantity' => 'Quantité'],
+                    'tab' => $tabOrder];
+            }
+            if ($fields['name'] === 'total') {
+                $fields['tab'] = $tabOrder;
+            }
+
+            if ($fields['name'] === 'user_id') {
+                $fields['label'] = 'Utilisateur';
+                $fields['tab'] = 'Informations';
+                $fields['type'] = 'SelectFromArray';
+                $fields['options'] = $this->userOptions();
+            }
+
+            $this->addField($fields);
+        }
 
         $this->addField(['name' => 'status',
             'label' => 'Status',
             'type' => 'SelectFromArray',
             'column' => true,
             'options' => self::$_optionsForSelect,
-            'default'     => 'new',
+            'default' => 'new',
             'tab' => $tabOrder
         ]);
 
@@ -64,19 +81,7 @@ class Order extends ECommerceCommon
             'column' => true,
             'tab' => $tabOrder]);
 
-        foreach ($this->fields as $fields) {
-            if ($fields['name'] === 'products') {
-                $fields = ['name' => 'products',
-                    'label' => 'Produits',
-                    'type' => 'Table',
-                    'columns' => ['id' => '#', 'ref' => 'Référence options', 'reference' => 'Référence produit', 'name' => 'Nom de produit', 'quantity' => 'Quantité'],
-                    'tab' => $tabOrder];
-            }
-            if ($fields['name'] === 'total') {
-                $fields['tab'] = $tabOrder;
-            }
-            $this->addField($fields);
-        }
+        parent::setFields();
     }
 
     public static function getOptionValue($key) {
@@ -85,4 +90,12 @@ class Order extends ECommerceCommon
 
         return false;
     }
+    public function userOptions() {
+        $infos = Client::getClientsApproved();
+        foreach ($infos as $info) {
+            $this->_optionsForUserInfo[$info['id']][] = $info['firstname'].' '.$info['lastname'].' ('.$info['company'].')';
+        }
+
+        return array_map(function($n) { return $n[0]; }, $this->_optionsForUserInfo);
+    }
 }
index 09041f311042eaf88b53cc8ceafffd5542e195a4..48875f4e74b0bf773197bfc26233828ebc486d8a 100644 (file)
@@ -502,11 +502,6 @@ class Product extends CubistMagicPageModel
         return self::$_cart_data;
     }
 
-    public function isJson($string) {
-        json_decode($string);
-        return json_last_error() === JSON_ERROR_NONE;
-    }
-
     public static function getProductsByIds($ids)
     {
         return self::whereIn('id', $ids)
index 47cd452a7c35c6d0a06c3970866855db3ad4968f..5a63cf0325c3a545146707757ced77e72c1c98fa 100644 (file)
@@ -1,3 +1,4 @@
+@isset($items)
 @foreach($items as $item)
     <div id="cart-saved-{{ $item['id'] }}" class="cartsave bg-grey-200 pr-2v pl-2v">
         <div class="cartsave-name">
@@ -13,6 +14,7 @@
             </div>
         </div>
         <div class="cartsave-products">
+            @if($item['products'])
             @foreach(json_decode($item['products'], true) as $key => $product)
                 <grid gap="md" class="sm:grid-cols-1 cartsave-grid">
                     <div class="product-thumbnail bg-center bg-contain bg-no-repeat"
@@ -58,6 +60,7 @@
                     </div>
                 </grid>
             @endforeach
+            @endif
         </div>
         <div class="cartsave-footer <?php echo $item['total'] ? 'pt-6' : 'pt-12' ?>">
             <div class="flex justify-between">
@@ -80,3 +83,4 @@
         </div>
     </div>
 @endforeach
+@endisset
index b44dc47c067d54e4fa5d1e155634ba869e4544f0..91b46f3d34df232ce0ba45d7e8d5d6ae048d7fbc 100644 (file)
@@ -1,5 +1,9 @@
 @extends('layouts/app')
 
+@php
+    //dump(\App\Models\Client::find(10)->getDatas());
+@endphp
+
 @section('content')
     <full-width padding="pb-3v">
         <content>