CompanyController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use Illuminate\Support\Facades\Hash;
  5. use Illuminate\Support\Facades\Validator;
  6. use Tymon\JWTAuth\Exceptions\JWTException;
  7. use Illuminate\Support\Facades\Storage;
  8. use Illuminate\Support\Facades\Input;
  9. use JWTAuth;
  10. use App\User;
  11. use App\Company;
  12. use App\CompanyLanguage;
  13. use App\CompanyAdmin;
  14. use App\CompanyBranch;
  15. use App\CompanyBranchUser;
  16. use DB;
  17. class CompanyController extends Controller
  18. {
  19. public function companies_list ($current_page, $per_page, $type ,$key = null)
  20. {
  21. $current_page = $current_page - 1 ;
  22. $companies = DB::table('companies');
  23. // if ($type!='all') {
  24. // $companies->where('type',$type);
  25. // }
  26. if (isset($key) && $key != null) {
  27. $companies->where(function ($query) use ($key){
  28. $query->where('name','like',"%$key%");
  29. // ->orWhere('email','like',"%$key%");
  30. // ->orWhere('phone','like',"%$key%");
  31. });
  32. }
  33. $companies = $companies->whereNull('deleted_at');
  34. $count = $companies->count();
  35. $companies = $companies->skip($per_page*$current_page)->limit($per_page)->orderBy('id','DESC')->get();
  36. return response()->json(compact('companies','count','per_page'));
  37. }
  38. public function register_company (Request $request)
  39. {
  40. $validator = Validator::make($request->all(), [
  41. 'company_name' => 'required|string|max:255',
  42. 'access_of_multiple_sites' => 'required| numeric',
  43. 'phone' => 'required|string|max:255|unique:companies',
  44. 'commercial_number' => 'required|string|max:255|unique:companies',
  45. 'industrial' => 'required|string|max:255',
  46. 'website_address' => 'required|string|max:255',
  47. 'address' => 'required|string|max:255',
  48. 'total_users_request' => 'required|numeric',
  49. 'http_host_demo' => 'required|string|max:255',
  50. 'version_number' => 'required|string|max:255',
  51. 'theme_id' => 'required|numeric',
  52. 'font_id' => 'required|numeric',
  53. 'register_date' => 'required',
  54. 'expire_date' => 'required',
  55. 'languages_ids.*' => 'required|numeric',
  56. 'company_admins' => 'required',
  57. 'company_admins.*.name' => 'required|string|max:255',
  58. 'company_admins.*.email' => 'required|string|email|max:255|unique:users',
  59. 'company_admins.*.password' => 'required|string|min:6|confirmed',
  60. 'company_branches' => 'required',
  61. 'company_branches.*.branch_name' => 'required',
  62. 'company_branches.*.branch_users.*.name' => 'required|string|max:255',
  63. 'company_branches.*.branch_users.*.email' => 'required|string|email|max:255|unique:users',
  64. 'company_branches.*.branch_users.*.password' => 'required|string|min:6|confirmed',
  65. 'company_branches.*.branch_users.*.type' =>
  66. 'required|string|in:manger_authority_person,decision_maker,supervisor,employee',
  67. 'company_branches.*.branch_users.*.position' => 'required|string|max:255',
  68. 'company_branches.*.branch_users.*.phone' => 'required|string|max:255',
  69. 'company_branches.*.branch_users.*.hire_date' => 'required|string|max:255',
  70. 'company_branches.*.branch_users.*.is_active' => 'required|in:0,1',
  71. ]);
  72. // check duplicates in company admins
  73. $emails[] = array_column($request['company_admins'], 'email');
  74. // check duplicates in branches users
  75. $branch_users = array_column($request['company_branches'], 'branch_users');
  76. $branch_users_emails = call_user_func_array('array_merge', $branch_users);
  77. $emails[] = array_column($branch_users_emails, 'email');
  78. $emails = call_user_func_array('array_merge', $emails);
  79. //combine all emails
  80. $emails = app('App\Http\Controllers\HelperController')
  81. ->display_duplicated_emails($emails);
  82. if(!empty($emails)){
  83. return $emails;
  84. }
  85. if($request['expire_date'] < $request['register_date']){
  86. return response()->json(['error' => 'register_date can not be greater than expire_date'],400);
  87. }
  88. if($request['access_of_multiple_sites'] < sizeof($request['company_branches']) ){
  89. return response()->json(['error' => 'company_branches can not be greater than access_of_multiple_sites'],400);
  90. }
  91. if($validator->fails()){
  92. $error = $validator->errors();
  93. return response()->json(compact('error'),400);
  94. }
  95. if (!empty($request['logo_photo'])) {
  96. if(empty($request['logo_photo_type'])){
  97. return response()->json(['error' => 'logo_photo_type is required .'], 400);
  98. }
  99. $logo_photo = app('App\Http\Controllers\HelperController')
  100. ->upload_single_photo($request['logo_photo'], $request['logo_photo_type']);
  101. }else{
  102. $logo_photo = NULL;
  103. }
  104. if (!empty($request['back_ground_photo'])) {
  105. if(empty($request['back_ground_photo_type'])){
  106. return response()->json(['error' => 'back_ground_photo_type is required .'], 400);
  107. }
  108. $back_ground_photo = app('App\Http\Controllers\HelperController')
  109. ->upload_single_photo($request['back_ground_photo'], $request['back_ground_photo_type']);
  110. }else{
  111. $back_ground_photo = NULL;
  112. }
  113. //create company ..
  114. $company = Company::create([
  115. 'name' => $request['company_name'],
  116. 'access_of_multiple_sites' => $request['access_of_multiple_sites'],
  117. 'phone' => $request['phone'],
  118. 'register_date' => $request['register_date'],
  119. 'expire_date' => $request['expire_date'],
  120. 'commercial_number' => $request['commercial_number'],
  121. 'industrial' => $request['industrial'],
  122. 'website_address' => $request['website_address'],
  123. 'address' => $request['address'],
  124. 'total_users_request' => $request['total_users_request'],
  125. 'http_host_demo' => $request['http_host_demo'],
  126. 'version_number' => $request['version_number'],
  127. 'theme_id' => $request['theme_id'],
  128. 'font_id' => $request['font_id'],
  129. 'logo' => $logo_photo,
  130. 'back_ground_photo' => $back_ground_photo,
  131. ]);
  132. //create company languages
  133. foreach ($request['languages_ids'] as $key => $lang_id) {
  134. $lang = CompanyLanguage::create([
  135. 'company_id' => $company->id,
  136. 'language_id' => $lang_id,
  137. ]);
  138. }
  139. //create admins and make relation with company ..
  140. foreach ($request['company_admins'] as $key => $admin) {
  141. if (!empty($admin['photo'])) {
  142. if(empty($admin['photo_type'])){
  143. return response()->json(['error' => 'photo_type is required .'], 400);
  144. }
  145. $photo = app('App\Http\Controllers\HelperController')
  146. ->upload_single_photo($admin['photo'], $admin['photo_type']);
  147. }else{
  148. $photo = NULL;
  149. }
  150. $new_admin = User::create([
  151. 'name' => $admin['name'],
  152. 'email' => $admin['email'],
  153. 'photo' => $photo,
  154. 'type' => 'company_admin',
  155. 'password' => Hash::make($admin['password']),
  156. ]);
  157. $company_admin = CompanyAdmin::create([
  158. 'company_id' => $company->id,
  159. 'admin_id' => $new_admin->id
  160. ]);
  161. }
  162. // create branches and admins for company_id
  163. foreach ($request['company_branches'] as $key => $branch) {
  164. $new_branch = CompanyBranch::create([
  165. 'name' => $branch['branch_name'],
  166. 'company_id' => $company->id,
  167. ]);
  168. foreach ($branch['branch_users'] as $key => $branch_user) {
  169. if (!empty($branch_user['photo'])) {
  170. if(empty($branch_user['photo_type'])){
  171. return response()->json(['error' => 'photo_type is required .'], 400);
  172. }
  173. $photo = app('App\Http\Controllers\HelperController')
  174. ->upload_single_photo($branch_user['photo'], $branch_user['photo_type']);
  175. }else{
  176. $photo = NULL;
  177. }
  178. $new_branch_user = User::create([
  179. 'name' => $branch_user['name'],
  180. 'password' => Hash::make($branch_user['password']),
  181. 'email' => $branch_user['email'],
  182. 'photo' => $photo,
  183. 'type' => $branch_user['type'],
  184. 'position' => $branch_user['position'],
  185. 'phone' => $branch_user['phone'],
  186. 'hire_date' => $branch_user['hire_date'],
  187. 'is_active' => $branch_user['is_active'],
  188. ]);
  189. $company_branch_admin = CompanyBranchUser::create([
  190. 'branch_id' => $new_branch->id,
  191. 'user_id' => $new_branch_user->id
  192. ]);
  193. }
  194. }
  195. return response()->json(compact('company'),201);
  196. }
  197. public function delete_current_companies (Request $request)
  198. {
  199. $validator = Validator::make($request->all(), [
  200. "companies_id.*" => "required|integer"
  201. ]);
  202. if($validator->fails()){
  203. $error = $validator->errors();
  204. return response()->json(compact('error'),400);
  205. }
  206. foreach ($request['companies_id'] as $key => $id) {
  207. $company = Company::find($id)->delete();
  208. }
  209. return response()->json(['status' => 'done'], 204);
  210. }
  211. public function handle_csv_file (Request $request)
  212. {
  213. $file = $request['csv_file'];
  214. $customerArr = app('App\Http\Controllers\HelperController')->csvToArray($file);
  215. $emails = array_column($customerArr['users'], 'email');
  216. $duplicates = array_diff_key($emails, array_unique($emails));
  217. if(!empty($duplicates)){
  218. $error = [];
  219. foreach ($duplicates as $key => $duplicate) {
  220. $error[] = $duplicate ." has already duplicate in the file";
  221. }
  222. return response()->json(compact('error'),400);
  223. }
  224. $validator = Validator::make($customerArr, [
  225. 'users.*.email' => 'required|string|email|max:255|unique:users',
  226. ]);
  227. if($validator->fails()){
  228. $error = $validator->errors();
  229. return response()->json(compact('error'),400);
  230. }
  231. return $customerArr;
  232. }
  233. } //class