middleware(function ($request, $next) { $this->account = auth()->guard('api')->user() ->account() ->with('profile') ->with('broker_applications') ->first(); return $next($request); }); } /** * Create new Legalization Application * @return BrokerApplicationResource|\Illuminate\Http\Response */ public function create() { //validate if profile is filled if(is_null($this->account) || is_null($this->account->profile)) { return response([ 'success' => false, 'message' => trans('app.application.fill_profile_message'), ],422); } //validate legalization number exists elseif(!$this->account->can_apply_broker && !$this->account->can_extend_broker){ $month = Config::get('settings.legalization_extend') ?? 1; return response([ 'success' => false, 'message' => trans('app.application.expire_message',['one'=>$month]) ],422); } //delete old application??? Should we replace it??? if($app = $this->account->last_broker_application()) { if($app->state != 'approved') return BrokerApplicationResource::make($app); else{ $app->state = 'archive'; $app->save(); } } //upload etmeli dokumentlaryn spisogy $documents = BrokerDocument::where($this->account->type,true)->get(); if($documents->count() == 0) { return response([ 'success' => false, 'message' => trans('application.list_not_found') ],404); } $application = BrokerApplication::create([ 'account_id' => $this->account->id, 'state' => 'draft' //default mysql value is new ]); foreach ($documents as $document){ $attachment = new BrokerAttachment([ 'name' => $document->name, 'broker_document_id' => $document->id ]); $application->broker_attachments()->save($attachment); } return BrokerApplicationResource::make($application); } public function get() { $application = $this->account ->broker_applications() ->latest() ->with('broker_attachments')->first(); if($application){ return BrokerApplicationResource::make($application); } return response()->json(['success' => false, 'message' => 'Not Found'], 404); } public function apply() { $app = $this->account ->broker_applications() ->whereIn('state',['draft','refine']) ->with('broker_attachments') ->latest() ->first(); if( $app ) { $app->state = 'new'; $app->save(); //todo send email to operators return response([ 'success' => true, 'message' => trans('app.application.app_success_message') ]); } return response([ 'success' => false, 'message' => trans('app.application.app_error_message') ],400); } public function upload(DocumentUploadRequest $request,$attachment_id) { $broker_attachment = BrokerAttachment::with(['broker_application','broker_document'])->find($attachment_id); if(!$broker_attachment || !$broker_attachment->broker_application || $broker_attachment->broker_application->account_id != $this->account->id ){ return response()->json(['success' => false, 'message' =>'Bad request'],400); } $uploadedFile = $request->file('file'); if($broker_attachment->broker_document->max_size != 0 && $uploadedFile->getSize() > $broker_attachment->broker_document->max_size * 1024){//max size in kilobytes return response()->json(['success' => false, 'message' =>trans('app.application.upload_max_size')],422); } $filename = Str::snake($broker_attachment->name).'.'.$uploadedFile->getClientOriginalExtension(); $directory = 'broker-documents/'.Carbon::today()->year.'/'.$this->account->id; $path = $uploadedFile->storePubliclyAs($directory,$filename); if(!$path){ return response()->json(['success' => false, 'message' =>trans('app.application.upload_failure')],400); } $broker_attachment->file = $directory.'/'.$filename; $broker_attachment->size = number_format($uploadedFile->getSize()/1024, 2, '.', ''); $broker_attachment->type = $uploadedFile->getClientOriginalExtension(); $broker_attachment->save(); return response()->json(['success' => true, 'message' =>trans('app.app.application.upload_success')]); //todo make attachment relation } public function downloadQuestionaire() { $headers = [ "Content-type"=>"text/html", "Content-Disposition"=>"attachment;Filename=myGeneratefile.doc" ]; $content = view('oprosniki.'.$this->account->type,$this->account->profile)->render(); return \Response::make($content,200, $headers); } }