sarga/packages/Webkul/Admin/src/Http/Controllers/DashboardController.php

320 lines
10 KiB
PHP
Raw Normal View History

2018-06-20 05:06:27 +00:00
<?php
namespace Webkul\Admin\Http\Controllers;
2018-10-23 11:45:44 +00:00
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;
2019-06-29 12:21:49 +00:00
use Webkul\Sales\Repositories\OrderRepository;
use Webkul\Sales\Repositories\OrderItemRepository;
use Webkul\Customer\Repositories\CustomerRepository;
use Webkul\Product\Repositories\ProductInventoryRepository;
2018-06-20 05:06:27 +00:00
/**
* Dashboard controller
*
* @author Jitendra Singh <jitendra@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class DashboardController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
2018-07-12 07:12:48 +00:00
protected $_config;
2018-10-23 11:45:44 +00:00
/**
* OrderRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
2019-06-29 12:21:49 +00:00
protected $orderRepository;
2018-10-23 11:45:44 +00:00
/**
* OrderItemRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
2019-06-29 12:21:49 +00:00
protected $orderItemRepository;
2018-10-23 11:45:44 +00:00
/**
* CustomerRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
2019-06-29 12:21:49 +00:00
protected $customerRepository;
2018-10-23 11:45:44 +00:00
/**
* ProductInventoryRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
2019-06-29 12:21:49 +00:00
protected $productInventoryRepository;
2018-10-23 11:45:44 +00:00
/**
* string object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
protected $startDate;
2018-10-23 11:45:44 +00:00
/**
* string object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
protected $lastStartDate;
2018-10-23 11:45:44 +00:00
/**
* string object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
protected $endDate;
2018-10-23 11:45:44 +00:00
/**
* string object
*
2019-07-01 11:33:36 +00:00
* @var Object
2018-10-23 11:45:44 +00:00
*/
protected $lastEndDate;
/**
* Create a new controller instance.
*
2019-06-29 12:21:49 +00:00
* @param \Webkul\Sales\Repositories\OrderRepository $orderRepository
* @param \Webkul\Sales\Repositories\OrderItemRepository $orderItemRepository
* @param \Webkul\Customer\Repositories\CustomerRepository $customerRepository
* @param \Webkul\Product\Repositories\ProductInventoryRepository $productInventoryRepository
2018-10-23 11:45:44 +00:00
* @return void
*/
public function __construct(
2019-06-29 12:21:49 +00:00
OrderRepository $orderRepository,
OrderItemRepository $orderItemRepository,
CustomerRepository $customerRepository,
ProductInventoryRepository $productInventoryRepository
2018-10-23 11:45:44 +00:00
)
2018-07-12 07:12:48 +00:00
{
$this->_config = request('_config');
$this->middleware('admin');
2018-10-23 11:45:44 +00:00
2019-06-29 12:21:49 +00:00
$this->orderRepository = $orderRepository;
2018-10-23 11:45:44 +00:00
2019-06-29 12:21:49 +00:00
$this->orderItemRepository = $orderItemRepository;
2018-10-23 11:45:44 +00:00
2019-06-29 12:21:49 +00:00
$this->customerRepository = $customerRepository;
2018-10-23 11:45:44 +00:00
2019-06-29 12:21:49 +00:00
$this->productInventoryRepository = $productInventoryRepository;
2018-10-23 11:45:44 +00:00
}
2019-07-01 11:33:36 +00:00
/**
* Returns percentage difference
*
* @return integer
*/
2018-10-23 11:45:44 +00:00
public function getPercentageChange($previous, $current)
{
2019-01-15 11:54:41 +00:00
if (! $previous)
2018-10-23 11:45:44 +00:00
return $current ? 100 : 0;
return ($current - $previous) / $previous * 100;
2018-07-12 07:12:48 +00:00
}
2018-10-23 11:45:44 +00:00
/**
* Display a listing of the resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-10-23 11:45:44 +00:00
*/
2018-06-20 05:06:27 +00:00
public function index()
{
2018-10-23 11:45:44 +00:00
$this->setStartEndDate();
$statistics = [
'total_customers' => [
2019-06-03 20:22:37 +00:00
'previous' => $previous = $this->getCustomersBetweenDates($this->lastStartDate, $this->lastEndDate)->count(),
'current' => $current = $this->getCustomersBetweenDates($this->startDate, $this->endDate)->count(),
2018-10-23 11:45:44 +00:00
'progress' => $this->getPercentageChange($previous, $current)
],
'total_orders' => [
2019-06-03 20:22:37 +00:00
'previous' => $previous = $this->previousOrders()->count(),
'current' => $current = $this->currentOrders()->count(),
2018-10-23 11:45:44 +00:00
'progress' => $this->getPercentageChange($previous, $current)
],
'total_sales' => [
'previous' => $previous = $this->previousOrders()->sum('base_grand_total_invoiced') - $this->previousOrders()->sum('base_grand_total_refunded'),
'current' => $current = $this->currentOrders()->sum('base_grand_total_invoiced') - $this->currentOrders()->sum('base_grand_total_refunded'),
2018-10-23 11:45:44 +00:00
'progress' => $this->getPercentageChange($previous, $current)
],
'avg_sales' => [
2019-09-24 07:05:18 +00:00
'previous' => $previous = $this->previousOrders()->avg('base_grand_total_invoiced') - $this->previousOrders()->avg('base_grand_total_refunded'),
'current' => $current = $this->currentOrders()->avg('base_grand_total_invoiced') - $this->currentOrders()->avg('base_grand_total_refunded'),
2018-10-23 11:45:44 +00:00
'progress' => $this->getPercentageChange($previous, $current)
],
'top_selling_categories' => $this->getTopSellingCategories(),
'top_selling_products' => $this->getTopSellingProducts(),
'customer_with_most_sales' => $this->getCustomerWithMostSales(),
'stock_threshold' => $this->getStockThreshold(),
];
foreach (core()->getTimeInterval($this->startDate, $this->endDate) as $interval) {
$statistics['sale_graph']['label'][] = $interval['start']->format('d M');
$total = $this->getOrdersBetweenDate($interval['start'], $interval['end'])->sum('base_grand_total_invoiced') - $this->getOrdersBetweenDate($interval['start'], $interval['end'])->sum('base_grand_total_refunded');
2018-10-23 11:45:44 +00:00
$statistics['sale_graph']['total'][] = $total;
$statistics['sale_graph']['formated_total'][] = core()->formatBasePrice($total);
}
return view($this->_config['view'], compact('statistics'))->with(['startDate' => $this->startDate, 'endDate' => $this->endDate]);
}
/**
* Returns the list of top selling categories
*
2019-07-01 11:33:36 +00:00
* @return Collection
2018-10-23 11:45:44 +00:00
*/
public function getTopSellingCategories()
{
2019-06-29 12:21:49 +00:00
return $this->orderItemRepository->getModel()
2018-10-23 11:45:44 +00:00
->leftJoin('products', 'order_items.product_id', 'products.id')
->leftJoin('product_categories', 'products.id', 'product_categories.product_id')
->leftJoin('categories', 'product_categories.category_id', 'categories.id')
->leftJoin('category_translations', 'categories.id', 'category_translations.category_id')
->where('category_translations.locale', app()->getLocale())
->where('order_items.created_at', '>=', $this->startDate)
->where('order_items.created_at', '<=', $this->endDate)
->addSelect(DB::raw('SUM(qty_invoiced - qty_refunded) as total_qty_invoiced'))
2019-12-05 07:51:52 +00:00
->addSelect(DB::raw('COUNT('.DB::getTablePrefix().'products.id) as total_products'))
2018-10-23 11:45:44 +00:00
->addSelect('order_items.id', 'categories.id as category_id', 'category_translations.name')
2019-02-11 09:43:38 +00:00
->groupBy('categories.id')
->havingRaw('SUM(qty_invoiced - qty_refunded) > 0')
->orderBy('total_qty_invoiced', 'DESC')
2018-10-23 11:45:44 +00:00
->limit(5)
->get();
}
/**
* Return stock threshold.
*
2019-07-01 11:33:36 +00:00
* @return Collection
2018-10-23 11:45:44 +00:00
*/
public function getStockThreshold()
{
2019-06-29 12:21:49 +00:00
return $this->productInventoryRepository->getModel()
2018-10-23 11:45:44 +00:00
->leftJoin('products', 'product_inventories.product_id', 'products.id')
->select(DB::raw('SUM(qty) as total_qty'))
->addSelect('product_inventories.product_id')
->groupBy('product_id')
->orderBy('total_qty', 'ASC')
->limit(5)
->get();
}
/**
* Returns top selling products
2019-07-01 11:33:36 +00:00
* @return Collection
2018-10-23 11:45:44 +00:00
*/
public function getTopSellingProducts()
{
2019-06-29 12:21:49 +00:00
return $this->orderItemRepository->getModel()
2018-10-23 11:45:44 +00:00
->select(DB::raw('SUM(qty_ordered) as total_qty_ordered'))
->addSelect('id', 'product_id', 'product_type', 'name')
->where('order_items.created_at', '>=', $this->startDate)
->where('order_items.created_at', '<=', $this->endDate)
->whereNull('parent_id')
->groupBy('product_id')
->orderBy('total_qty_ordered', 'DESC')
->limit(5)
->get();
}
/**
* Returns top selling products
*
2019-07-01 11:33:36 +00:00
* @return Collection
2018-10-23 11:45:44 +00:00
*/
public function getCustomerWithMostSales()
{
2019-06-29 12:21:49 +00:00
return $this->orderRepository->getModel()
2018-10-23 11:45:44 +00:00
->select(DB::raw('SUM(base_grand_total) as total_base_grand_total'))
->addSelect(DB::raw('COUNT(id) as total_orders'))
->addSelect('id', 'customer_id', 'customer_email', 'customer_first_name', 'customer_last_name')
->where('orders.created_at', '>=', $this->startDate)
->where('orders.created_at', '<=', $this->endDate)
->groupBy('customer_email')
->orderBy('total_base_grand_total', 'DESC')
->limit(5)
->get();
}
/**
* Sets start and end date
*
* @return void
*/
public function setStartEndDate()
{
$this->startDate = request()->get('start')
2018-10-23 11:45:44 +00:00
? Carbon::createFromTimeString(request()->get('start') . " 00:00:01")
: Carbon::createFromTimeString(Carbon::now()->subDays(30)->format('Y-m-d') . " 00:00:01");
$this->endDate = request()->get('end')
2018-10-23 11:45:44 +00:00
? Carbon::createFromTimeString(request()->get('end') . " 23:59:59")
: Carbon::now();
2019-01-15 11:54:41 +00:00
if ($this->endDate > Carbon::now())
2018-10-23 11:45:44 +00:00
$this->endDate = Carbon::now();
$this->lastStartDate = clone $this->startDate;
2018-11-27 05:23:47 +00:00
$this->lastEndDate = clone $this->startDate;
2018-10-23 11:45:44 +00:00
2018-11-27 05:23:47 +00:00
$this->lastStartDate->subDays($this->startDate->diffInDays($this->endDate));
// $this->lastEndDate->subDays($this->lastStartDate->diffInDays($this->lastEndDate));
2018-06-20 05:06:27 +00:00
}
2019-06-03 20:22:37 +00:00
2019-07-01 11:33:36 +00:00
/**
* Returns previous order query
*
* @return mixed
*/
2019-06-03 20:22:37 +00:00
private function previousOrders()
{
return $this->getOrdersBetweenDate($this->lastStartDate, $this->lastEndDate);
}
2019-07-01 11:33:36 +00:00
/**
* Returns current order query
*
* @return mixed
*/
2019-06-03 20:22:37 +00:00
private function currentOrders()
{
return $this->getOrdersBetweenDate($this->startDate, $this->endDate);
}
2019-07-01 11:33:36 +00:00
/**
* Returns orders between two dates
*
* @return mixed
*/
2019-06-03 20:22:37 +00:00
private function getOrdersBetweenDate($start, $end)
{
2019-06-29 12:21:49 +00:00
return $this->orderRepository->scopeQuery(function ($query) use ($start, $end) {
2019-06-03 20:22:37 +00:00
return $query->where('orders.created_at', '>=', $start)->where('orders.created_at', '<=', $end);
});
}
2019-07-01 11:33:36 +00:00
/**
* Returns customers between two dates
*
* @return mixed
*/
2019-06-03 20:22:37 +00:00
private function getCustomersBetweenDates($start, $end)
{
2019-06-29 12:21:49 +00:00
return $this->customerRepository->scopeQuery(function ($query) use ($start, $end) {
2019-06-03 20:22:37 +00:00
return $query->where('customers.created_at', '>=', $start)->where('customers.created_at', '<=', $end);
});
}
2018-10-23 11:45:44 +00:00
}