diff --git a/src/backend/app/Models/Asset.php b/src/backend/app/Models/Asset.php index ef627353..2224a996 100644 --- a/src/backend/app/Models/Asset.php +++ b/src/backend/app/Models/Asset.php @@ -3,17 +3,21 @@ namespace App\Models; use App\Models\Base\BaseModel; +use Illuminate\Database\Eloquent\Factories\HasFactory; +use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasOne; class Asset extends BaseModel { + use HasFactory; + public const RELATION_NAME = 'appliance'; protected $table = 'assets'; - public function assetType(): HasOne + public function assetType(): BelongsTo { - return $this->hasOne(AssetType::class, 'id', 'asset_type_id'); + return $this->belongsTo(AssetType::class); } public function agentAssignedAppliance(): HasMany diff --git a/src/backend/app/Models/SolarHomeSystem.php b/src/backend/app/Models/SolarHomeSystem.php index a4aeea13..da5f3de9 100644 --- a/src/backend/app/Models/SolarHomeSystem.php +++ b/src/backend/app/Models/SolarHomeSystem.php @@ -3,11 +3,14 @@ namespace App\Models; use App\Models\Base\BaseModel; +use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\MorphOne; class SolarHomeSystem extends BaseModel { + use HasFactory; + public const RELATION_NAME = 'solar_home_system'; protected $table = 'solar_home_systems'; diff --git a/src/backend/database/factories/AssetFactory.php b/src/backend/database/factories/AssetFactory.php new file mode 100644 index 00000000..870a088d --- /dev/null +++ b/src/backend/database/factories/AssetFactory.php @@ -0,0 +1,24 @@ + $this->faker->word, + 'price' => $this->faker->randomDigitNotNull() * 100000, + ]; + } +} diff --git a/src/backend/database/factories/AssetTypeFactory.php b/src/backend/database/factories/AssetTypeFactory.php index 6e9d8b8a..b90b9722 100644 --- a/src/backend/database/factories/AssetTypeFactory.php +++ b/src/backend/database/factories/AssetTypeFactory.php @@ -18,7 +18,6 @@ public function definition() { return [ 'name' => $this->faker->word, - 'price' => $this->faker->randomFloat(2, 0, 100), ]; } } diff --git a/src/backend/database/factories/ManufacturerFactory.php b/src/backend/database/factories/ManufacturerFactory.php index f769ea25..36c654a4 100644 --- a/src/backend/database/factories/ManufacturerFactory.php +++ b/src/backend/database/factories/ManufacturerFactory.php @@ -24,6 +24,20 @@ public function isMeterManufacturer() }); } + /** + * Indicate that the manufacturer is for SHS devices. + * + * @return Factory + */ + public function isShsManufacturer() + { + return $this->state(function (array $attributes) { + return [ + 'type' => 'shs', + ]; + }); + } + public function definition(): array { $inflector = InflectorFactory::create()->build(); diff --git a/src/backend/database/factories/SolarHomeSystemFactory.php b/src/backend/database/factories/SolarHomeSystemFactory.php new file mode 100644 index 00000000..41667006 --- /dev/null +++ b/src/backend/database/factories/SolarHomeSystemFactory.php @@ -0,0 +1,20 @@ + 1, + 'serial_number' => $this->faker->unique()->uuid, + 'manufacturer_id' => 1, + ]; + } +} diff --git a/src/backend/database/seeders/DatabaseSeeder.php b/src/backend/database/seeders/DatabaseSeeder.php index 8385ef7b..ef2badd5 100644 --- a/src/backend/database/seeders/DatabaseSeeder.php +++ b/src/backend/database/seeders/DatabaseSeeder.php @@ -18,6 +18,7 @@ public function run() ClusterSeeder::class, CustomerSeeder::class, MeterSeeder::class, + SolarHomeSystemSeeder::class, TicketSeeder::class, AgentSeeder::class, ]); diff --git a/src/backend/database/seeders/SolarHomeSystemSeeder.php b/src/backend/database/seeders/SolarHomeSystemSeeder.php new file mode 100644 index 00000000..2c2b1c08 --- /dev/null +++ b/src/backend/database/seeders/SolarHomeSystemSeeder.php @@ -0,0 +1,80 @@ +databaseProxyManagerService->buildDatabaseConnectionDummyCompany(); + } + + /** + * Run the database seeds. + * + * @return void + */ + public function run() + { + // Manufacturer + // For now, we just adding some dummy Manufacturers. + // Later, this should probably be synced with the manufacturers + // for which we have plugins in the Demo setup. + $manufacturers = Manufacturer::factory() + ->count(2) + ->isShsManufacturer() + ->create(); + + // Get the SHS asset type + $assetType = AssetType::where('name', 'Solar Home System')->first(); + + // Create our appliances, i.e. sales deals (?) + $appliances = Asset::factory() + ->count(5) + ->for($assetType) + ->sequence( + // Thank you ChatGPT for generating these names... 🤖 + [ + 'name' => 'SunPower Home 3000', + 'price' => 500000, + ], + [ + 'name' => 'SunPower Home 1000', + 'price' => 100000, + ], + ['name' => 'EcoBright Solar Kit'], + ['name' => 'HelioVolt Pro Series'], + ['name' => 'SolaraMax Home Energy'], + ) + ->create(); + + // Create not-yet-sold SHS such that we can "sell" them in the Demo + for ($i = 1; $i <= 10; ++$i) { + $solarHomeSystem = SolarHomeSystem::factory() + ->for(Asset::all()->random(), 'appliance') + ->for(Manufacturer::where('type', 'shs')->get()->random()) + ->create(); + + $device = Device::factory() + ->for($solarHomeSystem, 'device') + ->create([ + 'device_serial' => $solarHomeSystem->serial_number, + ]); + } + + // Create already-sold SHS + $sold_solar_home_systems = null; + + // TBD: assign appliances to Agents + // This is required to use the Agent app. + } +}