123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\Validator;
- use Tymon\JWTAuth\Exceptions\JWTException;
- use Illuminate\Support\Facades\Storage;
- use Illuminate\Support\Facades\Input;
- use JWTAuth;
- use App\User;
- use App\Company;
- use App\CompanyLanguage;
- use App\CompanyAdmin;
- use App\CompanyBranch;
- use App\CompanyBranchUser;
- use DB;
- class CompanyController extends Controller
- {
- public function companies_list ($current_page, $per_page, $type ,$key = null)
- {
- $current_page = $current_page - 1 ;
- $companies = DB::table('companies');
- // if ($type!='all') {
- // $companies->where('type',$type);
- // }
- if (isset($key) && $key != null) {
- $companies->where(function ($query) use ($key){
- $query->where('name','like',"%$key%");
- // ->orWhere('email','like',"%$key%");
- // ->orWhere('phone','like',"%$key%");
- });
- }
- $companies = $companies->whereNull('deleted_at');
- $count = $companies->count();
- $companies = $companies->skip($per_page*$current_page)->limit($per_page)->orderBy('id','DESC')->get();
- return response()->json(compact('companies','count','per_page'));
- }
- public function register_company (Request $request)
- {
- $validator = Validator::make($request->all(), [
- 'company_name' => 'required|string|max:255',
- 'access_of_multiple_sites' => 'required| numeric',
- 'phone' => 'required|string|max:255|unique:companies',
- 'commercial_number' => 'required|string|max:255|unique:companies',
- 'industrial' => 'required|string|max:255',
- 'website_address' => 'required|string|max:255',
- 'address' => 'required|string|max:255',
- 'total_users_request' => 'required|numeric',
- 'http_host_demo' => 'required|string|max:255',
- 'version_number' => 'required|string|max:255',
- 'theme_id' => 'required|numeric',
- 'font_id' => 'required|numeric',
- 'register_date' => 'required',
- 'expire_date' => 'required',
- 'languages_ids.*' => 'required|numeric',
- 'company_admins' => 'required',
- 'company_admins.*.name' => 'required|string|max:255',
- 'company_admins.*.email' => 'required|string|email|max:255|unique:users',
- 'company_admins.*.password' => 'required|string|min:6|confirmed',
- 'company_branches' => 'required',
- 'company_branches.*.branch_name' => 'required',
- 'company_branches.*.branch_users.*.name' => 'required|string|max:255',
- 'company_branches.*.branch_users.*.email' => 'required|string|email|max:255|unique:users',
- 'company_branches.*.branch_users.*.password' => 'required|string|min:6|confirmed',
- 'company_branches.*.branch_users.*.type' =>
- 'required|string|in:manger_authority_person,decision_maker,supervisor,employee',
- 'company_branches.*.branch_users.*.position' => 'required|string|max:255',
- 'company_branches.*.branch_users.*.phone' => 'required|string|max:255',
- 'company_branches.*.branch_users.*.hire_date' => 'required|string|max:255',
- 'company_branches.*.branch_users.*.is_active' => 'required|in:0,1',
- ]);
- // check duplicates in company admins
- $emails[] = array_column($request['company_admins'], 'email');
- // check duplicates in branches users
- $branch_users = array_column($request['company_branches'], 'branch_users');
- $branch_users_emails = call_user_func_array('array_merge', $branch_users);
- $emails[] = array_column($branch_users_emails, 'email');
- $emails = call_user_func_array('array_merge', $emails);
- //combine all emails
- $emails = app('App\Http\Controllers\HelperController')
- ->display_duplicated_emails($emails);
- if(!empty($emails)){
- return $emails;
- }
- if($request['expire_date'] < $request['register_date']){
- return response()->json(['error' => 'register_date can not be greater than expire_date'],400);
- }
- if($request['access_of_multiple_sites'] < sizeof($request['company_branches']) ){
- return response()->json(['error' => 'company_branches can not be greater than access_of_multiple_sites'],400);
- }
- if($validator->fails()){
- $error = $validator->errors();
- return response()->json(compact('error'),400);
- }
- if (!empty($request['logo_photo'])) {
- if(empty($request['logo_photo_type'])){
- return response()->json(['error' => 'logo_photo_type is required .'], 400);
- }
- $logo_photo = app('App\Http\Controllers\HelperController')
- ->upload_single_photo($request['logo_photo'], $request['logo_photo_type']);
- }else{
- $logo_photo = NULL;
- }
- if (!empty($request['back_ground_photo'])) {
- if(empty($request['back_ground_photo_type'])){
- return response()->json(['error' => 'back_ground_photo_type is required .'], 400);
- }
- $back_ground_photo = app('App\Http\Controllers\HelperController')
- ->upload_single_photo($request['back_ground_photo'], $request['back_ground_photo_type']);
- }else{
- $back_ground_photo = NULL;
- }
- //create company ..
- $company = Company::create([
- 'name' => $request['company_name'],
- 'access_of_multiple_sites' => $request['access_of_multiple_sites'],
- 'phone' => $request['phone'],
- 'register_date' => $request['register_date'],
- 'expire_date' => $request['expire_date'],
- 'commercial_number' => $request['commercial_number'],
- 'industrial' => $request['industrial'],
- 'website_address' => $request['website_address'],
- 'address' => $request['address'],
- 'total_users_request' => $request['total_users_request'],
- 'http_host_demo' => $request['http_host_demo'],
- 'version_number' => $request['version_number'],
- 'theme_id' => $request['theme_id'],
- 'font_id' => $request['font_id'],
- 'logo' => $logo_photo,
- 'back_ground_photo' => $back_ground_photo,
- ]);
- //create company languages
- foreach ($request['languages_ids'] as $key => $lang_id) {
- $lang = CompanyLanguage::create([
- 'company_id' => $company->id,
- 'language_id' => $lang_id,
- ]);
- }
- //create admins and make relation with company ..
- foreach ($request['company_admins'] as $key => $admin) {
- if (!empty($admin['photo'])) {
- if(empty($admin['photo_type'])){
- return response()->json(['error' => 'photo_type is required .'], 400);
- }
- $photo = app('App\Http\Controllers\HelperController')
- ->upload_single_photo($admin['photo'], $admin['photo_type']);
- }else{
- $photo = NULL;
- }
- $new_admin = User::create([
- 'name' => $admin['name'],
- 'email' => $admin['email'],
- 'photo' => $photo,
- 'type' => 'company_admin',
- 'password' => Hash::make($admin['password']),
- ]);
- $company_admin = CompanyAdmin::create([
- 'company_id' => $company->id,
- 'admin_id' => $new_admin->id
- ]);
- }
- // create branches and admins for company_id
- foreach ($request['company_branches'] as $key => $branch) {
- $new_branch = CompanyBranch::create([
- 'name' => $branch['branch_name'],
- 'company_id' => $company->id,
- ]);
- foreach ($branch['branch_users'] as $key => $branch_user) {
- if (!empty($branch_user['photo'])) {
- if(empty($branch_user['photo_type'])){
- return response()->json(['error' => 'photo_type is required .'], 400);
- }
- $photo = app('App\Http\Controllers\HelperController')
- ->upload_single_photo($branch_user['photo'], $branch_user['photo_type']);
- }else{
- $photo = NULL;
- }
- $new_branch_user = User::create([
- 'name' => $branch_user['name'],
- 'password' => Hash::make($branch_user['password']),
- 'email' => $branch_user['email'],
- 'photo' => $photo,
- 'type' => $branch_user['type'],
- 'position' => $branch_user['position'],
- 'phone' => $branch_user['phone'],
- 'hire_date' => $branch_user['hire_date'],
- 'is_active' => $branch_user['is_active'],
- ]);
- $company_branch_admin = CompanyBranchUser::create([
- 'branch_id' => $new_branch->id,
- 'user_id' => $new_branch_user->id
- ]);
- }
- }
- return response()->json(compact('company'),201);
- }
- public function delete_current_companies (Request $request)
- {
- $validator = Validator::make($request->all(), [
- "companies_id.*" => "required|integer"
- ]);
- if($validator->fails()){
- $error = $validator->errors();
- return response()->json(compact('error'),400);
- }
- foreach ($request['companies_id'] as $key => $id) {
- $company = Company::find($id)->delete();
- }
- return response()->json(['status' => 'done'], 204);
- }
- public function handle_csv_file (Request $request)
- {
- $file = $request['csv_file'];
- $customerArr = app('App\Http\Controllers\HelperController')->csvToArray($file);
- $emails = array_column($customerArr['users'], 'email');
- $duplicates = array_diff_key($emails, array_unique($emails));
- if(!empty($duplicates)){
- $error = [];
- foreach ($duplicates as $key => $duplicate) {
- $error[] = $duplicate ." has already duplicate in the file";
- }
- return response()->json(compact('error'),400);
- }
- $validator = Validator::make($customerArr, [
- 'users.*.email' => 'required|string|email|max:255|unique:users',
- ]);
- if($validator->fails()){
- $error = $validator->errors();
- return response()->json(compact('error'),400);
- }
- return $customerArr;
- }
- } //class
|