edms2023/app/Http/Controllers/RemoteTransferController.php

284 lines
8.7 KiB
PHP
Raw Normal View History

2023-09-14 20:59:46 +00:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\RemoteTransfer;
use App\WorkflowDocument;
use App\WorkflowDocumentSender;
use App\WorkflowDocumentFile;
use App\RemoteContact;
use App\Contact;
use Session;
class RemoteTransferController extends Controller
{
/**
* private filter function
* @return RemoteTransfer model
*/
private function search($str, $direction=2)
{
$a = new RemoteTransfer;
if(strlen(trim($str)) > 0)
{
$a = $a->where(function($query) use ($str){
$query->orWhere('login', 'LIKE', '%'. $str .'%')
->orWhere('receipt', 'LIKE', '%'. $str .'%')
->orWhere('content', 'LIKE', '%'. $str .'%')
->orWhere('description', 'LIKE', '%'. $str .'%')
->orWhereHas('contact', function($query) use ($str){
$query->where('organization_name', 'LIKE', '%'. $str .'%');
});;
});
}
if($direction != 2)
return $a->where('direction', $direction);
return $a;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
if(!in_array(12, auth()->user()->getPermissionList()))
{
\Session::flash('error_message', 'no permission');
return redirect()->back();
}
$search = $request->input('search');
$direction = $request->input('direction') ?? 2;
$row = $this->search($search, $direction);
$row = $row->orderBy('id', 'desc')->with('contact:id,organization_name')->paginate(30)->appends([
'search' => $search,
'direction' => $direction,
]);
return view('remote_transfers.index')->with('transfers', $row)->with('search', $search)->with('direction', $direction);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create($id=null)
{
if(!$id || !in_array(13, auth()->user()->getPermissionList()))
{
\Session::flash('error_message', 'no permission');
return redirect()->back();
}
$this->store($id);
return redirect()->route('transfers.index');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store($id)
{
if($id == null)
return back();
$sender = null;
$document = WorkflowDocument::findOrFail(base64_decode($id));
$senders = WorkflowDocumentSender::where('workflow_document_id', $document->id)->get();
foreach($senders as $sender)
{
$remotecontact = RemoteContact::where('archive', 0)->where('direction', 1)->where('contact_id', $sender->contact_id)->whereHas('remotecontactapis')->with('remotecontactapis')->first();
if(!$remotecontact)
continue;
$file = WorkflowDocumentFile::where('workflow_document_id', $document->id)->get()->last();
//fill mandatory fields
$content = [];
$fields = $remotecontact->remotecontactapis->first()->mandatory_fields;
$fields = array_map('trim', explode(',', $fields));
foreach ($fields as $field)
{
$pair = array_map('trim', explode(':', $field));
$key = $pair[0] ?? '';
$value = ($pair[1] == 'file') ? 'file' : $document->{$pair[1]};
if ($value == null || strlen($value) == 0 || $value == 'file')
continue;
$content[$key] = $value;
}
$remotetransfer = new RemoteTransfer;
$data = [
'login' => $remotecontact->login,
'workflow_document_id' => $document->id,
'direction' => 1,
'status' => 0,
'send_at' => now(),
'content' => json_encode($content),
'place_of_the_documents' => $file->place_of_the_documents,
'document_name' => $file->document_name,
'contact_id' => $remotecontact->contact_id,
];
$remotetransfer->fill($data);
$remotetransfer->save();
}
return redirect()->route('transfers.index');
}
public function send(RemoteTransfer $remotetransfer)
{
if(!in_array(13, auth()->user()->getPermissionList()))
{
\Session::flash('error_message', 'no permission');
return redirect()->back();
}
if($remotetransfer->status == 0)
{
$remotetransfer->status = 1;
$remotetransfer->save();
}
// call send Api
app('App\Http\Controllers\ApiController')->sendDocuments($remotetransfer->id);
return back();
}
public function cancel(RemoteTransfer $remotetransfer)
{
if(!in_array(13, auth()->user()->getPermissionList()))
{
\Session::flash('error_message', 'no permission');
return redirect()->back();
}
if(in_array($remotetransfer->status, ['default', 'in process']))
{
$remotetransfer->delete();
}
return back();
}
public function accept(RemoteTransfer $remotetransfer)
{
if(!in_array(13, auth()->user()->getPermissionList()))
{
\Session::flash('error_message', 'no permission');
return redirect()->back();
}
$contact = Contact::where('id', $remotetransfer->contact_id)->first();
if(!$contact)
{
\Session::flash('error_message', 'Contact not found');
return redirect()->back();
}
$data['registration_number'] = json_decode($remotetransfer->content)->registration_number;
$data['registration_date'] = json_decode($remotetransfer->content)->registration_date;
$data['topic'] = json_decode($remotetransfer->content)->topic;
$data['contact_id'] = $remotetransfer->contact_id;
$data['path'] = $remotetransfer->place_of_the_documents . '/' . $remotetransfer->document_name;
$data['file'] = $remotetransfer->document_name;
$remotetransfer->read_at = now();
$remotetransfer->save();
return redirect(route('create_document_workflow', $data));
// return redirect(app('App\Http\Controllers\WorkflowDocumentsController')->create("aman"));
// dd($remotetransfer);
// dd(json_decode($remotetransfer->content));
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($base_id)
{
$id=base64_decode($base_id);
$remotetransfer = RemoteTransfer::find($id);
if($remotetransfer)
{
if (\File::exists($remotetransfer->place_of_the_documents . '/' . pathinfo($remotetransfer->document_name, PATHINFO_FILENAME) . '.pdf'))
$link = asset($remotetransfer->place_of_the_documents.'/'. pathinfo($remotetransfer->document_name, PATHINFO_FILENAME) . '.pdf');
else
$link = asset($remotetransfer->place_of_the_documents.'/'. $remotetransfer->document_name);
if($link)
{
$notifications_update = auth()->user()->unreadNotifications()->where('workflow_document_id', $id)->update(['read_at'=>now()->format('Y-m-d H:i:s')]);
if($notifications_update){
if(Session::has('unread_notification_number')){
$notification_number = Session::get('unread_notification_number');
Session::put('unread_notification_number', $notification_number-1);
}
}
return redirect($link);
}
}
return back();
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}