Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REFACTOR🔨 Update Invoice, InvoiceLines models behavior (soft delete, types, attributes) #11

Merged
merged 10 commits into from
Jan 26, 2024
27 changes: 17 additions & 10 deletions app/Models/Invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;

class Invoice extends Model
{
use HasFactory;
use HasFactory, SoftDeletes;

protected $fillable = [
'invoice_date',
];
protected $guarded = [];

public function invoiceLines()
public function customer() : BelongsTo
{
return $this->hasMany(InvoiceLine::class);
return $this->belongsTo(Customer::class);
}

public function customer()
public function invoiceLines() : HasMany
{
// return $this->belongsTo(Customer::class);
return $this->hasMany(InvoiceLine::class);
}

public function price() {
public function getTotalPriceInCentsAttribute() : int
{
return $this->invoiceLines->sum(function ($invoiceLine) {
return $invoiceLine->getTotal();
return $invoiceLine->totalPriceInCents;
});
}

public function getTotalPriceFormattedAttribute() : string
{
return number_format($this->getTotalPriceInCentsAttribute() / 100, 2);
}
}
20 changes: 9 additions & 11 deletions app/Models/InvoiceLine.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,27 @@

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

class InvoiceLine extends Model
{
use HasFactory;
use HasFactory, SoftDeletes;

protected $fillable = [
'invoice_id',
'product_id',
'quantity',
];
protected $guarded = [];

public function invoice()
public function invoice(): BelongsTo
{
return $this->belongsTo(Invoice::class);
}

public function getTotal()
public function getTotalPriceInCentsAttribute(): int
{
return $this->quantity * $this->price;
return $this->quantity * $this->product->price_in_cents;
}

public function getTotalFormatted()
public function getTotalPriceFormattedAttribute(): string
{
return number_format($this->total, 2);
return number_format($this->getTotalPriceInCentsAttribute() / 100, 2);
}
}
4 changes: 4 additions & 0 deletions database/factories/InvoiceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Models\Customer;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -16,7 +17,10 @@ class InvoiceFactory extends Factory
*/
public function definition(): array
{
$cutomers = Customer::all();

return [
'customer_id' => $cutomers->random()->id,
'invoice_date' => $this->faker->date(),
];
}
Expand Down
5 changes: 5 additions & 0 deletions database/factories/InvoiceLineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Database\Factories;

use App\Models\Product;
use Illuminate\Database\Eloquent\Factories\Factory;

/**
Expand All @@ -16,7 +17,11 @@ class InvoiceLineFactory extends Factory
*/
public function definition(): array
{

$products = Product::all();

return [
'product_id' => $products->random()->id,
'quantity' => $this->faker->numberBetween(1, 10),
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ public function up(): void
Schema::create('invoices', function (Blueprint $table) {
$table->id();
$table->date('invoice_date');
$table->foreignId('customer_id')->nullable();
$table->foreignId('customer_id');
$table->timestamps();
$table->softDeletes();
});
}

Expand Down
1 change: 1 addition & 0 deletions database/migrations/2024_01_26_091404_add_invoice_line.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function up(): void
$table->foreignId('product_id')->nullable();
$table->integer('quantity');
$table->timestamps();
$table->softDeletes();
});
}

Expand Down