namespace App\Http\Controllers;
+use App\Models\Command;
use App\Models\Panier;
use Cubist\Backpack\Facades\App;
use App\Models\Page;
/**
*
*/
- $options = Product::getOptionsByProductsId($data['products']);
-
- $opt = [];
- $total = [];
-
- foreach ($currentCartRef as $keyRefs => $refs) {
- foreach (explode("/", $refs) as $key => $ref) {
- $key -= 1;
- if ($key > -1) {
- $opt_ = array_filter($options[$keyRefs][$key]["options"], function ($n) use ($ref) {
- return $n["ref"] === $ref;
- });
- $opt_ = array_values($opt_);
- $opt[$refs][] = $opt_[0]["sale_price"];
- }
- }
-
- $quantity = intval($data['products'][$keyRefs]["quantity"]);
- $basicSellingPrice = $data['products'][$keyRefs]["basic_selling_price"];
-
- $totalOptionsPrice = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) {
- return $carry + $item;
- }) : 0;
-
- $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"];
- $price = floatval($basicPriceByProductId + $totalOptionsPrice);
- $total[] = floatval($price * $quantity);
- $data['products'][$keyRefs]["price"] = $price;
- }
-
- $checkUnavailableEcommerceProduct = array_filter($data['products'], function($n) {
- return !$n['price'];
- });
-
- $ht = array_reduce($total, function($carry, $item) { return $carry + $item; });
- $ht += ($ht > 1000) ? 20 : 0;
- $tva = $ht * 0.2;
- $ttc = $ht + $tva;
- $data['total'] = !$checkUnavailableEcommerceProduct ? $ttc : null;
-
- //
- $data['addresses'] = json_encode($data['addresses']);
- $data['products'] = json_encode($data['products']);
+ $data = Panier::fillData($currentCartRef, $data);
if(sizeof($lastCartRefs) !== sizeof($intersect) ||
(sizeof($lastCartRefs) === sizeof($intersect) && sizeof($currentCartRef) != sizeof($lastCartRefs)) ) {
}
public function order(Request $request) {
- $request->validate([
- 'id' => 'required|numeric'
- ]);
+ $validation = [
+ 'addresses' => 'required',
+ 'products' => 'required',
+ 'user_id' => 'required|numeric'
+ ];
+ $data = $this->validation_form($request, $validation);
+
+ $currentCartRef = Panier::getRefs($data['products']);
+ $data = Command::fillData($currentCartRef, $data);
+
+ if($data) {
+ $order = Command::create($data);
+ $order->save();
+ }else{
+ abort('422', 'Data are empty');
+ }
+
+ // Clear cart session
+ $request->session()->forget('cart_items');
+
+ return __('Votre commande a bien été enregistrée.');
}
}
'singular' => 'Commande',
'plural' => 'Commandes'];
+ private static $_optionsForSelect = [
+ 'new' => 'Nouvelle',
+ 'pending' => 'En attente de règlement',
+ 'in_progress' => 'En cours',
+ 'done' => 'Clôturée',
+ ];
+
public function setFields()
{
parent::setFields();
+ foreach ($this->fields as $fields) {
+ $fields['tab'] = 'Informations';
+ $this->addField($fields);
+ }
+
$this->addField(['name' => 'status',
- 'type' => 'Text',
'label' => 'Statut',
+ 'type' => 'SelectFromArray',
'column' => true,
+ 'options' => self::$_optionsForSelect,
+ 'default' => 'new',
'tab' => 'Informations'
]);
- foreach ($this->fields as $fields) {
- $this->addField($fields);
- }
+ $this->addField(['name' => 'complete_status',
+ 'label' => 'Indication du statut',
+ 'type' => 'Text',
+ 'tab' => 'Informations'
+ ]);
+ }
+
+ public static function getOptionValue($key) {
+ if($key)
+ return self::$_optionsForSelect[$key];
+
+ return false;
}
}
$userID = Client::getClientConnected()->id;
return self::all()->where('user_id', $userID)->toArray();
}
+
+ public static function fillData($currentCartRef, $data) {
+ /**
+ *
+ */
+ $options = Product::getOptionsByProductsId($data['products']);
+
+ $opt = [];
+ $total = [];
+
+ foreach ($currentCartRef as $keyRefs => $refs) {
+ foreach (explode("/", $refs) as $key => $ref) {
+ $key -= 1;
+ if ($key > -1) {
+ $opt_ = array_filter($options[$keyRefs][$key]["options"], function ($n) use ($ref) {
+ return $n["ref"] === $ref;
+ });
+ $opt_ = array_values($opt_);
+ $opt[$refs][] = $opt_[0]["sale_price"];
+ }
+ }
+
+ $quantity = intval($data['products'][$keyRefs]["quantity"]);
+ $basicSellingPrice = $data['products'][$keyRefs]["basic_selling_price"];
+
+ $totalOptionsPrice = $basicSellingPrice ? array_reduce($opt[$refs], function ($carry, $item) {
+ return $carry + $item;
+ }) : 0;
+
+ $basicPriceByProductId = $data['products'][$keyRefs]["basic_selling_price"];
+ $price = floatval($basicPriceByProductId + $totalOptionsPrice);
+ $total[] = floatval($price * $quantity);
+ $data['products'][$keyRefs]["price"] = $price;
+ }
+
+ $checkUnavailableEcommerceProduct = array_filter($data['products'], function($n) {
+ return !$n['price'];
+ });
+
+ $ht = array_reduce($total, function($carry, $item) { return $carry + $item; });
+ $ht += ($ht > 1000) ? 20 : 0;
+ $tva = $ht * 0.2;
+ $ttc = $ht + $tva;
+ $data['total'] = !$checkUnavailableEcommerceProduct ? $ttc : null;
+
+ //
+ $data['addresses'] = json_encode($data['addresses']);
+ $data['products'] = json_encode($data['products']);
+
+ return $data;
+ }
}
{
parent::setFields();
+ foreach ($this->fields as $fields) {
+ $fields['tab'] = 'Informations';
+ $this->addField($fields);
+ }
+
$this->addField(['name' => 'name',
'type' => 'Text',
'label' => 'Nom',
'column' => true
]);
-
- foreach ($this->fields as $fields) {
- $this->addField($fields);
- }
}
public static function getRefs($products) {
.catch(function (error) {
})
},
- order(event) {
- const root = this,
- id = event.target.dataset.id
+ order() {
+ let root = this,
+ data = {
+ user_id: this.user.id,
+ addresses: this.addresses,
+ products: this.items
+ }
- axios.post('/ajax/order', {id: id})
+ axios.post('/ajax/order', data)
.then(function (response) {
root.validateAction = true
root.confirmMessageCart = response.data;
+ root.items = {}
})
.catch(function (error) {
})
<p class="no-m">{{ __('Date de commande') }}</p>
<p class="no-m text-navy">{{ \Carbon\Carbon::parse($item['created_at'])->isoFormat("D/MM/Y") }}</p>
</div>
- <div>
+ <div class="text-right">
<p class="no-m">{{ __('Numéro de commande') }}</p>
- <p class="no-m text-navy">{{ $item['id'] }}</p>
+ <p class="no-m text-navy">N°{{ $item['id'] }}</p>
</div>
</div>
@endif
@isset($product['basic_selling_price'])
@if(floatval($product['basic_selling_price']))
<p class="no-m">{{ __('Prix unitaire') }} <span class="text-navy">{{
- $product['basic_selling_price'] }} € HT</span>
+ $product['basic_selling_price'] }} € HT</span>
</p>
@endif
@endisset
@endforeach
</div>
<div class="cartsave-footer <?php echo $item['total'] ? 'pt-6' : 'pt-12' ?>">
- <div class=<?php echo $page !== "order" ?: "flex justify-between" ?>>
+ <div class="<?php echo $page !== 'order' ?: 'flex justify-between' ?>">
@if($page === "order")
<div>
<p class="no-m">{{ __('Statut') }}</p>
- <p class="no-m text-navy">{{ $item['status'] }}</p>
+ <p class="no-m text-navy">
+ {{ \App\Models\Command::getOptionValue($item['status']) }}
+ @if($item['complete_status'])
+ · {{ $item['complete_status'] }}
+ @endif
+ </p>
</div>
@endif
@if($item['total'])
<span>{{ __('Mes commandes') }}</span>
</a>
</li>
- <li class="{{ Request::is('panier-enregistré') ? "active" : '' }}">
+ <li class="{{ Request::is('paniers-enregistres') ? "active" : '' }}">
<a href="/paniers-enregistres">
<span>{{ __('Panier enregistrés') }}</span>
</a>