浏览代码

add db seeder

3.x
wallo 2 年前
父节点
当前提交
69da68073d

+ 1
- 7
app/Filament/Company/Pages/Setting/Invoice.php 查看文件

@@ -2,11 +2,7 @@
2 2
 
3 3
 namespace App\Filament\Company\Pages\Setting;
4 4
 
5
-use App\Enums\DocumentAmountColumn;
6
-use App\Enums\DocumentItemColumn;
7
-use App\Enums\DocumentPriceColumn;
8 5
 use App\Enums\DocumentType;
9
-use App\Enums\DocumentUnitColumn;
10 6
 use App\Enums\Font;
11 7
 use App\Enums\PaymentTerms;
12 8
 use App\Enums\Template;
@@ -18,7 +14,6 @@ use Filament\Forms\Components\ColorPicker;
18 14
 use Filament\Forms\Components\Component;
19 15
 use Filament\Forms\Components\FileUpload;
20 16
 use Filament\Forms\Components\Group;
21
-use Filament\Forms\Components\Radio;
22 17
 use Filament\Forms\Components\Section;
23 18
 use Filament\Forms\Components\Select;
24 19
 use Filament\Forms\Components\Textarea;
@@ -26,14 +21,12 @@ use Filament\Forms\Components\TextInput;
26 21
 use Filament\Forms\Components\ViewField;
27 22
 use Filament\Forms\Form;
28 23
 use Filament\Forms\Get;
29
-use Filament\Forms\Set;
30 24
 use Filament\Notifications\Notification;
31 25
 use Filament\Pages\Concerns\InteractsWithFormActions;
32 26
 use Filament\Pages\Page;
33 27
 use Filament\Support\Exceptions\Halt;
34 28
 use Illuminate\Auth\Access\AuthorizationException;
35 29
 use Illuminate\Database\Eloquent\Model;
36
-use Livewire\Attributes\Locked;
37 30
 use function Filament\authorize;
38 31
 
39 32
 /**
@@ -241,6 +234,7 @@ class Invoice extends Page
241 234
                             ),
242 235
                         Select::make('template')
243 236
                             ->label('Template')
237
+                            ->native(false)
244 238
                             ->options(Template::class)
245 239
                             ->required(),
246 240
                         Select::make('item_name.option')

+ 12
- 0
app/Models/Setting/CompanyProfile.php 查看文件

@@ -63,6 +63,18 @@ class CompanyProfile extends Model
63 63
         return country($this->country)->getName();
64 64
     }
65 65
 
66
+    public static function getAvailableCountryCodes(): array
67
+    {
68
+        $allCountries = countries();
69
+        $codes = [];
70
+
71
+        foreach ($allCountries as $code => $country) {
72
+            $codes[] = $code;
73
+        }
74
+
75
+        return $codes;
76
+    }
77
+
66 78
     public static function getAvailableCountryOptions(): array
67 79
     {
68 80
         $allCountries = countries();

+ 26
- 2
database/factories/Setting/CompanyProfileFactory.php 查看文件

@@ -2,13 +2,20 @@
2 2
 
3 3
 namespace Database\Factories\Setting;
4 4
 
5
+use App\Enums\EntityType;
6
+use App\Models\Setting\CompanyProfile;
5 7
 use Illuminate\Database\Eloquent\Factories\Factory;
6 8
 
7 9
 /**
8
- * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Setting\CompanyProfile>
10
+ * @extends Factory<CompanyProfile>
9 11
  */
10 12
 class CompanyProfileFactory extends Factory
