Completed
Push — development ( 5717ca...90583d )
by Ashutosh
08:29
created

DashboardController::getPendingPaymentsInInr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
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 Illuminate\Http\Request;
9
use App\User;
10
use Carbon\Carbon;
11
12
class DashboardController extends Controller
13
{
14
    public function __construct()
15
    {
16
        $this->middleware('auth', ['only' => ['index']]);
17
        $this->middleware('admin', ['only' => ['index']]);
18
    }
19
20
    public function index(Request $request)
21
    {
22
        $totalSalesINR = $this->getTotalSalesInInr();
23
        $totalSalesUSD = $this->getTotalSalesInUsd();
24
        $yearlySalesINR = $this->getYearlySalesInInr();
25
        $yearlySalesUSD = $this->getYearlySalesInUsd();
26
        $monthlySalesINR = $this->getMonthlySalesInInr();
27
        $monthlySalesUSD = $this->getMonthlySalesInUsd();
28
        $pendingPaymentINR = $this->getPendingPaymentsInInr();
29
        $pendingPaymentUSD = $this->getPendingPaymentsInUsd();
30
        $users = $this->getAllUsers();
31
        $count_users = User::get()->count();
32
        $productNameList = [];
33
        $productSoldlists = $this->recentProductSold();
34
        if (count($productSoldlists) > 0) {
35
            $productNameList = $this->getProductNameList($productSoldlists);
36
        }
37
38
        $arraylists = array_count_values($productNameList);
39
        $orders = $this->getRecentOrders();
40
        $subscriptions = $this->expiringSubscription();
41
        $invoices = $this->getRecentInvoices();
42
        $products = $this->totalProductsSold();
43
        $productName = [];
44
        if (!empty($products)) {
45
            foreach ($products as $product) {
46
                if ($product && $product->name) {
47
                    $productName[] = $product->name;
48
                }
49
            }
50
        }
51
        $arrayCountList = array_count_values($productName);
52
        $status = $request->input('status');
53
       
54
55
        return view('themes.default1.common.dashboard', compact('totalSalesINR', 'totalSalesUSD',
56
                'yearlySalesINR', 'yearlySalesUSD', 'monthlySalesINR', 'monthlySalesUSD', 'users',
57
58
                 'count_users', 'arraylists', 'productSoldlists','orders','subscriptions','invoices',
59
                 'products', 'arrayCountList','pendingPaymentINR','pendingPaymentUSD','status'));
60
    }
61
62
    /**
63
     * Get Total Sales in Indian Currency.
64
     *
65
     * @return float
66
     */
67
    public function getTotalSalesInInr()
68
    {
69
        $total = Invoice::where('currency', 'INR')
70
        ->where('status', '=','success')
71
        ->pluck('grand_total')->all();
72
        $grandTotal = array_sum($total);
73
74
        return $grandTotal;
75
    }
76
77
    /**
78
     * Get total sales in US Dollar.
79
     *
80
     * @return float
81
     */
82
    public function getTotalSalesInUsd()
83
    {
84
        $total = Invoice::where('currency', 'USD')
85
        ->where('status', '=','success')
86
        ->pluck('grand_total')->all();
87
        $grandTotal = array_sum($total);
88
89
        return $grandTotal;
90
    }
91
92
    /**
93
     * Get  Total yearly sale of present year IN INR.
94
     *
95
     * @return type
96
     */
97
    public function getYearlySalesInInr()
98
    {
99
        $currentYear = date('Y');
100
        $total = Invoice::whereYear('created_at', '=', $currentYear)
101
        ->where('status', '=','success')
102
        ->where('currency', 'INR')
103
        ->pluck('grand_total')->all();
104
        $grandTotal = array_sum($total);
105
106
        return $grandTotal;
107
    }
108
109
    /**
110
     * Get  Total yearly sale of present year in USD.
111
     *
112
     * @return type
113
     */
114
    public function getYearlySalesInUsd()
115
    {
116
        $currentYear = date('Y');
117
        $total = Invoice::whereYear('created_at', '=', $currentYear)
118
        ->where('status', '=','success')
119
        ->where('currency', 'USD')
120
        ->pluck('grand_total')->all();
121
        $grandTotal = array_sum($total);
122
123
        return $grandTotal;
124
    }
125
126
    /**
127
     * Get  Total Monthly sale of present month in Inr.
128
     *
129
     * @return type
130
     */
131
    public function getMonthlySalesInInr()
132
    {
133
        $currentMonth = date('m');
134
        $currentYear = date('Y');
135
        $total = Invoice::whereYear('created_at', '=', $currentYear)->whereMonth('created_at', '=', $currentMonth)
136
                ->where('currency', 'INR')
137
                ->where('status', '=','success')
138
                ->pluck('grand_total')->all();
139
        $grandTotal = array_sum($total);
140
141
        return $grandTotal;
142
    }
143
144
    /**
145
     * Get  Total Monthly sale of present month in Usd.
146
     *
147
     * @return type
148
     */
149
    public function getMonthlySalesInUsd()
150
    {
151
        $currentMonth = date('m');
152
        $currentYear = date('Y');
153
        // dd($currentYear,$currentMonth );
154
        $total = Invoice::whereYear('created_at', '=', $currentYear)->whereMonth('created_at', '=', $currentMonth)
155
                ->where('currency', 'USD')
156
                 ->where('status', '=','success')
157
                ->pluck('grand_total')->all();
158
        $grandTotal = array_sum($total);
159
160
        return $grandTotal;
161
    }
162
    
163
     /**
164
     * Get  Total Pending Payment Inr.
165
     *
166
     * @return type
167
     */
168
    public function getPendingPaymentsInInr()
169
    {
170
        $total = Invoice::where('currency', 'INR')
171
        ->where('status', '=','pending')
172
        ->pluck('grand_total')->all();
173
        $grandTotal = array_sum($total);
174
175
        return $grandTotal;
176
    }
177
178
     /**
179
     * Get  Total Pending Payment Inr.
180
     *
181
     * @return type
182
     */
183
    public function getPendingPaymentsInUsd()
184
    {
185
        $total = Invoice::where('currency', 'USD')
186
        ->where('status', '=','pending')
187
        ->pluck('grand_total')->all();
188
        $grandTotal = array_sum($total);
189
190
        return $grandTotal;
191
    }
192
193
194
    // getPendingPaymentsInInr
195
196
    /**
197
     * Get the list of previous 20 registered users.
198
     *
199
     * @return type
200
     */
201
    public function getAllUsers()
202
    {
203
        $allUsers = User::orderBy('created_at', 'desc')->where('active', 1)->where('mobile_verified', 1)
204
              ->take(20)
205
              ->get()
206
              ->toArray();
207
208
        return $allUsers;
209
    }
210
211
    /**
212
     * List of products sold in past 30 days.
213
     *
214
     * @return type
215
     */
216
    public function recentProductSold()
217
    {
218
        try {
219
            $dayUtc = new Carbon('-30 days');
220
            $minus30Day = $dayUtc->toDateTimeString();
221
            $product = [];
222
            $orders = Order::where('order_status', 'executed')->where('created_at', '>', $minus30Day)->get();
223
            foreach ($orders as $order) {
224
                $product[] = $order->product()->first();
225
            }
226
227
            return $product;
228
        } catch (\Exception $ex) {
229
            return redirect()->back()->with('fails', $ex->getMessage());
230
        }
231
    }
232
233
    /**
234
     * List of orders of past 30 days.
235
     */
236
    public function getRecentOrders()
237
    {
238
        $dayUtc = new Carbon('-30 days');
239
        $minus30Day = $dayUtc->toDateTimeString();
240
        $recentOrders = Order::where('created_at', '>', $minus30Day)->orderBy('created_at', 'desc')
241
                 ->where('price_override', '>', 0)->get();
242
243
        return $recentOrders;
244
    }
245
246
    /**
247
     * List of Invoices of past 30 ays.
248
     */
249
    public function getRecentInvoices()
250
    {
251
        $dayUtc = new Carbon('-30 days');
252
        $minus30Day = $dayUtc->toDateTimeString();
253
        $recentInvoice = Invoice::where('created_at', '>', $minus30Day)->orderBy('created_at', 'desc')
254
                        ->where('grand_total', '>', 0)->get();
255
256
        return $recentInvoice;
257
    }
258
259
    /**
260
     * List of orders expiring in next 30 days.
261
     */
262
    public function expiringSubscription()
263
    {
264
        $dayUtc = new Carbon('+30 days');
265
        $today = Carbon::now()->toDateTimeString();
266
        $plus30Day = $dayUtc->toDateTimeString();
267
        $subsEnds = Subscription::where('ends_at', '>', $today)->where('ends_at', '<=', $plus30Day)->get();
268
269
        return $subsEnds;
270
    }
271
272
    /**
273
     * List of the products sold.
274
     */
275
    public function totalProductsSold()
276
    {
277
        $product = [];
278
        $orders = Order::where('order_status', 'executed')->get();
279
        foreach ($orders as $order) {
280
            $product[] = $order->product()->first();
281
        }
282
283
        return $product;
284
    }
285
286
    public function getProductNameList($productSoldlists)
287
    {
288
        try {
289
            foreach ($productSoldlists as $productSoldlist) {
290
                if ($productSoldlist && $productSoldlist->name) {
291
                    $productNameList[] = $productSoldlist->name;
292
                }
293
            }
294
295
            return $productNameList;
296
        } catch (\Exception $ex) {
297
            return redirect()->back()->with('fails', $ex->getMessage());
298
        }
299
    }
300
}
301