--- /dev/null
+<?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;
+ }
+}
$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']);
$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);
<?php
namespace App\Models;
+use Cubist\Backpack\app\Magic\Controllers\CubistMagicController;
+use App\Events\OrderSaved;
+
class Order extends ECommerceCommon
{
protected $table = 'order';
'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
]);
'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) {
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);
+ }
}