Skip to content

Commit

Permalink
Code refactor
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Poyigi <[email protected]>
  • Loading branch information
sampoyigi committed Jun 24, 2024
1 parent 3be0f41 commit b23ddd3
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 2 deletions.
1 change: 1 addition & 0 deletions resources/lang/en/default.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
'text_tab_title_order' => 'Order',
'text_menu_price_include_tax' => 'Menu prices already include tax',
'text_apply_tax_on_menu_price' => 'Apply tax on top of my menu price',
'text_charts_orders' => 'Orders',

'text_basket' => 'Basket',
'text_update_heading' => 'Updating menu choices',
Expand Down
2 changes: 1 addition & 1 deletion src/Cart.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use Igniter\Cart\Contracts\Buyable;
use Igniter\Cart\Exceptions\InvalidRowIDException;
use Igniter\Cart\Exceptions\UnknownModelException;
use Illuminate\Events\Dispatcher;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Session\SessionManager;

class Cart
Expand Down
2 changes: 1 addition & 1 deletion src/Database/Factories/MenuFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function definition(): array
{
return [
'menu_name' => $this->faker->sentence(2),
'menu_price' => $this->faker->randomFloat(),
'menu_price' => $this->faker->randomFloat(null, 0, 100),
'minimum_qty' => 1,
'menu_status' => 1,
];
Expand Down
27 changes: 27 additions & 0 deletions src/Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

namespace Igniter\Cart;

use Igniter\Admin\DashboardWidgets\Charts;
use Igniter\Admin\DashboardWidgets\Statistics;
use Igniter\Admin\Widgets\Form;
use Igniter\Cart\Listeners\RegistersDashboardCards;
use Igniter\Cart\Models\Category;
use Igniter\Cart\Models\Concerns\LocationAction;
use Igniter\Cart\Models\Menu;
Expand Down Expand Up @@ -83,6 +86,7 @@ public function boot()
$this->bindCartEvents();
$this->bindCheckoutEvents();
$this->bindOrderStatusEvent();
$this->extendDashboardChartsDatasets();

LocationModel::implement(LocationAction::class);

Expand Down Expand Up @@ -124,6 +128,10 @@ public function boot()
],
], 'primary');
});

Statistics::registerCards(function() {
return (new RegistersDashboardCards())();
});
}

public function registerCartConditions()
Expand Down Expand Up @@ -442,4 +450,23 @@ protected function registerSystemSettings()
]);
});
}

protected function extendDashboardChartsDatasets()
{
Charts::extend(function($charts) {
$charts->bindEvent('charts.extendDatasets', function() use ($charts) {
$charts->addDataset('reports', [
'sets' => [
'orders' => [
'label' => 'lang:igniter.cart::default.text_charts_orders',
'color' => '#64B5F6',
'model' => Order::class,
'column' => 'order_date',
'priority' => 20,
],
],
]);
});
});
}
}
167 changes: 167 additions & 0 deletions src/Listeners/RegistersDashboardCards.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php

namespace Igniter\Cart\Listeners;

use Igniter\Cart\Models\Order;
use Igniter\System\Models\Settings;

class RegistersDashboardCards
{
public function __invoke()
{
return [
'sale' => [
'label' => 'lang:igniter::admin.dashboard.text_total_sale',
'icon' => ' text-success fa fa-4x fa-line-chart',
'valueFrom' => [$this, 'getValue']
],
'lost_sale' => [
'label' => 'lang:igniter::admin.dashboard.text_total_lost_sale',
'icon' => ' text-danger fa fa-4x fa-line-chart',
'valueFrom' => [$this, 'getValue']
],
'cash_payment' => [
'label' => 'lang:igniter::admin.dashboard.text_total_cash_payment',
'icon' => ' text-warning fa fa-4x fa-money-bill',
'valueFrom' => [$this, 'getValue']
],
'order' => [
'label' => 'lang:igniter::admin.dashboard.text_total_order',
'icon' => ' text-success fa fa-4x fa-shopping-cart',
'valueFrom' => [$this, 'getValue']
],
'delivery_order' => [
'label' => 'lang:igniter::admin.dashboard.text_total_delivery_order',
'icon' => ' text-primary fa fa-4x fa-truck',
'valueFrom' => [$this, 'getValue']
],
'collection_order' => [
'label' => 'lang:igniter::admin.dashboard.text_total_collection_order',
'icon' => ' text-info fa fa-4x fa-shopping-bag',
'valueFrom' => [$this, 'getValue']
],
'completed_order' => [
'label' => 'lang:igniter::admin.dashboard.text_total_completed_order',
'icon' => ' text-success fa fa-4x fa-receipt',
'valueFrom' => [$this, 'getValue']
],
];
}

public function getValue($code, $start, $end, callable $callback)
{
return match ($code) {
'sale' => $this->getTotalSaleSum($callback),
'lost_sale' => $this->getTotalLostSaleSum($callback),
'cash_payment' => $this->getTotalCashPaymentSum($callback),
'order' => $this->getTotalOrderSum($callback),
'delivery_order' => $this->getTotalDeliveryOrderSum($callback),
'collection_order' => $this->getTotalCollectionOrderSum($callback),
'completed_order' => $this->getTotalCompletedOrderSum($callback),
default => 0,
};
}

/**
* Return the total amount of order sales
*/
protected function getTotalSaleSum(callable $callback): string
{
$query = Order::query();
$query->where('status_id', '>', '0')
->where('status_id', '!=', Settings::get('canceled_order_status'));

$callback($query);

return currency_format($query->sum('order_total') ?? 0);
}

/**
* Return the total amount of lost order sales
*/
protected function getTotalLostSaleSum(callable $callback): string
{
$query = Order::query();
$query->where(function($query) {
$query->where('status_id', '<=', '0');
$query->orWhere('status_id', Settings::get('canceled_order_status'));
});

$callback($query);

return currency_format($query->sum('order_total') ?? 0);
}

/**
* Return the total amount of cash payment order sales
*/
protected function getTotalCashPaymentSum(callable $callback): string
{
$query = Order::query();
$query->where(function($query) {
$query->where('status_id', '>', '0');
$query->where('status_id', '!=', Settings::get('canceled_order_status'));
})->where('payment', 'cod');

$callback($query);

return currency_format($query->sum('order_total') ?? 0);
}

/**
* Return the total number of orders placed
*/
protected function getTotalOrderSum(callable $callback): int
{
$query = Order::query();

$callback($query);

return $query->count();
}

/**
* Return the total number of completed orders
*/
protected function getTotalCompletedOrderSum(callable $callback): int
{
$query = Order::query();
$query->whereIn('status_id', Settings::get('completed_order_status') ?? []);

$callback($query);

return $query->count();
}

/**
* Return the total number of delivery orders
*/
protected function getTotalDeliveryOrderSum(callable $callback): string
{
$query = Order::query();
$query->where(function($query) {
$query->where('order_type', '1');
$query->orWhere('order_type', 'delivery');
});

$callback($query);

return currency_format($query->sum('order_total') ?? 0);
}

/**
* Return the total number of collection orders
*/
protected function getTotalCollectionOrderSum(callable $callback): string
{
$query = Order::query();
$query->where(function($query) {
$query->where('order_type', '2');
$query->orWhere('order_type', 'collection');
});

$callback($query);

return currency_format($query->sum('order_total') ?? 0);
}
}

0 comments on commit b23ddd3

Please sign in to comment.