File size: 1,994 Bytes
52d9fb0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

namespace App\Http\Controllers\User;

use App\Constants\Status;
use App\Http\Controllers\Controller;
use App\Models\Order;
use App\Models\OrderItem;
use App\Models\Product;
use App\Models\ReviewCategory;

class OrderController extends Controller
{
    public function orderList()
    {
        $pageTitle = 'Purchase History';
        $author    = auth()->user();
        $orders    = $author->orders()->orderBy('id', 'desc')->paginate(getPaginate());
        return view('Template::user.orders.list', compact('pageTitle', 'orders', 'author'));
    }

    public function store()
    {
        $cartItems       = auth()->user()->cartItems;

        if (count($cartItems) == 0) {
            $notify[] = ['error', 'No items in your cart'];
            return back()->withNotify($notify);
        }

        $countOwnProduct = Product::where('user_id', auth()->id())->whereIn('id', $cartItems->pluck('product_id')->toArray())->count();

        if ($countOwnProduct) {
            $notify[] = ['error', 'you cannot purchase your product'];
            return back()->withNotify($notify);
        }

        return redirect()->route('user.deposit.index');
    }

    public function details($orderId)
    {
        $orderBy          = request()->order_by;
        $reviewCategories = ReviewCategory::active()->get();
        $order            = Order::where('user_id', auth()->id())->where('id', $orderId)->where('payment_status', Status::PAYMENT_SUCCESS)->first();

        if (!$order) {
            $notify[] = ['error', 'Payment process do not completed yet'];
            return back()->withNotify($notify);
        }

        $pageTitle      = 'Purchased Item: ' . $order->trx;
        $purchasedItems = OrderItem::where('order_id', $order->id)->latest('id')->searchable(['product:title', 'purchase_code'])->where('is_refunded', Status::NO)->paginate(getPaginate());

        return view('Template::user.download', compact('pageTitle', 'purchasedItems', 'reviewCategories'));
    }
}