431 lines
14 KiB
PHP
431 lines
14 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
|
|
use App\Models\Country;
|
|
use App\Models\Product;
|
|
use App\Models\Setting;
|
|
use App\Models\ShippingCountry;
|
|
use App\Models\ShoppingUser;
|
|
use App\Services\dbip\MyDBIP;
|
|
use App\Services\IPinfo\IPinfo;
|
|
use App\User;
|
|
use Yard;
|
|
|
|
class Shop
|
|
{
|
|
public static $user_country;
|
|
public static $shipping_country;
|
|
public static $user_tax_free;
|
|
public static $shipping_free;
|
|
public static $user_reverse_charge = false;
|
|
|
|
|
|
public static function newUserOrder($number)
|
|
{
|
|
ShoppingUserService::snycOrdersByNumber($number);
|
|
}
|
|
|
|
public static function getShippingCountryCountryId($shipping_country_id)
|
|
{
|
|
$shippingCountry = ShippingCountry::find($shipping_country_id);
|
|
if ($shippingCountry && $shippingCountry->country) {
|
|
return $shippingCountry->country->id;
|
|
}
|
|
return 1; //default DE
|
|
}
|
|
|
|
public static function getCountryShippingCountryId($country_id)
|
|
{
|
|
$shippingCountry = ShippingCountry::whereCountryId($country_id)->first();
|
|
if ($shippingCountry) {
|
|
return $shippingCountry->id;
|
|
}
|
|
return ShippingCountry::all()->first()->id;
|
|
}
|
|
|
|
|
|
public static function getLangChange()
|
|
{
|
|
$ret = [];
|
|
$countries = Country::whereActive(true)->whereSwitch(true)->get();
|
|
$first_country = null;
|
|
foreach ($countries as $country) {
|
|
$ShippingCountry = ShippingCountry::whereCountryId($country->id)->first();
|
|
if ($ShippingCountry && $ShippingCountry->shipping && $ShippingCountry->shipping->active) {
|
|
if (!$first_country) {
|
|
$first_country = $country;
|
|
}
|
|
$ret[strtolower($country->code)] = $country;
|
|
}
|
|
}
|
|
Shop::getUserShopLang($first_country);
|
|
return $ret;
|
|
}
|
|
|
|
public static function getUserShopLang($country = null)
|
|
{
|
|
if (\Session::has('user_shop_lang')) {
|
|
if ($user_shop_lang = \Session::get('user_shop_lang')) {
|
|
return $user_shop_lang;
|
|
}
|
|
}
|
|
if ($country) {
|
|
Shop::initUserShopLang($country);
|
|
return strtolower($country->code);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function initUserShopLang($country)
|
|
{
|
|
Yard::instance('shopping')->destroy();
|
|
\Session::put('user_shop_lang', strtolower($country->code));
|
|
//init Yard
|
|
self::initUserShopYard($country);
|
|
}
|
|
|
|
public static function initUserShopYard($country)
|
|
{
|
|
//Lieferadresse im Drittland?
|
|
self::$user_tax_free = $country->supply_country ? true : false;
|
|
$ShippingCountry = ShippingCountry::whereCountryId($country->id)->first();
|
|
self::$shipping_free = $ShippingCountry->shipping->free;
|
|
self::$shipping_country = $ShippingCountry;
|
|
self::$user_country = $country;
|
|
|
|
Yard::instance('shopping')->setShippingCountryWithPrice($ShippingCountry->id);
|
|
Yard::instance('shopping')->setUserPriceInfos(Shop::getShopYardInfo());
|
|
}
|
|
|
|
|
|
public static function getShopYardInfo()
|
|
{
|
|
return [
|
|
'user_tax_free' => self::$user_tax_free,
|
|
'shipping_free' => self::$shipping_free,
|
|
'user_reverse_charge' => self::$user_reverse_charge,
|
|
'user_country_id' => self::$user_country->id,
|
|
'shipping_country_id' => self::$shipping_country->id,
|
|
];
|
|
}
|
|
|
|
public static function checkShoppingUser($id, $user)
|
|
{
|
|
if ($id === null) {
|
|
abort(403, 'Error: Keine User ID');
|
|
}
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
if ($shopping_user->member_id !== $user->id) {
|
|
abort(403, 'Error: Falsche User ID');
|
|
}
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
if ($shopping_user->is_like) {
|
|
abort(403, 'Error: Kunde in Prüfung');
|
|
}
|
|
return $shopping_user;
|
|
}
|
|
|
|
public static function checkShoppingCountry($for, $id = null)
|
|
{
|
|
$country_id = null;
|
|
if ($for === 'me' || $for === 'abo-me') {
|
|
$user = User::find(\Auth::user()->id);
|
|
if ($user->account->same_as_billing) {
|
|
$country_id = $user->account->country_id;
|
|
} else {
|
|
$country_id = $user->account->shipping_country_id;
|
|
}
|
|
}
|
|
if (strpos($for, 'ot') !== false && $id) {
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
if ($shopping_user->same_as_billing) {
|
|
$country_id = $shopping_user->billing_country->id;
|
|
} else {
|
|
$country_id = $shopping_user->shipping_country->id;
|
|
}
|
|
}
|
|
if ($country_id) {
|
|
if ($shipping_country = ShippingCountry::whereCountryId($country_id)->first()) {
|
|
if ($shipping_country->shipping && $shipping_country->shipping->active) {
|
|
return $shipping_country->id;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function getDeliveryCountry($for, $id = null)
|
|
{
|
|
$country_id = null;
|
|
if ($for === 'me' || $for === 'abo-me') {
|
|
$user = User::find(\Auth::user()->id);
|
|
if ($user->account->same_as_billing) {
|
|
return $user->account->country ? $user->account->country->getLocated() : '';
|
|
} else {
|
|
return $user->account->shipping_country ? $user->account->shipping_country->getLocated() : '';
|
|
}
|
|
}
|
|
if (strpos($for, 'ot') !== false && $id) {
|
|
$shopping_user = ShoppingUser::findOrFail($id);
|
|
if ($shopping_user->same_as_billing) {
|
|
return $shopping_user->billing_country ? $shopping_user->billing_country->getLocated() : '';
|
|
} else {
|
|
return $shopping_user->shipping_country ? $shopping_user->shipping_country->getLocated() : '';
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
|
|
public static function getShippingPriceByShippingCountryId($shipping_country_id, $shipping_weight)
|
|
{
|
|
$shippingCountry = ShippingCountry::find($shipping_country_id);
|
|
if (!$shippingCountry) {
|
|
abort(403, 'Fehler: Versandland nicht gefunden');
|
|
}
|
|
if (!isset($shippingCountry->shipping) && count($shippingCountry->shipping->shipping_prices) === 0) {
|
|
abort(403, 'Fehler: Kein Preise für das Versandland angelegt');
|
|
}
|
|
$shipping_price = $shippingCountry->shipping->shipping_prices->first();
|
|
if (!$shipping_price) {
|
|
abort(403, 'Fehler: Preis vom Versandland nicht gefunden');
|
|
}
|
|
|
|
/*if(!$shipping_weight){
|
|
abort(403, 'Fehler: Kein Versandgewicht');
|
|
}*/
|
|
|
|
if ($shipping_weight == 0) {
|
|
$shipping_price->price = 0;
|
|
$shipping_price->price_comp = 0;
|
|
return $shipping_price;
|
|
}
|
|
|
|
if ($shipping_weight > 0) {
|
|
/*
|
|
if($this->shipping_free && $this->total(2, '.', '') >= $this->shipping_free){
|
|
if($this->weightByFreeShipping() == 0){
|
|
$shipping_price->price = 0;
|
|
$shipping_price->price_comp = 0;
|
|
}else{
|
|
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weightByFreeShipping());
|
|
}
|
|
|
|
}else{
|
|
*/
|
|
|
|
return self::shippingPriceByWeight($shippingCountry->shipping->shipping_prices, $shipping_weight);
|
|
}
|
|
/*if($this->weight() == 0){
|
|
|
|
}else{
|
|
|
|
$shipping_price = $this->shippingPriceByWeight($shipping->shipping_prices, $this->weight());
|
|
//first by price
|
|
//$shipping_price = $this->shippingPriceByTotal($shipping->shipping_prices, $this->total(2, '.', ''));
|
|
//sec by weight
|
|
//if(!$shipping_price){
|
|
//}
|
|
}
|
|
//default
|
|
|
|
}*/
|
|
}
|
|
|
|
public static function shippingPriceByWeight($shipping_prices, $shipping_weight)
|
|
{
|
|
foreach ($shipping_prices as $price) {
|
|
if ($price->weight_from > 0 && $price->weight_to > 0) {
|
|
if ($shipping_weight >= $price->weight_from && $shipping_weight <= $price->weight_to) {
|
|
return $price;
|
|
}
|
|
}
|
|
}
|
|
abort(403, 'Fehler: Preis nach Gewicht im Versandland nicht gefunden');
|
|
}
|
|
|
|
public static function isCompProducts($for)
|
|
{
|
|
/*
|
|
$for = me, ot-member, ot-customer, abo-ot-member, abo-ot-customer, abo-me
|
|
*/
|
|
if ($for === 'me') {
|
|
return (bool) Setting::getContentBySlug('is_comp_me_order');
|
|
}
|
|
if ($for === 'abo-me') {
|
|
return (bool) Setting::getContentBySlug('is_comp_me_abo');
|
|
}
|
|
return false;
|
|
}
|
|
|
|
public static function getCompProducts($for)
|
|
{
|
|
if (Shop::isCompProducts($for)) {
|
|
$show_on = '0';
|
|
switch ($for) {
|
|
case 'me':
|
|
$show_on = '2';
|
|
break;
|
|
case 'abo-me':
|
|
$show_on = '12';
|
|
break;
|
|
case 'ot-member':
|
|
$show_on = '3';
|
|
break;
|
|
case 'ot-customer':
|
|
$show_on = '3';
|
|
break;
|
|
case 'abo-ot-member':
|
|
$show_on = '13';
|
|
break;
|
|
case 'abo-ot-customer':
|
|
$show_on = '13';
|
|
break;
|
|
}
|
|
return Product::whereActive(true)->where('shipping_addon', true)->whereJsonContains('show_on', $show_on)->orderBy('pos', 'DESC')->get();
|
|
}
|
|
return [];
|
|
}
|
|
|
|
public static function getIPDatabaseInfo()
|
|
{
|
|
|
|
//first check the DBs for the IP
|
|
|
|
|
|
/* testing
|
|
|
|
foreach(self::testIps() as $ip_address){
|
|
$country = MyDBIP::lookup($ip_address);
|
|
dump($country);
|
|
if($country !== false){
|
|
continue;
|
|
//return strtolower($country);
|
|
}
|
|
dump("not found ".$ip_address);
|
|
//not found search in IPinfo
|
|
$access_token = 'e1054aa11daf1e';
|
|
$client = new IPinfo($access_token);
|
|
|
|
$details = $client->getDetails($ip_address);
|
|
if(isset($details->error) && $details->error !== null){
|
|
//return 'error';
|
|
dump("not foun on IPinfo ".$details->error);
|
|
}
|
|
if(isset($details->country) && $details->country !== null){
|
|
MyDBIP::insert($ip_address, $details->country);
|
|
dump("found on IPinfo ".$details->country);
|
|
//return strtolower($details->country);
|
|
}
|
|
//return 'error';
|
|
}
|
|
|
|
dd("done");
|
|
*/
|
|
|
|
/* live */
|
|
|
|
$ip_address = \Request::ip();
|
|
// $ip_address = '86.189.47.78'; //testing
|
|
$country = MyDBIP::lookup($ip_address);
|
|
if ($country !== false) {
|
|
return strtolower($country);
|
|
}
|
|
//not found search in IPinfo
|
|
$access_token = 'e1054aa11daf1e';
|
|
$client = new IPinfo($access_token);
|
|
|
|
$details = $client->getDetails($ip_address);
|
|
if (isset($details->error) && $details->error !== null) {
|
|
return 'error';
|
|
}
|
|
if (isset($details->country) && $details->country !== null) {
|
|
MyDBIP::insert($ip_address, $details->country);
|
|
return strtolower($details->country);
|
|
}
|
|
return 'error';
|
|
}
|
|
|
|
|
|
private static function testIps()
|
|
{
|
|
return [
|
|
'58.217.40.197',
|
|
'207.117.144.54',
|
|
'65.169.228.128',
|
|
'6.84.3.236',
|
|
'214.56.36.93',
|
|
'9.43.145.245',
|
|
'231.199.26.76',
|
|
'86.189.47.78',
|
|
'3.122.58.75',
|
|
'251.133.143.149',
|
|
'6.142.181.83',
|
|
'2.55.191.86',
|
|
'90.189.58.233',
|
|
'57.3.139.111',
|
|
'41.130.99.194',
|
|
'1.59.123.14',
|
|
'9.119.131.109',
|
|
'54.240.231.9',
|
|
'117.19.131.144',
|
|
'221.217.39.211',
|
|
'7.43.125.76',
|
|
'224.86.233.79',
|
|
'32.151.38.98',
|
|
'4.134.40.92',
|
|
'4.70.188.58',
|
|
'24.7.152.228',
|
|
'58.122.179.1',
|
|
'5.123.9.44',
|
|
'3.175.206.5',
|
|
'8.142.119.47',
|
|
'40.248.58.203',
|
|
'7.84.254.187',
|
|
'215.215.239.71',
|
|
'124.40.66.196',
|
|
'215.87.143.102',
|
|
'143.39.97.13',
|
|
'202.56.79.198',
|
|
'143.60.125.142',
|
|
'73.233.153.126',
|
|
'121.144.28.245',
|
|
'53.232.193.122',
|
|
'104.222.102.209',
|
|
'216.55.215.13',
|
|
'84.106.145.239',
|
|
'200.131.52.20',
|
|
'132.252.158.0',
|
|
'220.11.129.27',
|
|
'8.153.0.186',
|
|
'119.206.117.107',
|
|
'222.93.42.133',
|
|
'105.104.224.98',
|
|
'252.156.181.78',
|
|
'7.139.235.187',
|
|
'111.140.127.91',
|
|
'1.186.17.84',
|
|
'85.59.39.221',
|
|
'231.152.252.43',
|
|
'125.214.216.123',
|
|
'69.31.65.238',
|
|
'126.12.105.55',
|
|
'211.39.4.118',
|
|
'73.102.88.79',
|
|
'210.229.38.137',
|
|
'5.3.230.214',
|
|
'208.92.91.242',
|
|
'4.105.177.199',
|
|
'38.10.48.92',
|
|
'133.33.44.13',
|
|
'202.189.24.255',
|
|
'5.101.244.234',
|
|
'2.52.110.194',
|
|
'1.130.73.146',
|
|
'84.237.232.120',
|
|
'25.163.83.194'
|
|
];
|
|
}
|
|
}
|