File: D:/HostingSpaces/ZelfVerkopen/zelfverkopen.nl/app/KommaApp/Orders/OrderService.php
<?php
namespace App\KommaApp\Orders;
use App\KommaApp\Customers\Models\Customer;
use App\KommaApp\Orders\Models\Order;
use App\KommaApp\Products\Models\Product;
class OrderService
{
public function createDefaultOrder(Customer $customer)
{
// Create new Order
$order = new Order();
$order->fill([
'status' => Order::ORDER_STATUS_NEW,
'gender' => $customer->gender,
'last_name' => $customer->last_name,
'zip' => $customer->zip,
'house_number' => $customer->house_number,
'house_number_addition' => $customer->house_number_addition,
'street' => $customer->street,
'city' => $customer->city
]);
// Bind the order to the customer
$customer->orders()->save($order);
// Add the default product to the order
$defaultProduct = Product::where('code_name', 'default')
->with('translation')
->first();
$order->products()->save($defaultProduct, ['price' => $defaultProduct->price, 'name' => $defaultProduct->translation->name]);
$order->order_id = Order::ZELFVERKOPING_PREFIX.($order->id + Order::ZELFVERKOPING_STARTING_ID);
$order->save();
}
/**
* @param Customer $customer
* @param $productId
* @return Order
*/
public function createOrderForProduct(Customer $customer, $productId): Order
{
// Create new Order
$order = new Order();
$order->fill([
'status' => Order::ORDER_STATUS_NEW,
'gender' => $customer->gender,
'last_name' => $customer->last_name,
'zip' => $customer->zip,
'house_number' => $customer->house_number,
'house_number_addition' => $customer->house_number_addition,
'street' => $customer->street,
'city' => $customer->city
]);
// Bind the order to the customer
$customer->orders()->save($order);
$product = Product::where('id', $productId)
->with('translation')
->first();
$order->products()->save($product, ['price' => $product->price, 'name' => $product->translation->name]);
$order->order_id = Order::ZELFVERKOPING_PREFIX.($order->id + Order::ZELFVERKOPING_STARTING_ID);
$order->save();
return $order;
}
}