Skip to content

Commit

Permalink
chore: code refactor and optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
kamgrzeski committed Nov 26, 2024
1 parent 2cc6c19 commit b7a002b
Show file tree
Hide file tree
Showing 9 changed files with 143 additions and 176 deletions.
4 changes: 2 additions & 2 deletions app/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public function index(): \Illuminate\View\View
[
'tasksGraphData' => $graph->taskGraphData(),
'itemsCountGraphData' => $graph->itemsCountGraphData(),
'tasks' => $this->tasksService->formatTasks(),
'companies' => $this->companiesService->loadCompaniesByCreatedAt()->take(10),
'tasks' => TaskQueries::formatTasks(),
'companies' => CompanyQueries::getCompaniesSortedByCreatedAt()->take(10),
'products' => ProductQueries::getProductsByCreatedAt()->take(10),
'currency' => SettingQueries::getSettingValue(Settings::CURRENCY->value)
]
Expand Down
19 changes: 19 additions & 0 deletions app/Queries/TaskQueries.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Models\Setting;
use App\Models\Task;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
* Class TasksQueries
Expand Down Expand Up @@ -77,4 +78,22 @@ public static function getAllForFormat(): \Illuminate\Support\Collection
{
return Task::all()->sortBy('created_at', 0, true)->slice(0, 5);
}

