diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index e793e96..5f4052c 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -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); + } } diff --git a/app/Models/InvoiceLine.php b/app/Models/InvoiceLine.php index 3f69790..15452b0 100644 --- a/app/Models/InvoiceLine.php +++ b/app/Models/InvoiceLine.php @@ -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); } } diff --git a/database/factories/InvoiceFactory.php b/database/factories/InvoiceFactory.php index 3edaad4..aeede9f 100644 --- a/database/factories/InvoiceFactory.php +++ b/database/factories/InvoiceFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Customer; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -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(), ]; } diff --git a/database/factories/InvoiceLineFactory.php b/database/factories/InvoiceLineFactory.php index 16c48d4..67d3b72 100644 --- a/database/factories/InvoiceLineFactory.php +++ b/database/factories/InvoiceLineFactory.php @@ -2,6 +2,7 @@ namespace Database\Factories; +use App\Models\Product; use Illuminate\Database\Eloquent\Factories\Factory; /** @@ -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), ]; } diff --git a/database/migrations/2024_01_26_090327_create_invoice_table.php b/database/migrations/2024_01_26_090327_create_invoice_table.php index 66d0144..ad33217 100644 --- a/database/migrations/2024_01_26_090327_create_invoice_table.php +++ b/database/migrations/2024_01_26_090327_create_invoice_table.php @@ -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(); }); } diff --git a/database/migrations/2024_01_26_091404_add_invoice_line.php b/database/migrations/2024_01_26_091404_add_invoice_line.php index 771ef12..e55a9dc 100644 --- a/database/migrations/2024_01_26_091404_add_invoice_line.php +++ b/database/migrations/2024_01_26_091404_add_invoice_line.php @@ -18,6 +18,7 @@ public function up(): void $table->foreignId('product_id')->nullable(); $table->integer('quantity'); $table->timestamps(); + $table->softDeletes(); }); }