116 lines
3.8 KiB
PHP
116 lines
3.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Portal;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Product;
|
|
use App\Models\ShoppingOrder;
|
|
use App\Models\ShoppingUser;
|
|
use App\Services\Shop;
|
|
use App\Services\Util;
|
|
use Auth;
|
|
use Request;
|
|
use Validator;
|
|
use Yard;
|
|
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
private $instance = 'webshop';
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth:customers');
|
|
}
|
|
|
|
|
|
public function myOrders()
|
|
{
|
|
$user = Auth::guard('customers')->user();
|
|
if($user->shopping_user_id){
|
|
$shopping_user = ShoppingUser::findOrFail($user->shopping_user_id);
|
|
$shopping_orders = $shopping_user->getAllOrdersByMember();
|
|
}else{
|
|
$shopping_user = new ShoppingUser();
|
|
$shopping_orders = [];
|
|
}
|
|
$data = [
|
|
'shopping_user' => $shopping_user,
|
|
'shopping_orders' => $shopping_orders,
|
|
];
|
|
return view('portal.order.my_orders', $data);
|
|
|
|
}
|
|
|
|
public function myOrderShow($id)
|
|
{
|
|
$user = Auth::guard('customers')->user();
|
|
$shopping_user = ShoppingUser::findOrFail($user->shopping_user_id);
|
|
$shopping_order = ShoppingOrder::findOrFail($id);
|
|
if($shopping_order->shopping_user_id != $user->shopping_user_id){
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
return view('portal.order.my_order_show', [
|
|
'shopping_order' => $shopping_order,
|
|
'shopping_user' => $shopping_user,
|
|
]);
|
|
}
|
|
|
|
public function myOrderCreate($id)
|
|
{
|
|
$user = Auth::guard('customers')->user();
|
|
$shopping_order = ShoppingOrder::findOrFail($id);
|
|
if($shopping_order->shopping_user_id != $user->shopping_user_id){
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
$shopping_user = ShoppingUser::findOrFail($user->shopping_user_id);
|
|
$delivery_country = $shopping_user->getDeliveryCountry(true);
|
|
|
|
\Session::put('user_init_country', strtolower($delivery_country->code));
|
|
\Session::forget('user_init_country_options');
|
|
\Session::put('locale', strtolower(\App::getLocale()));
|
|
|
|
Shop::initUserShopLang($delivery_country, $this->instance);
|
|
|
|
//init Yard
|
|
|
|
foreach($shopping_order->shopping_order_items as $shopping_order_item){
|
|
if($shopping_order_item->product){
|
|
$this->addToCard($shopping_order_item->product_id, $shopping_order_item->qty);
|
|
}
|
|
}
|
|
$url = Util::getMyMivitaShopUrl("/user/card/show");
|
|
return redirect($url);
|
|
}
|
|
|
|
|
|
private function addToCard($id, $quantity = 1)
|
|
{
|
|
$product = Product::find($id);
|
|
if($product){
|
|
$image = "";
|
|
if($product->images->count()){
|
|
$image = $product->images->first()->slug;
|
|
}
|
|
$cartItem = Yard::instance($this->instance)
|
|
->add($product->id, $product->getLang('name'), $quantity,
|
|
$product->getPriceWith(Yard::instance($this->instance)->getUserTaxFree(), false, Yard::instance($this->instance)->getUserCountry()), false, false,
|
|
['image' => $image, 'slug' => $product->slug, 'weight' => $product->weight, 'points' => $product->points, 'no_commission' => $product->no_commission, 'no_free_shipping' => $product->no_free_shipping, 'show_on' => $product->show_on]);
|
|
if(Yard::instance($this->instance)->getUserTaxFree()){
|
|
Yard::setTax($cartItem->rowId, 0);
|
|
}else{
|
|
Yard::setTax($cartItem->rowId, $product->getTaxWith(Yard::instance($this->instance)->getUserCountry()));
|
|
}
|
|
Yard::instance($this->instance)->reCalculateShippingPrice();
|
|
|
|
\Session()->flash('show-card-after-add', true);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
}
|