mivita/app/Http/Controllers/Web/CardController.php
2025-10-20 17:42:08 +02:00

202 lines
No EOL
7.6 KiB
PHP

<?php
namespace App\Http\Controllers\Web;
use Yard;
use Request;
use App\Services\Shop;
use App\Services\Util;
use App\Models\Product;
use App\Models\ShoppingUser;
use App\Models\ShoppingInstance;
use App\Http\Controllers\Controller;
class CardController extends Controller
{
private $instance = 'webshop';
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
}
//Cart::instance('wishlist')->add('sdjk922', 'Product 2', 1, 19.95, ['size' => 'medium']);
public function addToCardGet($id, $quantity = 1, $product_slug = false)
{
$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);
}
return back();
}
public function addToCardPost($id)
{
$product = Product::find($id);
if($product){
$image = "";
if($product->images->count()){
$image = $product->images->first()->slug;
}
$quantity = Request::get('quantity') ? Request::get('quantity') : 1;
$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);
}
return back();
}
public function showCard(){
if(Request::get('selected_country')){
Yard::instance($this->instance)->setShippingCountryWithPrice(Request::get('selected_country'));
}else{
Yard::instance($this->instance)->reCalculateShippingPrice();
}
//show konflikt wenn user eingeloggt ist und country nicht gesetzt ist
$shipping_error = $this->checkShippingError();
$data = [
'user_shop' => Util::getUserShop(),
'mylangs' => Shop::getLangChange($this->instance),
'yard_instance' => $this->instance,
'shipping_error' => $shipping_error ?? false,
];
return view('web.templates.card', $data);
}
public function updateCard(){
$data = Request::all();
if(isset($data['quantity'])){
foreach ($data['quantity'] as $rowId => $qty){
Yard::instance($this->instance)->update($rowId, $qty);
Yard::instance($this->instance)->reCalculateShippingPrice();
}
}else{
$this->deleteCard();
}
return back();
}
public function checkoutServer(){
$user_shop = Util::getUserShop();
do {
$identifier = Util::getToken();
} while( ShoppingInstance::where('identifier', $identifier)->count() );
$data = [];
$data['is_from'] = 'shopping';
$data['user_price_infos'] = Yard::instance($this->instance)->getUserPriceInfos();
ShoppingInstance::create([
'identifier' => $identifier,
'user_shop_id' => $user_shop->id,
'payment' => 1, //Customer Shop Payment
'subdomain' => url('/'),
'country_id' => Yard::instance($this->instance)->getShippingCountryId(),
'language' => \App::getLocale(),
'shopping_data' => $data,
'back' => url()->previous(),
]);
Yard::instance($this->instance)->store($identifier);
//add to DB
$path = route('checkout.checkout_card', ['identifier'=>$identifier]);
if(strpos($path, 'https') === false){
$path = str_replace('http', 'https', $path);
}
return redirect()->secure($path);
}
public function backToShop(){
$this->deleteCard();
return redirect(url('/'));
}
public function removeCard($rowId){
Yard::instance($this->instance)->remove($rowId);
return back();
}
public function deleteCard(){
$setCode = Shop::getUserShopLang(null, $this->instance);
$mylangs = Shop::getLangChange($this->instance);
foreach($mylangs as $code => $country){
if(strtolower($setCode) === strtolower($code)){
Shop::initUserShopLang($country, $this->instance);
return back();
}
}
}
private function checkShippingError(){
$shipping_error = false;
if(\Auth::guard('customers')->check()){
$user = \Auth::guard('customers')->user();
if($user->shopping_user_id){
$shopping_user = ShoppingUser::find($user->shopping_user_id);
if($shopping_user->same_as_billing){
if($shopping_user->billing_country_id != Yard::instance($this->instance)->getUserCountryId()){
$user_country = Yard::instance($this->instance)->getUserCountry();
$user_country_name = $user_country ? $user_country->getLocated() : '';
$billing_country = $shopping_user->billing_country;
$country_name = $billing_country ? $billing_country->getLocated() : '';
$shipping_error = __('website.shipping_error_billing', ['shipping_country' => $user_country_name, 'billing_country' => $country_name]);
}
}else{
if($shopping_user->shipping_country_id != Yard::instance($this->instance)->getUserCountryId()){
$user_country = Yard::instance($this->instance)->getUserCountry();
$user_country_name = $user_country ? $user_country->getLocated() : '';
$shipping_country = $shopping_user->shipping_country;
$country_name = $shipping_country ? $shipping_country->getLocated() : '';
$shipping_error = __('website.shipping_error_delivery', ['shipping_country' => $user_country_name, 'billing_country' => $country_name]);
}
}
}
}
return $shipping_error;
}
}