Created
March 31, 2020 07:17
-
-
Save cygeorgel/7af0d111b8a8838fd2d45b847d638d50 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function handle() | |
{ | |
$filename = 'import/contacts.csv'; | |
if (Storage::disk('ftp')->exists($filename)) { | |
$content = Storage::disk('ftp')->get($filename); | |
$lines = explode("\n", $content); | |
$i = 0; | |
foreach ($lines as $line) { | |
$customer = null; | |
$values = str_getcsv($line, ',', ''); | |
// print_r ($values); | |
if ( ! empty ($values[6])) { | |
if (filter_var($values[6], FILTER_VALIDATE_EMAIL)) { | |
$control = User::where('email', $values[6])->count(); | |
if ( ! $control) { | |
$customer = Customer::where('name', $values[3])->first(); | |
if ( ! $customer) { | |
$customer = Customer::where('mainAddressLine1', $values[3])->first(); | |
} | |
if ( ! $customer) { | |
$genericDomains = [ | |
'orange.fr', | |
'gmail.com', | |
]; | |
$emailElements = explode('@', $values[6]); | |
if ( ! in_array ($emailElements[1], $genericDomains)) { | |
$user = User::where('email', 'like', '%' . $emailElements[1])->first(); | |
if ($user) { | |
$customer = $user->customers()->first(); | |
} | |
} | |
} | |
if ( ! $customer) { | |
$i++; | |
$this->warn('no customer for ' . $values[6] . ' ' . $i); | |
} else { | |
$user = User::create([ | |
'name' => $values[0], | |
'email' => $values[6], | |
'password' => str_random(40), | |
'active' => true, | |
'role' => 100, | |
'phone' => phoneNumberFormat($values[4]), | |
'mobilePhone' => phoneNumberFormat($values[5]), | |
'thirdParty' => 'Export Jérémy', | |
'thirdPartyId' => null, | |
'thirdPartyPeopleId' => null, | |
'thirdPartyCompanyId' => null, | |
'position' => null, | |
]); | |
$customer->users()->attach($user->id); | |
$this->info('get user ' . $values[6] . ' ok'); | |
} | |
} else { | |
$this->warn('contact ' . $values[6] . ' already exists'); | |
} | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment