311 lines
9.5 KiB
PHP
311 lines
9.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Portal;
|
|
|
|
use Auth;
|
|
use Yard;
|
|
use Request;
|
|
use Validator;
|
|
use App\Services\Shop;
|
|
use App\Services\Util;
|
|
use App\Models\Product;
|
|
use App\Models\UserAbo;
|
|
use App\Services\AboHelper;
|
|
use App\Models\ShoppingUser;
|
|
use App\Models\ShoppingOrder;
|
|
use App\Services\UserService;
|
|
use App\Models\ShoppingInstance;
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
class AboController extends Controller
|
|
{
|
|
private $instance = 'subscription';
|
|
private $yard;
|
|
|
|
/**
|
|
* Create a new controller instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
$this->middleware('auth:customers');
|
|
$this->yard = Yard::instance($this->instance);
|
|
}
|
|
|
|
|
|
public function myAbo()
|
|
{
|
|
$user = Auth::guard('customers')->user();
|
|
|
|
if (!$user->shopping_user_id) {
|
|
return view('portal.abo.my_abo_create', [
|
|
'user' => $user,
|
|
'no_shopping_user' => true,
|
|
'step' => 0,
|
|
]);
|
|
}
|
|
|
|
$shopping_user = ShoppingUser::findOrFail($user->shopping_user_id);
|
|
$user_abo = UserAbo::where('email', $shopping_user->billing_email)
|
|
->where('status', '>', 1)
|
|
->first();
|
|
|
|
return $user_abo
|
|
? view('portal.abo.my_abo', ['user_abo' => $user_abo])
|
|
: view('portal.abo.my_abo_create', [
|
|
'shopping_user' => $shopping_user,
|
|
'step' => 0,
|
|
]);
|
|
}
|
|
|
|
public function myAboCreate($step)
|
|
{
|
|
$user = Auth::guard('customers')->user();
|
|
|
|
if (!$user->shopping_user_id) {
|
|
abort(403, 'Unauthorized action.');
|
|
}
|
|
|
|
$shopping_user = ShoppingUser::findOrFail($user->shopping_user_id);
|
|
$data = $this->prepareAboCreateData($shopping_user, $step);
|
|
if(isset($data['checkout_url'])){
|
|
return redirect($data['checkout_url']);
|
|
}
|
|
return view('portal.abo.my_abo_create', $data);
|
|
}
|
|
|
|
private function prepareAboCreateData($shopping_user, $step)
|
|
{
|
|
$data = [
|
|
'shopping_user' => $shopping_user,
|
|
'basis_products' => Product::where('active', true)
|
|
->whereJsonContains('show_on', ['12'])
|
|
->orderBy('pos', 'ASC')
|
|
->get(),
|
|
'upgrade_products' => Product::where('active', true)
|
|
->whereJsonContains('show_on', ['13'])
|
|
->orderBy('pos', 'ASC')
|
|
->get(),
|
|
'step' => 0,
|
|
];
|
|
if(Request::get('action') == 'back') {
|
|
$step = $step - 2;
|
|
}
|
|
|
|
switch ($step) {
|
|
case 0:
|
|
$data['step'] = 0;
|
|
break;
|
|
case 1:
|
|
$this->initYard($shopping_user);
|
|
$data['step'] = 1;
|
|
break;
|
|
case 2:
|
|
UserService::setInstance($this->instance);
|
|
UserService::initCustomerYard($shopping_user, 'abo-ot-customer');
|
|
$data['step'] = 2;
|
|
break;
|
|
case 3:
|
|
UserService::setInstance($this->instance);
|
|
UserService::initCustomerYard($shopping_user, 'abo-ot-customer');
|
|
if(Request::get('action') == 'next'){
|
|
if (!$this->checkBasisProduct()) {
|
|
$data['error'] = __('abo.abo_error_basis_product');
|
|
$data['step'] = 2;
|
|
} else {
|
|
$data['step'] = 3;
|
|
}
|
|
}else{
|
|
$data['step'] = 3;
|
|
}
|
|
break;
|
|
case 4:
|
|
UserService::setInstance($this->instance);
|
|
UserService::initCustomerYard($shopping_user, 'abo-ot-customer');
|
|
$this->upgradeProductToCart();
|
|
$data['step'] = 4;
|
|
break;
|
|
case 5:
|
|
//chekout verarbeiten
|
|
UserService::setInstance($this->instance);
|
|
UserService::initCustomerYard($shopping_user, 'abo-ot-customer');
|
|
if(Request::get('action') == 'checkout'){
|
|
//checkout verarbeiten
|
|
if (!$this->preCheckCheckout()) {
|
|
$data['error'] = __('abo.abo_error_basis_product');
|
|
$data['step'] = 4;
|
|
} else {
|
|
$data['checkout_url'] = $this->processCheckout();
|
|
}
|
|
}
|
|
$data['step'] = 4;
|
|
break;
|
|
default:
|
|
abort(404, 'Page not found.');
|
|
}
|
|
|
|
return $data;
|
|
}
|
|
|
|
private function initYard($shopping_user)
|
|
{
|
|
$delivery_country = $shopping_user->getDeliveryCountry(true);
|
|
|
|
if (!$delivery_country) {
|
|
abort(404, 'No delivery country found, please edit your personal data.');
|
|
}
|
|
|
|
\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);
|
|
}
|
|
|
|
private function preCheckCheckout(){
|
|
$result = false;
|
|
//alle inhlate des warenkorb
|
|
$cartItems = $this->yard->content();
|
|
foreach($cartItems as $item){
|
|
if(in_array(12, $item->options->show_on)){
|
|
$result = true;
|
|
}
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
|
|
private function checkBasisProduct()
|
|
{
|
|
$data = Request::all();
|
|
$result = false;
|
|
|
|
if (!isset($data['base_product_qty'])) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($data['base_product_qty'] as $product_id => $quantity) {
|
|
$product = Product::find($product_id);
|
|
|
|
if (!$product || intval($quantity) <= 0) {
|
|
continue;
|
|
}
|
|
$result = true;
|
|
$this->addProductToCart($product, $quantity);
|
|
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function upgradeProductToCart(){
|
|
$data = Request::all();
|
|
$result = false;
|
|
|
|
if (!isset($data['upgrade_product_qty'])) {
|
|
return false;
|
|
}
|
|
|
|
foreach ($data['upgrade_product_qty'] as $product_id => $quantity) {
|
|
$product = Product::find($product_id);
|
|
|
|
if (!$product) {
|
|
continue;
|
|
}
|
|
|
|
$result = true;
|
|
$this->addProductToCart($product, $quantity);
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
|
|
private function addProductToCart($product, $quantity)
|
|
{
|
|
// Suche nach dem Produkt im Warenkorb
|
|
$cartItems = $this->yard->search(function($item) use ($product) {
|
|
return $item->id === $product->id;
|
|
});
|
|
|
|
// Wenn die Menge 0 ist, entferne das Produkt
|
|
if ($quantity <= 0) {
|
|
foreach ($cartItems as $item) {
|
|
$this->yard->remove($item->rowId);
|
|
}
|
|
return;
|
|
}
|
|
|
|
$image = $product->images->first()->slug ?? '';
|
|
$price = $product->getPriceWith(
|
|
$this->yard->getUserTaxFree(),
|
|
false,
|
|
$this->yard->getUserCountry()
|
|
);
|
|
|
|
// Wenn das Produkt bereits im Warenkorb ist, aktualisiere die Menge
|
|
if ($cartItems->count() > 0) {
|
|
$cartItem = $cartItems->first();
|
|
$this->yard->update($cartItem->rowId, $quantity);
|
|
} else {
|
|
// Wenn das Produkt noch nicht im Warenkorb ist, füge es hinzu
|
|
$cartItem = $this->yard->add(
|
|
$product->id,
|
|
$product->getLang('name'),
|
|
$quantity,
|
|
$price,
|
|
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
|
|
]
|
|
);
|
|
}
|
|
|
|
// $this->setProductTax($cartItem, $product);
|
|
$this->yard->reCalculateShippingPrice();
|
|
}
|
|
|
|
|
|
private function processCheckout(){
|
|
$user_shop = Util::getUserShop();
|
|
if(!$user_shop){
|
|
$user_shop = Util::getDefaultUserShop();
|
|
}
|
|
do {
|
|
$identifier = Util::getToken();
|
|
} while( ShoppingInstance::where('identifier', $identifier)->count() );
|
|
|
|
$data = [];
|
|
$data['is_from'] = 'shopping';
|
|
$data['user_price_infos'] = $this->yard->getUserPriceInfos();
|
|
|
|
ShoppingInstance::create([
|
|
'identifier' => $identifier,
|
|
'user_shop_id' => $user_shop->id,
|
|
'payment' => 1, //Customer Shop Payment
|
|
'subdomain' => url('/'),
|
|
'country_id' => $this->yard->getShippingCountryId(),
|
|
'language' => \App::getLocale(),
|
|
'shopping_data' => $data,
|
|
'back' => url()->previous(),
|
|
|
|
]);
|
|
|
|
$this->yard->store($identifier);
|
|
//add to DB
|
|
$path = route('checkout.checkout_card', ['identifier'=>$identifier]);
|
|
if(strpos($path, 'https') === false){
|
|
$path = str_replace('http', 'https', $path);
|
|
}
|
|
return $path;
|
|
}
|
|
}
|