/**
* Format tasks for display.
*
* @return array
*/
public static function formatTasks(): array
{
return Task::get()->map(function ($task) {
$nameTask = Str::limit($task->name, 70);
return [
'id' => $task->id,
'name' => $nameTask,
'duration' => $task->duration,
'created_at' => $task->created_at,
];
})->toArray();
}
}
125 changes: 49 additions & 76 deletions app/Services/CalculateCashService.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,15 @@ class CalculateCashService
*/
public function loadCountCashTurnover(): mixed
{
$products = Product::all();
$sales = Sale::all();
$finances = Finance::all();
$productSum = Product::sum(function ($product) {
return $product->price * $product->count;
});

$productSum = 0;
$salesSum = 0;
$financesSum = 0;
$salesSum = Sale::sum(function ($sale) {
return $sale->price * $sale->quantity;
});

foreach($products as $product) {
$productSum += $product->price * $product->count;
}

foreach($finances as $finance) {
$financesSum += $finance->net;
}

foreach($sales as $sale) {
$salesSum += $sale->price * $sale->quantity;
}
$financesSum = Finance::sum('net');

$officialSum = $productSum + $salesSum + $financesSum;

Expand All @@ -48,23 +38,17 @@ public function loadCountCashTurnover(): mixed
*/
public function loadCountTodayIncome(): mixed
{
$products = Product::whereDate('created_at', Carbon::today())->get();
$sales = Sale::whereDate('created_at', Carbon::today())->get();
$finances = Finance::whereDate('created_at', Carbon::today())->get();
$productSum = 0;
$salesSum = 0;
$financesSum = 0;

foreach($products as $product) {
$productSum += $product->price * $product->count;
}
$productSum = Product::whereDate('created_at', Carbon::today())
->sum(function ($product) {
return $product->price * $product->count;
});

foreach($sales as $sale) {
$salesSum += $sale->price * $sale->quantity;
}
foreach($finances as $finance) {
$financesSum += $finance->net;
}
$salesSum = Sale::whereDate('created_at', Carbon::today())
->sum(function ($sale) {
return $sale->price * $sale->quantity;
});

$financesSum = Finance::whereDate('created_at', Carbon::today())->sum('net');

$todayIncome = $productSum + $salesSum + $financesSum;

Expand All @@ -76,22 +60,17 @@ public function loadCountTodayIncome(): mixed
*/
public function loadCountYesterdayIncome(): mixed
{
$products = Product::whereDate('created_at', Carbon::yesterday())->get();
$sales = Sale::whereDate('created_at', Carbon::yesterday())->get();
$finances = Finance::whereDate('created_at', Carbon::yesterday())->get();
$salesSum = 0;
$productSum = 0;
$financesSum = 0;

foreach($products as $product) {
$productSum += $product->price * $product->count;
foreach($sales as $sale) {
$salesSum += $product->price * $sale->quantity;
}
foreach($finances as $finance) {
$financesSum += $finance->net;
}
}
$productSum = Product::whereDate('created_at', Carbon::yesterday())
->sum(function ($product) {
return $product->price * $product->count;
});

$salesSum = Sale::whereDate('created_at', Carbon::yesterday())
->sum(function ($sale) {
return $sale->price * $sale->quantity;
});

$financesSum = Finance::whereDate('created_at', Carbon::yesterday())->sum('net');

$yesterdayIncome = $productSum + $salesSum + $financesSum;

Expand All @@ -105,46 +84,40 @@ public function loadCountAllRowsInDb(): int
{
$counter = 0;
$tables = DB::select('SHOW TABLES');

$databaseName = DB::connection()->getDatabaseName();

foreach ($tables as $table) {
$counter += DB::table($table->{'Tables_in_' . $databaseName})->count();
$tableName = $table->{'Tables_in_' . $databaseName};
$counter += DB::table($tableName)->count();
}

return $counter;
}

public function loadTaskEveryMonth($isCompleted) {

public function loadTaskEveryMonth(bool $isCompleted): array
{
$dates = collect();
foreach( range( -6, 0 ) AS $i ) {
$date = Carbon::now()->addDays( $i )->format( 'Y-m-d' );
$dates->put( $date, 0);
foreach (range(-6, 0) as $i) {
$date = Carbon::now()->addDays($i)->format('Y-m-d');
$dates->put($date, 0);
}

if($isCompleted) {
$posts = Task::where( 'created_at', '>=', $dates->keys()->first() )->where('completed', '=', 1)
->groupBy( 'date' )
->orderBy( 'date' )
->get( [
DB::raw( 'DATE( created_at ) as date' ),
DB::raw( 'COUNT( * ) as "count"' )
] )
->pluck( 'count', 'date' );
} else {
$posts = Task::where( 'created_at', '>=', $dates->keys()->first() )
->groupBy( 'date' )
->orderBy( 'date' )
->get( [
DB::raw( 'DATE( created_at ) as date' ),
DB::raw( 'COUNT( * ) as "count"' )
] )
->pluck( 'count', 'date' );
$query = Task::where('created_at', '>=', $dates->keys()->first())
->groupBy(DB::raw('DATE(created_at)'))
->orderBy('created_at');

if ($isCompleted) {
$query->where('completed', 1);
}

$dates = $dates->merge($posts)->toArray();
$posts = $query->get([
DB::raw('DATE(created_at) as date'),
DB::raw('COUNT(*) as "count"')
])->pluck('count', 'date');

// Merge posts data with the default zeroed dates
$dates = $dates->merge($posts);

return array_values($dates);
return $dates->values()->toArray();
}
}
3 changes: 1 addition & 2 deletions app/Services/ClientService.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public function loadClientDetails(Client $client): Client
public function loadClientsInLatestMonth(): float
{
$clientsCountInLatestMonth = ClientQueries::getCountAllInLatestMonth();

$allClientCount = ClientQueries::getCountAll();

return ($allClientCount / 100) * $clientsCountInLatestMonth;
return ($clientsCountInLatestMonth / $allClientCount) * 100;
}
}
12 changes: 1 addition & 11 deletions app/Services/CompaniesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@
*/
class CompaniesService
{
/**
* Load companies sorted by creation date.
*
* @return \Illuminate\Support\Collection
*/
public function loadCompaniesByCreatedAt(): \Illuminate\Support\Collection
{
return CompanyQueries::getCompaniesSortedByCreatedAt();
}

/**
* Load the list of companies added in the latest month.
*
Expand All @@ -31,6 +21,6 @@ public function loadCompaniesInLatestMonth(): float
$companiesCount = CompanyQueries::getCompaniesInLatestMonth();
$allCompanies = CompanyQueries::countAll();

return ($allCompanies / 100) * $companiesCount;
return ($companiesCount / $allCompanies) * 100;
}
}
11 changes: 4 additions & 7 deletions app/Services/DealsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function loadDealsInLatestMonth(): int
$dealsInLatestMonth = DealQueries::getDealsInLatestMonth();
$allDeals = DealQueries::countAll();

return ($allDeals / 100) * count($dealsInLatestMonth);
return (int) (($dealsInLatestMonth->count() / $allDeals) * 100);
}

/**
Expand All @@ -36,12 +36,9 @@ public function loadDealsInLatestMonth(): int
*/
public function loadGenerateDealTermsInPDF(DealTerm $dealTerm, Deal $deal): \Illuminate\Http\Response
{
$data = [
'body' => $dealTerm->body
];
$data = ['body' => $dealTerm->body];

$pdf = PDF::loadView('crm.deals.terms.terms_pdf', $data);

return $pdf->download($deal->name . '.pdf');
return PDF::loadView('crm.deals.terms.terms_pdf', $data)
->download($deal->name . '.pdf');
}
}
6 changes: 3 additions & 3 deletions app/Services/EmployeesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public function loadEmployees(): \Illuminate\Support\Collection
{
$employees = Employee::orderBy('created_at')->get();

foreach($employees as $key => $employee) {
Arr::add($employees[$key], 'taskCount', TaskQueries::getEmployeesTaskCount($employee->id));
foreach ($employees as $employee) {
Arr::add($employee, 'taskCount', TaskQueries::getEmployeesTaskCount($employee->id));
}

return $employees;
Expand All @@ -39,6 +39,6 @@ public function loadEmployeesInLatestMonth(): int
$employeesCountInLatestMonth = EmployeeQueries::getEmployeesInLatestMonth();
$allEmployees = EmployeeQueries::countAll();

return ($allEmployees / 100) * $employeesCountInLatestMonth;
return (int) (($employeesCountInLatestMonth / $allEmployees) * 100);
}
}
Loading

0 comments on commit b7a002b

Please sign in to comment.