Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
4.1 kB
2
Indexable
Never
<?php

namespace App\Ui\Table;

use App\Models\Order;
use App\Models\OrderStatus;
use App\Models\SalesTarget;
use App\Support\Actions\Create;
use App\Support\Actions\Delete;
use App\Support\Actions\Link;
use App\Support\Form\Field;
use App\Support\Table\ActionCell;
use App\Support\Table\Column;
use App\Table\Table;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Builder;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\QueryBuilder;

class SalesTargetTable extends Table
{
    public $model = SalesTarget::class;
    

    public function headerAction(): array
    {
        return [
            Create::make('Assign Target')
                ->route('operations.sales-target.create')
                ->ifUserCan('create', SalesTarget::class)
        ];
    }

    public function columns()
    {
        return [
            Column::make('Sales Executive', 'user'),
            Column::make('Month', 'month'),
            Column::make('Target', 'target'),
            Column::make('Achieved Target', 'achieved_target'),
            Column::make('Assigned By', 'assigned_by'),
            Column::make('Assigned At', 'assigned_at'),
            Column::make('Actions')->hasAction(),
        ];
    }

    public function transform(SalesTarget $salesTarget)
    {
        $targetDate = Carbon::parse($salesTarget->target_month);

        $achievedTarget = Order::query()
            ->whereHas('currentStatus', function (Builder $query) {
                $query->where('status', OrderStatus::DELIVERED);
            })
            ->whereHas('customer', function (Builder $query) use ($salesTarget) {
                $query->where('sales_person_id', $salesTarget->user_id);
            })
            // Using the target month and year instead of defaulting to current month
            ->whereYear('delivery_date', $targetDate->year)
            ->whereMonth('delivery_date', $targetDate->month)
            ->sum('total_cost');


        return [
            'user' => $salesTarget->user->name,
            'month' => Carbon::parse($salesTarget->target_month)->format('M Y'),
            'target' => displayCurrency($salesTarget->target),
            'achieved_target' => displayCurrency($achievedTarget),
            'assigned_by' => $salesTarget->createdBy->name,
            'assigned_at' => getReadableDateTime($salesTarget->created_at),
            'actions' => ActionCell::make([
                Link::make('Edit')
                    ->route('operations.sales-target.edit', $salesTarget)
                    ->ifUserCan('update', $salesTarget)
                    ->onlyModalData(),

                Delete::make('Delete')
                    ->typeText()
                    ->ifUserCan('delete', $salesTarget)
                    ->route('operations.sales-target.destroy', $salesTarget),
            ])
        ];
    }

    public function query(QueryBuilder $builder): QueryBuilder
    {
        $startOfMonth =
            Carbon
            ::now()->startOfMonth()->format(
                'Y-m-d'
            );
        $endOfMonth =
            Carbon
            ::now()->endOfMonth()->format(
                'Y-m-d'
            );
        // Add eager loading for relationships
        $builder->with(
                'user',
                'createdBy'
            );
        // Filter records where target_month is within the current month
        if (!(request()->hasAny(['filter.month']))){
        $builder->whereBetween(
                'target_month',
                [$startOfMonth, $endOfMonth]
            );}
        return
            $builder;
    }

    public function filters(): array
    {
        return [
            Field::make('month', 'Month')->type('month'),
        ];
    }

    public function allowedFilters(): array
    {
        return [
            AllowedFilter::callback('month', function ($query, $value, $property) {
                $query->whereDate('target_month', Carbon::parse($value));
            })
        ];
    }
}
Leave a Comment