public static function getDatatable() { $id = Session::get('user_id'); $first_day = date("Y-m-01"); $last_day = date("Y-m-t"); $today = date("Y-m-d"); /* MONTH user consumption*/ $monthUserConsumption = WaterRegister::where('state', '=', 'Mendoza')->where('user_id', '=', $id)->whereBetween('date', [$first_day, $last_day])->sum('value'); /* MONTH city consumption total and average lts/hour*/ $user = User::select(['city_id'])->with('state', 'city')->where('id', $id)->first(); $cityConsumption = WaterRegister::select(DB::raw('sum(value) as total, avg(value) as avg'))->where('city', '=', $user->city->name)->whereBetween('date', [$first_day, $last_day])->groupBy('city')->get(); $cityTotalConsumption = $cityConsumption[0]->total; $cityAverage = $cityConsumption[0]->avg; /*current consumption and liters per hour (DAY)*/ $consumption = WaterRegister::where('date', '=', $today)->where('user_id', '=', $id)->sum('value'); $consumptionPerHour = $consumption / 60; /*water price*/ $waterPrice = WaterPrice::select('price')->active()->get(); /*water bill*/ $price = $waterPrice[0]->price; $liters = WaterRegister::where('date', '=', $today)->where('user_id', '=', $id)->sum('value'); $bill = $liters * $price; $aux = new stdClass(); $aux->monthUserConsumption = $monthUserConsumption; $aux->cityTotalConsumption = $cityTotalConsumption; $aux->cityAverage = $cityAverage; $aux->consumptionPerHour = $consumptionPerHour; $aux->consumption = $consumption; $aux->waterPrice = $price; $aux->bill = $bill; $data = [collect($aux)]; $datatable = Datatables::of(collect($data)); return $datatable->make(true); }
public static function delete(Request $request, $id) { $price = WaterPrice::find($id); if (!$price) { return redirect()->back()->withErrors('El registro seleccionado no existe'); } $price->delete(); if ($request->ajax()) { return response()->json(['code' => 200]); } else { return redirect()->route('water-prices.index')->with('success', 'El registro se ha eliminado correctamente'); } }
/** * Run the database seeds. * * @return void */ public function run() { $faker = Faker::create(); $date = date("Y-m-d H:m:s"); $waterprice = WaterPrice::create(['description' => 'febrero', 'price' => $faker->randomFloat($nbMaxDecimals = 4, $min = 0, $max = 1), 'created_at' => $date, 'updated_at' => $date, 'active' => 1]); }