File: D:/HostingSpaces/SBogers10/blije-gasten.komma.pro/app/Komma/Shop/Checkout/CheckoutService.php
<?php
namespace App\Komma\Shop\Checkout;
use App\Komma\Addresses\Models\Address;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Session\HasSessionTrait;
use App\Komma\Shop\Orders\Models\Order;
use App\Komma\Users\Models\SiteUser;
use App\Komma\Users\Models\SiteUserRole;
use Illuminate\Support\Facades\Auth;
/**
* Class CheckoutService
*
* The checkout service sits between Order related, product related and payment related code.
* It directs those domains to work together.
*
* @package App\Komma\Shop\Checkout
*/
class CheckoutService
{
use HasSessionTrait;
/** @var Address $shippingAddress */
private $shippingAddress;
/** @var Address $shippingAddress */
private $invoiceAddress;
private $userData;
/** @var SiteUser $guestUser */
private $guestUser;
/** @var array The names of variables in this class to store and restore. */
protected $variablesForSession = [
'userData',
'shippingAddress',
'invoiceAddress',
'guestUser'
];
public function __construct()
{
$this->shippingAddress = null;
$this->invoiceAddress = null;
$this->userData = null;
$this->restoreFromSession();
}
/**
* Returns the last used shipping address for user or null if that not exists.
*
* @param SiteUser $user
* @return Address
*/
public function getLastUsedShippingAddressForUser(SiteUser $user): ? Address
{
$addressQuery = $user->addresses();
/** @var Order $latestOrder */
$latestOrder = $user->orders()->latest()->first();
if(!$latestOrder) return null;
$attributes = $latestOrder->getShippingAddressAttributes();
foreach ($attributes as $attribute => $value) $addressQuery->where(str_replace('shipping_', '', $attribute), '=', $value);
return $addressQuery->first();
}
/**
* Returns the last used invoice address for the user or null if that not exists
*
* @param SiteUser $user
* @return Address
* @see Address
*/
public function getLastUsedInvoiceAddressForUser(SiteUser $user): ? Address
{
$addressQuery = $user->addresses();
/** @var Order $latestOrder */
$latestOrder = $user->orders()->latest()->first();
if(!$latestOrder) return null;
$attributes = $latestOrder->getInvoiceAddressAttributes();
foreach ($attributes as $attribute => $value) $addressQuery->where(str_replace('invoice_', '', $attribute), '=', $value);
return $addressQuery->first();
}
/**
* @return |null
*/
public function getUserData()
{
$this->restoreFromSession();
return $this->userData;
}
/**
* @param $userData
*/
public function setUserData($userData)
{
$this->userData = $userData;
$this->saveSession();
}
/**
* @return Address
*/
public function getShippingAddress(): ? Address
{
$this->restoreFromSession();
return $this->shippingAddress;
}
/**
* @param Address $shippingAddress
*/
public function setShippingAddress(Address $shippingAddress)
{
$this->shippingAddress = $shippingAddress;
$this->saveSession();
}
/**
* @return Address
*/
public function getInvoiceAddress(): ? Address
{
$this->restoreFromSession();
return $this->invoiceAddress;
}
/**
* @param Address $invoiceAddress
*/
public function setInvoiceAddress(Address $invoiceAddress)
{
$this->invoiceAddress = $invoiceAddress;
$this->saveSession();
}
/**
* Gets the user that checks out.
* This can be a guest user. Or it can be a authenticated recurring customer.
*
* @param bool $updateUserFromRequest Update the user with data from the request if available.
* @return SiteUser
*/
public function getUser($updateUserFromRequest = false): SiteUser
{
$authUser = Auth::guard('site')->user();
if($authUser) return $authUser;
if(!$this->guestUser) {
$this->guestUser = new SiteUser();
$this->guestUser->guest = 1;
$this->guestUser->role = SiteUserRole::Customer;
// $this->saveSession();
}
if($updateUserFromRequest) {
//If we know his e-mail address we try to get his previously used guest user. If it does not exist, use the existing guest user.
if(request()->has('email') && !$this->guestUser->exists) {
$previouslyUsedUser = SiteUser::where('email', '=', request()->get('email'))->first();
if($previouslyUsedUser) $this->guestUser = $previouslyUsedUser;
}
//Use the requests email, first_name and last_name fields to update the guest user when available.
if(request()->has('email')) $this->guestUser->email = request()->get('email');
if(request()->has('first_name')) $this->guestUser->first_name = request()->get('first_name');
if(request()->has('last_name')) $this->guestUser->last_name = request()->get('last_name');
//Use the guests country and language to build a culture string when available.
if(request()->has('invoice_country')) {
/** @var RegionInfo|null $usersRegionInfo */
$usersRegionInfo = RegionInfo::getWhere('threeLetterISORegionName', '=', request()->get('invoice_country'))->first();
/** @var \App\Komma\Globalization\Language $language */
$language = $usersRegionInfo->getLanguages()[0];
$usersRegionInfo = RegionInfo::getInstance($language->getTwoLetterISOLanguageName().'-'.$usersRegionInfo->getTwoLetterISORegionName());
if($usersRegionInfo) $this->guestUser->culture = $usersRegionInfo->getName();
}
}
return $this->guestUser;
}
/**
* Clears session variables values.
*/
public function clearCheckoutData(): void
{
$this->clearSession();
}
}