File: D:/HostingSpaces/Eurotools/euro-tools.nl/app/KommaApp/Shop/Orders/OrderService.php
<?php
namespace App\KommaApp\Shop\Orders;
use App\KommaApp\Shop\Cart\ShoppingCartItemInterface;
use App\KommaApp\Shop\Cart\ShoppingCartServiceInterface;
use App\KommaApp\Shop\Discounts\Actions\ModifyPriceAction;
use App\KommaApp\Shop\Discounts\Conditions\QuantityDiscountCondition;
use App\KommaApp\Shop\Discounts\DiscountServiceInterface;
use App\KommaApp\Shop\Products\Product\Product;
use App\KommaApp\Users\Models\User;
/**
* Class OrderService
*
* Manages orders
*
* @package App\KommaApp\Shop\Orders
*/
class OrderService
{
/**
* @var DiscountServiceInterface $discountService
*/
private $discountService;
public function __construct(DiscountServiceInterface $discountService)
{
$this->discountService = $discountService;
}
/**
* Create an ordered product from a shoppingCartItem and an order it is going to belong to.
*
* @param ShoppingCartItemInterface $shoppingCartItem
* @param Order $order
* @return OrderedProduct
*/
public function makeOrderedProductFromShoppingCartItemForOrder(ShoppingCartItemInterface $shoppingCartItem, Order $order) {
$orderedProduct = new OrderedProduct();
$productable = $shoppingCartItem->getProductable();
$orderedProduct->product()->associate($productable);
$orderedProduct->order()->associate($order);
$orderedProduct->name = $productable->getName();
if (is_a($productable, Product::class)) {
/** @var Product $productable */
$orderedProduct->article_number = $productable->internal_number;
$orderedProduct->sku = $productable->stock_keeping_unit;
}
//Notice that this is not the applied discount, but the discount that COULD be applied if it meets its condition
$discount = $this->discountService->getQuantityDiscountForASpecificDiscountable($shoppingCartItem->getProductable());
if($discount)
{
/** @var QuantityDiscountCondition $quantityDiscountCondition */
$quantityDiscountCondition = $discount->getDiscountCondition();
/** @var ModifyPriceAction $modifyPriceAction */
$modifyPriceAction = $discount->getDiscountAction();
$orderedProduct->quantity_discount_price = (float) $shoppingCartItem->getTotal(false) - (float) $modifyPriceAction->getModificationValue(); //Price after quantity discount is applied
$orderedProduct->quantity_discount_threshold = $quantityDiscountCondition->getQuantity();
}
$orderedProduct->original_price = $shoppingCartItem->getTotal(false);
$orderedProduct->total = $shoppingCartItem->getTotal(true);
$orderedProduct->quantity = $shoppingCartItem->getAmount();
return $orderedProduct;
}
/**
* Creates an order for a certain customer
*
* @param User $customer
* @param ShoppingCartServiceInterface $shoppingCart
* @param string $remarks
* @return Order
*/
public function makeOrderForCustomer(User $customer, ShoppingCartServiceInterface $shoppingCart, string $remarks = ''): Order
{
$order = new Order();
$order->order_number = $this->getNewOrderNumber();
$order->customer()->associate($customer);
$order->invoice_company = $customer->company_name;
$order->invoice_company_coc_number = $customer->chamber_of_commerce;
$order->invoice_company_vat_number = $customer->company_vat_number;
$order->invoice_salutation = $customer->getSalutation();
$order->invoice_first_name = $customer->first_name;
$order->invoice_last_name = $customer->last_name;
$order->invoice_email = $customer->email;
$order->invoice_phone = $customer->telephone;
$order->invoice_street = $customer->company_street;
$order->invoice_house_number = $customer->company_house_number;
$order->invoice_postal_code = $customer->company_zip_code;
$order->invoice_city = $customer->company_city;
$order->invoice_country = $customer->company_country;
$order->shipping_street = $customer->shipping_street;
$order->shipping_street = $customer->shipping_street;
$order->shipping_house_number = $customer->shipping_house_number;
$order->shipping_postal_code = $customer->shipping_zip_code;
$order->shipping_country = $customer->shipping_country;
$order->cart_total = $shoppingCart->getTotal(true);
$order->remarks = $remarks;
return $order;
}
/**
* Gets a new order number for a new order.
*
* @return string
*/
private function getNewOrderNumber()
{
$newId = 1;
$lastOrder = Order::orderBy('id', 'desc')->first(['id']);
if($lastOrder) $newId = $lastOrder->id + 1;
$paddedId = str_pad($newId, 8, 0, STR_PAD_LEFT);
return 'ET_'.$paddedId;
}
}