Completed
Push — development ( 28ddf9...419f29 )
by Ashutosh
10:12
created

DashboardController::getProductNameList()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 9.6111
c 0
b 0
f 0
cc 5
nc 4
nop 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
        $productNameList = [];
30
        $productSoldlists = $this->recentProductSold();
31
32
        if (count($productSoldlists) > 0) {
33
34
            $productNameList[] = $this->getProductNameList($productSoldlists);
35
           }
36
37
        $arraylists = array_count_values($productNameList);
38
        $orders = $this->getRecentOrders();
39
        $subscriptions = $this->expiringSubscription();
40
        $invoices = $this->getRecentInvoices();
41
        $products = $this->totalProductsSold();
42
        $productName = [];
43
        if (!empty($products)) {
44
            foreach ($products as $product) {
45
                if ($product && $product->name) {
46
                    $productName[] = $product->name;
47
                }
48
            }
49
        }
50
        $arrayCountList = array_count_values($productName);
51
52
        return view('themes.default1.common.dashboard', compact('totalSalesINR', 'totalSalesUSD',
53
                'yearlySalesINR', 'yearlySalesUSD', 'monthlySalesINR', 'monthlySalesUSD', 'users',
54
55
                 'count_users', 'arraylists', 'productSoldlists','orders','subscriptions','invoices',
56
                 'products', 'arrayCountList'));
57
    }
58
59
    /**
60
     * Get Total Sales in Indian Currency.
61
     *
62
     * @return float
63
     */
64
    public function getTotalSalesInInr()
65
    {
66
        $total = Invoice::where('currency', 'INR')->pluck('grand_total')->all();
67
        $grandTotal = array_sum($total);
68
69
        return $grandTotal;
70
    }
71
72
    /**
73
     * Get total sales in US Dollar.
74
     *
75
     * @return float
76
     */
77
    public function getTotalSalesInUsd()
78
    {
79
        $total = Invoice::where('currency', 'USD')->pluck('grand_total')->all();
80
        $grandTotal = array_sum($total);
81
82
        return $grandTotal;
83
    }
84
85
    /**
86
     * Get  Total yearly sale of present year IN INR.
87
     *
88
     * @return type
89
     */
90
    public function getYearlySalesInInr()
91
    {
92
        $currentYear = date('Y');
93
        $total = Invoice::whereYear('created_at', '=', $currentYear)->where('currency', 'INR')
94
                ->pluck('grand_total')->all();
95
        $grandTotal = array_sum($total);
96
97
        return $grandTotal;
98
    }
99
100
    /**
101
     * Get  Total yearly sale of present year in USD.
102
     *
103
     * @return type
104
     */
105
    public function getYearlySalesInUsd()
106
    {
107
        $currentYear = date('Y');
108
        $total = Invoice::whereYear('created_at', '=', $currentYear)->where('currency', 'USD')
109
                ->pluck('grand_total')->all();
110
        $grandTotal = array_sum($total);
111
112
        return $grandTotal;
113
    }
114
115
    /**
116
     * Get  Total Monthly sale of present month in Inr.
117
     *
118
     * @return type
119
     */
120
    public function getMonthlySalesInInr()
121
    {
122
        $currentMonth = date('m');
123
        $currentYear = date('Y');
124
        $total = Invoice::whereYear('created_at', '=', $currentYear)->whereMonth('created_at', '=', $currentMonth)
125
                ->where('currency', 'INR')
126
                ->pluck('grand_total')->all();
127
        $grandTotal = array_sum($total);
128
129
        return $grandTotal;
130
    }
131
132
    /**
133
     * Get  Total Monthly sale of present month in Usd.
134
     *
135
     * @return type
136
     */
137
    public function getMonthlySalesInUsd()
138
    {
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 20 registered users.
152
     *
153
     * @return type
154
     */
155
    public function getAllUsers()
156
    {
157
        $allUsers = User::orderBy('created_at', 'desc')->where('active', 1)->where('mobile_verified', 1)
158
              ->take(20)
159
              ->get()
160
              ->toArray();
161
162
        return $allUsers;
163
    }
164
165
    /**
166
     * List of products sold in past 30 days.
167
     *
168
     * @return type
169
     */
170
    public function recentProductSold()
171
    {
172
        try {
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
    }catch(\Exception $ex )
183
     {
184
        return redirect()->back()->with('fails', $ex->getMessage());
185
     }
186
187
    }
188
189
    /**
190
     * List of orders of past 30 days.
191
     */
192
    public function getRecentOrders()
193
    {
194
        $dayUtc = new Carbon('-30 days');
195
        $minus30Day = $dayUtc->toDateTimeString();
196
        $recentOrders = Order::where('created_at', '>', $minus30Day)->orderBy('created_at', 'desc')
197
                 ->where('price_override', '>', 0)->get();
198
199
        return $recentOrders;
200
    }
201
202
    /**
203
     * List of Invoices of past 30 ays.
204
     */
205
    public function getRecentInvoices()
206
    {
207
        $dayUtc = new Carbon('-30 days');
208
        $minus30Day = $dayUtc->toDateTimeString();
209
        $recentInvoice = Invoice::where('created_at', '>', $minus30Day)->orderBy('created_at', 'desc')
210
                        ->where('grand_total', '>', 0)->get();
211
212
        return $recentInvoice;
213
    }
214
215
    /**
216
     * List of orders expiring in next 30 days.
217
     */
218
    public function expiringSubscription()
219
    {
220
        $dayUtc = new Carbon('+30 days');
221
        $today = Carbon::now()->toDateTimeString();
222
        $plus30Day = $dayUtc->toDateTimeString();
223
        $subsEnds = Subscription::where('ends_at', '>', $today)->where('ends_at', '<=', $plus30Day)->get();
224
225
        return $subsEnds;
226
    }
227
228
    /**
229
     * List of the products sold.
230
     */
231
    public function totalProductsSold()
232
    {
233
        $product = [];
234
        $orders = Order::where('order_status', 'executed')->get();
235
        foreach ($orders as $order) {
236
            $product[] = $order->product()->first();
237
        }
238
239
        return $product;
240
    }
241
242
    public function getProductNameList($productSoldlists)
243
    {
244
        try{
245
         foreach ($productSoldlists as $productSoldlist) {
246
            if($productSoldlist && $productSoldlist->name){
247
                $productNameList[] = $productSoldlist->name;
248
            }
249
                
250
        }
251
        return $productNameList;
252
      }catch (\Exception $ex){
253
        return redirect()->back()->with('fails',$ex->getMessage());
254
      }
255
    }
256
}
257