Skip to content

Commit 34b7c7d

Browse files
authored
Merge pull request #1
Add vehicle and fuel management
2 parents b85cd89 + 99957b8 commit 34b7c7d

28 files changed

+880
-18
lines changed

app/Livewire/CreateFuelEntry.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Livewire\FuelEntryForm;
6+
use Livewire\Component;
7+
use App\Models\Vehicle;
8+
use Illuminate\Support\Number;
9+
10+
class CreateFuelEntry extends Component
11+
{
12+
public FuelEntryForm $form;
13+
public Vehicle $vehicle;
14+
15+
public function updated($property)
16+
{
17+
// If the fuel amount is changed, update the total cost using the price per unit
18+
if ($property === 'form.fuel_amount' && $this->form->price_per_unit && $this->form->fuel_amount) {
19+
$this->form->total_cost = Number::format($this->form->price_per_unit * $this->form->fuel_amount, 2);
20+
}
21+
// If the price per unit is changed, update the total cost using the fuel amount
22+
if ($property === 'form.price_per_unit' && $this->form->price_per_unit > 0) {
23+
$this->form->total_cost = Number::format($this->form->price_per_unit * $this->form->fuel_amount, 2);
24+
}
25+
// If the total cost is changed, update the fuel amount using the price per unit
26+
if ($property === 'form.total_cost' && $this->form->price_per_unit > 0) {
27+
$this->form->fuel_amount = Number::format($this->form->total_cost / $this->form->price_per_unit, 2);
28+
}
29+
}
30+
public function render()
31+
{
32+
return view('livewire.create-fuel-entry');
33+
}
34+
35+
public function create()
36+
{
37+
$this->form->setVehicle($this->vehicle);
38+
$this->form->store();
39+
40+
$this->redirect(route('fuel-entries.index', ['vehicle' => $this->vehicle->id]));
41+
}
42+
}

app/Livewire/CreateVehicle.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Livewire\VehicleForm;
6+
use App\Models\Vehicle;
7+
use Livewire\Attributes\Url;
8+
use Livewire\Component;
9+
10+
class CreateVehicle extends Component
11+
{
12+
public VehicleForm $form;
13+
14+
public ?Vehicle $vehicle = null;
15+
16+
public function render()
17+
{
18+
return view('livewire.create-vehicle');
19+
}
20+
21+
public function create()
22+
{
23+
$this->form->store();
24+
25+
$this->redirect(route('vehicles.index'));
26+
}
27+
}

app/Livewire/EditFuelEntry.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Models\FuelEntry;
6+
use App\Models\Vehicle;
7+
use Livewire\Component;
8+
use App\Livewire\FuelEntryForm;
9+
use Illuminate\Support\Number;
10+
11+
class EditFuelEntry extends Component
12+
{
13+
public FuelEntryForm $form;
14+
public Vehicle $vehicle;
15+
16+
public function mount(Vehicle $vehicle, FuelEntry $fuelEntry)
17+
{
18+
$this->vehicle = $vehicle;
19+
$this->form->setVehicle($vehicle);
20+
$this->form->setFuelEntry($fuelEntry);
21+
}
22+
23+
public function update()
24+
{
25+
$this->form->update();
26+
27+
$this->redirect(route('fuel-entries.index', $this->vehicle));
28+
}
29+
30+
public function delete()
31+
{
32+
$this->form->delete();
33+
34+
$this->redirect(route('fuel-entries.index', $this->vehicle));
35+
}
36+
37+
public function updated($property)
38+
{
39+
// If the fuel amount is changed, update the total cost using the price per unit
40+
if ($property === 'form.fuel_amount' && $this->form->price_per_unit && $this->form->fuel_amount) {
41+
$this->form->total_cost = Number::format($this->form->price_per_unit * $this->form->fuel_amount, 2);
42+
}
43+
// If the price per unit is changed, update the total cost using the fuel amount
44+
if ($property === 'form.price_per_unit' && $this->form->price_per_unit > 0) {
45+
$this->form->total_cost = Number::format($this->form->price_per_unit * $this->form->fuel_amount, 2);
46+
}
47+
// If the total cost is changed, update the fuel amount using the price per unit
48+
if ($property === 'form.total_cost' && $this->form->price_per_unit > 0) {
49+
$this->form->fuel_amount = Number::format($this->form->total_cost / $this->form->price_per_unit, 2);
50+
}
51+
}
52+
53+
public function render()
54+
{
55+
return view('livewire.edit-fuel-entry');
56+
}
57+
}

app/Livewire/EditVehicle.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Livewire\VehicleForm;
6+
use App\Models\Vehicle;
7+
use Livewire\Component;
8+
9+
class EditVehicle extends Component
10+
{
11+
public VehicleForm $form;
12+
13+
public function mount(Vehicle $vehicle)
14+
{
15+
$this->form->setVehicle($vehicle);
16+
}
17+
18+
public function update()
19+
{
20+
$this->form->update();
21+
22+
$this->redirect(route('vehicles.index'));
23+
}
24+
25+
public function delete()
26+
{
27+
$this->form->delete();
28+
29+
$this->redirect(route('vehicles.index'));
30+
}
31+
32+
public function render()
33+
{
34+
return view('livewire.edit-vehicle');
35+
}
36+
}

app/Livewire/FuelEntries.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Models\Vehicle;
6+
use Livewire\Component;
7+
8+
class FuelEntries extends Component
9+
{
10+
public Vehicle $vehicle;
11+
public $fuelEntries;
12+
13+
public function mount(Vehicle $vehicle)
14+
{
15+
$this->vehicle = $vehicle;
16+
$this->fuelEntries = $vehicle->fuelEntries()->latest()->get();
17+
}
18+
19+
20+
public function render()
21+
{
22+
return view('livewire.fuel-entries');
23+
}
24+
}

