Completed
Branch development (b1b115)
by Johannes
10:28
created

DashboardController::getRecentInvoices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
nc 1
nop 0
cc 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Model\Order\Invoice;
6
use App\Model\Order\Order;
7
use App\Model\Product\Subscription;
8
use App\User;
9
use Carbon\Carbon;
10
11
class DashboardController extends Controller
12
{
13
    public function __construct()
14
    {
15
        $this->middleware('auth', ['only' => ['index']]);
16
        $this->middleware('admin', ['only' => ['index']]);
17
    }
18
19
    public function index()
20
    {
21
        $totalSalesINR = $this->getTotalSalesInInr();
22
        $totalSalesUSD = $this->getTotalSalesInUsd();
23
        $yearlySalesINR = $this->getYearlySalesInInr();
24
        $yearlySalesUSD = $this->getYearlySalesInUsd();
25
        $monthlySalesINR = $this->getMonthlySalesInInr();
26
        $monthlySalesUSD = $this->getMonthlySalesInUsd();
27
        $users = $this->getAllUsers();
28
        $count_users = User::get()->count();
29
        $productSoldlists = $this->recentProductSold();
30
        $productNameList = [];
31
32
        foreach ($productSoldlists as $productSoldlist) {
33
            $productNameList[] = $productSoldlist->name;
34
        }
35
        $arraylists = array_count_values($productNameList);
36
        $orders = $this->getRecentOrders();
37
        $subscriptions = $this->expiringSubscription();
38
        $invoices = $this->getRecentInvoices();
39
        $products = $this->totalProductsSold();
40
        $productName = [];
41
        foreach ($products as $product) {
42
            $productName[] = $product->name;
43
        }
44
        $arrayCountList = array_count_values($productName);
45
46
        return view('themes.default1.common.dashboard', compact('totalSalesINR', 'totalSalesUSD',
47
                'yearlySalesINR', 'yearlySalesUSD', 'monthlySalesINR', 'monthlySalesUSD', 'users',
48
49
                 'count_users', 'arraylists', 'productSoldlists','orders','subscriptions','invoices',
50
                 'products', 'arrayCountList'));
51
    }
52
53
    /**
54
     * Get Total Sales in Indian Currency.
55
     *
56
     * @return float
57
     */
58
    public function getTotalSalesInInr()
59
    {
60
        $invoice = new Invoice();
61
        $total = $invoice->where('currency', 'INR')->pluck('grand_total')->all();
62
        $grandTotal = array_sum($total);
63
64
        return $grandTotal;
65
    }
66
67
    /**
68
     * Get total sales in US Dollar.
69
     *
70
     * @return float
71
     */
72
    public function getTotalSalesInUsd()
73
    {
74
        $invoice = new Invoice();
75
        $total = $invoice->where('currency', 'USD')->pluck('grand_total')->all();
76
        $grandTotal = array_sum($total);
77
78
        return $grandTotal;
79
    }
80
81
    /**
82
     * Get  Total yearly sale of present year IN INR.
83
     *
84
     * @return type
85
     */
86
    public function getYearlySalesInInr()
87
    {
88
        $invoice = new Invoice();
89
        $currentYear = date('Y');
90
        $total = $invoice::whereYear('created_at', '=', $currentYear)->where('currency', 'INR')
91
                ->pluck('grand_total')->all();
92
        $grandTotal = array_sum($total);
93
94
        return $grandTotal;
95
    }
96
97
    /**
98
     * Get  Total yearly sale of present year in USD.
99
     *
100
     * @return type
101
     */
102
    public function getYearlySalesInUsd()
103
    {
104
        $invoice = new Invoice();
105
        $currentYear = date('Y');
106
        $total = $invoice::whereYear('created_at', '!=', $currentYear)->where('currency', 'USD')
107
                ->pluck('grand_total')->all();
108
        $grandTotal = array_sum($total);
109
110
        return $grandTotal;
111
    }
112
113
    /**
114
     * Get  Total Monthly sale of present month in Inr.
115
     *
116
     * @return type
117
     */
118
    public function getMonthlySalesInInr()
119
    {
120
        $invoice = new Invoice();
121
        $currentMonth = date('m');
122
        $currentYear = date('Y');
123
        $total = $invoice::whereYear('created_at', '=', $currentYear)->whereMonth('created_at', '=', $currentMonth)
124
                ->where('currency', 'INR')
125
                ->pluck('grand_total')->all();
126
        $grandTotal = array_sum($total);
127
128
        return $grandTotal;
129
    }
130
131
    /**
132
     * Get  Total Monthly sale of present month in Usd.
133
     *
134
     * @return type
135
     */
136
    public function getMonthlySalesInUsd()
137
    {
138
        $invoice = new Invoice();
139
        $currentMonth = date('m');
140
        $currentYear = date('Y');
141
        // dd($currentYear,$currentMonth );
142
        $total = $invoice::whereYear('created_at', '=', $currentYear)->whereMonth('created_at', '=', $currentMonth)
143
                ->where('currency', 'USD')
144
                ->pluck('grand_total')->all();
145
        $grandTotal = array_sum($total);
146
147
        return $grandTotal;
148
    }
149
150
    /**
151
     * Get the list of previous 8 registered users.
152
     *
153
     * @return type
154
     */
155
    public function getAllUsers()
156
    {
157
        $user = new User();
158
        $allUsers = $user->orderBy('created_at', 'desc')->where('active', 1)->where('mobile_verified', 1)
159
              ->take(8)
160
              ->get()
161
              ->toArray();
162
163
        return $allUsers;
164
    }
165
166
    /**
167
     * List of products sold in past 30 days.
168
     *
169
     * @return type
170
     */
171
    public function recentProductSold()
172
    {
173
        $dayUtc = new Carbon('-30 days');
174
        $minus30Day = $dayUtc->toDateTimeString();
175
        $product = [];
176
        $orders = Order::where('order_status', 'executed')->where('created_at', '>', $minus30Day)->get();
177
        foreach ($orders as $order) {
178
            $product[] = $order->product()->first();
179
        }
180
181
        return $product;
182
    }
183
184
    /**
185
     * List of orders of past 30 days.
186
     */
187
    public function getRecentOrders()
188
    {
189
        $dayUtc = new Carbon('-30 days');
190
        $minus30Day = $dayUtc->toDateTimeString();
191
        $recentOrders = Order::where('created_at', '>', $minus30Day)->orderBy('created_at', 'desc')
192
                 ->where('price_override', '>', 0)->get();
193
194
        return $recentOrders;
195
    }
196
197
    /**
198
     * List of Invoices of past 30 ays.
199
     */
200
    public function getRecentInvoices()
201
    {
202
        $dayUtc = new Carbon('-30 days');
203
        $minus30Day = $dayUtc->toDateTimeString();
204
        $recentInvoice = Invoice::where('created_at', '>', $minus30Day)->orderBy('created_at', 'desc')
205
                        ->where('grand_total', '>', 0)->get();
206
207
        return $recentInvoice;
208
    }
209
210
    /**
211
     * List of orders expiring in next 30 days.
212
     */
213
    public function expiringSubscription()
214
    {
215
        $dayUtc = new Carbon('+30 days');
216
        $today = Carbon::now()->toDateTimeString();
217
        $plus30Day = $dayUtc->toDateTimeString();
218
        $subsEnds = Subscription::where('ends_at', '>', $today)->where('ends_at', '<=', $plus30Day)->get();
219
220
        return $subsEnds;
221
    }
222
223
    /**
224
     * List of the products sold.
225
     */
226
    public function totalProductsSold()
227
    {
228
        $product = [];
229
        $orders = Order::where('order_status', 'executed')->get();
230
        foreach ($orders as $order) {
231
            $product[] = $order->product()->first();
232
        }
233
234
        return $product;
235
    }
236
}
237