/**
  * Store a newly created resource in storage.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function store(Request $request)
 {
     $info = $request->only(['detail', 'total_atentions']);
     $detail = $info['detail'];
     $input = $request->only(['patient_id', 'medic_id', 'total_atentions']);
     $patient = Patient::find($input['patient_id']);
     $discount = $patient->discount($input['total_atentions']);
     $total = $input['total_atentions'] - $discount;
     $input['total'] = $total;
     $input['discount'] = $discount;
     $input['company_id'] = $patient->Company->id;
     $input['status'] = "Pendiente";
     $input['user_id'] = $request->user()->id;
     $rules = ['patient_id' => 'required', 'medic_id' => 'required', 'total_atentions' => 'required'];
     $validation = \Validator::make($input, $rules);
     if ($validation->passes()) {
         //
         $budget = new Budget($input);
         $budget->save();
         $json = json_decode($detail);
         //return $json;
         foreach ($json as $key => $value) {
             $data_detail = ["price" => $value->valor, "budget_id" => $budget->id, "atention_id" => $value->id];
             //return $value->valor;
             $budgetDetail = new BudgetDetail($data_detail);
             $budgetDetail->save();
         }
         $respuesta = "Guardado";
         $numero = $budget->id;
         return response()->json(compact('respuesta', 'numero'));
     }
     $messages = $validation->errors();
     return response()->json($messages);
 }
 public function create(Request $request)
 {
     $customerId = $request->input('customer_id');
     $budgetType = $request->input('budget_type');
     $name = $request->input('name');
     $maxAmount = $request->input('max_amount');
     $budget = new Budget();
     $existingBudget = Budget::where(array('customer_id' => $customerId, 'name' => $name))->first();
     if (isset($existingBudget)) {
         return json_encode(array('message' => 'duplicate'));
     } else {
         $budget->customer_id = $customerId;
         $budget->name = $name;
         $budget->budget_type = $budgetType;
         $budget->max_amount = $maxAmount;
         $budget->remarks = '';
         // $request->input('remarks');
         $budget->status = 'active';
         if ($budgetType == "date range") {
             $budget->start_date = date('Y-m-d h:i:s', $request->input('start_date'));
             $budget->end_date = date('Y-m-d h:i:s', $request->input('end_date'));
         }
         $budget->created_at = date('Y-m-d h:i:s');
         $budget->updated_at = date('Y-m-d h:i:s');
         $budget->save();
         return json_encode(array('message' => 'done'));
     }
 }