File: D:/HostingSpaces/SBogers10/slenders.komma.pro/app/Komma/Shop/Orders/Kms/OrderController.php
<?php
namespace App\Komma\Shop\Orders\Kms;
/**
* @author Komma <info@komma.pro>
* @copyright (c) 2012-2016, Komma
*/
use App\Komma\Globalization\RegionInfo;
use App\Komma\Kms\Core\SectionController;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Shop\Orders\OrderStatus;
use App\Komma\Shop\Orders\SearchRequest;
use App\Komma\Shop\Shipmentgroups\Shipmentgroupservice;
use App\Komma\Shop\Shipments\Shipment;
use App\Komma\Shop\Shipments\ShipmentService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class OrderController extends SectionController
{
protected $slug = "orders";
/** @var string */
protected $forModelName = Order::class;
/** @var OrderService */
private $orderService;
/**
* @var ShipmentService
*/
private $shipmentService;
/**
* @var OrderMailServiceInterface
*/
private $orderMailService;
/**
* @var Shipmentgroupservice
*/
private $shipmentgroupservice;
/**
* Constructor
* @param OrderSection $section
* @param OrderService $orderService
* @param OrderMailServiceInterface $orderMailService
* @param ShipmentService $shipmentService
*/
public function __construct(OrderSection $section, OrderService $orderService, OrderMailServiceInterface $orderMailService, ShipmentService $shipmentService, Shipmentgroupservice $shipmentgroupservice)
{
parent::__construct($section);
$this->treeService->setSectionService($this->section->getSectionService());
$this->treeService->setForModelName(new Order());
$this->orderService = $orderService;
$this->shipmentService = $shipmentService;
$this->orderMailService = $orderMailService;
$this->shipmentgroupservice = $shipmentgroupservice;
}
/**
* 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', $this->forModelName);
/** @var OrderSection $section */
$section = $this->section;
$shopRegionInfo = new RegionInfo('NL'); //Shop was build with euros in mind and the Dutch language as the primary one
$amount = 20;
$ordersCollection = $this->orderService->getLatest($amount)->with('shipments')->get();
$section->setForModelName($this->forModelName);
$view = $section->renderOrderIndex();
$view->with('orders', $ordersCollection)
->with('shopRegionInfo', $shopRegionInfo)
->with('resultsTypeTranslation', __('shop/orders.latest_orders', ['amount' => $amount]));
return $view;
}
/**
* Search for orders
*
* @param SearchRequest $request
* @return \Illuminate\Contracts\View\View|\Illuminate\Http\Resources\Json\AnonymousResourceCollection
*/
public function OrderSearch(SearchRequest $request)
{
$resultsPerPage = (is_numeric(\Input::get('perPage')) ? (int) \Input::get('perPage') : 5);
$ordersCollection = $this->orderService->search(\Input::all())->orderBy('created_at', 'desc')->with('shipments')->paginate($resultsPerPage);
$ordersCollection->appends(\Input::all()); //Add existing query string parameters to the current pagination links
$shopRegionInfo = new RegionInfo('NL'); //Shop was build with euros in mind and the Dutch language as the primary one
if(request()->ajax()) {
//Return results as json
return \App\Komma\Shop\Orders\Resources\Order::collection($ordersCollection);
} else {
$request->flash();
/** @var OrderSection $section */
$section = $this->section;
$section->setForModelName($this->forModelName);
$view = $section->renderOrderIndex();
return $view->with('orders', $ordersCollection)
->with('shopRegionInfo', $shopRegionInfo)
->with('perPage', $resultsPerPage)
->with('status', \Input::get('status'))
->with('resultsTypeTranslation', __('shop/orders.search_results'));
}
}
/**
* Batch edit orders
* @param Request $request
* @return \Illuminate\Http\RedirectResponse
*/
public function batchEdit(Request $request)
{
//TODO add authorisation via policy
$orderIds = \Input::get('order_ids');
if(!$orderIds) return redirect()->back()->withErrors(['general' => __('shop/orders.batch.no_orders_selected')]);
//Convert te order ids to integers instead of strings and use them to get the orders we need to batch edit
$orderIds = array_map(function(string $orderId) {
return (int) $orderId;
}, $orderIds);
$orders = Order::with(['customer', 'shipments'])->whereIn('id', $orderIds)->get();
if(\Input::has('create_shipments')) {
//Authorize
foreach($orders as $order) $this->authorize('createShipmentForOrder', $order);
//User pressed the create shipments button
$shipments = $this->shipmentService->makeForOrders($orders);
$shipments->each(function(Shipment $shipment) { $shipment->save(); });
$orders = $this->orderService->changeOrdersStatus($orders, OrderStatus::AWAITING_SHIPMENT);
$orders->each(function(Order $order) {
$this->orderMailService->mailCustomerAboutCurrentOrderStatus($order, $order->customer);
});
$shipmentCount = count($shipments);
return redirect()->back()->withSuccess(trans_choice('shop/shipments.shipments_created', $shipmentCount, ['count' => $shipmentCount]));
} elseif(\Input::has('group_shipments')) {
foreach($orders as $order) $this->authorize('createShipmentGroupForOrder', $order);
//User pressed the group shipments button
$shipments = $orders->map(function(Order $order) {
return $order->shipments;
})->collapse();
if($shipments->count() == 0) return redirect()->back()->withErrors(__('shop/shipmentgroups.no_shipmentGroup_created_no_shipments'));
$shipmentGroup = $this->shipmentgroupservice->createForShipments($shipments);
if(!$shipmentGroup) return redirect()->back()->withErrors(__('shop/shipmentgroups.no_shipmentGroup_created'));
return redirect()->back()->withSuccess(trans_choice('shop/shipmentgroups.shipmentGroup_created', $shipmentGroup->shipments->count(), ['count' => $shipmentGroup->shipments->count(), 'id' => $shipmentGroup->id]));
} elseif(\Input::has('change_status') && \Input::has('change_status_to')) {
$this->orderService->changeOrdersStatus($orders, \Input::get('change_status_to'));
$orders->each(function(Order $order) {
$this->orderMailService->mailCustomerAboutCurrentOrderStatus($order, $order->customer);
});
return redirect()->back()->withSuccess(trans_choice('shop/orders.batch.status_changed', $orders->count(), ['count' => $orders->count(), 'status' => __('shop/orders.status.'.\Input::get('change_status_to'))]));
}
return redirect()->back()->withErrors(['general' => __('shop/orders.batch.edit_fail')]);
}
/**
* This method handles the update functionality.
* And is called by the edit form.
* The method is route model binded via the RouteServiceProvider to the user class
*
* @param $idOrModel User|int
* @return mixed
* @throws \Exception
*/
public function update($idOrModel)
{
$this->authorize('store', $idOrModel);
if(Input::has('create_shipment'))
{
$shipment = $this->shipmentService->makeForOrder($idOrModel);
$shipment->save();
return redirect()->back();
}
return parent::update($idOrModel); // TODO: Change the autogenerated stub
}
}