app/Livewire/FuelEntryForm.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace App\Livewire;
4+
5+
use App\Models\FuelEntry;
6+
use App\Models\Vehicle;
7+
use Illuminate\Support\Carbon;
8+
use Illuminate\Validation\Rule;
9+
use Livewire\Form;
10+
use Illuminate\Support\Facades\Auth;
11+
12+
class FuelEntryForm extends Form
13+
{
14+
public ?FuelEntry $fuelEntry = null;
15+
public ?Vehicle $vehicle = null;
16+
17+
public ?Carbon $date = null;
18+
public ?float $odometer = null;
19+
public ?float $fuel_amount = null;
20+
public ?float $price_per_unit = null;
21+
public ?float $total_cost = null;
22+
23+
public function rules(): array
24+
{
25+
$rules = [
26+
'date' => ['required', 'date'],
27+
'odometer' => [
28+
'required',
29+
'numeric',
30+
'min:0',
31+
],
32+
'fuel_amount' => ['required', 'numeric', 'min:0'],
33+
'price_per_unit' => ['required', 'numeric', 'min:0'],
34+
'total_cost' => ['required', 'numeric', 'min:0'],
35+
];
36+
37+
// When creating a new fuel entry, add a rule to check if the odometer reading is greater than the previous reading
38+
if ($this->fuelEntry == null) {
39+
$rules['odometer'][] =function ($attribute, $value, $fail) {
40+
if ($this->vehicle && $this->vehicle->lastOdometer >= $value) {
41+
$fail('The odometer reading must be greater than the previous reading of ' . $this->vehicle->lastOdometer . ' ' . $this->vehicle->distance_units);
42+
}
43+
};
44+
} else {
45+
// When updating a fuel entry, add a rule to check if the odometer reading is greater than the previous reading
46+
$rules['odometer'][] = function ($attribute, $value, $fail) {
47+
if ($value <= $this->fuelEntry->previousOdometer) {
48+
$fail('The odometer reading must be greater than the previous reading of ' . $this->fuelEntry->previousOdometer . ' ' . $this->vehicle->distance_units);
49+
}
50+
};
51+
52+
// Also, add a rule to check if the odometer reading is less than the next reading if it exists
53+
if ($this->fuelEntry->nextFuelEntry) {
54+
$rules['odometer'][] =function ($attribute, $value, $fail) {
55+
if ($value >= $this->fuelEntry->nextOdometer) {
56+
$fail('The odometer reading must be less than the next reading of ' . $this->fuelEntry->nextOdometer . ' ' . $this->vehicle->distance_units);
57+
}
58+
};
59+
}
60+
}
61+
62+
return $rules;
63+
}
64+
65+
public function setVehicle(Vehicle $vehicle): void
66+
{
67+
$this->vehicle = $vehicle;
68+
}
69+
70+
public function setFuelEntry(FuelEntry $fuelEntry): void
71+
{
72+
$this->fuelEntry = $fuelEntry;
73+
$this->date = $fuelEntry->date;
74+
// If the distance units of the vehicle are km, convert the fuel entry odometer from miles to kilometers
75+
if ($this->vehicle->distance_units === 'km') {
76+
$this->odometer = ceil($fuelEntry->odometer * 1.60934);
77+
} else {
78+
$this->odometer = $fuelEntry->odometer;
79+
}
80+
81+
// If the volume units of the user are liters, convert the fuel entry fuel amount from gallons to liters
82+
if (Auth::user()->volume_units === 'l') {
83+
$this->fuel_amount = ceil($this->fuelEntry->fuelAmountConverted);
84+
} else {
85+
$this->fuel_amount = $fuelEntry->fuel_amount;
86+
}
87+
$this->price_per_unit = $fuelEntry->price_per_unit;
88+
$this->total_cost = $fuelEntry->total_cost;
89+
}
90+
91+
public function store(): void
92+
{
93+
$this->validate();
94+
95+
// Before storing the fuel entry, if the distance units of the vehicle are km, convert the fuel
96+
// entry odometer from kilometers to miles
97+
if ($this->vehicle->distance_units === 'km') {
98+
$this->odometer = $this->odometer * 0.621371;
99+
}
100+
101+
// Before storing the fuel entry, if the volume unites of the user are l, convert the fuel entry
102+
// fuel amount from liters to gallons
103+
if (Auth::user()->volume_units === 'l') {
104+
$this->fuel_amount = $this->fuel_amount * 0.264172;
105+
}
106+
107+
$this->vehicle->fuelEntries()->create($this->all());
108+
109+
$this->reset();
110+
}
111+
112+
public function update(): void
113+
{
114+
if (!$this->fuelEntry) {
115+
return;
116+
}
117+
118+
$this->validate();
119+
120+
// Before updating the fuel entry, if the distance units of the vehicle are km, convert the fuel
121+
// entry odometer from kilometers to miles
122+
if ($this->vehicle->distance_units === 'km') {
123+
$this->odometer = $this->odometer * 0.621371;
124+
}
125+
126+
// Before updating the fuel entry, if the volume unites of the user are l, convert the fuel entry
127+
// fuel amount from liters to gallons
128+
if (Auth::user()->volume_units === 'l') {
129+
$this->fuel_amount = $this->fuel_amount * 0.264172;
130+
}
131+
$this->fuelEntry->update($this->all());
132+
133+
$this->reset();
134+
}
135+
136+
public function delete(): void
137+
{
138+
$this->fuelEntry->delete();
139+
140+
$this->reset();
141+
}
142+
}

0 commit comments

Comments
 (0)