Add initial Code
This commit is contained in:
@@ -0,0 +1,142 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\User;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Invoice;
|
||||
use App\Models\Contact;
|
||||
use Carbon\Carbon;
|
||||
use App\Models\LineItem;
|
||||
use Database\Factories\CustomerFactory;
|
||||
|
||||
class TooloopSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$this->call([
|
||||
SettingsTableSeeder::class,
|
||||
]);
|
||||
|
||||
User::factory()->create([
|
||||
'name' => 'Daniel Stock',
|
||||
'email' => 'daniel.stock@tooloop.de',
|
||||
'password' => bcrypt('6T0az2JGO5oA'),
|
||||
]);
|
||||
|
||||
$csvFilePath = __DIR__ . '/Rechnungen.csv';
|
||||
|
||||
if (!file_exists($csvFilePath)) {
|
||||
$this->command->error("CSV file not found at: $csvFilePath");
|
||||
return;
|
||||
}
|
||||
|
||||
$csvFile = fopen($csvFilePath, 'r');
|
||||
|
||||
if ($csvFile === false) {
|
||||
$this->command->error("Failed to open CSV file at: $csvFilePath");
|
||||
return;
|
||||
}
|
||||
|
||||
$header = fgetcsv($csvFile, 1000, ';');
|
||||
|
||||
if ($header === false) {
|
||||
$this->command->error("Failed to read header from CSV file");
|
||||
fclose($csvFile);
|
||||
return;
|
||||
}
|
||||
|
||||
// Trim header values
|
||||
$header = array_map('trim', $header);
|
||||
|
||||
$this->command->info("CSV Header: " . implode(', ', $header));
|
||||
|
||||
// Create a new instance of the CustomerFactory
|
||||
$customerFactory = new CustomerFactory();
|
||||
|
||||
while (($data = fgetcsv($csvFile, 1000, ';')) !== false) {
|
||||
// Trim data values
|
||||
$data = array_map('trim', $data);
|
||||
|
||||
$row = array_combine($header, $data);
|
||||
|
||||
if ($row === false) {
|
||||
$this->command->error("Failed to combine header and data");
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->command->info("Row Data: " . implode(', ', $row));
|
||||
|
||||
// Generate a unique email for the customer
|
||||
$email = strtolower(str_replace(' ', '.', $row['Kunde'])) . '@example.com';
|
||||
|
||||
// Generate fake address data using the CustomerFactory
|
||||
$fakeAddress = $customerFactory->definition()['billing_address'];
|
||||
|
||||
// Create or find the customer
|
||||
$customer = Customer::firstOrCreate(
|
||||
['company_name' => $row['Kunde']],
|
||||
[
|
||||
'type' => 'business',
|
||||
'vat_id' => '',
|
||||
'tax_id' => '',
|
||||
'global_id' => '',
|
||||
'legal_registration_id' => '',
|
||||
'email' => $email,
|
||||
'phone' => '',
|
||||
'billing_address' => $fakeAddress,
|
||||
'payment_terms_id' => 3,
|
||||
'status' => 'active',
|
||||
'notes' => ''
|
||||
]
|
||||
);
|
||||
|
||||
// Check if the customer already has contacts
|
||||
if ($customer->contacts()->count() === 0) {
|
||||
// Create a primary contact for the customer
|
||||
Contact::factory()->create([
|
||||
'customer_id' => $customer->id,
|
||||
'is_primary' => true,
|
||||
]);
|
||||
|
||||
// Create additional contacts for the customer (1-2 additional contacts)
|
||||
Contact::factory(rand(1, 2))->create([
|
||||
'customer_id' => $customer->id,
|
||||
]);
|
||||
}
|
||||
|
||||
// Parse the date field
|
||||
$date = Carbon::createFromFormat('Y-m-d', $row['Gestellt'])->format('Y-m-d');
|
||||
|
||||
// Calculate the due date as 14 days after the date
|
||||
$dueDate = Carbon::createFromFormat('Y-m-d', $date)->addDays(14)->format('Y-m-d');
|
||||
|
||||
// Create the invoice
|
||||
$invoice = Invoice::create([
|
||||
'nr' => 'RE-' . $row['#'],
|
||||
|
||||
'invoice_date' => $date,
|
||||
'due_date' => $dueDate,
|
||||
'service_start_date' => $date,
|
||||
'service_end_date' => $date,
|
||||
'is_recurring' => false,
|
||||
'is_partial_service' => false,
|
||||
'customer_id' => $customer->id,
|
||||
'payment_status' => $row['Bezahlt'] === 'WAHR' ? 'paid' : ($row['Gemahnt'] ? 'reminded' : 'due'),
|
||||
'total_amount' => floatval(str_replace(['€', ','], ['', '.'], $row['Netto'])),
|
||||
'title' => $row['Betreff'],
|
||||
'text' => '',
|
||||
]);
|
||||
|
||||
LineItem::factory(rand(5, 10))->create([
|
||||
'invoice_id' => $invoice->id,
|
||||
]);
|
||||
}
|
||||
|
||||
fclose($csvFile);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user