File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Shop/Orders/OrderController.php
<?php
namespace App\Komma\Shop\Orders;
use App\Komma\Base\Controller;
use App\Komma\Globalization\RegionInfoInterface;
use App\Komma\Shop\Orders\Mail\OrderDepositPaymentCustomer;
use App\Komma\Shop\Orders\Models\Order;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
class OrderController extends Controller
{
private OrderService $orderService;
private RegionInfoInterface $regioInfo;
public function __construct()
{
parent::__construct();
$this->orderService = app(OrderService::class);
$this->regionInfo = app(RegionInfoInterface::class);
}
/**
* This method is called on the overview page.
* It will render the section and view.
*
* @return mixed
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function index()
{
$this->authorize('index', Order::class);
$orders = $this->orderService->getOrdersForType(OrderStatus::AWAITING_FULFILLMENT);
$orders->loadCount('orderedProducts');
return view('deposit.templates.orders_index', [
'orders' => $orders,
'regionInfo' => $this->regionInfo,
'status' => OrderStatus::AWAITING_FULFILLMENT
]);
}
public function category(int $status)
{
$this->authorize('index', Order::class);
$orders = $this->orderService->getOrdersForType($status);
$orders->loadCount('orderedProducts');
return view('deposit.templates.orders_index', [
'orders' => $orders,
'regionInfo' => $this->regionInfo,
'status' => $status
]);
}
/**
* @param Order $order
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function show(Order $order)
{
$this->authorize('show', $order);
$order->load('intakeTransactions', 'orderedProducts', 'orderedProducts.product', 'orderedProducts.product.translation', 'orderedProducts.product.images');
// Return view
return view('deposit.templates.orders_show',[
'order' => $order,
'regionInfo' => $this->regionInfo,
]);
}
public function search()
{
$orders = $this->orderService->findByOrderNumber(request('id'));
return view('deposit.templates.orders_index', [
'orders' => $orders,
'regionInfo' => $this->regionInfo,
]);
}
/**
* @param Order $order
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(Order $order)
{
$this->authorize('update', $order);
$order->remarks = request('remarks');
$order->status = request('status');
$order->open_fee_note = request('open_fee_note');
$openFee = (float) request('open_fee');
if(is_numeric($openFee)){
$order->open_fee = $openFee * 100;
}
else throw new \UnexpectedValueException('Handling for open fee could not be handled because it is not a numeric.');
$order->save();
request()->session()->flash('flashMessage', [ 'message' => __('deposit.is_saved'), 'type' => 'positive' ]);
return redirect()->route('orders.show', ['order' => $order]);
}
/**
* Update the status and reload the page.
*
* @param Order $order
* @param int $status
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function updateStatus(Order $order, int $status)
{
$this->authorize('update', $order);
$order->status = $status;
$order->save();
return redirect(route('orders.index'));
// return redirect(route('orders.category', ['status' => $status]));
}
/**
* Intake view page
*
* @param Order $order
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function intake(Order $order)
{
$this->authorize('update', $order);
if(!in_array($order->status, [OrderStatus::SHIPPED, OrderStatus::VERIFICATION] )) abort(405, 'Order has not the required status for intake.');
$order->load('orderedProducts', 'orderedProducts.product', 'orderedProducts.product.translation', 'orderedProducts.product.images');
return view('deposit.templates.orders_intake',[
'order' => $order,
'regionInfo' => $this->regionInfo,
]);
}
/**
* Handle the Intake order process
* @param Order $order
* @return \Illuminate\Http\RedirectResponse
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function handleIntake(Order $order)
{
$this->authorize('update', $order);
if(!in_array($order->status, [OrderStatus::SHIPPED, OrderStatus::VERIFICATION] )) abort(405, 'Order has not the required status for intake.');
$order->load('orderedProducts', 'orderedProducts.product');
// Get the products out of the request
$requestProducts = request('products');
// Check that the amount of products match
if(sizeof($requestProducts) !== $order->orderedProducts->count() ) abort(500, 'Amount of products from the request and the ordered products do not match.');
$this->orderService->handleIntakeProducts($requestProducts, $order);
request()->session()->flash('flashMessage', [ 'message' => __('deposit.intake_saved'), 'type' => 'positive' ]);
return redirect()->route('orders.category', ['status' => OrderStatus::VERIFICATION]);
}
public function payOut(Order $order)
{
$intakeRedirectRoute = $this->handleIntake($order);
if($order->getShortageTotal() < $order->deposit) {
$this->orderService->createRefund($order);
request()->session()->flash('flashMessage', [ 'message' => __('deposit.intake_completed'), 'type' => 'positive' ]);
return redirect()->route('orders.category', ['status' => OrderStatus::COMPLETED]);
}
// If the shortage is the same as the deposit, only change the order status to completed
if($order->getShortageTotal() == $order->deposit) {
$order->status = OrderStatus::COMPLETED;
$order->save();
request()->session()->flash('flashMessage', [ 'message' => __('deposit.intake_completed'), 'type' => 'positive' ]);
return redirect()->route('orders.category', ['status' => OrderStatus::COMPLETED]);
}
if($order->getShortageTotal() > $order->deposit) {
$order->status = OrderStatus::DEPOSIT_PAYMENT_SEND;
$order->save();
Mail::to($order->email)->send(new OrderDepositPaymentCustomer($order));
request()->session()->flash('flashMessage', [ 'message' => __('deposit.deposit_payment_send'), 'type' => 'positive' ]);
return redirect()->route('orders.category', ['status' => OrderStatus::VERIFICATION]);
}
throw new \UnexpectedValueException(self::class.': Code should not be able to come here. Why does this happen?');
}
}