IMMREX7
<?php
namespace App\Http\Controllers\School;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Auth;
use DB;
use Carbon\Carbon;
use \App\Http\SendNotificationApi;
use Session;
class EmployeeNoticeController extends SchoolController {
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request) {
$today_date = Carbon::today()->format('Y-m-d');
if ($request->has('fromDate') && $request->fromDate != null) {
$from_date = Carbon::createFromFormat('d-m-Y', $request->fromDate);
}
if ($request->has('toDate') && $request->toDate != null) {
$to_date = Carbon::createFromFormat('d-m-Y', $request->toDate);
}
$classes = ['' => '--Select--','All' => 'All Department'] + \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('departmentName')->get()->pluck('departmentName', 'idDepartment')->toArray();
$notices = \App\EmployeeNoticeboard::leftJoin('employees', 'noticeboard_employee.idEmployee', '=', 'employees.idEmployee')->where('noticeboard_employee.idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear', '=', Session::get('idFinancialYear'))->orderBy('idEmployeeNoticeboard', 'desc');
if ($request->toDate != null && $request->fromDate != null) {
$notices = $notices->whereBetween('created_at', [$from_date->format('Y-m-d') . " 00:00:00", $to_date->format('Y-m-d') . " 23:59:59"])->get();
} else if ($request->has('fromDate') && $request->fromDate != null) {
$notices = $notices->whereBetween('created_at', [$from_date->format('Y-m-d') . " 00:00:00", $today_date . " 23:59:59"])->get();
} else if ($request->has('toDate') && $request->toDate != null) {
$notices = $notices->whereDate('created_at', '<=', $to_date)->get();
} else {
$notices = $notices->get();
}
return view('schools.noticeboard.employee_index', compact('notices', 'classes'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create() {
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request) {
//dd($request->all());
$rules = [
'idDepartment' => 'required',
'subject' => 'required|max:120',
'notice' => 'required|max:500',
// 'noticeFile' => 'mimes:doc,pdf,docx,jpg,png,jpeg|max:10000'
];
$messages = [
'idDepartment.required' => 'Department must be selected.',
'idDesignation.required' => 'Designation must be selected.'
];
$this->Validate($request, $rules, $messages);
if (!empty($request->idEmployee)) {
foreach ($request->idEmployee as $key => $value) {
$std = \App\Employee::where('idEmployee', '=', $value)->first();
$notice = new \App\EmployeeNoticeboard();
$notice->fill($request->all());
$notice->idEmployee = $value;
$notice->idDesignation = $std->idDesignation;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idEmployeeNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids=DB::table('employees')
->select('employees.firebase')
->where('employees.idEmployee', $value)
->get()->pluck('firebase')->toArray();
SendNotificationApi::sendSingleNotification( $reg_ids,"Employee Circular : " . $request->notice);
}
}
}else if ($request->idDepartment == 'All') {
$notice = new \App\EmployeeNoticeboard();
$notice->fill($request->all());
$notice->idDepartment = null;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idEmployeeNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids = DB::table('employees')
->select('firebase')
->whereNotNull('firebase')
->where('idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('firebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Employee Circular : " . $request->notice);
}
} else if ($request->has('all')) {
$notice = new \App\EmployeeNoticeboard();
$notice->fill($request->all());
$notice->idDesignation = null;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idEmployeeNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids = DB::table('employees')
->select('employees.firebase')
->where('employees.idDepartment', $request->idDepartment)
->whereNotNull('employees.firebase')
->where('employees.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('firebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Employee Circular : " . $request->notice);
}
} else {
foreach ($request->idDesignation as $key1 => $value1) {
$notice = new \App\EmployeeNoticeboard();
$notice->fill($request->all());
$notice->idDesignation = $value1;
$notice->idSchool = Auth::guard('school')->user()->idSchool;
$notice->idFinancialYear = Session::get('idFinancialYear');
if (($request->has('publishDate')) && ($request->publishDate > today_date())) {
$notice->isPublished = 'N';
} else {
$notice->publishDate = today_date();
$notice->isPublished = 'Y';
}
DB::beginTransaction();
$notice->save();
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idEmployeeNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
DB::commit();
if($notice->isPublished == 'Y'){
$reg_ids = DB::table('employees')
->select('employees.firebase')
->whereNotNull('employees.firebase')
->where('employees.idDesignation', $value1)
->where('employees.idDepartment', $request->idDepartment)
->where('employees.idSchool', Auth::guard('school')->user()->idSchool)
->get()->pluck('firebase')->toArray();
SendNotificationApi::sendNotification($reg_ids, "Employee Circular : " . $request->notice);
}
}
}
if ($request->ajax()) {
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
return redirect('school/employee-circular');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id) {
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id) {
$classes = ['' => '--Select--'] + \App\Department::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->orderBy('idDepartment')->get()->pluck('departmentName', 'idDepartment')->toArray();
$notices = \App\EmployeeNoticeboard::where('idSchool', '=', Auth::guard('school')->user()->idSchool)
->where('idFinancialYear','=', Session::get('idFinancialYear'))->orderBy('idEmployeeNoticeboard', 'desc')->get();
$notice = \App\EmployeeNoticeboard::findOrFail($id);
$section = \App\Designation::where('idDesignation', '=', $notice->idDesignation)->get()->pluck('sectionName', 'idDesignation')->toArray();
return view('schools.noticeboard.employee_index', compact('notices', 'notice', 'section', 'classes', 'student'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id) {
$rules = [
'idClass' => 'required',
'subject' => 'required|max:120',
'notice' => 'required|max:500',
// 'noticeFile' => 'required|mimes:pdf|max:10000'
];
if (count($request->sections) == 0) {
$rules += ['idSection' => 'required'];
}
$messages = [
'idClass.required' => 'Class must be selected.',
'idSection.required' => 'Section must be selected.'
];
$this->Validate($request, $rules, $messages);
$notice = \App\EmployeeNoticeboard::findOrFail($id);
$notice->fill($request->all());
if ($request->hasFile('noticeFile')) {
$nw = 'notice_' . $notice->idEmployeeNoticeboard . '.' . $request->file('noticeFile')->getClientOriginalExtension();
$request->file('noticeFile')->storeAs('public/schools/' . Auth::guard('school')->user()->idSchool . '/noticeboard/', $nw);
$notice->noticeFile = $nw;
}
$notice->update();
return redirect('school/noticeboards');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id) {
$notice = \App\EmployeeNoticeboard::findOrFail($id);
$notice->delete();
return response()->json(['success' => "SUCCESS"], 200, ['app-status' => 'success']);
}
public function viewNDoc($id) {
$notice = \App\EmployeeNoticeboard::findOrFail($id);
$path = storage_path('app/public/schools/' . $notice->idSchool . '/noticeboard/' . $notice->noticeFile);
return response()->file($path);
}
}
Copyright © 2021 -