14-04-2026

This commit is contained in:
Kevin Adametz 2026-04-14 18:07:45 +02:00
parent f58c709945
commit 0f82fea88a
72 changed files with 7414 additions and 148 deletions

View file

@ -2,15 +2,13 @@
namespace App\Http\Controllers;
use Auth;
use Request;
use App\Models\UserInvoice;
use App\Models\UserCredit;
use App\Services\HTMLHelper;
use App\Exports\UserTeamExport;
use App\Http\Controllers\Controller;
use App\Models\UserCredit;
use App\Models\UserInvoice;
use App\Services\HTMLHelper;
use Illuminate\Support\Facades\DB;
use Maatwebsite\Excel\Facades\Excel;
use Request;
class RevenueReportController extends Controller
{
@ -22,42 +20,42 @@ class RevenueReportController extends Controller
public function index()
{
$this->setFilterVars();
$data = [
'filter_months' => HTMLHelper::getTransMonths(),
'filter_years' => HTMLHelper::getYearRange(2022),
'revenue_summary' => $this->getRevenueSummary(),
'credit_summary' => $this->getCreditSummary()
'credit_summary' => $this->getCreditSummary(),
];
return view('admin.revenue.index', $data);
}
public function export()
{
$this->setFilterVars();
$filter_year = session('revenue_filter_year');
// Get data like in the HTML view
$revenue_summary = $this->getRevenueSummary();
$credit_summary = $this->getCreditSummary();
$filename = "umsatz-gutschrift-bericht-{$filter_year}";
$columns = [];
// Umsätze Section Header
$columns[] = ['Typ' => 'UMSÄTZE', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Yearly Revenue Summary
if(isset($revenue_summary['yearly']) && $revenue_summary['yearly']->count() > 0) {
foreach($revenue_summary['yearly'] as $item) {
if (isset($revenue_summary['yearly']) && $revenue_summary['yearly']->count() > 0) {
foreach ($revenue_summary['yearly'] as $item) {
$columns[] = [
'Typ' => $item->period_label,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.')
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
} else {
@ -65,22 +63,22 @@ class RevenueReportController extends Controller
'Typ' => "Jahr {$filter_year}",
'Netto' => '0,00',
'Steuer' => '0,00',
'Brutto' => '0,00'
'Brutto' => '0,00',
];
}
// Empty row
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Monthly Revenue Breakdown
$columns[] = ['Typ' => 'MONATLICHE AUFSCHLÜSSELUNG UMSÄTZE', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
if(isset($revenue_summary['monthly']) && $revenue_summary['monthly']->count() > 0) {
foreach($revenue_summary['monthly'] as $item) {
if (isset($revenue_summary['monthly']) && $revenue_summary['monthly']->count() > 0) {
foreach ($revenue_summary['monthly'] as $item) {
$columns[] = [
'Typ' => $item->period_label,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.')
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
} else {
@ -88,25 +86,25 @@ class RevenueReportController extends Controller
'Typ' => 'Keine monatlichen Umsätze gefunden',
'Netto' => '',
'Steuer' => '',
'Brutto' => ''
'Brutto' => '',
];
}
// Two empty rows for separation
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Gutschriften Section Header
$columns[] = ['Typ' => 'GUTSCHRIFTEN', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Yearly Credit Summary
if(isset($credit_summary['yearly']) && $credit_summary['yearly']->count() > 0) {
foreach($credit_summary['yearly'] as $item) {
if (isset($credit_summary['yearly']) && $credit_summary['yearly']->count() > 0) {
foreach ($credit_summary['yearly'] as $item) {
$columns[] = [
'Typ' => $item->period_label,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.')
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
} else {
@ -114,22 +112,22 @@ class RevenueReportController extends Controller
'Typ' => "Jahr {$filter_year}",
'Netto' => '0,00',
'Steuer' => '0,00',
'Brutto' => '0,00'
'Brutto' => '0,00',
];
}
// Empty row
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Monthly Credit Breakdown
$columns[] = ['Typ' => 'MONATLICHE AUFSCHLÜSSELUNG GUTSCHRIFTEN', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
if(isset($credit_summary['monthly']) && $credit_summary['monthly']->count() > 0) {
foreach($credit_summary['monthly'] as $item) {
if (isset($credit_summary['monthly']) && $credit_summary['monthly']->count() > 0) {
foreach ($credit_summary['monthly'] as $item) {
$columns[] = [
'Typ' => $item->period_label,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.')
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
} else {
@ -137,24 +135,106 @@ class RevenueReportController extends Controller
'Typ' => 'Keine monatlichen Gutschriften gefunden',
'Netto' => '',
'Steuer' => '',
'Brutto' => ''
'Brutto' => '',
];
}
// Two empty rows for separation
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Umsätze nach Ländern - Jährlich
$columns[] = ['Typ' => 'UMSÄTZE NACH LÄNDERN', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
if (isset($revenue_summary['country_yearly']) && $revenue_summary['country_yearly']->count() > 0) {
foreach ($revenue_summary['country_yearly'] as $item) {
$columns[] = [
'Typ' => $item->country_name,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
} else {
$columns[] = ['Typ' => 'Keine Umsätze nach Ländern gefunden', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
}
// Empty row
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Umsätze nach Ländern - Monatlich
$columns[] = ['Typ' => 'MONATLICHE AUFSCHLÜSSELUNG UMSÄTZE NACH LÄNDERN', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
if (isset($revenue_summary['country_monthly']) && $revenue_summary['country_monthly']->count() > 0) {
$revenueByMonth = $revenue_summary['country_monthly']->groupBy('month');
foreach ($revenueByMonth as $countries) {
$columns[] = ['Typ' => $countries->first()->month_label, 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
foreach ($countries as $item) {
$columns[] = [
'Typ' => ' '.$item->country_name,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
}
} else {
$columns[] = ['Typ' => 'Keine monatlichen Umsätze nach Ländern gefunden', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
}
// Two empty rows for separation
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Gutschriften nach Ländern - Jährlich
$columns[] = ['Typ' => 'GUTSCHRIFTEN NACH LÄNDERN', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
if (isset($credit_summary['country_yearly']) && $credit_summary['country_yearly']->count() > 0) {
foreach ($credit_summary['country_yearly'] as $item) {
$columns[] = [
'Typ' => $item->country_name,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
} else {
$columns[] = ['Typ' => 'Keine Gutschriften nach Ländern gefunden', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
}
// Empty row
$columns[] = ['Typ' => '', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
// Gutschriften nach Ländern - Monatlich
$columns[] = ['Typ' => 'MONATLICHE AUFSCHLÜSSELUNG GUTSCHRIFTEN NACH LÄNDERN', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
if (isset($credit_summary['country_monthly']) && $credit_summary['country_monthly']->count() > 0) {
$creditByMonth = $credit_summary['country_monthly']->groupBy('month');
foreach ($creditByMonth as $countries) {
$columns[] = ['Typ' => $countries->first()->month_label, 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
foreach ($countries as $item) {
$columns[] = [
'Typ' => ' '.$item->country_name,
'Netto' => number_format($item->total_net, 2, ',', '.'),
'Steuer' => number_format($item->total_tax, 2, ',', '.'),
'Brutto' => number_format($item->total_gross, 2, ',', '.'),
];
}
}
} else {
$columns[] = ['Typ' => 'Keine monatlichen Gutschriften nach Ländern gefunden', 'Netto' => '', 'Steuer' => '', 'Brutto' => ''];
}
$headers = ['Zeitraum', 'Netto (€)', 'Steuer (€)', 'Brutto (€)'];
return Excel::download(new UserTeamExport($columns, $headers), $filename . '.xlsx');
return Excel::download(new UserTeamExport($columns, $headers), $filename.'.xlsx');
}
private function setFilterVars()
{
if (!session('revenue_filter_month')) {
if (! session('revenue_filter_month')) {
session(['revenue_filter_month' => intval(date('m'))]);
}
if (!session('revenue_filter_year')) {
if (! session('revenue_filter_year')) {
session(['revenue_filter_year' => intval(date('Y'))]);
}
if(!session('revenue_filter_type')){
if (! session('revenue_filter_type')) {
session(['revenue_filter_type' => 'year']);
}
if (Request::get('revenue_filter_month')) {
@ -171,20 +251,24 @@ class RevenueReportController extends Controller
private function getRevenueSummary()
{
$year = session('revenue_filter_year');
return [
'yearly' => $this->getRevenueByYear($year),
'monthly' => $this->getRevenueByMonthsInYear($year)
'monthly' => $this->getRevenueByMonthsInYear($year),
'country_yearly' => $this->getRevenueByCountryYear($year),
'country_monthly' => $this->getRevenueByCountryMonthly($year),
];
}
private function getCreditSummary()
{
$year = session('revenue_filter_year');
return [
'yearly' => $this->getCreditByYear($year),
'monthly' => $this->getCreditByMonthsInYear($year)
'monthly' => $this->getCreditByMonthsInYear($year),
'country_yearly' => $this->getCreditByCountryYear($year),
'country_monthly' => $this->getCreditByCountryMonthly($year),
];
}
@ -340,4 +424,116 @@ class RevenueReportController extends Controller
->orderBy('month')
->get();
}
}
private function getRevenueByCountryYear(int $year)
{
return UserInvoice::join('shopping_orders', 'user_invoices.shopping_order_id', '=', 'shopping_orders.id')
->join('shipping_countries', 'shopping_orders.country_id', '=', 'shipping_countries.id')
->join('countries', 'shipping_countries.country_id', '=', 'countries.id')
->selectRaw('
countries.id as country_id,
countries.de as country_name,
countries.code as country_code,
SUM(shopping_orders.subtotal_ws) as total_net,
SUM(shopping_orders.tax) as total_tax,
SUM(shopping_orders.total_shipping) as total_gross
')
->where('user_invoices.year', $year)
->where('user_invoices.cancellation', false)
->groupBy('countries.id', 'countries.de', 'countries.code')
->orderByDesc('total_gross')
->get();
}
private function getRevenueByCountryMonthly(int $year)
{
return UserInvoice::join('shopping_orders', 'user_invoices.shopping_order_id', '=', 'shopping_orders.id')
->join('shipping_countries', 'shopping_orders.country_id', '=', 'shipping_countries.id')
->join('countries', 'shipping_countries.country_id', '=', 'countries.id')
->selectRaw('
user_invoices.month,
CONCAT(CASE user_invoices.month
WHEN 1 THEN \'Januar\'
WHEN 2 THEN \'Februar\'
WHEN 3 THEN \'März\'
WHEN 4 THEN \'April\'
WHEN 5 THEN \'Mai\'
WHEN 6 THEN \'Juni\'
WHEN 7 THEN \'Juli\'
WHEN 8 THEN \'August\'
WHEN 9 THEN \'September\'
WHEN 10 THEN \'Oktober\'
WHEN 11 THEN \'November\'
WHEN 12 THEN \'Dezember\'
END, \' \', user_invoices.year) as month_label,
countries.id as country_id,
countries.de as country_name,
countries.code as country_code,
SUM(shopping_orders.subtotal_ws) as total_net,
SUM(shopping_orders.tax) as total_tax,
SUM(shopping_orders.total_shipping) as total_gross
')
->where('user_invoices.year', $year)
->where('user_invoices.cancellation', false)
->groupBy('user_invoices.month', 'countries.id', 'countries.de', 'countries.code')
->orderBy('user_invoices.month')
->orderByDesc('total_gross')
->get();
}
private function getCreditByCountryYear(int $year)
{
return UserCredit::join('users', 'user_credits.user_id', '=', 'users.id')
->join('user_accounts', 'users.account_id', '=', 'user_accounts.id')
->join('countries', 'user_accounts.country_id', '=', 'countries.id')
->selectRaw('
countries.id as country_id,
countries.de as country_name,
countries.code as country_code,
SUM(user_credits.net) as total_net,
SUM(user_credits.tax) as total_tax,
SUM(user_credits.total) as total_gross
')
->where('user_credits.year', $year)
->where('user_credits.cancellation', false)
->groupBy('countries.id', 'countries.de', 'countries.code')
->orderByDesc('total_gross')
->get();
}
private function getCreditByCountryMonthly(int $year)
{
return UserCredit::join('users', 'user_credits.user_id', '=', 'users.id')
->join('user_accounts', 'users.account_id', '=', 'user_accounts.id')
->join('countries', 'user_accounts.country_id', '=', 'countries.id')
->selectRaw('
user_credits.month,
CONCAT(CASE user_credits.month
WHEN 1 THEN \'Januar\'
WHEN 2 THEN \'Februar\'
WHEN 3 THEN \'März\'
WHEN 4 THEN \'April\'
WHEN 5 THEN \'Mai\'
WHEN 6 THEN \'Juni\'
WHEN 7 THEN \'Juli\'
WHEN 8 THEN \'August\'
WHEN 9 THEN \'September\'
WHEN 10 THEN \'Oktober\'
WHEN 11 THEN \'November\'
WHEN 12 THEN \'Dezember\'
END, \' \', user_credits.year) as month_label,
countries.id as country_id,
countries.de as country_name,
countries.code as country_code,
SUM(user_credits.net) as total_net,
SUM(user_credits.tax) as total_tax,
SUM(user_credits.total) as total_gross
')
->where('user_credits.year', $year)
->where('user_credits.cancellation', false)
->groupBy('user_credits.month', 'countries.id', 'countries.de', 'countries.code')
->orderBy('user_credits.month')
->orderByDesc('total_gross')
->get();
}
}