11 13
 {
14
+    /**
15
+     * @var string The related model's name.
16
+     */
17
+    protected $model = CompanyProfile::class;
18
+
12 19
     /**
13 20
      * Define the model's default state.
14 21
      *
@@ -17,7 +24,24 @@ class CompanyProfileFactory extends Factory
17 24
     public function definition(): array
18 25
     {
19 26
         return [
20
-            //
27
+            'address' => $this->faker->streetAddress,
28
+            'city' => $this->faker->city,
29
+            'zip_code' => $this->faker->postcode,
30
+            'country' => $this->faker->randomElement(CompanyProfile::getAvailableCountryCodes()),
31
+            'phone_number' => $this->faker->e164PhoneNumber,
32
+            'email' => $this->faker->email,
33
+            'entity_type' => $this->faker->randomElement(EntityType::class),
34
+            'fiscal_year_start' => (new \DateTime('first day of January'))->format('Y-m-d'),
35
+            'fiscal_year_end' => (new \DateTime('last day of December'))->format('Y-m-d'),
21 36
         ];
22 37
     }
38
+
39
+    public function configure(): static
40
+    {
41
+        return $this->afterCreating(function (CompanyProfile $companyProfile) {
42
+            $companyProfile->timezone = $this->faker->randomElement(CompanyProfile::getTimezoneOptions($companyProfile->country));
43
+            $companyProfile->state = $this->faker->randomElement(CompanyProfile::getStateOptions($companyProfile->country));
44
+            $companyProfile->save();
45
+        });
46
+    }
23 47
 }

+ 2
- 0
database/factories/UserFactory.php 查看文件

@@ -3,6 +3,7 @@
3 3
 namespace Database\Factories;
4 4
 
5 5
 use App\Models\Company;
6
+use App\Models\Setting\CompanyProfile;
6 7
 use App\Models\User;
7 8
 use Illuminate\Database\Eloquent\Factories\Factory;
8 9
 use Illuminate\Support\Str;
@@ -58,6 +59,7 @@ class UserFactory extends Factory
58 59
 
59 60
         return $this->has(
60 61
             Company::factory()
62
+                ->has(CompanyProfile::factory(), 'profile')
61 63
                 ->state(function (array $attributes, User $user) {
62 64
                     return ['name' => $user->name.'\'s Company', 'user_id' => $user->id, 'personal_company' => true];
63 65
                 }),

+ 87
- 5
database/seeders/DatabaseSeeder.php 查看文件

@@ -16,11 +16,93 @@ class DatabaseSeeder extends Seeder
16 16
      */
17 17
     public function run(): void
18 18
     {
19
-        // \App\Models\User::factory(10)->create();
19
+        $startDate = today()->startOfYear();
20
+        $endDate = today();
20 21
 
21
-        // \App\Models\User::factory()->create([
22
-        //     'name' => 'Test User',
23
-        //     'email' => 'test@example.com',
24
-        // ]);
22
+        // Change Company Name to ERPSAAS after Login
23
+        $firstCompanyOwner = User::factory()
24
+            ->withPersonalCompany()
25
+            ->create([
26
+                'name' => 'Admin',
27
+                'email' => 'admin@gmail.com',
28
+                'password' => bcrypt('password'),
29
+                'current_company_id' => 1,
30
+                'created_at' => $startDate->copy(),
31
+            ]);
32
+
33
+        $firstCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->copy()]);
34
+
35
+        // Function to create employees for a company (also creates companies for the employees)
36
+        $createUsers = static function ($company_id, $userCount, $minPercentage, $maxPercentage) use ($endDate, $startDate) {
37
+            $users = User::factory($userCount)
38
+                ->withPersonalCompany()
39
+                ->create([
40
+                    'password' => bcrypt('password'),
41
+                    'current_company_id' => $company_id,
42
+                ]);
43
+
44
+            $dateRange = $endDate->diffInMinutes($startDate);
45
+            $minOffset = (int) ($dateRange * $minPercentage);
46
+            $maxOffset = (int) ($dateRange * $maxPercentage);
47
+
48
+            for ($i = 0; $i < $userCount; $i++) {
49
+                $increment = (int) ($minOffset + ($i * ($maxOffset - $minOffset) / $userCount));
50
+                $userCreatedAt = $startDate->copy()->addMinutes($increment);
51
+
52
+                $user = $users[$i];
53
+
54
+                // Randomly assign a role to the user
55
+                $roles = ['editor', 'admin'];
56
+                $role = $roles[array_rand($roles)];
57
+
58
+                $user->companies()->attach($company_id, compact('role'));
59
+
60
+                $user->update(['created_at' => $userCreatedAt]);
61
+                $user->ownedCompanies->first()?->update(['created_at' => $userCreatedAt]);
62
+
63
+                // Generate random created_at date for the company_user pivot table (for employees)
64
+                $user->companies->first()?->users()->updateExistingPivot($user->id, ['created_at' => $userCreatedAt]);
65
+            }
66
+        };
67
+
68
+        // Users for the first company (excluding the first company owner)
69
+        $createUsers(1, 5, 0, 0.1);
70
+
71
+        // Second company owner
72
+        $secondCompanyOwner = User::factory()
73
+            ->withPersonalCompany()
74
+            ->create([
75
+                'password' => bcrypt('admin2'),
76
+                'current_company_id' => 2,
77
+                'created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.1),
78
+            ]);
79
+
80
+        $secondCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.1)]);
81
+
82
+        // Users for the second company (excluding the second company owner)
83
+        $createUsers(2, 5, 0.1, 0.2);
84
+
85
+        // Third company owner
86
+        $thirdCompanyOwner = User::factory()
87
+            ->withPersonalCompany()
88
+            ->create([
89
+                'password' => bcrypt('admin3'),
90
+                'current_company_id' => 3,
91
+                'created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.2),
92
+            ]);
93
+
94
+        $thirdCompanyOwner->ownedCompanies->first()->update(['created_at' => $startDate->addMinutes($endDate->diffInMinutes($startDate) * 0.2)]);
95
+
96
+        // Users for the third company (excluding the third company owner)
97
+        $createUsers(3, 5, 0.2, 0.3);
98
+
99
+        // Create employees for each company (each employee has a company)
100
+        $createUsers(4, 5, 0.3, 0.4);
101
+        $createUsers(5, 5, 0.4, 0.5);
102
+        $createUsers(6, 5, 0.5, 0.6);
103
+        $createUsers(7, 5, 0.6, 0.7);
104
+        $createUsers(8, 5, 0.7, 0.8);
105
+        $createUsers(9, 5, 0.8, 0.9);
106
+        $createUsers(10, 5, 0.9, 1);
25 107
     }
26 108
 }

+ 1
- 1
package-lock.json 查看文件

@@ -1,5 +1,5 @@
1 1
 {
2
-    "name": "erpsaas2",
2
+    "name": "erpsaas",
3 3
     "lockfileVersion": 3,
4 4
     "requires": true,
5 5
     "packages": {

正在加载...
取消
保存