ldc-boutique-cnc/app/Http/Controllers/Bo/OrderController.php

341 lines
11 KiB
PHP
Raw Normal View History

2020-11-17 13:41:31 +01:00
<?php
namespace App\Http\Controllers\Bo;
use App\Enums\Order\ValidationStatus;
2020-11-17 13:41:31 +01:00
use App\Http\Controllers\Controller;
use App\Http\Requests\SortOrders;
use App\Jobs\CsvOrderWrite;
2022-12-20 11:32:06 +01:00
use App\Mail\OrderConfirmAdminMail;
use App\Mail\OrderConfirmMail;
use App\Mail\OrderStatusMail;
use App\Models\Customer;
2022-12-20 11:32:06 +01:00
use App\Models\Order;
use App\Models\OrderLine;
use App\Models\Setting;
2022-12-20 11:32:06 +01:00
use App\Models\Shop;
use App\Models\Timeslot;
2021-03-27 20:55:16 +01:00
use Carbon\Carbon;
2021-03-29 15:10:07 +02:00
use Carbon\CarbonImmutable;
2021-03-27 20:55:16 +01:00
use Gloudemans\Shoppingcart\Facades\Cart;
2021-03-29 15:10:07 +02:00
use Illuminate\Http\Request;
2022-12-20 11:32:06 +01:00
use Illuminate\Support\Facades\Mail;
2021-03-29 15:10:07 +02:00
use Illuminate\Support\Facades\Session;
2020-11-17 13:41:31 +01:00
2022-10-03 17:13:44 +02:00
class OrderController extends Controller
2020-11-17 13:41:31 +01:00
{
2021-04-14 09:34:23 +02:00
/**
2022-09-03 16:56:16 +02:00
* Display a listing of the resource.
*
2022-10-03 17:13:44 +02:00
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Contracts\View\View|\Illuminate\Contracts\View\Factory
2021-04-14 09:34:23 +02:00
*/
2022-10-03 17:13:44 +02:00
public function index(Request $request)
{
2022-10-03 17:13:44 +02:00
/** @var \Illuminate\Database\Eloquent\Builder $orders */
$orders = Order::query();
if ($request->search) {
$this->searchQuery(
$orders,
$request->search,
null,
'label'
);
}
2022-10-03 17:13:44 +02:00
// 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('shops', 'timeslots.shop_id', '=', 'shops.id');
// $orders = $orders->orderBy('shops.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;
// }//end switch
// }//end if
$this->sortQuery($orders);
$searchFields = trans('Label');
$orders->orderBy('updated_at', 'DESC');
$orders = $orders->paginate(10);
2022-10-03 17:13:44 +02:00
return view('back.orders.index', compact('orders', 'searchFields'));
}
/**
* Met a jour le statut de validation d'une commande en annulé
*
* @param \App\Models\Order $order
* @return \Illuminate\Http\RedirectResponse
*/
public function cancelOrder(Order $order)
{
$order->validation_status = ValidationStatus::cancelled();
$order->saveOrFail();
return back()->with('success', "La commande {$order->reference} a été annulée.");
}
/**
* bo : page détail commande
*
* @param \App\Models\Order $order
* @return \Illuminate\Contracts\View\View
*/
public function orderDetails(Order $order)
{
return view('back.orders.details', [
'orderModel' => $order
]);
2020-11-17 13:41:31 +01:00
}
2022-10-03 17:13:44 +02:00
2022-09-03 16:56:16 +02:00
/**
* orders that ship today
*
* @param SortOrders $request
* @return \Illuminate\Contracts\View\View
*/
public function today(SortOrders $request)
{
$orders = $this->filterOrders($request);
2022-09-03 16:56:16 +02:00
// Filter today.
$today = CarbonImmutable::now()->startOfDay();
$orders = $orders->whereHas('timeslot', function ($q) use ($today) {
$q->where('day', '=', $today->format('d/m/Y'));
});
2022-08-25 12:26:43 +02:00
$orders = $orders->get();
2022-10-03 17:13:44 +02:00
return view('back.orders.index', compact('orders', 'today'));
}
2021-04-14 09:34:23 +02:00
/**
* action de changement de statut vers l'avant
* envoi de mails transctionnels
2022-09-03 16:56:16 +02:00
*
* @param string $reference Reference de la commande.
* @return \Illuminate\Http\RedirectResponse
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function changeStatus(string $reference)
{
$order = Order::where('reference', $reference)->first();
2020-11-17 13:41:31 +01:00
$order->status_id = $order->status_id + 1;
$order->save();
2022-09-03 16:56:16 +02:00
// Send confirm mail.
2021-04-14 10:39:20 +02:00
Mail::to($order->customer->email)->send(new OrderStatusMail($order));
2021-04-13 18:45:36 +02:00
return back()->with('success', "Changement de status enregistré pour la commande " . $order->reference . ".");
2020-11-17 13:41:31 +01:00
}
2021-03-27 20:55:16 +01:00
/**
2021-04-14 09:34:23 +02:00
* front
2021-03-27 20:55:16 +01:00
* Storing order from sales tunnel
2022-09-03 16:56:16 +02:00
*
* @return \Illuminate\Http\RedirectResponse
2021-03-27 20:55:16 +01:00
*/
public function store()
{
2022-09-03 16:56:16 +02:00
// Save.
2021-07-22 16:22:45 +02:00
Cart::instance('panier');
$order = new Order();
2022-09-03 16:56:16 +02:00
// Generate order ref.
2021-07-21 17:10:11 +02:00
$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()));
2022-12-19 16:20:00 +01:00
$order->customer_id = auth('backend')->user()->id;
2021-03-27 20:55:16 +01:00
$order->save();
2022-09-03 16:56:16 +02:00
// 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) {
$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;
$order_line->quantity = $cartItem->qty;
2022-09-03 16:56:16 +02:00
// Gross unit price.
$order_line->unit_price = $cartItem->model->priceHt;
2022-09-03 16:56:16 +02:00
// Discount per unit.
$order_line->unit_discount = $cartItem->model->discountAmount;
2022-09-03 16:56:16 +02:00
// Tax rate for this line.
2021-03-27 20:55:16 +01:00
$order_line->tax_rate = $cartItem->taxRate;
2022-09-03 16:56:16 +02:00
// Tax amount par unit.
$order_line->unit_tax = round(
(($order_line->unit_price - $order_line->unit_discount) * ($order_line->tax_rate / 100)) * 100
) / 100;
// Discount amount for the line.
2021-03-27 20:55:16 +01:00
$order_line->total_discount = $order_line->unit_discount * $order_line->quantity;
2022-09-03 16:56:16 +02:00
// Gross amount for the line.
$order_line->total_ht = ($order_line->unit_price - $order_line->unit_discount) * $order_line->quantity;
2022-09-03 16:56:16 +02:00
// Tax amount for the line.
2021-03-27 20:55:16 +01:00
$order_line->total_tax = $order_line->unit_tax * $order_line->quantity;
2022-09-03 16:56:16 +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-03-27 20:55:16 +01:00
$order_line->save();
2022-10-03 17:13:44 +02:00
} //end foreach
2021-03-27 20:55:16 +01:00
2022-09-03 16:56:16 +02:00
// Empty cart.
2021-03-27 20:55:16 +01:00
Cart::destroy();
2022-09-03 16:56:16 +02:00
// Then redirect.
return redirect()->route('order-tunnel.pick-slot', ['reference' => $order->reference]);
2021-03-27 20:55:16 +01:00
}
2021-04-14 09:34:23 +02:00
/**
* front tunnel de commande étape 3 : choix de créneau
2022-09-03 16:56:16 +02:00
*
* @param string $reference
* @return \Illuminate\Contracts\View\View
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function timeSlotPick(string $reference)
2021-03-27 20:55:16 +01:00
{
$activestep = 3;
if (config('theme.multi_relay')) {
2022-10-03 17:13:44 +02:00
$shop = Shop::all();
} else {
2022-10-03 17:13:44 +02:00
$shop = Shop::first();
}
$availCreneaux = array();
2022-09-03 16:56:16 +02:00
$order = Order::where('reference', '=', $reference)->firstOrFail();
2022-10-03 17:13:44 +02:00
return view('sales.pick-slot', compact('activestep', 'availCreneaux', 'order', 'shop'));
}
2021-04-14 09:34:23 +02:00
/**
* front
* enregistrement du créneau de collect
* et validation de la commande
2022-09-03 16:56:16 +02:00
*
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
2021-04-14 09:34:23 +02:00
*/
2021-03-29 15:10:07 +02:00
public function setTimeSlot(Request $request)
2021-03-27 20:55:16 +01:00
{
$request->validate([
'selectedmagasin' => 'required',
'selectedcreneau' => 'required',
'ok_cgv' => 'accepted',
]);
$time_slot = Carbon::createFromFormat('d/m/Y-H:i', $request->selectedcreneau);
$tsday = explode('-', $request->selectedcreneau)[0];
$tstime = explode('-', $request->selectedcreneau)[1];
$magasin_id = $request->selectedmagasin;
$reference = $request->order_ref;
$order = Order::where('reference', '=', $reference)->firstOrFail();
2021-03-29 15:10:07 +02:00
if ($order->status_id == 1) {
2022-09-03 16:56:16 +02:00
// On ne traite l'enregistrement que pour les commandes en attente venant de l'étape de récap.
// Save timeslot.
$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();
2022-09-03 16:56:16 +02:00
// Save order status.
$order->status_id = 3;
2021-03-29 15:10:07 +02:00
$order->save();
2022-09-03 16:56:16 +02:00
// Send confirm mail.
Mail::to($order->customer->email)->queue(new OrderConfirmMail($order));
2022-09-03 16:56:16 +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;
}
Mail::to($toMail)->queue(new OrderConfirmAdminMail($order));
2022-10-03 17:13:44 +02:00
} //end if
2021-03-29 15:10:07 +02:00
2022-09-03 16:56:16 +02:00
// Redirect to final confirmation.
return redirect()->route('order-tunnel.confirm', ['reference' => $order->reference]);
2021-03-27 20:55:16 +01:00
}
2022-09-03 16:56:16 +02:00
/**
* generate Order
* Test method
*
* @param string $reference
* @return void
*/
public function generateOrder(string $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
2022-09-03 16:56:16 +02:00
*
* @param string $reference
* @return \Illuminate\Contracts\View\View
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function confirmOrder(string $reference)
2021-03-27 20:55:16 +01:00
{
$activestep = 4;
2021-03-29 15:10:07 +02:00
$idcommande = Session::get('idcommande');
$order = Order::where('reference', '=', $reference)->firstOrFail();
2021-03-29 15:10:07 +02:00
2022-08-25 12:26:43 +02:00
// 1 seul magasin en base pour le moment
2022-10-03 17:13:44 +02:00
$shop = Shop::find($order->timeslot->shop->id);
2021-04-21 17:50:57 +02:00
2022-10-03 17:13:44 +02:00
return view('sales.confirm', compact('activestep', 'order', 'shop'));
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
2022-09-03 16:56:16 +02:00
*
* @param integer $id
* @param string|null $mode
* @return \Illuminate\Contracts\View\View
2021-04-14 09:34:23 +02:00
*/
2022-09-03 16:56:16 +02:00
public function getCustomerInfo(int $id, string $mode = null)
{
$cust = Customer::findOrFail($id);
if (!is_null($mode)) {
if ($mode == "tippy") {
2022-10-03 17:13:44 +02:00
return view('back.customer.tippy-card', compact('cust'));
}
if ($mode == "json") {
return $cust->apiData;
}
2022-10-03 17:13:44 +02:00
return view('back.customer.info-card', compact('cust'));
}
2022-10-03 17:13:44 +02:00
return view('back.customer.info-popup', compact('cust'));
2021-04-13 17:21:39 +02:00
}
2020-11-17 13:41:31 +01:00
}