Spamworldpro Mini Shell
Spamworldpro


Server : Apache
System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64
User : corals ( 1002)
PHP Version : 7.4.33
Disable Function : exec,passthru,shell_exec,system
Directory :  /home/corals/ts.corals.io/corals-api/Corals/modules/Timesheet/Classes/Dashboard/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /home/corals/ts.corals.io/corals-api/Corals/modules/Timesheet/Classes/Dashboard/Accounting.php
<?php

namespace Corals\Modules\Timesheet\Classes\Dashboard;


use Corals\Modules\Timesheet\Models\Client;
use Corals\Modules\Timesheet\Models\Expense;
use Corals\Modules\Timesheet\Models\Income;
use Corals\Modules\Timesheet\Models\Invoice;
use Corals\Modules\Timesheet\Models\Project;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class Accounting
{
    protected $statuses = [
        'paid' => '0',
        'pending' => '0',
    ];
    protected $result = [];

    protected $from_date;
    protected $to_date;

    public function __construct(Request $request)
    {
        $this->from_date = $request->get('from_date', now()->subMonth()->startOfMonth());
        $this->to_date = $request->get('to_date', now()->subMonth()->endOfMonth());
    }

    public function incomesStatuses()
    {
        $modelClass = Income::class;
        $this->statusesTotal($modelClass, 'incomes', 'success');

        return $this->result['incomes'];
    }

    public function expensesStatuses()
    {
        $modelClass = Expense::class;
        $this->statusesTotal($modelClass, 'expenses', 'danger');

        return $this->result['expenses'];
    }

    public function expensesVSincomesStatuses()
    {
        $this->statusesTotal(Income::class, 'incomes', 'success');
        $this->statusesTotal(Expense::class, 'expenses', 'danger');

        $expensesAndIncomesData = [];

        foreach ($this->statuses as $status => $total) {
            $incomesTotal = $this->result['incomes']['data'][$status];
            $expensesTotal = $this->result['expenses']['data'][$status];

            $expensesAndIncomesData[$status] = round(($incomesTotal - $expensesTotal), 2);
        }

        return [
            'label' => strtoupper('total profit'),
            'level' => 'primary',
            'icon' => ['fa', 'coins'],
            'data' => $expensesAndIncomesData
        ];
    }

    public function incomesCategory()
    {
        $modelClass = Income::class;
        return $this->categoryTotal($modelClass, 'incomes');
    }

    public function expensesCategory()
    {
        $modelClass = Expense::class;
        return $this->categoryTotal($modelClass, 'expenses',);
    }

    public function incomesLatest()
    {
        $modelClass = Income::class;
        return $this->getLatest($modelClass, 'income');
    }

    public function expensesLatest()
    {
        $modelClass = Expense::class;
        return $this->getLatest($modelClass, 'expense');
    }

    protected function statusesTotal($modelClass, $type, $level)
    {
        $model = (new $modelClass);

        $dateColumn = strtolower(class_basename($modelClass)) . '_date';

        $data = $model->query()
            ->whereBetween($dateColumn,[$this->from_date,$this->to_date])
            ->select("status", DB::raw('round(SUM(amount),2) as `total`'))
            ->groupby('status')
            ->pluck('total', 'status')->toArray();

        $statusesTotal = array_merge($this->statuses, $data);

        if ($type == 'incomes') {
            $invoiceStatusesTotal = array_merge($this->statuses, $this->getInvoiceStatusesTotal());

            foreach ($this->statuses as $status => $total) {
                $statusesTotal[$status] = round(($statusesTotal[$status] + $invoiceStatusesTotal[$status]), 2);
            }
        }

        $this->result[$type] = [
            'label' => strtoupper('total ' . $type),
            'level' => $level,
            'icon' => ['far', 'money-bill-alt'],
            'data' => $statusesTotal
        ];
    }

    protected function getInvoiceStatusesTotal()
    {
        return Invoice::query()
            ->whereBetween('invoice_date',[$this->from_date,$this->to_date])
            ->select("status", DB::raw('round(SUM(total),2) as `total`'))
            ->groupby('status')
            ->pluck('total', 'status')->toArray();
    }

    protected function categoryTotal($modelClass, $type)
    {
        $model = (new $modelClass);
        $targetTable = $model->getTable();

        $dateColumn = strtolower(class_basename($modelClass)) . '_date';

        $data = $model
            ->whereBetween($dateColumn,[$this->from_date,$this->to_date])
            ->join('utility_categories', 'utility_categories.id', $targetTable . '.category_id')
            ->select(
                DB::raw('round(SUM(amount),2) as `total`'),
                "utility_categories.name as category"
            )->groupby('category')
            ->get()->toArray();

        if ($type == 'incomes') {
            $invoicesTotal = optional(Invoice::query()
                    ->whereBetween('invoice_date',[$this->from_date,$this->to_date])
                    ->select(DB::raw('round(SUM(total),2) as `total`'))
                    ->first())->total ?? 0;

            $data [] = ['category' => 'Invoices', 'total' => $invoicesTotal];
        }

        $chartData = [];
        if ($data) {
            $chartData['labels'] = array_column($data, 'category');
            $chartData['datasets'] = [[
                'backgroundColor' => ["rgb(255, 205, 86)", "rgb(54, 162, 235)", "rgb(255, 99, 132)", "#5e4548", "#28a745",
                    '#8dd3c7', '#1f78b4', '#ffffb3', '#b2df8a', '#bebada', '#33a02c', '#fb9a99', '#80b1d3', '#e31a1c',
                    '#fdb462', '#fdbf6f', '#b3de69', '#ff7f00', '#fccde5', '#cab2d6', '#d9d9d9', '#6a3d9a', '#bc80bd',
                    '#ffff99', '#ccebc5', '#b15928', '#ffed6f'],
                'data' => array_column($data, 'total')
            ]];
        }
        return [
            'chartData' => $chartData,
            'label' => Str::title($type) . ' Category',
        ];
    }

    protected function getLatest($modelClass, $type)
    {
        $model = (new $modelClass);
        $targetTable = $model->getTable();
        $tableData = [];

        $date = $targetTable . '.' . $type . '_date';
        if ($type == 'income') {
            $date = $date . ' as date';
        }
        $dateColumn = strtolower(class_basename($modelClass)) . '_date';

        $result = $model
            ->whereBetween($dateColumn,[$this->from_date,$this->to_date])
            ->join('utility_categories', 'utility_categories.id', '=', $targetTable . '.category_id')
            ->with(['client', 'project'])
            ->select($date,
                "utility_categories.name as category",
                $targetTable . '.amount',
                $targetTable . '.status',
                $targetTable . '.notes',
                'client_id',
                'project_id');


        if ($type == 'income') {
            $invoice = Invoice::query()
                ->whereBetween('invoice_date',[$this->from_date,$this->to_date])
                ->select('timesheet_invoices.invoice_date as date',
                    DB::raw("'Invoices' as category"),
                    'timesheet_invoices.total as amount',
                    'timesheet_invoices.status',
                    'timesheet_invoices.notes',
                    'timesheet_invoices.client_id',
                    'timesheet_invoices.project_id'
                );

            $result = $result
                ->union($invoice)
                ->latest('date');
        } else {
            $result = $result->latest($targetTable . '.' . $type . '_date');
        }

        $result->take(5)
            ->get()
            ->each(function ($data) use (&$tableData, $type) {
                $dataArr = $data->toArray();
                $dataArr['client'] = '-';
                $dataArr['project'] = '-';

                if ($dataArr['client_id']) {
                    $client = Client::query()->where('id', $dataArr['client_id'])->first();
                    $dataArr['client'] = $client ? '<a href="' . '/clients/' . $client->id . '">'
                        . $client->name . '</a>' : '';
                }

                if ($dataArr['project_id']) {
                    $project = Project::query()->where('id', $dataArr['project_id'])->first();
                    $dataArr['project'] = $project ? '<a href="' . '/clients/' . $project->client_id . '/projects/' . $project->id . '">'
                        . $project->name . '</a>' : '';
                }

                $dataArr['status'] = formatStatusAsLabels($dataArr['status']);

                $tableData[] = Arr::except($dataArr, [
                    'project_id',
                    'client_id'
                ]);
            });

        return [
            'label' => ucwords('latest ' . $type . 's'),
            'tableData' => $tableData,
        ];
    }
}

Spamworldpro Mini