Exemplo n.º 1
0
 /**
  * Get the validation rules that apply to the request.
  *
  * @return array
  */
 public function rules()
 {
     $validateData = ['datetime_input' => 'required|date_format:"' . config('format.input.datetime-local') . '"', 'crop_id' => 'required', 'work_diary_ids' => 'required', 'work_id' => 'required'];
     if (!empty($this->input('work_id'))) {
         $this->work = Work::findOrFail($this->input('work_id'));
         if ($this->work->use_seeding) {
             // 播種、定植の場合に追加で必須にする属性
             $validateData['cultivar_id'] = 'required';
             $validateData['intrarow_spacing'] = 'required|numeric|between:1,100';
         }
     }
     return $validateData;
 }
Exemplo n.º 2
0
 /**
  * 作成処理
  *
  * @param WorkRecordStoreRequest $request
  * @return \Illuminate\Http\RedirectResponse
  */
 public function store(WorkRecordStoreRequest $request)
 {
     $cropId = $request->input('crop_id');
     $errors = new MessageBag();
     DB::transaction(function () use($request, $cropId, &$errors) {
         $workId = $request->input('work_id');
         $workDiaryIds = (array) $request->input('work_diary_ids');
         $work = Work::findOrFail($workId);
         $workDiaries = WorkDiary::whereIn('id', $workDiaryIds)->lockForUpdate()->get();
         if ($workDiaries->count() !== $workDiaries->where('crop_id', $cropId)->where('archive', false)->count()) {
             // 不正な作業日誌が選択されている
             $errors->add('work_diary_ids', message('others_update'));
             DB::rollBack();
             return;
         }
         // 作業記録登録
         $workRecord = new WorkRecord();
         $workRecord->fill($request->all());
         $workRecord->work_id = $workId;
         $workRecord->crop_id = $cropId;
         // use_complete=falseの場合は常にtrue
         $workRecord->complete = !$work->use_complete || $request->has('complete');
         $workRecord->save();
         // 防除記録
         if ($work->use_pest_control) {
             $sessionPesticides = session()->get('workRecord.pesticides');
             $pesticideIds = $sessionPesticides->keys();
             $pesticides = Pesticide::whereIn('id', $pesticideIds)->whereHas('crops', function ($query) use($cropId) {
                 $query->where('crop_id', $cropId);
             });
             if ($pesticideIds->count() !== $pesticides->count()) {
                 // 農薬の選択が不正
                 $errors->add('pesticide', message('others_update'));
                 DB::rollBack();
                 return;
             }
             foreach ($sessionPesticides as $sessionPesticide) {
                 $workPestControl = new WorkPestControl();
                 $workPestControl->work_record_id = $workRecord->id;
                 $workPestControl->pesticide_id = $sessionPesticide->get('pesticide_id');
                 $workPestControl->usage = $sessionPesticide->get('usage');
                 $workPestControl->save();
             }
         }
         // 播種/定植記録
         if ($work->use_seeding) {
             $workSeeding = new WorkSeeding();
             $workSeeding->work_record_id = $workRecord->id;
             $workSeeding->cultivar_id = $request->input('cultivar_id');
             $workSeeding->fill($request->all());
             $workSeeding->save();
         }
         // 日誌紐付け
         $workRecord->workDiaries()->attach($workDiaryIds);
     });
     if ($errors->any()) {
         return $this->buildFailedValidationResponse($request, $errors->toArray());
     }
     // 農薬情報をクリア
     session()->forget('workRecord.pesticides');
     return redirect()->route('workRecord.index', ['crop_id' => $cropId])->with('complete', 'store');
 }