157 lines
4.5 KiB
PHP
157 lines
4.5 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\frontend\Order;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Order\OrderRequest;
|
|
use App\Http\Services\Invoice\InvoiceService;
|
|
use App\Models\Cart;
|
|
use App\Models\Order;
|
|
use App\Models\OrderAddon;
|
|
use App\Models\OrderItem;
|
|
use App\Repositories\Order\OrderRepositoryInterface;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class OrderController extends Controller
|
|
{
|
|
public function __construct(
|
|
public OrderRepositoryInterface $orderRepository,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
|
|
public function index()
|
|
{
|
|
$title = 'Danh sách đơn hàng';
|
|
|
|
return view('frontend.order.index', compact('title'));
|
|
}
|
|
|
|
|
|
|
|
public function payment($order_id)
|
|
{
|
|
$orders = $this->orderRepository->findWithRelationships($order_id, ['orderItems.addons.addonItems', 'orderItems.os.osGroup', 'customer']);
|
|
if (!$orders || $orders->customer_id !== customer()?->id) {
|
|
|
|
return redirect()->route('client.order.index')
|
|
->with('error', 'Đơn hàng không thuộc về bạn. Vui lòng kiểm tra lại!');
|
|
}
|
|
|
|
return view('frontend.order.payment', compact('orders'));
|
|
}
|
|
|
|
|
|
|
|
public function destroy(Request $request, $order_id)
|
|
{
|
|
$order_num = $request->input('order_num');
|
|
|
|
$order = $this->orderRepository->findWithRelationships($order_id, [
|
|
'orderItems.addons',
|
|
'orderItems.os.osGroup',
|
|
'customer'
|
|
]);
|
|
|
|
if (
|
|
!$order ||
|
|
$order->customer_id !== customer()?->id ||
|
|
$order->order_num !== $order_num
|
|
) {
|
|
return redirect()->route('home')
|
|
->with('error', 'Không tìm thấy đơn hàng hợp lệ để xoá!');
|
|
}
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
foreach ($order->orderItems as $item) {
|
|
$item->addons()->delete();
|
|
}
|
|
|
|
$order->orderItems()->delete();
|
|
|
|
$order->delete();
|
|
|
|
DB::commit();
|
|
|
|
return redirect()->route('home')
|
|
->with('success', 'Đơn hàng đã được huỷ thành công.');
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
return redirect()->route('home')
|
|
->with('error', 'Đã có lỗi xảy ra khi huỷ đơn hàng. Vui lòng thử lại.');
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function store(OrderRequest $request, InvoiceService $invoiceService)
|
|
{
|
|
$data = $request->validated();
|
|
|
|
$orderNum = generate_order_number();
|
|
|
|
$order = Order::create([
|
|
'order_num' => $orderNum,
|
|
'customer_id' => customer()?->id,
|
|
'date' => now()->timestamp,
|
|
'discount_id' => 0,
|
|
'discount_value' => null,
|
|
'amount' => $data['totalPrice'],
|
|
'note' => null,
|
|
'client_ip' => request()->ip(),
|
|
'status' => 1,
|
|
]);
|
|
|
|
$orderItemId = OrderItem::create([
|
|
'order_id' => $order->id,
|
|
'product_id' => $data['product_id'],
|
|
'os_id' => $data['os_id'],
|
|
'server_name' => $data['server_name'],
|
|
'months' => $data['months'],
|
|
'quantity' => 1,
|
|
'price' => $data['totalPrice'],
|
|
'product_type' => $data['product_type'],
|
|
]);
|
|
|
|
if (isset($data['addons']) && is_array($data['addons'])) {
|
|
foreach ($data['addons'] as $addonId => $addonData) {
|
|
if (isset($addonData['qty'], $addonData['price']) && intval($addonData['qty']) > 0) {
|
|
$qty = intval($addonData['qty']);
|
|
$unitPrice = intval($addonData['price']);
|
|
$totalPrice = $qty * $unitPrice;
|
|
|
|
OrderAddon::create([
|
|
'order_item_id' => $orderItemId->id,
|
|
'addon_id' => $addonId,
|
|
'qty' => $qty,
|
|
'price' => $totalPrice,
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
$__order = $this->orderRepository->findWithRelationships($order->id, [
|
|
'orderItems.addons',
|
|
'orderItems.os.osGroup',
|
|
'customer'
|
|
]);
|
|
|
|
$invoice = $invoiceService->createInvoice($__order);
|
|
|
|
if (!empty($data['cart_id'])) {
|
|
Cart::where('id', $data['cart_id'])->delete();
|
|
}
|
|
|
|
|
|
|
|
return redirect()->route('client.order.payment', ['order_id' => $order])
|
|
->with('success', 'Đặt đơn thành công');
|
|
}
|
|
|
|
}
|