Make dashboard configurable.
This commit is contained in:
27
app/Http/Controllers/Api/DashboardController.php
Normal file
27
app/Http/Controllers/Api/DashboardController.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Dashboard;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DashboardController extends Controller
|
||||
{
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json(Dashboard::all());
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Dashboard $dashboard): JsonResponse
|
||||
{
|
||||
return response()->json($dashboard->update($request->all()));
|
||||
}
|
||||
}
|
||||
54
app/Models/Dashboard.php
Normal file
54
app/Models/Dashboard.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Dashboard extends Model
|
||||
{
|
||||
public $timestamps = false;
|
||||
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*
|
||||
* @var array<int, string>
|
||||
*/
|
||||
protected $fillable = [
|
||||
'width',
|
||||
'height',
|
||||
'sort',
|
||||
'active',
|
||||
'settings'
|
||||
];
|
||||
|
||||
protected $appends = [
|
||||
'title',
|
||||
];
|
||||
|
||||
public function getTitleAttribute()
|
||||
{
|
||||
return __('dashboard.title_' . $this->name);
|
||||
}
|
||||
|
||||
public static function toObject()
|
||||
{
|
||||
$all = self::orderBy('sort')->get();
|
||||
$tiles = new \stdClass();
|
||||
foreach ($all as $tile) {
|
||||
$key = $tile->name;
|
||||
$tiles->$key = $tile;
|
||||
}
|
||||
return $tiles;
|
||||
}
|
||||
|
||||
public static function activeToObject()
|
||||
{
|
||||
$all = self::where('active', true)->orderBy('sort')->get();
|
||||
$tiles = new \stdClass();
|
||||
foreach ($all as $tile) {
|
||||
$key = $tile->name;
|
||||
$tiles->$key = $tile;
|
||||
}
|
||||
return collect($tiles);
|
||||
}
|
||||
}
|
||||
@@ -38,8 +38,8 @@ class AppServiceProvider extends ServiceProvider
|
||||
{
|
||||
View::composer('components.tax-dropdown', TaxDropdown::class);
|
||||
View::composer('components.company-logo', OptionLogo::class);
|
||||
View::composer('components.graph-year', YearGraph::class);
|
||||
View::composer('components.graph-month', MonthGraph::class);
|
||||
View::composer('components.dashboard_graph_year', YearGraph::class);
|
||||
View::composer('components.dashboard_graph_month', MonthGraph::class);
|
||||
Number::useLocale(config('app.locale'));
|
||||
Number::useCurrency(config('app.currency'));
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\View\Composers;
|
||||
|
||||
use App\Models\Dashboard;
|
||||
use App\Models\Incoming;
|
||||
use App\Models\Invoice;
|
||||
use Illuminate\Support\Carbon;
|
||||
@@ -11,7 +12,10 @@ class MonthGraph
|
||||
{
|
||||
public function compose(View $view): void
|
||||
{
|
||||
$monthly_invoices = Invoice::whereYear('created_at', '=', Carbon::now()->year)->get()
|
||||
$config = Dashboard::where('id', '=', 6)->first();
|
||||
$config_year = (is_null($config->settings)) ? Carbon::now()->year : intval($config->settings);
|
||||
|
||||
$monthly_invoices = Invoice::whereYear('created_at', '=', $config_year)->get()
|
||||
->groupBy(function ($invoice) {
|
||||
return $invoice->created_at->format('n');
|
||||
})
|
||||
@@ -19,7 +23,7 @@ class MonthGraph
|
||||
return $month->sum('sum');
|
||||
});
|
||||
|
||||
$monthly_incoming = Incoming::whereYear('issue_date', '=', Carbon::now()->year)->get()
|
||||
$monthly_incoming = Incoming::whereYear('issue_date', '=', $config_year)->get()
|
||||
->groupBy(function ($incoming) {
|
||||
return Carbon::parse($incoming->issue_date)->format('n');
|
||||
})
|
||||
@@ -36,6 +40,6 @@ class MonthGraph
|
||||
$monthly_invoices[$year] = 0;
|
||||
}
|
||||
|
||||
$view->with(['monthly_invoices' => $monthly_invoices, 'monthly_incoming' => $monthly_incoming]);
|
||||
$view->with(['monthly_invoices' => $monthly_invoices, 'monthly_incoming' => $monthly_incoming, 'year' => $config_year]);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user