File manager - Edit - /home/autoph/public_html/projects/document_tracking/app/Http/Controllers/DocumentController.php
Back
<?php namespace App\Http\Controllers; use Illuminate\Support\Carbon; use Illuminate\Support\Str; use App\Models\Document; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Auth; use App\Http\Resources\DocumentResource; use App\Models\User; use Mail; class DocumentController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { $users['data'] = User::orderBy("id","asc") ->select('users.id','users.first_name','users.last_name','companies.comp_name','dealers.dealer_name','departments.department_name') ->join('companies','companies.comp_id',"=",'users.company') ->join('dealers','dealers.dealer_id',"=",'users.dealer') ->join('departments','departments.department_id',"=",'users.dept_id') ->get(); // dd($users); $const = DB::table('documents')->latest()->first('id'); if($const != null){ $incr = $const->id; } else { $incr = 0; } $incremented_value = $incr + 1; $length = 3; $node_head = 1; $unique_ctr = $node_head.substr(str_repeat(0,$length).$incremented_value, - $length); return view('document.index',["unique_ctr"=>$unique_ctr])->with("users", $users); } public function search(Request $request) { return view('auth.search'); } public function searchDoc(Request $request) { $document = Document::select('documents.*', 'sent_files.recipient_id', 'sent_files.sender_id', 'sent_files.created_at', 'sent_files.updated_at', 'sent_files.date_diff', 'ua.first_name as recipient_name', 'ua.last_name as recipient_surname', 'ub.first_name as sender_name', 'ub.last_name as sender_surname', 'departments.department_name', 'dealers.dealer_code', 'companies.comp_code', ) ->join('sent_files','file_id',"=",'documents.id') ->join('users as ua','ua.id',"=",'sent_files.recipient_id') ->join('users as ub','ub.id',"=",'sent_files.sender_id') ->join('departments','department_id',"=",'ua.dept_id') ->join('dealers','dealer_id',"=",'ua.dealer') ->join('companies','companies.comp_id',"=",'ua.company') ->where('control_number',$request->control_number) ->get(); return response()->json($document); } public function fetchall(Request $request) { $user_id = Auth::user()->id; // $documents = Document::where('creator_id',$user_id)->get(); // return response()->json($documents); return DocumentResource::collection(Document::where('creator_id',$user_id)->get()); } public function return(Request $request) { //dd($request->all()); $document = Document::findOrFail($request->file_id,); // get the whole document $string_id = explode(",",$document->received_by); //explode receive_by field and make it an array // dd($string_id); $array = $document->received_by; $to_remove = $request->recipient_id; // dd($to_remove); $key = array_search($to_remove,$string_id); unset($string_id[$key]); $doc_update = Document::where('id',$request->file_id) ->first(); $doc_update->received_by= implode(",",$string_id); $doc_update->update(); $timeDate = Carbon::now()->timezone('Asia/Manila'); $data = array('sender_id'=>$request->sender_id, 'name'=>Auth::user()->first_name, 'file_id'=>$request->file_id, 'path'=>$request->sender_id, 'recipient_id'=>$request->recipient_id, 'status'=>$request->status, 'remarks'=>$request->remarks, 'created_at'=>$timeDate, 'updated_at'=>$timeDate); DB::table('sent_files')->insert($data); //insert the data sent_files DB::table('sent_files_table')->insert($data); //insert the data in sent_files_table for backup purposes //the cc or recipient can return the file to the previous sender DB::table('sent_files') ->where([["file_id","=",$request->file_id],["recipient_id","=",$request->sender_id]]) ->orWhere([["file_id","=",$request->file_id],["cc_id","=",$request->sender_id]]) ->delete(); //delete the record if the document is returned to the previous sender } public function receiveFile(Request $request) { // dd($request->all()); $timeDate = Carbon::now()->timezone('Asia/Manila'); //for getting the datetime based on the current timezone //$objTimeDate = implode("",explode('-',$timeDate)); //for getting the right format of sql datetime //receive file then search document id and receiver id on documents table $user = Auth::user(); $id = $request->file_id; //the recipient should receive if the cc receive the document too $document = Document::findOrFail($id); // get the whole document $string_id = explode(",",$document->received_by); //explode receive_by field and make it an array // dd([$string_id,$request->sender_id]); if(in_array($request->sender_id, $string_id )){ //match the receiver id in array of receive_by field array return response()->json(["message" => "You have already received this file","type"=>"error"]); //return error } elseif (!in_array($request->sender_id, $string_id)) { //if the id is not in the array of id's // else { $document = Document::where('id',$id)->first();//get the document $document_receiver = DB::table('sent_files')->where('file_id',$id)->latest()->first(); $doc_update = Document::where('id',$id)->first();//get the document with the same id as the request from frontend // dd($document_receiver); if($request->sender_id == $document_receiver->recipient_id){ //check if the sender id is recipient in sent_files table //$document_receiver = DB::table('sent_files')->where('file_id',$id)->latest()->first(); //->where('recipient_id',$user->id); // $document_receiver->updated_at=$timeDate->toDateTimeString(); // $document_receiver->save(); if(!$document_receiver->cc_id){ // dd(count(array_filter(array($doc_update->received_by)))); // dd(count(array($doc_update->received_by))); if(count(array_filter(array($doc_update->received_by))) == 0){ // dd(count(array($doc_update->received_by))); $doc_update->received_by = $request->sender_id; $doc_update->updated_at=$timeDate; // dd($doc_update); $doc_update->update(); //DB::update('update sent_files set created_at = ? where file_id = ? and where recipient_id = ?',[$timeDate->format('Y-m-d H:i:s'),$id,Auth::user()->id]); DB::table('sent_files')->where('file_id',$id)->where('recipient_id',Auth::user()->id)->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s')]); } else { $doc_update->received_by=implode(",",[$document->received_by,$request->sender_id]); $doc_update->updated_at=$timeDate; $doc_update->update(); DB::table('sent_files')->where('file_id',$id)->where('recipient_id',Auth::user()->id)->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s')]); // $document_receiver->updated_at=$timeDate->toDateTimeString(); // $document_receiver->update(); } } else { if(count(array_filter(array($doc_update->received_by))) == 0){ $doc_update->received_by = $request->sender_id; $doc_update->updated_at=$timeDate; $doc_update->update(); DB::table('sent_files')->where('file_id',$id)->where('recipient_id',Auth::user()->id)->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s')]); // $document_receiver->updated_at=$timeDate->toDateTimeString(); // $document_receiver->update(); } else { $doc_update->received_by=implode(",",[$document->received_by,$request->sender_id.",".$document_receiver->cc_id]); $doc_update->updated_at=$timeDate; $doc_update->update(); DB::table('sent_files')->where('file_id',$id)->where('recipient_id',Auth::user()->id)->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s')]); // $document_receiver->updated_at=$timeDate->toDateTimeString(); // $document_receiver->update(); } } return response()->json(["message"=>"File received!","type"=>"success"]); }elseif($request->sender_id == $document_receiver->cc_id){ //check if the cc_id is not null if(count(array_filter(array($doc_update->received_by))) == 0){ // dd(count(array($doc_update->received_by))); $doc_update = Document::where('id',$id)->first(); $doc_update->received_by=$request->sender_id.",".$document_receiver->recipient_id; $doc_update->updated_at=$timeDate; $doc_update->update(); } else { $doc_update = Document::where('id',$id)->first(); $doc_update->received_by=implode(",",[$request->sender_id.",".$document_receiver->recipient_id]); $doc_update->updated_at=$timeDate; $doc_update->update(); } DB::table('sent_files')->where('file_id',$id)->where('cc_id',Auth::user()->id)->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s')]); // $document_receiver->updated_at=$timeDate->toDateTimeString(); // $document_receiver->update(); return response()->json(["message"=>"File received!","type"=>"success"]); } else { return response()->json(["message"=>"Something went wrong!","type"=>"error"]); } } } public function finalizeFile(Request $request){ $timeDate = Carbon::now()->timezone('Asia/Manila'); //for getting the datetime based on the current timezone $id = $request->file_id; $document = Document::findOrFail($id); // get the whole document $string_id = explode(",",$document->received_by); //explode receive_by field and make it an array if(in_array($request->sender_id, $string_id )){ //match the receiver id in array of receive_by field array return response()->json(["message" => "You have already received this file","type"=>"error"]); //return error } elseif (!in_array($request->sender_id, $string_id)) { //if the id is not in the array of id's $document = Document::where('id',$id)->first();//get the document $document_receiver = DB::table('sent_files')->where('file_id',$id)->latest()->first(); $doc_update = Document::where('id',$id)->first();//get the document with the same id as the request from frontend if($request->sender_id == $document_receiver->recipient_id){ //check if the sender id is recipient in sent_files table if(!$document_receiver->cc_id){ if(count(array_filter(array($doc_update->received_by))) == 0){ $doc_update->received_by = $request->sender_id; $doc_update->updated_at=$timeDate; $doc_update->dc_status=1; $doc_update->update(); DB::table('sent_files') ->where('file_id',$id) ->where('recipient_id',Auth::user()->id) ->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s'),'remarks'=>"File Transport Ended"]); } else { $doc_update->received_by=implode(",",[$document->received_by,$request->sender_id]); $doc_update->updated_at=$timeDate; $doc_update->dc_status=1; $doc_update->update(); DB::table('sent_files') ->where('file_id',$id) ->where('recipient_id',Auth::user()->id) ->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s'),'remarks'=>"File Transport Ended"]); } } else { if(count(array_filter(array($doc_update->received_by))) == 0){ $doc_update->received_by = $request->sender_id; $doc_update->updated_at=$timeDate; $doc_update->dc_status=1; $doc_update->update(); DB::table('sent_files') ->where('file_id',$id) ->where('recipient_id',Auth::user()->id) ->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s'),'remarks'=>"File Transport Ended"]); } else { $doc_update->received_by=implode(",",[$document->received_by,$request->sender_id.",".$document_receiver->cc_id]); $doc_update->updated_at=$timeDate; $doc_update->dc_status=1; $doc_update->update(); DB::table('sent_files') ->where('file_id',$id) ->where('recipient_id',Auth::user()->id) ->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s'), 'remarks'=>"File Transport Ended"]); } } return response()->json(["message"=>"File received!","type"=>"success"]); }elseif($request->sender_id == $document_receiver->cc_id){ //check if the cc_id is not null if(count(array_filter(array($doc_update->received_by))) == 0){ $doc_update = Document::where('id',$id)->first(); $doc_update->received_by=$request->sender_id.",".$document_receiver->recipient_id; $doc_update->updated_at=$timeDate; $doc_update->dc_status=1; $doc_update->update(); } else { $doc_update = Document::where('id',$id)->first(); $doc_update->received_by=implode(",",[$request->sender_id.",".$document_receiver->recipient_id]); $doc_update->updated_at=$timeDate; $doc_update->dc_status=1; $doc_update->update(); } DB::table('sent_files') ->where('file_id',$id) ->where('cc_id',Auth::user()->id) ->update(['updated_at'=>$timeDate->format('Y-m-d H:i:s'), 'remarks'=>"File Transport Ended"]); return response()->json(["message"=>"File received!","type"=>"success"]); } else { return response()->json(["message"=>"Something went wrong!","type"=>"error"]); } } } public function getDocumentsOnAnalyticsDashboard(Request $request){ $from_month = Carbon::now()->startOfMonth(); $to_month = Carbon::now()->endOfMonth(); $from_year = Carbon::now()->startOfYear(); $to_year = Carbon::now()->endOfYear(); $from_yesterday = Carbon::now()->startOfDay(); $to_tomorrow = Carbon::now()->endOfDay(); $second_quarter_start = Carbon::now()->startOfYear()->addQuarters(1); $second_quarter_end = Carbon::now()->startOfYear()->addQuarters(1)->endOfQuarter(); if($request->status == 1){ $document = DB::table('sent_files') ->select(DB::raw('COUNT(1) as documents')) ->select('documents.*','users.last_name as sender', 'users.last_name as recipient','departments.department_name','dealers.dealer_code','companies.comp_code') ->join('documents','documents.id',"=",'sent_files.file_id') ->join('users','users.id',"=",'sent_files.recipient_id') ->join('companies','comp_id',"=",'users.company') ->join('dealers','dealer_id',"=",'users.dealer') ->join('departments','department_id',"=",'users.dept_id') ->whereBetween('sent_files.created_at',[$from_year,$to_year]) ->where('companies.comp_id',$request->comp_id) ->where('dealers.dealer_id',$request->dealer_id) ->groupBy('documents.id') ->get(); return response()->json($document); }elseif($request->status == 2){ $document = DB::table('sent_files') ->select(DB::raw('COUNT(1) as documents')) ->select('documents.*','users.last_name as sender', 'users.last_name as recipient','departments.department_name','dealers.dealer_code','companies.comp_code') ->join('documents','documents.id',"=",'sent_files.file_id') ->join('users','users.id',"=",'sent_files.recipient_id') ->join('companies','comp_id',"=",'users.company') ->join('dealers','dealer_id',"=",'users.dealer') ->join('departments','department_id',"=",'users.dept_id') ->whereBetween('sent_files.created_at',[$second_quarter_start,$second_quarter_end]) ->where('companies.comp_id',$request->comp_id) ->where('dealers.dealer_id',$request->dealer_id) ->and('users','users.id',"=",'sent_files.sender_id') ->groupBy('documents.id') ->get(); return response()->json($document); }elseif($request->status == 3){ $document = DB::table('sent_files') ->select(DB::raw('COUNT(1) as documents')) ->select('documents.*','users.last_name','departments.department_name','dealers.dealer_code','companies.comp_code') ->join('documents','documents.id',"=",'sent_files.file_id') ->join('users','users.id',"=",'sent_files.recipient_id') ->join('companies','comp_id',"=",'users.company') ->join('dealers','dealer_id',"=",'users.dealer') ->join('departments','department_id',"=",'users.dept_id') ->whereBetween('sent_files.created_at',[$from_month,$to_month]) ->where('companies.comp_id',$request->comp_id) ->where('dealers.dealer_id',$request->dealer_id) ->groupBy('documents.id') ->get(); return response()->json($document); }elseif($request->status == 4){ $document = DB::table('sent_files') ->select(DB::raw('COUNT(1) as documents')) ->select('documents.*','users.last_name','departments.department_name','dealers.dealer_code','companies.comp_code') ->join('documents','documents.id',"=",'sent_files.file_id') ->join('users','users.id',"=",'sent_files.recipient_id') ->join('companies','comp_id',"=",'users.company') ->join('dealers','dealer_id',"=",'users.dealer') ->join('departments','department_id',"=",'users.dept_id') ->whereBetween('sent_files.created_at',[$from_yesterday,$to_tomorrow]) ->where('companies.comp_id',$request->comp_id) ->where('dealers.dealer_id',$request->dealer_id) ->groupBy('documents.id') ->get(); return response()->json($document); } return view('auth.adminview'); } public function DocumentAnalyticsDashboardByCompany(Request $request){ $document = DB::table('sent_files') ->select('companies.comp_code',DB::raw('SEC_TO_TIME(SUM(TIME_TO_SEC(sent_files.date_diff))) as totalTime')) ->join('users','users.id',"=",'sent_files.recipient_id') ->join('companies','users.company',"=",'companies.comp_id') ->groupBy('companies.comp_code') ->get(); // dd($document); foreach($document as $docs){ $foobar[] = (object) array( 'department' => $docs->comp_code, // 'start' => $docs->updated_at, // 'end' => $docs->updated_at, 'time' => $docs->totalTime ); } return response()->json($foobar); } public function DocumentAnalyticsDashboardByDealer(Request $request){ $documents = DB::table('sent_files') ->select(DB::raw('DISTINCT(dealers.dealer_code)'),DB::raw('SUM(date_diff) as totalTime')) ->join('users','users.id',"=",'sent_files.recipient_id') ->join('dealers','users.dealer',"=",'dealers.dealer_id') ->groupBy('dealers.dealer_code') ->get(); foreach($documents as $docs){ $foobar[] = (object) array( 'department' => $docs->dealer_code, 'time' => $docs->totalTime ); } return response()->json($foobar); } public function DocumentAnalyticsDashboardByDepartment(Request $request){ $document = DB::table('sent_files') ->select('departments.department_name','sent_files.updated_at','sent_files.created_at',DB::raw('SEC_TO_TIME(SUM(TIME_TO_SEC(TIMEDIFF(sent_files.updated_at,sent_files.created_at)))) as totalTime')) ->join('users','users.id',"=",'sent_files.recipient_id') ->join('departments','users.dept_id',"=",'departments.department_id') ->groupBy('departments.department_name') ->get(5); foreach($document as $docs){ $foobar[] = (object) array( 'department' => $docs->department_name, 'start' => $docs->updated_at, 'end' => $docs->updated_at, 'time' => $docs->totalTime ); } // return response()->json($document); return response()->json($foobar); } public function redirect(Request $request) { $data = array( 'sender_id'=>$request->sender_id, 'name'=>Auth::user()->first_name, 'file_id'=>$request->file_id, 'path'=>$request->sender_id, 'recipient_id'=>$request->recipient_id, 'status'=>$request->status, 'remarks'=>$request->remarks, 'created_at'=>Carbon::now()->timezone('Asia/Manila')->format('y-m-d H:i:s'), 'updated_at'=>Carbon::now()->timezone('Asia/Manila')->format('Y-m-d H:i:s') ); DB::table('sent_files')->insert($data); DB::table('sent_files_table')->insert($data); $to = User::find($request->recipient_id); $email_data = array('f_name'=>$to['first_name'],'subject'=>$request->document_title, 'sender'=>$data['name']); Mail::send('mail.mail',$email_data, function($message) use ($to) { $message->to('clarenceandaya8@gmail.com', 'Notification')->subject('New Document'); $message->from(Auth::user()->email,'AutoHub Group of Companies, Inc.'); }); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { return view('document.create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $const = DB::table('documents')->latest()->first('id'); if($const != null){ $incr = $const->id; } else { $incr = 0; } $incremented_value = $incr + 1; $length = 3; $node_head = 1; $unique_ctr = $node_head.substr(str_repeat(0,$length).$incremented_value, - $length); $user = Auth::user(); $date = Carbon::now()->timezone('Asia/Manila'); // $objTimeDate = implode("",explode('-',$date)); // use this only if needed for some reason foreach($request->documents as $file) { $url=$file->getClientOriginalName(); $file->move(public_path().'/files/', $url); $file_data[] = $url; } if(!$request->cc_id){ $document = new Document; $document->control_number=$unique_ctr; $document->url=json_encode($file_data); $document->document_title=$request->document_title; $document->short_desc=$request->document_short_description; $document->long_desc=$request->document_long_description; $document->creation_date=$request->$date; $document->due_date=$request->document_due_date; $document->creator_id=$user->id; $document->dc_status=0; // dd($document); $document->save(); $data = array('sender_id'=>$user->id,'name'=>$user->first_name,'file_id'=>$document->id,'recipient_id'=>$request->recipient_id, 'status'=>"in transit", 'created_at'=>$date, 'updated_at'=>$date); DB::table('sent_files')->insert($data); DB::table('sent_files_table')->insert($data); $to = User::find($request->recipient_id); $email_data = array('f_name'=>$to['first_name'],'subject'=>$request->document_title, 'sender'=>$data['name']); // Mail::send('mail.mail',$email_data, function($message) use ($to) { // $message->to('clarenceandaya8@gmail.com', 'Notification')->subject('New Document'); // $message->from('ahub56072@gmail.com','AutoHub Group of Companies, Inc.'); // }); return json_encode(["message"=>"document successfuly created"]); } $document = new Document; $document->control_number=$unique_ctr; $document->url=json_encode($file_data); $document->document_title=$request->document_title; $document->short_desc=$request->document_short_description; $document->long_desc=$request->document_long_description; $document->creation_date=$request->$date; $document->due_date=$request->document_due_date; $document->creator_id=$user->id; $document->dc_status=0; $document->save(); $data = array('sender_id'=>$user->id,'name'=>$user->first_name,'file_id'=>$document->id,'recipient_id'=>$request->recipient_id, 'cc_id'=>$request->cc_id, 'status'=>"in transit", 'created_at'=>$date, 'updated_at'=>$date); DB::table('sent_files')->insert($data); DB::table('sent_files_table')->insert($data); return json_encode(["message"=>"document successfuly created"]); } /** * Display the specified resource. * * @param \App\Models\Document $document * @return \Illuminate\Http\Response */ public function show(Document $document) { // } /** * Show the form for editing the specified resource. * * @param \App\Models\Document $document * @return \Illuminate\Http\Response */ public function edit($id) { $user = Auth::user(); $documentCreatorName = Document::find($id) ->select('first_name','last_name') ->join('users','users.id',"=",'documents.creator_id') ->get(); //dd($documentCreatorName); $document = Document::findOrFail($id); $string_id = explode(",",$document->received_by); if(in_array($user->id, $string_id )){ $document_received_by_last_id = 0; } elseif (!in_array($user->id, $string_id)){ $document_received_by_last_id = $id; } $remarks = DB::table('sent_files') ->select('remarks','name','created_at','sender_id') ->where('file_id',$id) ->get(); //get the remarks, name, date created and sender id from sent files table $return_sender = DB::table('sent_files') ->select('sent_files.sender_id','users.first_name as senderName','users.last_name as senderSurname') ->join('users','users.id',"=",'sent_files.sender_id') ->where('file_id',$id) ->latest('sent_files.created_at') ->first(); // get the sender id from the latest created file from sent files table return response()->json(["document"=>$document,"creator_name"=>$documentCreatorName,"receiver_id"=>$document_received_by_last_id,"user"=>$user, "remarks"=>$remarks, "return_sender"=>$return_sender]); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param \App\Models\Document $document * @return \Illuminate\Http\Response */ public function update(Request $request, Document $document) { // } /** * Remove the specified resource from storage. * * @param \App\Models\Document $document * @return \Illuminate\Http\Response */ public function destroy(Document $document) { // } }
| ver. 1.4 |
.
| PHP 8.1.32 | Generation time: 0 |
proxy
|
phpinfo
|
Settings