From 870f52505d11e8a45fbf3b8ec1d1d9b486e7e4b1 Mon Sep 17 00:00:00 2001 From: Fahim Date: Tue, 10 Aug 2021 01:23:24 +0600 Subject: [PATCH] Added: Laravel Debugbar & Charts In Dashboard --- .../Resources/views/products/show.blade.php | 2 +- app/Helpers/helpers.php | 8 +- app/Http/Controllers/HomeController.php | 139 +++++++++-- app/Http/Livewire/Adjustment/ProductTable.php | 2 +- app/Http/Livewire/Barcode/ProductTable.php | 4 + composer.json | 3 +- composer.lock | 220 +++++++++++++++++- resources/views/home.blade.php | 156 +++++++++++++ resources/views/layouts/app.blade.php | 46 ++-- resources/views/layouts/header.blade.php | 11 +- .../livewire/barcode/product-table.blade.php | 30 ++- .../views/livewire/search-product.blade.php | 14 +- routes/web.php | 9 +- storage/debugbar/.gitignore | 2 + 14 files changed, 585 insertions(+), 61 deletions(-) create mode 100644 storage/debugbar/.gitignore diff --git a/Modules/Product/Resources/views/products/show.blade.php b/Modules/Product/Resources/views/products/show.blade.php index 001220df..f58cbe61 100644 --- a/Modules/Product/Resources/views/products/show.blade.php +++ b/Modules/Product/Resources/views/products/show.blade.php @@ -41,7 +41,7 @@ Cost - {{ format_currency($product->product_price) }} + {{ format_currency($product->product_cost) }} Price diff --git a/app/Helpers/helpers.php b/app/Helpers/helpers.php index 18d1d0ab..6bf60a0b 100644 --- a/app/Helpers/helpers.php +++ b/app/Helpers/helpers.php @@ -12,10 +12,12 @@ if (!function_exists('format_currency')) { return $value; } - if (settings()->default_currency_position == 'prefix') { - $formatted_value = settings()->currency->symbol . number_format((float) $value, 2, settings()->currency->decimal_separator, settings()->currency->thousand_separator); + $settings = settings(); + + if ($settings->default_currency_position == 'prefix') { + $formatted_value = $settings->currency->symbol . number_format((float) $value, 2, $settings->currency->decimal_separator, $settings->currency->thousand_separator); } else { - $formatted_value = number_format((float) $value, 2, settings()->currency->decimal_separator, settings()->currency->thousand_separator) . settings()->currency->symbol; + $formatted_value = number_format((float) $value, 2, $settings->currency->decimal_separator, $settings->currency->thousand_separator) . $settings->currency->symbol; } return $formatted_value; diff --git a/app/Http/Controllers/HomeController.php b/app/Http/Controllers/HomeController.php index 7cbc2c3f..c84ba790 100644 --- a/app/Http/Controllers/HomeController.php +++ b/app/Http/Controllers/HomeController.php @@ -3,26 +3,133 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use Illuminate\Support\Carbon; +use Illuminate\Support\Facades\DB; +use Modules\Expense\Entities\Expense; +use Modules\Product\Entities\Product; +use Modules\Purchase\Entities\Purchase; +use Modules\PurchasesReturn\Entities\PurchaseReturn; +use Modules\Sale\Entities\Sale; +use Modules\SalesReturn\Entities\SaleReturn; class HomeController extends Controller { - /** - * Create a new controller instance. - * - * @return void - */ - public function __construct() - { - $this->middleware('auth'); + + public function index() { + $sales = Sale::sum('total_amount'); + $sale_returns = SaleReturn::sum('total_amount'); + $purchase_returns = PurchaseReturn::sum('total_amount'); + $product_costs = 0; + + foreach (Sale::with('saleDetails')->get() as $sale) { + foreach ($sale->saleDetails as $saleDetail) { + $product_costs += $saleDetail->product->product_cost; + } + } + + $revenue = ($sales - $sale_returns) / 100; + $profit = $revenue - $product_costs; + + return view('home', [ + 'revenue' => $revenue, + 'sale_returns' => $sale_returns / 100, + 'purchase_returns' => $purchase_returns / 100, + 'profit' => $profit + ]); } - /** - * Show the application dashboard. - * - * @return \Illuminate\Contracts\Support\Renderable - */ - public function index() - { - return view('home'); + + public function currentMonthChart() { + $currentMonthSales = Sale::whereMonth('date', date('m')) + ->whereYear('date', date('Y')) + ->sum('total_amount') / 100; + $currentMonthPurchases = Purchase::whereMonth('date', date('m')) + ->whereYear('date', date('Y')) + ->sum('total_amount') / 100; + $currentMonthExpenses = Expense::whereMonth('date', date('m')) + ->whereYear('date', date('Y')) + ->sum('amount') / 100; + + return response()->json([ + 'sales' => $currentMonthSales, + 'purchases' => $currentMonthPurchases, + 'expenses' => $currentMonthExpenses + ]); + } + + + public function salesPurchasesChart() { + $sales = $this->salesChartData(); + $purchases = $this->purchasesChartData(); + + return response()->json(['sales' => $sales, 'purchases' => $purchases]); + } + + + public function salesChartData() { + // Build an array of the dates we want to show, the oldest first + $dates = collect(); + foreach (range(-6, 0) as $i) { + $date = Carbon::now()->addDays($i)->format('d-m-y'); + $dates->put($date, 0); + } + + $date_range = Carbon::today()->subDays(6); + // Get the sales counts + $sales = Sale::where('date', '>=', $date_range) + ->groupBy(DB::raw("DATE_FORMAT(date,'%d-%m-%y')")) + ->orderBy('date', 'asc') + ->get([ + DB::raw(DB::raw("DATE_FORMAT(date,'%d-%m-%y') as date")), + DB::raw('SUM(total_amount) AS count'), + ]) + ->pluck('count', 'date'); + + // Merge the two collections; + $dates = $dates->merge($sales); + + $data = []; + $days = []; + foreach ($dates as $key => $value) { + $data[] = $value / 100; + $days[] = $key; + } + + return response()->json(['data' => $data, 'days' => $days]); + } + + + public function purchasesChartData() { + // Build an array of the dates we want to show, the oldest first + $dates = collect(); + foreach (range(-6, 0) as $i) { + $date = Carbon::now()->addDays($i)->format('d-m-y'); + $dates->put($date, 0); + } + + $date_range = Carbon::today()->subDays(6); + + // Get the purchases counts + $purchases = Purchase::where('date', '>=', $date_range) + ->groupBy(DB::raw("DATE_FORMAT(date,'%d-%m-%y')")) + ->orderBy('date', 'asc') + ->get([ + DB::raw(DB::raw("DATE_FORMAT(date,'%d-%m-%y') as date")), + DB::raw('SUM(total_amount) AS count'), + ]) + ->pluck('count', 'date'); + + // Merge the two collections; + $dates = $dates->merge($purchases); + + $data = []; + $days = []; + foreach ($dates as $key => $value) { + $data[] = $value / 100; + $days[] = $key; + } + + return response()->json(['data' => $data, 'days' => $days]); + } } diff --git a/app/Http/Livewire/Adjustment/ProductTable.php b/app/Http/Livewire/Adjustment/ProductTable.php index 8cc8f1a8..550f248a 100644 --- a/app/Http/Livewire/Adjustment/ProductTable.php +++ b/app/Http/Livewire/Adjustment/ProductTable.php @@ -44,7 +44,7 @@ class ProductTable extends Component } break; default: - return session()->flash('message', 'Something went wrong!');; + return session()->flash('message', 'Something went wrong!'); } array_push($this->products, $product); diff --git a/app/Http/Livewire/Barcode/ProductTable.php b/app/Http/Livewire/Barcode/ProductTable.php index 0500a55d..85b4ea02 100644 --- a/app/Http/Livewire/Barcode/ProductTable.php +++ b/app/Http/Livewire/Barcode/ProductTable.php @@ -31,6 +31,10 @@ class ProductTable extends Component } public function generateBarcodes(Product $product, $quantity) { + if ($quantity > 100) { + return session()->flash('message', 'Max quantity is 100 per barcode generation!'); + } + $this->barcodes = []; for ($i = 1; $i <= $quantity; $i++) { diff --git a/composer.json b/composer.json index 25fb1987..752f7238 100644 --- a/composer.json +++ b/composer.json @@ -24,6 +24,7 @@ "yajra/laravel-datatables": "^1.5" }, "require-dev": { + "barryvdh/laravel-debugbar": "^3.6", "facade/ignition": "^2.5", "fakerphp/faker": "^1.9.1", "laravel/sail": "^1.0.1", @@ -69,7 +70,7 @@ "preferred-install": "dist", "sort-packages": true, "platform": { - "php": "8.0" + "php": "7.4" } }, "minimum-stability": "dev", diff --git a/composer.lock b/composer.lock index e4d06dca..3de9cef9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "81cce1ef2defb71f07fb109dda14cc2c", + "content-hash": "646db8c9d7e97562bbf1eacf7731e23e", "packages": [ { "name": "asm89/stack-cors", @@ -7222,6 +7222,91 @@ } ], "packages-dev": [ + { + "name": "barryvdh/laravel-debugbar", + "version": "v3.6.2", + "source": { + "type": "git", + "url": "https://github.com/barryvdh/laravel-debugbar.git", + "reference": "70b89754913fd89fef16d0170a91dbc2a5cd633a" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/barryvdh/laravel-debugbar/zipball/70b89754913fd89fef16d0170a91dbc2a5cd633a", + "reference": "70b89754913fd89fef16d0170a91dbc2a5cd633a", + "shasum": "" + }, + "require": { + "illuminate/routing": "^6|^7|^8", + "illuminate/session": "^6|^7|^8", + "illuminate/support": "^6|^7|^8", + "maximebf/debugbar": "^1.16.3", + "php": ">=7.2", + "symfony/debug": "^4.3|^5", + "symfony/finder": "^4.3|^5" + }, + "require-dev": { + "mockery/mockery": "^1.3.3", + "orchestra/testbench-dusk": "^4|^5|^6", + "phpunit/phpunit": "^8.5|^9.0", + "squizlabs/php_codesniffer": "^3.5" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.5-dev" + }, + "laravel": { + "providers": [ + "Barryvdh\\Debugbar\\ServiceProvider" + ], + "aliases": { + "Debugbar": "Barryvdh\\Debugbar\\Facade" + } + } + }, + "autoload": { + "psr-4": { + "Barryvdh\\Debugbar\\": "src/" + }, + "files": [ + "src/helpers.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "PHP Debugbar integration for Laravel", + "keywords": [ + "debug", + "debugbar", + "laravel", + "profiler", + "webprofiler" + ], + "support": { + "issues": "https://github.com/barryvdh/laravel-debugbar/issues", + "source": "https://github.com/barryvdh/laravel-debugbar/tree/v3.6.2" + }, + "funding": [ + { + "url": "https://fruitcake.nl", + "type": "custom" + }, + { + "url": "https://github.com/barryvdh", + "type": "github" + } + ], + "time": "2021-06-14T14:29:26+00:00" + }, { "name": "doctrine/instantiator", "version": "1.4.0", @@ -7732,6 +7817,71 @@ }, "time": "2021-07-13T14:20:58+00:00" }, + { + "name": "maximebf/debugbar", + "version": "v1.17.1", + "source": { + "type": "git", + "url": "https://github.com/maximebf/php-debugbar.git", + "reference": "0a3532556be0145603f8a9de23e76dc28eed7054" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/maximebf/php-debugbar/zipball/0a3532556be0145603f8a9de23e76dc28eed7054", + "reference": "0a3532556be0145603f8a9de23e76dc28eed7054", + "shasum": "" + }, + "require": { + "php": "^7.1|^8", + "psr/log": "^1.0", + "symfony/var-dumper": "^2.6|^3|^4|^5" + }, + "require-dev": { + "phpunit/phpunit": "^7.5.20 || ^9.4.2" + }, + "suggest": { + "kriswallsmith/assetic": "The best way to manage assets", + "monolog/monolog": "Log using Monolog", + "predis/predis": "Redis storage" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.17-dev" + } + }, + "autoload": { + "psr-4": { + "DebugBar\\": "src/DebugBar/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Maxime Bouroumeau-Fuseau", + "email": "maxime.bouroumeau@gmail.com", + "homepage": "http://maximebf.com" + }, + { + "name": "Barry vd. Heuvel", + "email": "barryvdh@gmail.com" + } + ], + "description": "Debug bar in the browser for php application", + "homepage": "https://github.com/maximebf/php-debugbar", + "keywords": [ + "debug", + "debugbar" + ], + "support": { + "issues": "https://github.com/maximebf/php-debugbar/issues", + "source": "https://github.com/maximebf/php-debugbar/tree/v1.17.1" + }, + "time": "2021-08-01T09:19:02+00:00" + }, { "name": "mockery/mockery", "version": "1.4.3", @@ -9671,6 +9821,74 @@ ], "time": "2020-09-28T06:39:44+00:00" }, + { + "name": "symfony/debug", + "version": "v4.4.27", + "source": { + "type": "git", + "url": "https://github.com/symfony/debug.git", + "reference": "2f9160e92eb64c95da7368c867b663a8e34e980c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/debug/zipball/2f9160e92eb64c95da7368c867b663a8e34e980c", + "reference": "2f9160e92eb64c95da7368c867b663a8e34e980c", + "shasum": "" + }, + "require": { + "php": ">=7.1.3", + "psr/log": "^1|^2|^3" + }, + "conflict": { + "symfony/http-kernel": "<3.4" + }, + "require-dev": { + "symfony/http-kernel": "^3.4|^4.0|^5.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "Symfony\\Component\\Debug\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Provides tools to ease debugging PHP code", + "homepage": "https://symfony.com", + "support": { + "source": "https://github.com/symfony/debug/tree/v4.4.27" + }, + "funding": [ + { + "url": "https://symfony.com/sponsor", + "type": "custom" + }, + { + "url": "https://github.com/fabpot", + "type": "github" + }, + { + "url": "https://tidelift.com/funding/github/packagist/symfony/symfony", + "type": "tidelift" + } + ], + "time": "2021-07-22T07:21:39+00:00" + }, { "name": "theseer/tokenizer", "version": "1.2.0", diff --git a/resources/views/home.blade.php b/resources/views/home.blade.php index 4393333c..d0727b2c 100644 --- a/resources/views/home.blade.php +++ b/resources/views/home.blade.php @@ -9,5 +9,161 @@ @endsection @section('content') +
+
+
+
+
+
+ +
+
+
{{ format_currency($revenue) }}
+
Revenue
+
+
+
+
+
+
+
+
+ +
+
+
{{ format_currency($sale_returns) }}
+
Sales Return
+
+
+
+
+ +
+
+
+
+ +
+
+
{{ format_currency($purchase_returns) }}
+
Purchases Return
+
+
+
+
+ +
+
+
+
+ +
+
+
{{ format_currency($profit) }}
+
Profit
+
+
+
+
+
+ +
+
+
+
+ Sales & Purchases of Last 7 Days +
+
+ +
+
+
+
+
+
+ {{ now()->format('F, Y') }} +
+
+ +
+
+
+
+
@endsection + +@section('third_party_scripts') + +@endsection + +@push('page_scripts') + +@endpush diff --git a/resources/views/layouts/app.blade.php b/resources/views/layouts/app.blade.php index c05f7463..e509792a 100644 --- a/resources/views/layouts/app.blade.php +++ b/resources/views/layouts/app.blade.php @@ -25,36 +25,42 @@ -@include('layouts.sidebar') + @include('layouts.sidebar') -
-
- @include('layouts.header') -
- @yield('breadcrumb') +
+
+ @include('layouts.header') +
+ @yield('breadcrumb') +
+
+ +
+
+ @yield('content') +
-
-
-
- @yield('content') -
+ @include('layouts.footer')
- @include('layouts.footer') -
+ + + - - - + -@include('sweetalert::alert') + @include('sweetalert::alert') -@yield('third_party_scripts') + @yield('third_party_scripts') -@livewireScripts + @livewireScripts -@stack('page_scripts') + @stack('page_scripts') diff --git a/resources/views/layouts/header.blade.php b/resources/views/layouts/header.blade.php index fa84460c..0a269da7 100644 --- a/resources/views/layouts/header.blade.php +++ b/resources/views/layouts/header.blade.php @@ -10,13 +10,13 @@