mivita/app/Services/Shop.php

135 lines
No EOL
4.8 KiB
PHP

<?php
namespace App\Services;
use Yard;
use App\Models\Country;
use App\Models\ShoppingUser;
use App\Models\ShippingCountry;
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 userOrders() {
$shopping_users = ShoppingUser::whereHas('shopping_order', function($q) {
$q->where('txaction', 'paid')->OrWhere('txaction', 'appointed')->OrWhere('txaction', 'extern')->OrWhere('txaction', 'invoice_open')->OrWhere('txaction', 'invoice_paid');
})->where('orders', '=', NULL)->get();
foreach ($shopping_users as $shopping_user) {
if ($shopping_user->number) {
$orders = ShoppingUser::where('number', '=', $shopping_user->number)->max('orders');
$orders = $orders + 1;
} else {
$orders = ShoppingUser::where('billing_email', '=', $shopping_user->billing_email)->max('orders');
$orders = $orders + 1;
}
$shopping_user->orders = $orders;
$shopping_user->save();
}
}
public static function newUserOrder($number){
if($number > 0){
$shopping_users = ShoppingUser::where('number', '=', $number)->get();
$orders = 1;
foreach ($shopping_users as $shopping_user) {
if($shopping_user->shopping_order && ($shopping_user->shopping_order->txaction === 'paid' ||
$shopping_user->shopping_order->txaction === 'appointed' ||
$shopping_user->shopping_order->txaction === 'extern' ||
$shopping_user->shopping_order->txaction === 'extern_paid' )){
$shopping_user->orders = $orders++;
}else{
$shopping_user->orders = NULL;
}
$shopping_user->save();
}
}
}
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,
];
}
}