Created
May 21, 2015 13:17
-
-
Save sleeping-owl/b1ed982625e3ddc3b712 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers\Shop; | |
use App\Http\Controllers\Controller; | |
use App\Repositories\CartRepository; | |
use App\Repositories\OrderRepository; | |
use App\Repositories\UserRepository; | |
use Config; | |
use Illuminate\Http\Request; | |
use Redirect; | |
use View; | |
class OrderController extends Controller | |
{ | |
/** | |
* @var CartRepository | |
*/ | |
private $cartRepository; | |
/** | |
* @var UserRepository | |
*/ | |
private $userRepository; | |
/** | |
* @var OrderRepository | |
*/ | |
private $orderRepository; | |
function __construct(CartRepository $cartRepository, UserRepository $userRepository, | |
OrderRepository $orderRepository) | |
{ | |
$this->cartRepository = $cartRepository; | |
$this->userRepository = $userRepository; | |
$this->orderRepository = $orderRepository; | |
} | |
public function getShoppingCart($shop) | |
{ | |
$user = $this->userRepository->current(); | |
return View::make('shop.order.cart', compact('shop', 'user')); | |
} | |
public function postShoppingCartAdd($shop, $product, Request $request) | |
{ | |
$amount = $request->get('amount', 1); | |
$this->cartRepository->addItem($product->id, $amount); | |
return Redirect::back(); | |
} | |
public function postShoppingCartDelete($shop, $product) | |
{ | |
$this->cartRepository->deleteItem($product->id); | |
return Redirect::back(); | |
} | |
public function getCheckout($shop) | |
{ | |
$infos = Config::get('shop.user_info'); | |
$userInfos = []; | |
foreach ($infos as $info) | |
{ | |
$userInfos[$info['name']] = $info['description']; | |
} | |
$userData = []; | |
$lastOrder = $this->orderRepository->getLastOrder(); | |
if ( ! is_null($lastOrder)) | |
{ | |
$userData = $lastOrder->data; | |
} | |
$deliveries = $this->getDeliveries($shop); | |
$weight = $this->cartRepository->getWeight(); | |
return View::make('shop.order.checkout', compact('shop', 'userInfos', 'userData', 'deliveries', 'weight')); | |
} | |
public function postCheckout(Request $request, $shop) | |
{ | |
$this->validate($request, [ | |
'delivery_type' => 'required', | |
]); | |
$rules = Config::get('delivery.' . $request->get('delivery_type') . '.rules'); | |
$this->validate($request, $rules); | |
$this->validate($request, $shop->getOrderRules()); | |
$data = $request->except('_token'); | |
$items = $this->cartRepository->getItems(); | |
$price = $this->cartRepository->getPrice($shop)->value(); | |
$order = $this->orderRepository->makeOrder($items, $data, $price, $shop); | |
$this->cartRepository->clearItems(); | |
return Redirect::to(shopRoute('shop.user.order.show', [$order->id])); | |
} | |
public function getUserOrders($shop) | |
{ | |
$orders = $this->orderRepository->getAll(); | |
return View::make('shop.order.index', compact('shop', 'orders')); | |
} | |
public function getUserOrder($shop, $orderId) | |
{ | |
$order = $this->orderRepository->find($orderId); | |
return View::make('shop.order.show', compact('shop', 'order')); | |
} | |
protected function getDeliveries($shop) | |
{ | |
$result = []; | |
$deliveries = $shop->deliveries->where('active', '1'); | |
foreach ($deliveries as $delivery) | |
{ | |
$config = Config::get('delivery.' . $delivery->delivery_type); | |
$result[] = app($config['class'], [ | |
$delivery->delivery_type, | |
$config, | |
$delivery | |
]); | |
} | |
return $result; | |
} | |
public function postDeliveryPrice(Request $request, $shop, $deliveryType) | |
{ | |
$class = Config::get('delivery.' . $deliveryType . '.class'); | |
$delivery = app($class, [$deliveryType]); | |
$data = $request->all(); | |
$deliveryData = $shop->deliveries->where('delivery_type', $deliveryType)->first()->data; | |
return $delivery->getPrice($data, $deliveryData); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment