File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Shop/Checkout/CheckoutService.php
<?php
namespace App\KommaApp\Shop\Checkout;
use App\KommaApp\Shop\Cart\ShoppingCartItemInterface;
use App\KommaApp\Shop\Cart\ShoppingCartService;
use App\KommaApp\Shop\Discounts\DiscountServiceInterface;
use App\KommaApp\Shop\Notifications\AdminOrderConfirmation;
use App\KommaApp\Shop\Orders\Order;
use App\KommaApp\Shop\Orders\OrderService;
use App\KommaApp\Users\Models\Role;
use App\KommaApp\Users\Models\User;
use Illuminate\Support\Facades\Log;
class CheckoutService
{
/** @var DiscountServiceInterface $discountService */
private $discountService;
/** @var OrderService $orderService */
private $orderService;
public function __construct()
{
$this->discountService = \App::make(DiscountServiceInterface::class);
$this->orderService = \App::make(OrderService::class);
}
/**
* Returns an extension of the original request that also has validation stuff inside it.
*
* @param ShoppingCartService $shoppingCartService
* @return QuotationRequest
*/
public function getQuotationRequestForShoppingCart(ShoppingCartService $shoppingCartService):QuotationRequest
{
$requestData = [
'origin' => 'checkout',
'productables' => [],
'cart_total' => $shoppingCartService->getTotal(false),
'cart_total_including_discounts' => $shoppingCartService->getTotal(true),
];
$items = $shoppingCartService->getItems();
foreach($items as $item) {
/** @var $shoppingCartService $item */
$requestData['productables'][] = [
'amount' => $item->getAmount(),
'name' => $item->getProductable()->title,
'stock_keeping_unit' => $item->getProductable()->stock_keeping_unit,
'original_total' => $item->getTotal(false),
'total_with_discounts' => $item->getTotal(true),
];
}
$requestData['productables'] = json_encode($requestData['productables']);
$shoppingCartService->clear();
return new QuotationRequest($requestData);
}
/**
* Notify admins that a user wanted to order something
*
* @param User $customer
* @param Order $order
*/
public function notifyAdminsForNewOrder(User $customer, Order $order)
{
User::where('role_id', '=', Role::Admin)->get()->each(function(User $admin) use ($order, $customer) {
Log::debug('Notifying: '.$admin->email.' about order '.$order->id);
$admin->notify(new AdminOrderConfirmation($order));
Log::debug('Notified');
});
// User::where('role_id', '=', Role::Admin)->get()->each(function(User $admin) use ($order, $customer) {
// $admin->notify(new AdminOrderConfirmation($order));
// });
}
/**
* Creates an order with ordered products from the items in the shopping cart for the customer you give it.
* You can also give the order remarks to leave a message to the admins for that order using the remarks variable.
*
* @param ShoppingCartService $shoppingCart
* @param User $customer
* @param string $remarks
* @return Order
*/
public function createOrderFromShoppingCart(ShoppingCartService $shoppingCart, User $customer, string $remarks = '')
{
/** @var Order $order */
$order = \DB::transaction(function () use ($shoppingCart, $customer, $remarks) {
$order = $this->orderService->makeOrderForCustomer($customer, $shoppingCart, $remarks);
$order->save();
$orderedProducts = collect($shoppingCart->getItems())->each(function ( ShoppingCartItemInterface $shoppingCartItem) use ($customer, $order) {
$orderedProduct = $this->orderService->makeOrderedProductFromShoppingCartItemForOrder($shoppingCartItem, $order);
// dd($orderedProduct, $order);
$orderedProduct->save();
return $orderedProduct;
});
return $order;
});
$shoppingCart->clear();
return $order;
}
}