Beispiel #1
0
 public function delete_destroy($object_id = false)
 {
     $donor = Donor::find($object_id);
     $donor->delete();
     return Redirect::to_action('donors@index');
 }
 /**
  * Remove the specified resource from storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function destroy($id)
 {
     Donor::find($id)->delete();
     Session::flash('success', 'Successfully deleted donor!');
     return Redirect::back();
 }
 /**
  * Store a newly created resource in storage.
  *
  * @return Response
  */
 public function store()
 {
     /*$validator = $this->validate();
     
     		if($validator->fails()){
     			return Redirect::back()
     					->withErrors($validator->messages())
     					->withInput(Input::all());	
     		}*/
     $validator = $this->validate();
     if ($validator->fails()) {
         if (Request::ajax()) {
             return Response::json(array('success' => false, 'messages' => $validator->messages()));
         } else {
             return Redirect::back()->withErrors($validator->messages())->withInput(Input::all());
         }
     }
     $donor = Donor::find(Input::get('donor_id'));
     $product = Product::find(Input::get('product_id'));
     // Donor can only give blood of it's own blood type
     if (strtolower($product->category->name) == 'blood') {
         if (strtolower($donor->blood_group->name) != strtolower($product->name)) {
             //apply ajax
             if (Request::ajax()) {
                 return Response::json(array('success' => false, 'messages' => ['This donor is only able to donate blood of type ' . $donor->blood_group->name . ' only.']));
             } else {
                 return Redirect::back()->withErrors(['This donor is only able to donate blood of type ' . $donor->blood_group->name . ' only.'])->withInput(Input::all());
             }
         }
     }
     // Check if donor is eligible to make the donation
     if (count($donor->donations)) {
         // Check if donor is donating within the validity period
         $validity_days = $product->validity_period;
         $last_donated_at = new Carbon\Carbon($donor->donations->last()->donated_at);
         $next_possible_donation_date = $last_donated_at->addDays($validity_days);
         $today = Carbon\Carbon::now();
         if ($next_possible_donation_date->gt($today)) {
             //if ajax request nam return response.
             if (Request::ajax()) {
                 return Response::json(array('success' => false, 'messages' => ['This donor is not able to donate untill ' . $next_possible_donation_date->format('Y-m-d') . '.']));
             } else {
                 return Redirect::back()->withErrors(['This donor is not able to donate untill ' . $next_possible_donation_date->format('Y-m-d') . '.'])->withInput(Input::all());
             }
         }
     }
     DB::beginTransaction();
     $donation = new Donation();
     $donation->donor_id = Input::get('donor_id');
     $donation->product_id = Input::get('product_id');
     $donation->location_id = Input::get('location_id');
     $donation->quantity = Input::get('quantity');
     $donation->donated_at = Input::get('donated_at');
     $donation->save();
     /*if (Request::ajax()){
     			return Response::json(array(
     				'success' => true,
     				'donors' => [''] + Donor::all()->lists('name_with_blood_group', 'id'),
     				'donor_id' => $donor->id,
     			));
     		}*/
     // Increment product quantity
     /*$product = $donation->product;
     		$product->quantity = $product->quantity + $donation->quantity;
     		$product->save();*/
     // increment count in item_in table
     // increment count in location_products table
     $response = Event::fire('donation.create', array($donation));
     DB::commit();
     if (Request::ajax()) {
         return Response::json(array('success' => true, 'donors' => [''] + Donor::all()->lists('name_with_blood_group', 'id'), 'donor_id' => $donor->id));
     } else {
         Session::flash('success', 'Successfully created donation!');
         return Redirect::route('donation.index');
     }
 }