Files
kalendar/app/Http/Controllers/DashboardController.php
2018-10-30 15:24:18 +01:00

113 lines
4.0 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Company;
use App\Confirm;
use App\Term;
use Illuminate\Http\Request;
class DashboardController extends Controller
{
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$terms = Term::all();
$companies = Company::all();
$q = [];
$m = [];
$comp = [];
foreach ($companies as $company) {
if ($company->monthly_payer) {
$m[] = $company;
} else {
$q[] = $company;
}
$comp[] = $company->getAttributes() + ['checked' => false];
}
$confirms = Confirm::orderBy('term_date')->get();
$list = [];
$date = date('Y-01-01');
while (true) {
$c = [];
foreach ($confirms as $confirm) {
if ($confirm->term_date == $date) {
$c[$confirm->company_id.'_'.$confirm->term_id] = $confirm;
}
}
foreach ($terms as $term) {
if ($term->applies_to) {
if (($term->applies_to == 1 && ! count($m)) || ($term->applies_to == 2 && ! count($q))) {
continue;
}
}
[$year, $month, $day] = explode('-', $date);
if ($day == $term->day && $term->month == null && $term->inter == 0) {
foreach ($comp as $item=>$value) {
if (isset($c[$comp[$item]['id'] . '_' . $term->id])) {
$comp[$item]['checked'] = true;
} else {
$comp[$item]['checked'] = false;
}
}
$list[] =
(object) ['id' => $term->id, 'div' => $year.(int)$month, 'date' => $date, 'name' => $term->name, 'desc' => $term->desc, 'companies' => $comp];
} elseif ($day == $term->day && $term->month == $month && $term->inter == 2) {
foreach ($comp as $item=>$value) {
if (isset($c[$comp[$item]['id'] . '_' . $term->id])) {
$comp[$item]['checked'] = true;
} else {
$comp[$item]['checked'] = false;
}
}
$list[] =
(object) ['id' => $term->id, 'div' => $year.(int)$month, 'date' => $date, 'name' => $term->name, 'desc' => $term->desc, 'companies' => (object)$comp];
}
}
$date = date('Y-m-d', strtotime("$date +1 DAYS"));
if ($year != date('Y')) {
break;
}
}
return view('dashboard', compact('list'));
}
public function ajax(Request $request)
{
$return = [
'status' => 'error'
];
if ($request->isMethod('post')) {
$date = $request->get('date');
$company = (int)$request->get('company');
$term = (int)$request->get('term');
$checked = $request->get('checked');
if ($date && $company && $term) {
if ('true' == $checked) {
$confirm = Confirm::firstOrNew([
'term_date' => $date,
'company_id' => $company,
'term_id' => $term,
]);
$confirm->save();
} else {
$confirm = Confirm::where('term_date', $date)->where('company_id', $company)
->where('term_id', $term)->first();
$confirm->delete();
}
$request->session()->flash('message', 'Uloženo');
$request->session()->flash('message-type', 'success');
return response()->json(['status' => 'success']);
}
}
return response()->json($return);
}
}