From e2a013f9673b12fa3156fa134033a6a0d844f07b Mon Sep 17 00:00:00 2001 From: fvaldes0109 Date: Fri, 26 Jan 2024 11:48:43 +0100 Subject: [PATCH 1/9] Implement customer relationship in Invoice model and factory --- app/Models/Invoice.php | 2 +- database/factories/InvoiceFactory.php | 4 ++++ .../migrations/2024_01_26_090327_create_invoice_table.php | 2 +- database/seeders/DatabaseSeeder.php | 2 +- 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index e793e96..80e1351 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -20,7 +20,7 @@ public function invoiceLines() public function customer() { - // return $this->belongsTo(Customer::class); + return $this->belongsTo(Customer::class); } public function price() { 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/migrations/2024_01_26_090327_create_invoice_table.php b/database/migrations/2024_01_26_090327_create_invoice_table.php index 66d0144..36208bc 100644 --- a/database/migrations/2024_01_26_090327_create_invoice_table.php +++ b/database/migrations/2024_01_26_090327_create_invoice_table.php @@ -14,7 +14,7 @@ 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(); }); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 135143b..15c9354 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -50,6 +50,6 @@ public function run(): void }); - + } } From 086b48540d816af9f023be5936b990f95d06080f Mon Sep 17 00:00:00 2001 From: fvaldes0109 Date: Fri, 26 Jan 2024 11:50:55 +0100 Subject: [PATCH 2/9] Add soft deletes to Invoice model and migration --- app/Models/Invoice.php | 15 +++++++-------- .../2024_01_26_090327_create_invoice_table.php | 1 + 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 80e1351..9181a58 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -4,23 +4,22 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +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() { - return $this->hasMany(InvoiceLine::class); + return $this->belongsTo(Customer::class); } - public function customer() + public function invoiceLines() { - return $this->belongsTo(Customer::class); + return $this->hasMany(InvoiceLine::class); } public function price() { 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 36208bc..ad33217 100644 --- a/database/migrations/2024_01_26_090327_create_invoice_table.php +++ b/database/migrations/2024_01_26_090327_create_invoice_table.php @@ -16,6 +16,7 @@ public function up(): void $table->date('invoice_date'); $table->foreignId('customer_id'); $table->timestamps(); + $table->softDeletes(); }); } From c2dc85d8975a42fe6f9b07f0d448025f3df5a79b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20David=20Su=C3=A1rez=20Rodr=C3=ADguez?= Date: Fri, 26 Jan 2024 11:48:42 +0100 Subject: [PATCH 3/9] refactor: update attributes & add return types --- app/Models/InvoiceLine.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/Models/InvoiceLine.php b/app/Models/InvoiceLine.php index 3f69790..68021a8 100644 --- a/app/Models/InvoiceLine.php +++ b/app/Models/InvoiceLine.php @@ -4,6 +4,7 @@ use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\Relations\BelongsTo; class InvoiceLine extends Model { @@ -15,18 +16,18 @@ class InvoiceLine extends Model 'quantity', ]; - 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 getTotalPriceInCentsFormattedAttribute(): string { - return number_format($this->total, 2); + return number_format($this->getTotalPriceInCentsAttribute() / 100, 2); } } From 6424fbefbdcbf4617c446d9c0a8d908c50409468 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20David=20Su=C3=A1rez=20Rodr=C3=ADguez?= Date: Fri, 26 Jan 2024 11:49:24 +0100 Subject: [PATCH 4/9] feat: add soft deletes to `InvoiceLine` --- app/Models/InvoiceLine.php | 3 ++- database/migrations/2024_01_26_091404_add_invoice_line.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Models/InvoiceLine.php b/app/Models/InvoiceLine.php index 68021a8..1907843 100644 --- a/app/Models/InvoiceLine.php +++ b/app/Models/InvoiceLine.php @@ -5,10 +5,11 @@ 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', 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(); }); } From 12c5d48b9a87f70f96e46033c0cf4cda31d8447d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20David=20Su=C3=A1rez=20Rodr=C3=ADguez?= Date: Fri, 26 Jan 2024 11:51:23 +0100 Subject: [PATCH 5/9] refactor: change conditions from `$fillable` to `$guarded` --- app/Models/InvoiceLine.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/app/Models/InvoiceLine.php b/app/Models/InvoiceLine.php index 1907843..d348846 100644 --- a/app/Models/InvoiceLine.php +++ b/app/Models/InvoiceLine.php @@ -11,11 +11,7 @@ class InvoiceLine extends Model { use HasFactory, SoftDeletes; - protected $fillable = [ - 'invoice_id', - 'product_id', - 'quantity', - ]; + protected $guarded = []; public function invoice(): BelongsTo { From 69a0e1b437ce3f8e8e493fddf47a66a26cb2562e Mon Sep 17 00:00:00 2001 From: fvaldes0109 Date: Fri, 26 Jan 2024 11:52:46 +0100 Subject: [PATCH 6/9] Add return types to Invoice model --- app/Models/Invoice.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 9181a58..81e5c37 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -4,6 +4,8 @@ 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 @@ -12,17 +14,18 @@ class Invoice extends Model protected $guarded = []; - public function customer() + public function customer() : BelongsTo { return $this->belongsTo(Customer::class); } - public function invoiceLines() + public function invoiceLines() : HasMany { return $this->hasMany(InvoiceLine::class); } - public function price() { + public function price() : int + { return $this->invoiceLines->sum(function ($invoiceLine) { return $invoiceLine->getTotal(); }); From b19c38e5010dd90416f256cebaaeb93ae6c555d3 Mon Sep 17 00:00:00 2001 From: fvaldes0109 Date: Fri, 26 Jan 2024 11:54:32 +0100 Subject: [PATCH 7/9] Refactor Invoice model to calculate total price in cents and provide a formatted version --- app/Models/Invoice.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 81e5c37..3ed9ce6 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -24,10 +24,15 @@ public function invoiceLines() : HasMany return $this->hasMany(InvoiceLine::class); } - public function price() : int + public function getTotalPriceInCentsAttribute() : int { return $this->invoiceLines->sum(function ($invoiceLine) { - return $invoiceLine->getTotal(); + return $invoiceLine->totalPriceInCents; }); } + + public function getTotalPriceInCentsFormattedAttribute() : string + { + return number_format($this->getTotalPriceInCentsAttribute() / 100, 2); + } } From 956f0adc92838148bb9451065d910b3ed6c5fc02 Mon Sep 17 00:00:00 2001 From: fvaldes0109 Date: Fri, 26 Jan 2024 11:59:16 +0100 Subject: [PATCH 8/9] Add random product selection in InvoiceLineFactory --- database/factories/InvoiceLineFactory.php | 5 +++++ 1 file changed, 5 insertions(+) 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), ]; } From f7d836cd154b17dd7dddc53d7c7d20db83e9662a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20David=20Su=C3=A1rez=20Rodr=C3=ADguez?= Date: Fri, 26 Jan 2024 12:05:13 +0100 Subject: [PATCH 9/9] refactor: fix formatted attribute name --- app/Models/Invoice.php | 2 +- app/Models/InvoiceLine.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/Models/Invoice.php b/app/Models/Invoice.php index 3ed9ce6..5f4052c 100644 --- a/app/Models/Invoice.php +++ b/app/Models/Invoice.php @@ -31,7 +31,7 @@ public function getTotalPriceInCentsAttribute() : int }); } - public function getTotalPriceInCentsFormattedAttribute() : string + public function getTotalPriceFormattedAttribute() : string { return number_format($this->getTotalPriceInCentsAttribute() / 100, 2); } diff --git a/app/Models/InvoiceLine.php b/app/Models/InvoiceLine.php index d348846..15452b0 100644 --- a/app/Models/InvoiceLine.php +++ b/app/Models/InvoiceLine.php @@ -23,7 +23,7 @@ public function getTotalPriceInCentsAttribute(): int return $this->quantity * $this->product->price_in_cents; } - public function getTotalPriceInCentsFormattedAttribute(): string + public function getTotalPriceFormattedAttribute(): string { return number_format($this->getTotalPriceInCentsAttribute() / 100, 2); }