This commit is contained in:
Mekan1206
2025-12-23 19:40:28 +05:00
parent 9f27fe260e
commit f477142f11
2 changed files with 46 additions and 21 deletions

View File

@@ -40,7 +40,11 @@ class CheckoutOrderRequest extends FormRequest
'customer_address' => ['required', 'string', 'max:255'],
'shipping_method' => ['required', 'string', 'max:255', Rule::in(array_keys(OrderShipping::values()))],
'shipping_price' => ['nullable', 'numeric'],
'product_ids' => ['required', 'array'],
'product_ids.*' => ['required', 'integer', 'exists:products,id'],
'payment_type_id' => ['required', Rule::in(array_keys(OrderPayment::values()))],
'notes' => ['nullable', 'string', 'max:255'],

View File

@@ -6,6 +6,7 @@ use App\Events\Ecommerce\Product\Order\OrderCreated;
use App\Models\Ecommerce\Product\Order\Order;
use App\Models\Ecommerce\Product\Order\Shipping\OrderShipping;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
class OrderRepository
{
@@ -21,30 +22,50 @@ class OrderRepository
*/
public function create()
{
$order = Order::create($this->data);
$order = Order::create(Arr::except($this->data, ['product_ids']));
auth()->user()->carts()->with(['product' => [
'media',
'channels',
]])->get()->each(function ($cart) use ($order) {
DB::table('order_items')->insert([
'product_name' => $cart->product->name,
'product_id' => $cart->product_id,
'order_id' => $order->id,
'channel_id' => $cart->product->channels->first()?->id ?? tmpostChannel()->id,
'quantity' => $cart->product_quantity,
'unit_price_amount' => $cart->product->price_amount,
'unit_cost_amount' => $cart->product->cost_amount,
'created_at' => now(),
'updated_at' => now(),
]);
if (isset($this->data['product_ids'])) {
foreach ($this->data['product_ids'] as $product_id) {
$cartItems = auth()->user()->carts()->where('product_id', $product_id)->get();
foreach ($cartItems as $cartItem) {
DB::table('order_items')->insert([
'product_name' => $cartItem->product->name,
'product_id' => $cartItem->product_id,
'order_id' => $order->id,
'channel_id' => $cartItem->product->channels->first()?->id ?? tmpostChannel()->id,
'quantity' => $cartItem->product_quantity,
'unit_price_amount' => $cartItem->product->price_amount,
'unit_cost_amount' => $cartItem->product->cost_amount,
]);
}
$cartItems->each(function ($cartItem) {
$cartItem->delete();
});
}
} else {
auth()->user()->carts()->with(['product' => [
'media',
'channels',
]])->get()->each(function ($cart) use ($order) {
DB::table('order_items')->insert([
'product_name' => $cart->product->name,
'product_id' => $cart->product_id,
'order_id' => $order->id,
'channel_id' => $cart->product->channels->first()?->id ?? tmpostChannel()->id,
'quantity' => $cart->product_quantity,
'unit_price_amount' => $cart->product->price_amount,
'unit_cost_amount' => $cart->product->cost_amount,
'created_at' => now(),
'updated_at' => now(),
]);
$cart->product->update([
'stock' => $cart->product->stock - $cart->product_quantity,
]);
});
$cart->product->update([
'stock' => $cart->product->stock - $cart->product_quantity,
]);
});
auth()->user()->carts()->delete();
auth()->user()->carts()->delete();
}
OrderCreated::dispatch($order);