File: D:/HostingSpaces/SBogers10/farmfun.komma.pro/app/Komma/Orders/OrderService.php
<?php
namespace App\Komma\Orders;
use App\Komma\Base\Service;
use App\Komma\Orders\Models\Order;
use App\Komma\Orders\Models\OrderLine;
use App\Komma\Reservations\Models\Reservation;
use App\Komma\ShoppingCart\Interfaces\ShoppingCartInterface;
class OrderService extends Service
{
/** @var OrderNumberSequence */
private $orderNumberGenerator;
public function __construct()
{
parent::__construct();
$this->orderNumberGenerator = app(OrderNumberSequence::class);
}
/**
* Get order
*
* @param $orderId
* @return Order
*/
public function getOrderById($orderId): Order
{
return Order::where('id', $orderId)->first();
}
/**
* @param ShoppingCartInterface $shoppingCart
* @param $checkoutData
* @return Order|null
*/
public function createOrder(ShoppingCartInterface $shoppingCart, $checkoutData): ?Order
{
$order = null;
\DB::transaction(function () use ($shoppingCart, $checkoutData, &$order) {
//Create the order, link customer and addresses to it and save it. We need to save it because the ordered productables need a order id
/** @var Order $order */
$order = new Order();
$order->status = Order::STATUS_NEW;
$orderNumber = (string) $this->orderNumberGenerator->next();
$order->order_number = $orderNumber;
$order->order_reservation_number = str_replace('O', strtoupper(substr($shoppingCart->getLocation()->city, 0, 2) . '-'), $orderNumber);
$order = $this->fillOrderWithCheckoutData($order, clone $checkoutData);
$order->date = $shoppingCart->getDate();
// Get pricing from shopping cart
$order->total = $shoppingCart->getTotal(false);
$order->vat = $shoppingCart->getVat(false);
$order->total_without_vat = $order->total - $order->vat;
$order->deposit = $shoppingCart->getDeposit(false);
$order->save();
//Loop over all shopping cart items and create order lines for them.
foreach ($shoppingCart->getItems() as $shoppingCartItem) {
$timeSlot = $shoppingCartItem->getSelectedTimeSlot();
$productName = $shoppingCartItem->getProduct()->translation->name;
if (isset($timeSlot->start) && isset($timeSlot->visualEnd)) {
$productName .= '<br/>' . $timeSlot->start->format('H:i') . ' - ' . $timeSlot->visualEnd->format('H:i');
}
$orderLine = new OrderLine([
'product_id' => $shoppingCartItem->getProduct()->id,
'location_id' => $shoppingCartItem->getLocation()->id,
'name' => $productName,
'quantity' => (isset($shoppingCartItem->amountOfPersons) ? $shoppingCartItem->amountOfPersons : 1), // If amount of persons is undefined, it has a fixed amount like Bumperball
'price_each_unit' => $shoppingCartItem->getProduct()->price_each_unit,
'price_start_up' => $shoppingCartItem->getProduct()->price_start_up,
'vat_percentage' => $shoppingCartItem->getProduct()->vat_percentage,
'price_total' => $shoppingCartItem->getTotal(true, false),
'remarks' => $shoppingCartItem->notification,
]);
$order->lines()->save($orderLine);
}
});
return $order->load('lines');
}
public function createFakeOrderFromReservation(Reservation $reservation)
{
$order = new Order();
$orderLines = collect();
$orderTotal = 0;
foreach ($reservation->items as $reservationItem) {
$productName = $reservationItem->product->translation->name ?? 'onbekend';
if ($reservationItem->getStartInDateTime() !== null && $reservationItem->getEndInDateTime(true) !== null) {
$productName .= '<br/>' . $reservationItem->getStartInDateTime()->format('H:i') . ' - ' . $reservationItem->getEndInDateTime(true)->format('H:i');
}
$orderTotal += $reservationItem->price_total;
$orderLines->push(new OrderLine([
'product_id' => $reservationItem->product_id,
'location_id' => $reservationItem->location_id,
'name' => $productName,
'quantity' => $reservationItem->quantity,
'price_each_unit' => $reservationItem->price_each_unit,
'price_start_up' => $reservationItem->price_start_up,
'vat_percentage' => $reservationItem->vat_percentage,
'price_total' => $reservationItem->price_total,
'remarks' => $reservationItem->remarks,
]));
}
$order->setRelation('lines', $orderLines);
$order->date = $reservation->date;
$order->total = $orderTotal;
$order->vat = calculate_price_excluding_vat($orderTotal, config('site.vat_percentage'));
$order->total_without_vat = $order->total - $order->vat;
$order->deposit = (int) round($orderTotal / 100 * config('site.deposit_percentage'));
return $order;
}
public function createFakeOrderFromCart(ShoppingCartInterface $shoppingCart): Order
{
$order = new Order();
$orderLines = collect();
foreach ($shoppingCart->getItems() as $shoppingCartItem) {
$timeSlot = $shoppingCartItem->getSelectedTimeSlot();
$timeSlotLine = isset($timeSlot) ? $timeSlot->start->format('H:i') . ' - ' . $timeSlot->visualEnd->format('H:i') : '';
$orderLine = new OrderLine([
'product_id' => $shoppingCartItem->getProduct()->id,
'location_id' => $shoppingCartItem->getLocation()->id,
'name' => $shoppingCartItem->getProduct()->translation->name . '<br/>' . $timeSlotLine,
'slug' => $shoppingCartItem->getProduct()->slug,
'quantity' => (isset($shoppingCartItem->amountOfPersons) ? $shoppingCartItem->amountOfPersons : 1), // If amount of persons is undefined, it has a fixed amount like Bumperball
'price_each_unit' => $shoppingCartItem->getProduct()->price_each_unit,
'price_start_up' => $shoppingCartItem->getProduct()->price_start_up,
'vat_percentage' => $shoppingCartItem->getProduct()->vat_percentage,
'price_total' => $shoppingCartItem->getTotal(true, false),
'remarks' => $shoppingCartItem->notification,
]);
$orderLines->push($orderLine);
}
$order->setRelation('lines', $orderLines);
$order->total = $shoppingCart->getTotal(false);
$order->vat = $shoppingCart->getVat(false);
$order->total_without_vat = $order->total - $order->vat;
$order->deposit = $shoppingCart->getDeposit(false);
$order->date = $shoppingCart->getDate();
return $order;
}
/**
* Fill the order with the given checkout data
*
* @param Order $order
* @param $checkoutData
* @return Order
*/
private function fillOrderWithCheckoutData(Order $order, $checkoutData): Order
{
// Remove the vies values
unset($checkoutData->vies_found);
unset($checkoutData->vies_valid);
unset($checkoutData->vies_result);
// Populate invoice_email from email if no other email for invoice
if (! $checkoutData->checkout_send_invoice_to_other_email) {
$order->invoice_email = $checkoutData->email;
} else {
$order->invoice_email = $checkoutData->checkout_email_for_invoice;
}
unset($checkoutData->checkout_send_invoice_to_other_email);
unset($checkoutData->checkout_email_for_invoice);
// Append the house number addition to the house number
if (! empty($checkoutData->invoice_house_number_addition)) {
$checkoutData->invoice_house_number .= $checkoutData->invoice_house_number_addition;
}
unset($checkoutData->invoice_house_number_addition);
foreach ($checkoutData as $propertyKey => $propertyValue) {
$order->{$propertyKey} = $propertyValue;
}
return $order;
}
}