2020-11-17 13:41:31 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Bo;
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
2021-03-17 09:28:02 +01:00
|
|
|
use App\Models\Order;
|
2021-03-27 20:55:16 +01:00
|
|
|
use App\Models\OrderLine;
|
2020-11-17 13:41:31 +01:00
|
|
|
use App\Models\OrdersProducts;
|
2021-03-17 09:28:02 +01:00
|
|
|
use App\Models\Product;
|
2021-03-29 15:10:07 +02:00
|
|
|
use App\Models\Timeslot;
|
2021-04-21 17:50:57 +02:00
|
|
|
use App\Models\Magasin;
|
2021-04-28 11:49:44 +02:00
|
|
|
use App\Http\Requests\SortOrders;
|
2021-03-29 15:10:07 +02:00
|
|
|
use Illuminate\Support\Facades\Mail;
|
2021-04-14 10:39:20 +02:00
|
|
|
use App\Mail\OrderStatusMail;
|
2021-03-29 15:10:07 +02:00
|
|
|
use App\Mail\OrderConfirmMail;
|
2021-06-01 10:58:20 +02:00
|
|
|
use App\Mail\OrderConfirmAdminMail;
|
2021-07-23 15:12:55 +02:00
|
|
|
use App\Jobs\CsvOrderWrite;
|
2021-04-13 17:21:39 +02:00
|
|
|
use App\Models\Customers;
|
2021-04-23 11:59:35 +02:00
|
|
|
use App\Models\Setting;
|
2021-03-27 20:55:16 +01:00
|
|
|
use Carbon\Carbon;
|
2021-03-29 15:10:07 +02:00
|
|
|
use Carbon\CarbonImmutable;
|
|
|
|
|
use Carbon\CarbonInterval;
|
2021-03-27 20:55:16 +01:00
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart;
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
use Illuminate\Support\Str;
|
2021-03-29 15:10:07 +02:00
|
|
|
use Illuminate\Http\Request;
|
|
|
|
|
use Illuminate\Support\Facades\Session;
|
2020-11-17 13:41:31 +01:00
|
|
|
|
|
|
|
|
class OrdersController extends Controller
|
|
|
|
|
{
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* index of orders (Bo)
|
|
|
|
|
*/
|
2021-04-28 11:49:44 +02:00
|
|
|
public function index(SortOrders $request)
|
2021-04-16 09:00:36 +02:00
|
|
|
{
|
2021-04-28 11:49:44 +02:00
|
|
|
//$orders = Order::orderBy('date', 'DESC')->get();
|
|
|
|
|
|
2021-04-28 13:42:43 +02:00
|
|
|
$orders = $this->filterOrders($request);
|
2021-04-28 11:49:44 +02:00
|
|
|
|
|
|
|
|
$orders = $orders->paginate(10);
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
return view('bo.orders.index', compact('orders'));
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-28 11:49:44 +02:00
|
|
|
public function today(SortOrders $request)
|
|
|
|
|
{
|
|
|
|
|
//$orders = Order::orderBy('date', 'DESC')->get();
|
|
|
|
|
|
2021-04-28 13:42:43 +02:00
|
|
|
$orders = $this->filterOrders($request);
|
|
|
|
|
|
|
|
|
|
//filter today
|
|
|
|
|
$today = CarbonImmutable::now()->startOfDay();
|
|
|
|
|
$orders = $orders->whereHas('timeslot', function($q) use($today) {
|
|
|
|
|
$q->where('day', '=', $today->format('d/m/Y'));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$orders = $orders->get(); //paginate(20);
|
|
|
|
|
|
|
|
|
|
return view('bo.orders.index', compact('orders','today'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function filterOrders(Request $request) {
|
2021-04-28 11:49:44 +02:00
|
|
|
$orders = Order::query();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($request->rst) {
|
|
|
|
|
Session::remove('orders.sort_col');
|
|
|
|
|
Session::remove('orders.sort_way');
|
|
|
|
|
}
|
|
|
|
|
if ($request->sort_col) {
|
|
|
|
|
Session::put('orders.sort_col', $request->sort_col);
|
|
|
|
|
Session::save();
|
|
|
|
|
}
|
|
|
|
|
if ($request->sort_way) {
|
|
|
|
|
Session::put('orders.sort_way', $request->sort_way);
|
|
|
|
|
Session::save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (Session::has('orders.sort_col') and Session::has('orders.sort_way')) {
|
|
|
|
|
switch (Session::get('orders.sort_col')) {
|
|
|
|
|
case 'date':
|
|
|
|
|
$orders = $orders->orderBy('date', Session::get('orders.sort_way'));
|
|
|
|
|
break;
|
|
|
|
|
case 'status_id':
|
|
|
|
|
$orders = $orders->orderBy('status_id', Session::get('orders.sort_way'));
|
|
|
|
|
break;
|
|
|
|
|
case 'magasin_id':
|
|
|
|
|
$orders = $orders->select('orders.*');
|
|
|
|
|
$orders = $orders->leftJoin('timeslots', 'timeslots.order_id', '=', 'orders.id');
|
|
|
|
|
$orders = $orders->leftJoin('magasins', 'timeslots.magasin_id', '=', 'magasins.id');
|
|
|
|
|
$orders = $orders->orderBy('magasins.id', Session::get('orders.sort_way'));
|
|
|
|
|
break;
|
|
|
|
|
case 'date_retrait':
|
|
|
|
|
$orders = $orders->select('orders.*');
|
|
|
|
|
$orders = $orders->leftJoin('timeslots', 'timeslots.order_id', '=', 'orders.id');
|
|
|
|
|
$orders = $orders->orderBy('timeslots.day', Session::get('orders.sort_way'));
|
|
|
|
|
$orders = $orders->orderBy('timeslots.time', Session::get('orders.sort_way'));
|
|
|
|
|
break;
|
|
|
|
|
case 'customer_id':
|
|
|
|
|
$orders = $orders->orderBy('customer_id', Session::get('orders.sort_way'));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-28 13:42:43 +02:00
|
|
|
return $orders;
|
2021-04-28 11:49:44 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* action de changement de statut vers l'avant
|
2021-04-16 09:00:36 +02:00
|
|
|
* envoi de mails transctionnels
|
|
|
|
|
* @param String $reference reference de la commande
|
2021-04-14 09:34:23 +02:00
|
|
|
*/
|
2021-04-16 09:00:36 +02:00
|
|
|
public function changeStatus($reference)
|
|
|
|
|
{
|
|
|
|
|
$order = Order::where('reference', $reference)->first();
|
2020-11-17 13:41:31 +01:00
|
|
|
$order->status_id = $order->status_id + 1;
|
|
|
|
|
$order->save();
|
|
|
|
|
|
2021-04-14 10:39:20 +02:00
|
|
|
//send confirm mail
|
|
|
|
|
Mail::to($order->customer->email)->send(new OrderStatusMail($order));
|
|
|
|
|
|
2021-04-13 18:45:36 +02:00
|
|
|
//return redirect()->route('bo.orders')->with('success', "Changement de status enregistré pour la commande " . $order->reference . ".");
|
|
|
|
|
return back()->with('success', "Changement de status enregistré pour la commande " . $order->reference . ".");
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* mise au statut annulé d'une commande
|
|
|
|
|
*/
|
2021-04-16 09:00:36 +02:00
|
|
|
public function cancelOrder($reference)
|
|
|
|
|
{
|
|
|
|
|
$order = Order::where('reference', $reference)->first();
|
2020-11-17 13:41:31 +01:00
|
|
|
$order->status_id = 7;
|
|
|
|
|
$order->save();
|
|
|
|
|
|
2021-04-13 18:45:36 +02:00
|
|
|
//return redirect()->route('bo.orders')->with('success', "La commande à été annulée.");
|
|
|
|
|
return back()->with('success', "La commande " . $order->reference . " à été annulée.");
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* @deprecated
|
|
|
|
|
* ajout de produits à la commande
|
|
|
|
|
* @todo : supprimer cette méthode qui repose sur un modèle de donnée osolète
|
|
|
|
|
*/
|
2021-04-16 09:00:36 +02:00
|
|
|
public function addProduct($orderReference, $productId, $quantity)
|
|
|
|
|
{
|
2021-03-17 09:28:02 +01:00
|
|
|
$product = Product::findOrFail($productId);
|
2021-04-16 09:00:36 +02:00
|
|
|
$order = Order::where('reference', $orderReference)->first();
|
2020-11-17 13:41:31 +01:00
|
|
|
|
2021-04-16 09:00:36 +02:00
|
|
|
$order->total = $order->total + (($product->price - $product->price_buy) * $quantity);
|
|
|
|
|
$orderProduct = new OrdersProducts();
|
2020-11-17 13:41:31 +01:00
|
|
|
$orderProduct->product_id = $product->id;
|
2021-04-16 09:00:36 +02:00
|
|
|
$orderProduct->order_id = $order->id;
|
|
|
|
|
$orderProduct->quantity = $quantity;
|
2020-11-17 13:41:31 +01:00
|
|
|
$orderProduct->save();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('bo.orders')->with('success', "Le produit à été ajouté.");
|
|
|
|
|
}
|
2021-03-27 20:55:16 +01:00
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* front
|
|
|
|
|
* récap panier du tunnel de commande
|
|
|
|
|
* @todo : déplacer vers le controller idoine
|
|
|
|
|
*/
|
2021-03-27 20:55:16 +01:00
|
|
|
public function recapOrder()
|
|
|
|
|
{
|
|
|
|
|
$activestep = 2;
|
2021-07-21 17:10:11 +02:00
|
|
|
Cart::instance('panier');
|
2021-03-27 20:55:16 +01:00
|
|
|
$cart = Cart::content();
|
|
|
|
|
|
|
|
|
|
$cartInfos = array(
|
|
|
|
|
"totalTTC" => Cart::total(),
|
|
|
|
|
'totalTVA' => Cart::tax(), //total des taxes
|
|
|
|
|
'totalHT' => Cart::subtotal(), //total sans les taxes
|
|
|
|
|
'totalweight' => Cart::weight(), //poids total des items
|
|
|
|
|
'totalHTbefore' => Cart::priceTotal(), //total avant taxes et remises
|
|
|
|
|
'totaldiscount' => Cart::discount(), //total des remises
|
|
|
|
|
'countItems' => Cart::count(), //nb de produits dans le cart
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return view('sales.recap', compact('activestep', 'cart', 'cartInfos'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-14 09:34:23 +02:00
|
|
|
* front
|
2021-03-27 20:55:16 +01:00
|
|
|
* Storing order from sales tunnel
|
|
|
|
|
*/
|
|
|
|
|
public function store()
|
|
|
|
|
{
|
|
|
|
|
//save
|
2021-07-22 16:22:45 +02:00
|
|
|
Cart::instance('panier');
|
2021-03-29 15:10:07 +02:00
|
|
|
$order = new Order();
|
2021-07-21 17:10:11 +02:00
|
|
|
//generate order ref
|
|
|
|
|
$order->reference = $this->generateOrderRef();
|
2021-03-29 15:10:07 +02:00
|
|
|
$order->date = Carbon::now();
|
|
|
|
|
$order->status_id = 1;
|
|
|
|
|
$order->total = str_replace(',', '.', str_replace(' ', '', Cart::total()));
|
2021-03-27 20:55:16 +01:00
|
|
|
$order->customer_id = Auth::user()->id;
|
|
|
|
|
|
|
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
//order_id
|
2021-07-21 17:10:11 +02:00
|
|
|
Cart::instance('panier');
|
2021-03-27 20:55:16 +01:00
|
|
|
$cart = Cart::content();
|
2021-03-29 15:10:07 +02:00
|
|
|
foreach ($cart as $cartItem) {
|
2021-04-16 09:00:36 +02:00
|
|
|
$order_line = new OrderLine();
|
|
|
|
|
$order_line->order_id = $order->id;
|
2021-03-27 20:55:16 +01:00
|
|
|
$order_line->product_id = $cartItem->model->id;
|
2021-04-16 09:00:36 +02:00
|
|
|
$order_line->quantity = $cartItem->qty;
|
2021-04-08 15:49:20 +02:00
|
|
|
//gross unit price
|
|
|
|
|
$order_line->unit_price = $cartItem->model->priceHt;
|
|
|
|
|
//discount per unit
|
|
|
|
|
$order_line->unit_discount = $cartItem->model->discountAmount;
|
|
|
|
|
//tax rate for this line
|
2021-03-27 20:55:16 +01:00
|
|
|
$order_line->tax_rate = $cartItem->taxRate;
|
2021-04-08 15:49:20 +02:00
|
|
|
//tax amount par unit
|
2021-06-10 17:02:28 +02:00
|
|
|
$order_line->unit_tax = round(( ($order_line->unit_price - $order_line->unit_discount) * ($order_line->tax_rate / 100) ) * 100) / 100;
|
2021-04-08 15:49:20 +02:00
|
|
|
//discount amount for the line
|
2021-03-27 20:55:16 +01:00
|
|
|
$order_line->total_discount = $order_line->unit_discount * $order_line->quantity;
|
2021-04-08 15:49:20 +02:00
|
|
|
//gross amount for the line
|
|
|
|
|
$order_line->total_ht = ($order_line->unit_price - $order_line->unit_discount) * $order_line->quantity;
|
|
|
|
|
//tax amount for the line
|
2021-03-27 20:55:16 +01:00
|
|
|
$order_line->total_tax = $order_line->unit_tax * $order_line->quantity;
|
2021-04-08 15:49:20 +02:00
|
|
|
//total TTC avec remises de la ligne
|
2021-03-27 20:55:16 +01:00
|
|
|
$order_line->total_ttc = $order_line->total_ht + $order_line->total_tax;
|
2021-04-08 15:49:20 +02:00
|
|
|
|
2021-03-27 20:55:16 +01:00
|
|
|
$order_line->save();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//empty cart
|
|
|
|
|
Cart::destroy();
|
|
|
|
|
//then redirect
|
2021-03-29 15:10:07 +02:00
|
|
|
return redirect()->route('sales.pick-slot', ['reference' => $order->reference]);
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-21 17:10:11 +02:00
|
|
|
/**
|
|
|
|
|
* generate order Reference
|
|
|
|
|
* for a new order
|
|
|
|
|
*/
|
|
|
|
|
public function generateOrderRef() {
|
|
|
|
|
|
|
|
|
|
//Avant
|
|
|
|
|
//Str::uuid();
|
|
|
|
|
//maintenant
|
|
|
|
|
//format : YYYYMMDDHHMM000
|
|
|
|
|
$now = Carbon::now();
|
|
|
|
|
//count todays orders
|
|
|
|
|
$countorders = Order::whereDate('created_at', Carbon::today())->count();
|
|
|
|
|
//add one
|
|
|
|
|
$countorders++;
|
|
|
|
|
$ref = $now->isoFormat('YYYYMMDDHHmm') . str_pad($countorders,3,"0",STR_PAD_LEFT);
|
|
|
|
|
return $ref;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* front tunnel de commande étape 3 : choix de créneau
|
|
|
|
|
*/
|
2021-03-29 15:10:07 +02:00
|
|
|
public function timeSlotPick($reference)
|
2021-03-27 20:55:16 +01:00
|
|
|
{
|
|
|
|
|
$activestep = 3;
|
|
|
|
|
|
2021-07-27 14:33:32 +02:00
|
|
|
$magasin = Magasin::all(); //first();
|
2021-04-22 18:18:52 +02:00
|
|
|
|
|
|
|
|
$availCreneaux = array();
|
|
|
|
|
//plus besoin car chargé par le composant vue.
|
|
|
|
|
//$availCreneaux = $this->getAvailableCreneauxByShop($magasin->id);
|
|
|
|
|
|
|
|
|
|
$order = Order::where('reference', '=', $reference)->firstOrFail();
|
|
|
|
|
|
|
|
|
|
return view('sales.pick-slot', compact('activestep', 'availCreneaux', 'order', 'magasin'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
|
|
|
|
public function getAvailableCreneauxByShop($id)
|
|
|
|
|
{
|
2021-04-20 17:15:25 +02:00
|
|
|
//setting à stocker et réutiliser
|
2021-04-23 11:59:35 +02:00
|
|
|
$plage_days = Setting::where('key', 'order_withdrawal_period')->firstOrFail()->value;
|
2021-03-29 15:10:07 +02:00
|
|
|
|
2021-04-22 18:18:52 +02:00
|
|
|
$magasin = Magasin::findOrFail($id);
|
|
|
|
|
$timeslotSettings = $magasin->timeslotSettings;
|
|
|
|
|
//$timeslotSettings = TimeslotSetting::all();
|
2021-04-20 17:15:25 +02:00
|
|
|
//fabriquons un tableau recap
|
|
|
|
|
$plage = array();
|
2021-04-22 18:18:52 +02:00
|
|
|
foreach ($timeslotSettings as $day) {
|
2021-04-20 17:15:25 +02:00
|
|
|
|
|
|
|
|
$plage[$day->day_index] = array(
|
|
|
|
|
'morning_start' => $day->morning_start,
|
|
|
|
|
'morning_end' => $day->morning_end,
|
|
|
|
|
'afternoon_start' => $day->afternoon_start,
|
|
|
|
|
'afternoon_end' => $day->afternoon_end,
|
|
|
|
|
'interval' => $day->interval,
|
2021-04-22 11:16:05 +02:00
|
|
|
'min_order_preparation' => $day->min_order_preparation,
|
|
|
|
|
'max_orders_per_interval' => $day->max_orders_per_interval
|
2021-04-20 17:15:25 +02:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-29 15:10:07 +02:00
|
|
|
//génération des plages
|
|
|
|
|
//start today
|
2021-04-22 18:18:52 +02:00
|
|
|
$today = CarbonImmutable::now()->startOfDay();
|
|
|
|
|
$weekDay = $today->weekday();
|
2021-04-21 16:48:37 +02:00
|
|
|
//final boundary (permet de limiter la liste des jours d'exception)
|
|
|
|
|
$finalday = $today->add($plage_days, 'day');
|
2021-04-22 18:18:52 +02:00
|
|
|
$now = CarbonImmutable::now();
|
2021-04-21 16:48:37 +02:00
|
|
|
//jours exceptions
|
2021-04-22 18:18:52 +02:00
|
|
|
// $noCollectDays = CollecteException::where('magasin_id',$magasin->id)
|
|
|
|
|
// ->whereDate('date', '>', $now)
|
|
|
|
|
// ->whereDate('date', '<=', $finalday)
|
|
|
|
|
// ->orderBy('date')->get();
|
|
|
|
|
$noCollectDays = $magasin->collecteExceptions()
|
|
|
|
|
->whereDate('date', '>', $now)
|
|
|
|
|
->whereDate('date', '<=', $finalday)
|
|
|
|
|
->orderBy('date')->get();
|
|
|
|
|
|
|
|
|
|
$delaiPrepa = CarbonInterval::createFromFormat('h:i', $plage[$weekDay]['min_order_preparation'] ?? '01:00');
|
2021-03-29 15:10:07 +02:00
|
|
|
|
|
|
|
|
$availCreneaux = array();
|
|
|
|
|
|
|
|
|
|
for ($i = 0; $i < $plage_days; $i++) {
|
|
|
|
|
/** @var Carbon */
|
2021-04-22 18:18:52 +02:00
|
|
|
$current = $today->add($i, 'day');
|
|
|
|
|
$weekDay = $current->weekday();
|
|
|
|
|
$interval = CarbonInterval::createFromFormat('h:i', $plage[$weekDay]['interval'] ?? '00:15');
|
|
|
|
|
|
2021-04-21 16:48:37 +02:00
|
|
|
//si c'est un jour exception skip
|
|
|
|
|
$skip = false;
|
|
|
|
|
foreach ($noCollectDays as $not_today) {
|
2021-04-22 18:18:52 +02:00
|
|
|
if ($current->isSameDay($not_today->date)) {
|
2021-04-21 16:48:37 +02:00
|
|
|
$skip = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-22 18:18:52 +02:00
|
|
|
if ($skip) {
|
2021-04-21 16:48:37 +02:00
|
|
|
continue;
|
|
|
|
|
}
|
2021-04-20 17:15:25 +02:00
|
|
|
//matinée
|
|
|
|
|
if (!empty($plage[$weekDay]['morning_start'])) {
|
|
|
|
|
$currentTime = Carbon::createFromTimeString($plage[$weekDay]['morning_start']);
|
|
|
|
|
$endTime = Carbon::createFromTimeString($plage[$weekDay]['morning_end']);
|
2021-03-29 15:10:07 +02:00
|
|
|
do {
|
|
|
|
|
$creneau = $currentTime->format('H:i');
|
2021-04-23 11:59:35 +02:00
|
|
|
$nbtakens = $magasin->timeslots()
|
|
|
|
|
->where('day', $current->format('d/m/Y'))
|
2021-04-22 18:18:52 +02:00
|
|
|
->where('time', $creneau)->count();
|
|
|
|
|
|
2021-04-23 11:59:35 +02:00
|
|
|
// $nbtakens = Timeslot::where('day', $current->format('d/m/Y'))
|
|
|
|
|
// ->where('time', $creneau)->count();
|
|
|
|
|
|
2021-04-22 18:18:52 +02:00
|
|
|
if ($nbtakens >= $plage[$weekDay]['max_orders_per_interval']) {
|
2021-04-22 11:16:05 +02:00
|
|
|
$isDispo = false;
|
|
|
|
|
} else {
|
|
|
|
|
$isDispo = true;
|
|
|
|
|
}
|
2021-04-22 18:18:52 +02:00
|
|
|
|
|
|
|
|
if($current->addHours($currentTime->format('H'))->addMinutes($currentTime->format('i')) < $now->add($delaiPrepa)) {
|
|
|
|
|
$isDispo = false;
|
|
|
|
|
}
|
2021-04-22 11:16:05 +02:00
|
|
|
//attention si la date est passée (today mais avant maintenant) : pas de créneau
|
2021-04-22 18:18:52 +02:00
|
|
|
if ($current > $today || $currentTime >= $now) {
|
2021-04-22 11:16:05 +02:00
|
|
|
$availCreneaux[$current->format('d/m/Y')][] = array($creneau,$isDispo);
|
2021-04-20 17:15:25 +02:00
|
|
|
}
|
|
|
|
|
$currentTime->add($interval);
|
|
|
|
|
} while ($currentTime <= $endTime);
|
|
|
|
|
}
|
|
|
|
|
//aprem
|
|
|
|
|
if (!empty($plage[$weekDay]['afternoon_start'])) {
|
|
|
|
|
$currentTime = Carbon::createFromTimeString($plage[$weekDay]['afternoon_start']);
|
|
|
|
|
$endTime = Carbon::createFromTimeString($plage[$weekDay]['afternoon_end']);
|
|
|
|
|
do {
|
|
|
|
|
$creneau = $currentTime->format('H:i');
|
2021-04-22 11:16:05 +02:00
|
|
|
$nbtakens = Timeslot::where('day',$current->format('d/m/Y'))->where('time',$creneau)->count();
|
|
|
|
|
if($nbtakens >= $plage[$weekDay]['max_orders_per_interval']) {
|
|
|
|
|
$isDispo = false;
|
|
|
|
|
} else {
|
|
|
|
|
$isDispo = true;
|
|
|
|
|
}
|
2021-04-22 18:18:52 +02:00
|
|
|
|
|
|
|
|
if($current->addHours($currentTime->format('H'))->addMinutes($currentTime->format('i')) < $now->add($delaiPrepa)) {
|
|
|
|
|
$isDispo = false;
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-20 17:15:25 +02:00
|
|
|
//attention si la date est passée (today mais avant maintenant) : pas de créneau
|
2021-04-22 18:18:52 +02:00
|
|
|
if ($current > $today || $currentTime >= $now) {
|
2021-04-22 11:16:05 +02:00
|
|
|
$availCreneaux[$current->format('d/m/Y')][] = array($creneau,$isDispo);
|
2021-03-29 15:10:07 +02:00
|
|
|
}
|
|
|
|
|
$currentTime->add($interval);
|
|
|
|
|
} while ($currentTime <= $endTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-04-22 18:18:52 +02:00
|
|
|
return $availCreneaux;
|
|
|
|
|
}
|
2021-04-20 17:15:25 +02:00
|
|
|
|
2021-04-22 18:18:52 +02:00
|
|
|
//call ajax pour le composant vue de timeslots
|
|
|
|
|
public function getAvailableCreneaux(Request $request) {
|
|
|
|
|
return $this->getAvailableCreneauxByShop($request->id);
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* front
|
|
|
|
|
* enregistrement du créneau de collect
|
|
|
|
|
* et validation de la commande
|
|
|
|
|
*/
|
2021-03-29 15:10:07 +02:00
|
|
|
public function setTimeSlot(Request $request)
|
2021-03-27 20:55:16 +01:00
|
|
|
{
|
2021-04-22 11:16:05 +02:00
|
|
|
$request->validate([
|
2021-04-23 11:59:35 +02:00
|
|
|
'selectedmagasin' => 'required',
|
2021-04-22 11:16:05 +02:00
|
|
|
'selectedcreneau' => 'required',
|
|
|
|
|
'ok_cgv' => 'accepted',
|
|
|
|
|
]);
|
|
|
|
|
|
2021-03-29 15:10:07 +02:00
|
|
|
$time_slot = Carbon::createFromFormat('d/m/Y-H:i', $request->selectedcreneau);
|
2021-04-16 09:00:36 +02:00
|
|
|
$tsday = explode('-', $request->selectedcreneau)[0];
|
|
|
|
|
$tstime = explode('-', $request->selectedcreneau)[1];
|
2021-04-23 11:59:35 +02:00
|
|
|
$magasin_id = $request->selectedmagasin;
|
2021-03-29 15:10:07 +02:00
|
|
|
$reference = $request->order_ref;
|
2021-04-16 09:00:36 +02:00
|
|
|
$order = Order::where('reference', '=', $reference)->firstOrFail();
|
2021-03-29 15:10:07 +02:00
|
|
|
|
2021-04-16 09:00:36 +02:00
|
|
|
if ($order->status_id == 1) {
|
2021-03-29 15:10:07 +02:00
|
|
|
// on ne traite l'enregistrement que pour les commandes en attente venant de l'étape de récap
|
|
|
|
|
//save timeslot
|
2021-04-23 11:59:35 +02:00
|
|
|
$timeslot = Timeslot::firstOrNew(['order_id' => $order->id]);
|
|
|
|
|
$timeslot->order_id = $order->id;
|
|
|
|
|
$timeslot->magasin_id = $magasin_id;
|
|
|
|
|
$timeslot->day = $tsday;
|
|
|
|
|
$timeslot->time = $tstime;
|
2021-03-29 15:10:07 +02:00
|
|
|
$timeslot->save();
|
|
|
|
|
|
|
|
|
|
//save order status
|
2021-04-16 09:00:36 +02:00
|
|
|
$order->status_id = 3;
|
2021-03-29 15:10:07 +02:00
|
|
|
$order->save();
|
|
|
|
|
|
|
|
|
|
//send confirm mail
|
2021-07-26 15:24:12 +02:00
|
|
|
Mail::to($order->customer->email)->queue(new OrderConfirmMail($order));
|
2021-05-26 15:40:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
//envoi du mail de confirmation aux admins site
|
|
|
|
|
$toMailSetting = Setting::where('key', 'send_order_confirm_mail_to')->first();
|
|
|
|
|
if(empty($toMailSetting->value)) {
|
|
|
|
|
$toMail = config('mail.from.address');
|
|
|
|
|
} else {
|
|
|
|
|
$toMail = $toMailSetting->value;
|
|
|
|
|
}
|
2021-07-26 15:24:12 +02:00
|
|
|
Mail::to($toMail)->queue(new OrderConfirmAdminMail($order));
|
2021-07-23 15:12:55 +02:00
|
|
|
|
2021-07-26 15:24:12 +02:00
|
|
|
//export commande
|
|
|
|
|
if (config('bosettings.regate_orders')) {
|
|
|
|
|
CsvOrderWrite::dispatch($order);
|
|
|
|
|
}
|
2021-03-29 15:10:07 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-27 20:55:16 +01:00
|
|
|
//redirect to final confirmation
|
2021-03-29 15:10:07 +02:00
|
|
|
return redirect()->route('sales.confirm', ['reference' => $order->reference]);
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-07-23 15:12:55 +02:00
|
|
|
public function generateOrder($reference) {
|
|
|
|
|
$order = Order::where('reference', '=', $reference)->with('lines')->firstOrFail();
|
|
|
|
|
$job = new CsvOrderWrite($order);
|
|
|
|
|
$job->handle();
|
|
|
|
|
dd('end');
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* front
|
|
|
|
|
* page de confirmation de commande
|
|
|
|
|
*/
|
2021-03-29 15:10:07 +02:00
|
|
|
public function confirmOrder($reference)
|
2021-03-27 20:55:16 +01:00
|
|
|
{
|
|
|
|
|
$activestep = 4;
|
|
|
|
|
|
2021-03-29 15:10:07 +02:00
|
|
|
$idcommande = Session::get('idcommande');
|
2021-04-16 09:00:36 +02:00
|
|
|
$order = Order::where('reference', '=', $reference)->firstOrFail();
|
2021-03-29 15:10:07 +02:00
|
|
|
|
2021-04-21 17:50:57 +02:00
|
|
|
//1 seul magasin en base pour le moment
|
|
|
|
|
$magasin = Magasin::first();
|
|
|
|
|
|
|
|
|
|
return view('sales.confirm', compact('activestep', 'order', 'magasin'));
|
2021-03-27 20:55:16 +01:00
|
|
|
}
|
2021-04-13 17:21:39 +02:00
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* bo
|
|
|
|
|
* ajx : affichage d'infos sur un client
|
|
|
|
|
* @todo : déplacer dans le controller idoine
|
|
|
|
|
*/
|
2021-04-27 12:56:42 +02:00
|
|
|
public function getCustomerInfo($id, $mode = null)
|
2021-04-16 09:00:36 +02:00
|
|
|
{
|
2021-04-13 17:21:39 +02:00
|
|
|
$cust = Customers::findOrFail($id);
|
2021-04-27 12:56:42 +02:00
|
|
|
if(!is_null($mode)) {
|
|
|
|
|
if($mode=="tippy") {
|
|
|
|
|
return view('bo.customer.tippy-card', compact('cust'));
|
|
|
|
|
}
|
|
|
|
|
if($mode=="json") {
|
|
|
|
|
return $cust;
|
|
|
|
|
}
|
|
|
|
|
return view('bo.customer.info-card', compact('cust'));
|
|
|
|
|
}
|
2021-04-13 18:45:36 +02:00
|
|
|
return view('bo.customer.info-popup', compact('cust'));
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-14 09:34:23 +02:00
|
|
|
/**
|
|
|
|
|
* bo : page détatil commande
|
|
|
|
|
*/
|
2021-04-16 09:00:36 +02:00
|
|
|
public function orderDetail($reference)
|
|
|
|
|
{
|
2021-04-13 18:45:36 +02:00
|
|
|
$order = Order::where('reference', '=', $reference)->firstOrFail();
|
2021-04-14 09:34:23 +02:00
|
|
|
return view('bo.orders.detail', compact('order'));
|
2021-04-13 17:21:39 +02:00
|
|
|
}
|
2020-11-17 13:41:31 +01:00
|
|
|
}
|