-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5bb6114
commit 8834104
Showing
14 changed files
with
1,141 additions
and
217 deletions.
There are no files selected for viewing
123 changes: 123 additions & 0 deletions
123
app/Http/Controllers/Api/v1/Modules/ReportController.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Api\v1\Modules; | ||
|
||
use App; | ||
use App\Http\Controllers\Controller; | ||
use App\Models\Einheiten; | ||
use App\Models\Objekte; | ||
use App\Models\RentDefinition; | ||
use Carbon\Carbon; | ||
use Illuminate\Http\Request; | ||
use XLSXWriter; | ||
|
||
class ReportController extends Controller | ||
{ | ||
/** | ||
* @param Request $request | ||
* @return \Illuminate\Http\JsonResponse | ||
*/ | ||
public function revenue(Request $request, Objekte $object) | ||
{ | ||
$file = tempnam(sys_get_temp_dir(), 'revenue_report_'); | ||
$start = Carbon::today()->firstOfYear(); | ||
$end = $start->copy()->lastOfYear(); | ||
|
||
if ($request->has('date')) { | ||
$start = Carbon::parse($request->input('date')); | ||
$end = Carbon::parse($request->input('date')); | ||
} | ||
if ($request->has('period')) { | ||
$period = $request->input('period'); | ||
switch ($period) { | ||
case 'year': | ||
$start->firstOfYear(); | ||
$end->lastOfYear(); | ||
break; | ||
case 'quarter': | ||
$start->firstOfQuarter(); | ||
$end->lastOfQuarter(); | ||
break; | ||
case 'month': | ||
$start->firstOfMonth(); | ||
$end->lastOfMonth(); | ||
break; | ||
} | ||
} | ||
|
||
$header = [ | ||
'Apartment No.' => 'string', | ||
'Space' => '0.00', | ||
'Occupant Name' => 'string', | ||
'Contract Start' => 'date', | ||
'Contract End' => 'date', | ||
'Days Occupied' => 'integer', | ||
'Basic Rent' => 'euro', | ||
'Operating Costs' => 'euro', | ||
'Heating Costs' => 'euro', | ||
'Actual Payments' => 'euro' | ||
]; | ||
|
||
$sheet = 'Revenue'; | ||
|
||
$writer = new XLSXWriter(); | ||
|
||
$writer->writeSheetHeader($sheet, $header); | ||
|
||
$units = Einheiten::whereHas('haus.objekt', function ($query) use ($object) { | ||
$query->where('OBJEKT_ID', $object->OBJEKT_ID); | ||
})->with(['haus.objekt', 'mietvertraege.mieter'])->defaultOrder()->get(); | ||
$rows = 1; | ||
|
||
foreach ($units as $unit) { | ||
$rentalContracts = $unit->mietvertraege() | ||
->where(function ($query) use ($start, $end) { | ||
$query->where(function ($query) use ($start, $end) { | ||
$query->active('>=', $start) | ||
->active('<=', $end); | ||
})->orWhere(function ($query) use ($start, $end) { | ||
$query->whereHas('postings', function ($query) use ($start, $end) { | ||
$query->where('KONTENRAHMEN_KONTO', 80001) | ||
->where('KOSTENTRAEGER_TYP', 'Mietvertrag') | ||
->whereDate('DATUM', '>=', $start) | ||
->whereDate('DATUM', '<=', $end); | ||
}); | ||
}); | ||
})->defaultOrder()->get(); | ||
if ($rentalContracts->isEmpty()) { | ||
$writer->writeSheetRow($sheet, [$unit->EINHEIT_KURZNAME, $unit->EINHEIT_QM, '', '', '', 0, 0, 0, 0, 0]); | ||
$rows++; | ||
} else { | ||
foreach ($rentalContracts as $index => $rentalContract) { | ||
$basicRent = RentDefinition::sumDefinitions($rentalContract->basicRentDefinitions($start, $end), $start, $end); | ||
$heatingCosts = RentDefinition::sumDefinitions($rentalContract->heatingExpenseDefinitions($start, $end), $start, $end); | ||
$operatingCosts = RentDefinition::sumDefinitions($rentalContract->operatingCostDefinitions($start, $end), $start, $end); | ||
$postings = $rentalContract->postings($start, $end)->where('KONTENRAHMEN_KONTO', 80001)->sum('BETRAG'); | ||
$occupantName = $rentalContract->mieter->implode('full_name', '; '); | ||
$to = $rentalContract->MIETVERTRAG_BIS === '0000-00-00' ? '' : $rentalContract->MIETVERTRAG_BIS; | ||
$row = ['', '', $occupantName, $rentalContract->MIETVERTRAG_VON, $to, $rentalContract->overlaps($start, $end), $basicRent, $operatingCosts, $heatingCosts, $postings]; | ||
if ($index === 0) { | ||
$row[0] = $unit->EINHEIT_KURZNAME; | ||
$row[1] = $unit->EINHEIT_QM; | ||
} | ||
$writer->writeSheetRow($sheet, $row); | ||
$rows++; | ||
} | ||
} | ||
} | ||
$row = ['Total', "=SUM(B2:B$rows)", '', '', '', '', "=SUM(G2:G$rows)", "=SUM(H2:H$rows)", "=SUM(I2:I$rows)", "=SUM(J2:J$rows)"]; | ||
$writer->writeSheetRow($sheet, $row); | ||
$writer->writeSheetRow($sheet, ['', '', '', '', '', '', '', '', '', '']); | ||
$writer->writeSheetRow($sheet, ['', '', 'Query Period:', $start->toDateString(), $end->toDateString(), '', '', '', '', '']); | ||
|
||
$writer->writeToFile($file); | ||
|
||
$filename = 'revenue_report_' | ||
. $object->OBJEKT_KURZNAME . '_' | ||
. $start->toDateString() . '_' | ||
. $end->toDateString() . '_created_at_' | ||
. Carbon::today()->toDateString(); | ||
|
||
return response()->file($file, ['Content-Disposition' => 'inline; filename="' . $filename . '.xlsx'])->deleteFileAfterSend(true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use App\Models\Scopes\AktuellScope; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class Posting extends Model | ||
{ | ||
protected $table = 'GELD_KONTO_BUCHUNGEN'; | ||
protected $primaryKey = 'GELD_KONTO_BUCHUNGEN_ID'; | ||
|
||
/** | ||
* The attributes that are not mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* The attributes that should be hidden for arrays. | ||
* | ||
* @var array | ||
*/ | ||
protected $hidden = []; | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
static::addGlobalScope(new AktuellScope()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
<?php | ||
|
||
namespace App\Models; | ||
|
||
use Carbon\Carbon; | ||
use Illuminate\Database\Eloquent\Builder; | ||
use Illuminate\Database\Eloquent\Model; | ||
|
||
class RentDefinition extends Model | ||
{ | ||
protected $table = 'MIETENTWICKLUNG'; | ||
protected $primaryKey = 'MIETENTWICKLUNG_ID'; | ||
|
||
/** | ||
* The attributes that are not mass assignable. | ||
* | ||
* @var array | ||
*/ | ||
protected $guarded = []; | ||
|
||
/** | ||
* The attributes that should be hidden for arrays. | ||
* | ||
* @var array | ||
*/ | ||
protected $hidden = []; | ||
|
||
public static function sumDefinitions($query, $from = null, $to = null) | ||
{ | ||
return $query->get()->reduce(function ($carry, $item) use ($from, $to) { | ||
return $carry + $item->value($from, $to); | ||
}, 0); | ||
} | ||
|
||
protected static function boot() | ||
{ | ||
parent::boot(); | ||
|
||
static::addGlobalScope('aktuell', function (Builder $builder) { | ||
$builder->where('MIETENTWICKLUNG_AKTUELL', '1'); | ||
}); | ||
} | ||
|
||
public function value($from = null, $to = null) | ||
{ | ||
$definition_from = Carbon::parse($this->ANFANG); | ||
$definition_to = $this->ENDE === '0000-00-00' ? Carbon::maxValue() : Carbon::parse($this->ENDE); | ||
if (is_null($from)) { | ||
$from = $definition_from; | ||
} | ||
if (is_null($to)) { | ||
$to = $definition_to; | ||
} | ||
if (is_string($from)) { | ||
$from = Carbon::parse($from); | ||
} | ||
if (is_string($to)) { | ||
$to = Carbon::parse($to); | ||
} | ||
$from = $definition_from->max($from); | ||
$to = $definition_to->min($to); | ||
$diffInMonths = $from->diffInMonths($to); | ||
$value = 0; | ||
if ($definition_from->diffInMonths($definition_to) == 0) { | ||
$value = $this->BETRAG; | ||
} else { | ||
if ($diffInMonths == 0) { | ||
$value = (($from->diffInDays($to) + 1) / $from->daysInMonth) * $this->BETRAG; | ||
} else { | ||
if ($from->day == 1) { | ||
$value += $this->BETRAG; | ||
} else { | ||
$value += ($from->day / $from->daysInMonth) * $this->BETRAG; | ||
} | ||
if ($to->day == $to->daysInMonth) { | ||
$value += $this->BETRAG; | ||
} else { | ||
$value += ($to->day / $to->daysInMonth) * $this->BETRAG; | ||
} | ||
$value += $this->BETRAG * ($diffInMonths - 1); | ||
} | ||
} | ||
return $value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.