mivita/app/Http/Controllers/RevenueReportController.php
2026-04-14 18:07:45 +02:00

539 lines
22 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Exports\UserTeamExport;
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
{
public function __construct()
{
$this->middleware('admin');
}
public function index()
{
$this->setFilterVars();
$data = [
'filter_months' => HTMLHelper::getTransMonths(),
'filter_years' => HTMLHelper::getYearRange(2022),
'revenue_summary' => $this->getRevenueSummary(),
'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) {
$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, ',', '.'),
];
}
} else {
$columns[] = [
'Typ' => "Jahr {$filter_year}",
'Netto' => '0,00',
'Steuer' => '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) {
$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, ',', '.'),
];
}
} else {
$columns[] = [
'Typ' => 'Keine monatlichen Umsätze gefunden',
'Netto' => '',
'Steuer' => '',
'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) {
$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, ',', '.'),
];
}
} else {
$columns[] = [
'Typ' => "Jahr {$filter_year}",
'Netto' => '0,00',
'Steuer' => '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) {
$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, ',', '.'),
];
}
} else {
$columns[] = [
'Typ' => 'Keine monatlichen Gutschriften gefunden',
'Netto' => '',
'Steuer' => '',
'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');
}
private function setFilterVars()
{
if (! session('revenue_filter_month')) {
session(['revenue_filter_month' => intval(date('m'))]);
}
if (! session('revenue_filter_year')) {
session(['revenue_filter_year' => intval(date('Y'))]);
}
if (! session('revenue_filter_type')) {
session(['revenue_filter_type' => 'year']);
}
if (Request::get('revenue_filter_month')) {
session(['revenue_filter_month' => Request::get('revenue_filter_month')]);
}
if (Request::get('revenue_filter_year')) {
session(['revenue_filter_year' => Request::get('revenue_filter_year')]);
}
if (Request::get('revenue_filter_type')) {
session(['revenue_filter_type' => Request::get('revenue_filter_type')]);
}
}
private function getRevenueSummary()
{
$year = session('revenue_filter_year');
return [
'yearly' => $this->getRevenueByYear($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),
'country_yearly' => $this->getCreditByCountryYear($year),
'country_monthly' => $this->getCreditByCountryMonthly($year),
];
}
private function getRevenueByYear($year)
{
return UserInvoice::join('shopping_orders', 'user_invoices.shopping_order_id', '=', 'shopping_orders.id')
->selectRaw("
{$year} as year,
CONCAT('Jahr ', {$year}) as period_label,
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(DB::raw('1'))
->get();
}
private function getRevenueByMonth($year, $month)
{
return UserInvoice::join('shopping_orders', 'user_invoices.shopping_order_id', '=', 'shopping_orders.id')
->selectRaw("
{$year} as year,
{$month} as month,
CONCAT(CASE {$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, ' ', {$year}) as period_label,
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.month', $month)
->where('user_invoices.cancellation', false)
->groupBy(DB::raw('1'))
->get();
}
private function getRevenueByMonthsInYear($year)
{
return UserInvoice::join('shopping_orders', 'user_invoices.shopping_order_id', '=', 'shopping_orders.id')
->selectRaw("
user_invoices.year,
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 period_label,
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.year', 'user_invoices.month')
->orderBy('user_invoices.month')
->get();
}
private function getCreditByYear($year)
{
return UserCredit::selectRaw("
{$year} as year,
CONCAT('Jahr ', {$year}) as period_label,
SUM(net) as total_net,
SUM(tax) as total_tax,
SUM(total) as total_gross
")
->where('year', $year)
->where('cancellation', false)
->groupBy(DB::raw('1'))
->get();
}
private function getCreditByMonth($year, $month)
{
return UserCredit::selectRaw("
{$year} as year,
{$month} as month,
CONCAT(CASE {$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, ' ', {$year}) as period_label,
SUM(net) as total_net,
SUM(tax) as total_tax,
SUM(total) as total_gross
")
->where('year', $year)
->where('month', $month)
->where('cancellation', false)
->groupBy(DB::raw('1'))
->get();
}
private function getCreditByMonthsInYear($year)
{
return UserCredit::selectRaw("
year,
month,
CONCAT(CASE 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, ' ', year) as period_label,
SUM(net) as total_net,
SUM(tax) as total_tax,
SUM(total) as total_gross
")
->where('year', $year)
->where('cancellation', false)
->groupBy('year', 'month')
->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();
}
}