diff --git a/app/Console/Commands/ConvertUser.php b/app/Console/Commands/ConvertUser.php new file mode 100644 index 0000000..2420262 --- /dev/null +++ b/app/Console/Commands/ConvertUser.php @@ -0,0 +1,254 @@ +get(); + return $data; + } + + /** + * Insert a row in table users + */ + private function insertRowToUsers($data) + { + Database::table('users')->insertOrIgnore($data); + $lastUser = Database::table('users')->orderBy('id', 'DESC')->first(); + return $lastUser->id; + } + + + /** + * Insert to wmuser_common phone + */ + private function insertPhone($phones,$userId) + { + $phones = json_decode($phones); + + foreach($phones as $phone) + { + + Database::table('wmuser_common.phones')->insertOrIgnore([ + 'phonable_id' => $userId, + 'number' => $phone->tel, + 'label' => $phone->label, + 'phonable_type' => 'user', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + ]); + + } + + } + + + /** + * Insert address + */ + + private function InsertAddress($addressData,$tmp_id, $userId) + { + $userAddress = Database::select( + 'select * from `addresses` where addressable_id=? and addressable_type =?', + [ + $tmp_id, + 'App\TempBusiness' + ] + ); + + foreach ($userAddress as $address) + { + Database::table('wmuser_common.addresses')->insertOrIgnore([ + 'title' => $address->title, + 'city_id' => $addressData['city_id'], + 'district_id' => $addressData['district_id'], + 'postal_code' => $address->postal_code, + 'addressable_id' => $userId, + 'addressable_type' => 'user', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() + ]); + } + + + } + + + + /** + * insert Status to client status + * + */ + private function insertToClientStatuses($clientStatus) + { + + Database::table('wmuser_crm.client_statuses')->insertOrIgnore([ + 'name' => $clientStatus, + 'user_id' => 52, + 'business_id' => 3, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() + ]); + + $lastStatusid = Database::table('wmuser_crm.client_statuses')->orderBy('id', 'DESC')->first(); + + return $lastStatusid->id; + + } + + /** + * update user status + **/ + + private function updateToUserStatus($userId,$userStatusId) + { + Database::table('users') + ->where('id', $userId) + ->update(['user_status_id' => $userStatusId]); + + } + + /** + * Insert categories with Id + */ + private function categoriesConfig($categoryId, $client_id) + { + + $category = Database::table('categories')->where('id' , $categoryId)->get(); + + Database::table('wmuser_crm.client_categories')->insertOrIgnore([ + 'name_en' => $category[0]->name_en, + 'name' => $category[0]->name_fa, + 'parent_id' => $category[0]->parent_id, + 'business_id' => 3, + 'user_id' => $client_id, + 'level' => 1, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() + ]); + + $clientCategories = Database::table('wmuser_crm.client_categories')->latest()->first(); + + + Database::table('wmuser_crm.client_client_category')->insertOrIgnore([ + 'client_category_id' => $clientCategories->id, + 'client_id' => $client_id + + ]); + + + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + + $count = 0; + foreach($this->getBusinessRows() as $userData) + { + + $phones = json_decode($userData->phones); + + $cellPhone = null; + + for( $i=0; $i <= count($phones)-1 ; $i++ ) + { + if($phones[$i]->label == 'Mobile') + { + $cellPhone = $phones[$i]->tel; + } + } + + + $userTableDedails = [ + 'address' => [ + 'latitude' => $userData->latitude, + 'longitude' => $userData->longitude + ], + 'details' => json_decode($userData->details), + ]; + + + $userTableData = [ + 'name' => $userData->brand_fa, + 'business_id' => 3, + 'cell_number' => $cellPhone, + 'detail' => json_encode($userTableDedails), + 'instagram' => json_decode($userData->details)->InstagramID, + 'telegram' => json_decode($userData->details)->TelegramID, + 'email' => json_decode($userData->details)->Email, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now(), + 'deleted_at' => Carbon::now(), + 'user_status_id' => null, + ]; + + $userAddress = [ + 'district_id' => $userData->district_id, + 'city_id' => $userData->city_id + ]; + + + $newUserId = $this->insertRowToUsers($userTableData); + $this->categoriesConfig($userData->category_id, $newUserId); + $this->InsertAddress($userAddress, $userData->id,$newUserId); + $this->insertPhone($userData->phones , $newUserId); + $lastStatusId = $this->insertToClientStatuses($userData->status); + $this->updateToUserStatus($newUserId,$lastStatusId); + $count ++; + + $this->info('success transfer user : '. $newUserId . ' count of add : '.$count); + + } + + + + + } + +}