File: D:/HostingSpaces/SBogers10/stafa.komma.pro/app/Komma/Shop/Checkout/CheckoutController.php
<?php
namespace App\Komma\Shop\Checkout;
use App\Komma\Base\Controller;
use App\Komma\Addresses\AddressService;
use App\Komma\Globalization\RegionInfo;
use App\Komma\Shop\Cart\ShoppingCartItemInterface;
use App\Komma\Shop\Cart\ShoppingCartService;
use App\Komma\Shop\Cart\ShoppingCartServiceInterface;
use App\Komma\Shop\Discounts\Discount;
use App\Komma\Shop\Discounts\DiscountTypes;
use App\Komma\Shop\Orders\Kms\OrderMailServiceInterface;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\View\View;
/**
* Handles the checkout process and the different views
*
* Class ShoppingCartService
* @package App\Komma\Shop\Cart
*/
class CheckoutController extends Controller
{
/** @var CheckoutServiceInterface $checkoutService */
private $checkoutService;
/** @var OrderMailServiceInterface $orderMailService */
private $orderMailService;
/** @var AddressService $addressService */
private $addressService;
/** @var RegionInfo $regionInfo */
private $regionInfo;
/**
* CheckoutController constructor.
*/
public function __construct()
{
if(\App::runningInConsole()) return;
parent::__construct();
$this->checkoutService = app(CheckoutServiceInterface::class);
$this->orderMailService = app(OrderMailServiceInterface::class);;
$this->addressService = app(AddressService::class);
$this->regionInfo = new RegionInfo('NL');
parent::__construct();
}
/**
* Shows a checkout form
*
* return
*/
public function showCheckoutForm() {
$shoppingCartService = app(ShoppingCartServiceInterface::class);
/** @var Collection $addresses */
$addresses = \Auth::user()->addresses()->get();
$lastUsedShippingAddress = $this->checkoutService->getLastUsedShippingAddressForUser(\Auth::user());
$lastUsedInvoiceAddress = $this->checkoutService->getLastUsedInvoiceAddressForUser(\Auth::user());
$vatTotal = ceil($shoppingCartService->getTotal(true) * 0.21); //We must do ceil here. Try this with example total 32980. times 0.21 = 6925.8. If you don't ceil it to 6926. You are going to lose the .8 in your tax. That MAY be not what we want
$shoppingCartService->updateDiscounts();
$couponCodes = $shoppingCartService->getDiscountsByType(DiscountTypes::Coupon);
return view('shop/pages/checkout/index', [
'addresses' => $addresses,
'lastUsedShippingAddress' => $lastUsedShippingAddress,
'lastUsedInvoiceAddress' => $lastUsedInvoiceAddress,
'items' => $shoppingCartService->getItems(),
'total_ex_vat_and_discounts' => $shoppingCartService->getTotal(false),
'total_ex_vat' => $shoppingCartService->getTotal(true),
'vat_total' => $vatTotal,
'total' => $shoppingCartService->getTotal(true) + $vatTotal,
'total_without_discount' => $shoppingCartService->getTotal(false),
'total_formatted' => $this->regionInfo->getNumberFormat()->centsToCurrency($shoppingCartService->getTotal(), true, true),
'regionInfo' => $this->regionInfo,
'links' => $this->links,
'applied_codes' => $couponCodes
]);
}
/**
* Triggered in response of a push on a checkout button
* @param CheckoutRequest $checkoutRequest
* @return \Illuminate\Http\RedirectResponse
*/
public function checkout(CheckoutRequest $checkoutRequest)
{
$shoppingCartService = app(ShoppingCartServiceInterface::class);
//Add coupon code if needed.
if(\Input::has('add_coupon')) { //Add coupon button pressed
$couponCodesBeforeAddedNewOne = $shoppingCartService->getDiscountsByType(DiscountTypes::Coupon);
$shoppingCartService->addCouponCode(\Input::get('coupon'));
$couponCodesAfterAddedNewOne = $shoppingCartService->getDiscountsByType(DiscountTypes::Coupon);
if(count($couponCodesAfterAddedNewOne) == count($couponCodesBeforeAddedNewOne)) { //If the amount of valid coupon codes is the same as before we tried to add one, the coupon code is invalid. Else if would be added.
return redirect()->back()->withInput()->withErrors(['coupon_code' => __('shop/discounts.coupon_fail')]);
}
$checkoutRequest->flash();
return $this->showCheckoutForm();
} elseif(\Input::has('delete_coupon')) {
$shoppingCartService->removeCouponCode(\Input::get('delete_coupon'));
$checkoutRequest->flash();
return $this->showCheckoutForm();
}
//Create addresses for shipping and invoice.
$addressForInvoice = $this->addressService->getAddressForInvoiceFromInput();
$addressForShipping = $this->addressService->getAddressForShippingFromInput();
$addressForInvoice = $this->addressService->saveAddressForUser($addressForInvoice, \Auth::user());
$addressForShipping = $this->addressService->saveAddressForUser($addressForShipping, \Auth::user(), $addressForInvoice);
/** @var ShoppingCartItemInterface[] $items */
$order = $this->checkoutService->createOrderFromShoppingCartItems($shoppingCartService->getItems(), \Auth::user(), $addressForShipping, $addressForInvoice);
$this->orderMailService->mailCustomerAboutCurrentOrderStatus($order, \Auth::user());
$this->orderMailService->mailStaffAboutCurrentOrderStatus($order);
return $this->checkoutService->startPaymentForOrder($order);
}
/**
* @return View
*/
public function index()
{
return $this->checkoutView();
}
/**
* @return View
*/
protected function checkoutView(): View
{
return view('shop.pages.checkout.index');
}
}