343 lines
No EOL
12 KiB
PHP
343 lines
No EOL
12 KiB
PHP
<?php
|
|
|
|
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 Illuminate\Support\Facades\DB;
|
|
use Maatwebsite\Excel\Facades\Excel;
|
|
|
|
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' => ''
|
|
];
|
|
}
|
|
|
|
$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)
|
|
];
|
|
}
|
|
|
|
private function getCreditSummary()
|
|
{
|
|
$year = session('revenue_filter_year');
|
|
|
|
return [
|
|
'yearly' => $this->getCreditByYear($year),
|
|
'monthly' => $this->getCreditByMonthsInYear($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();
|
|
}
|
|
} |