TaskController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use App\Http\Controllers\Controller;
  4. use App\Http\Requests;
  5. use App\Models\ActivityLog;
  6. use App\Models\AssignProject;
  7. use App\Models\Comment;
  8. use App\Models\CompanyActivity;
  9. use App\Models\CompanyModel;
  10. use App\Models\Group;
  11. use App\Models\Notification;
  12. use App\Models\Project;
  13. use App\Models\Task;
  14. use App\User;
  15. use Auth;
  16. use DateTime;
  17. use Illuminate\Http\Request;
  18. use Mail;
  19. class TaskController extends Controller
  20. {
  21. public function index(){
  22. $data['title'] = "All Task";
  23. $data['projectss'] = Project::all();
  24. $data['groups'] = Group::where('user_id',0)->get();
  25. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  26. $data['tasks'] = Task::with('proj_name')->orderBy('created_time')->paginate(25);
  27. return view('admin.task.task',$data);
  28. /*
  29. 1=Created;
  30. 2=in process;
  31. 3=Completed;
  32. 4=checking;
  33. 5=bugfixing;
  34. 6=done;
  35. */
  36. }
  37. public function deadlineToday(){
  38. $data['title'] = "Deadline Today";
  39. $data['projectss'] = Project::all();
  40. $data['groups'] = Group::where('user_id',0)->get();
  41. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  42. $data['today_tasks'] = Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status','!=',6)->whereDate('dead_line', '=', date('Y-m-d'))->orderBy('created_time')->paginate(25);
  43. return view('admin.task.today_task',$data);
  44. }
  45. public function deadlineMissed(){
  46. $data['title'] = "Deadline Missed";
  47. $data['projectss'] = Project::all();
  48. $data['groups'] = Group::where('user_id',0)->get();
  49. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  50. $data['deadline_missed'] = Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status','!=',6)->whereDate('dead_line', '<', date('Y-m-d'))->orderBy('created_time')->paginate(25);
  51. return view('admin.task.deadline_missed',$data);
  52. }
  53. public function completedToday(){
  54. $data['title'] = "Completed Today";
  55. $data['projectss'] = Project::all();
  56. $data['groups'] = Group::where('user_id',0)->get();
  57. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  58. $data['completed_today'] = Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status','=',6)->whereDate('closing_time', '=', date('Y-m-d'))->orderBy('created_time')->paginate(25);
  59. return view('admin.task.completed_today',$data);
  60. }
  61. public function completedYesterday(){
  62. $data['title'] = "Completed Yesterday";
  63. $data['projectss'] = Project::all();
  64. $data['groups'] = Group::where('user_id',0)->get();
  65. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  66. $data['completed_yesterday'] = Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status',6)->whereDate('closing_time', '=', date('Y-m-d'))->orderBy('created_time')->paginate(25);
  67. return view('admin.task.completed_yesterday',$data);
  68. }
  69. public function pendingTask(){
  70. $data['title'] = "Pending Task";
  71. $data['projectss'] = Project::all();
  72. $data['groups'] = Group::where('user_id',0)->get();
  73. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  74. $data['pending_task'] = Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status','!=',6)->orderBy('created_time')->paginate(25);
  75. return view('admin.task.pending_task',$data);
  76. }
  77. public function inProcess(){
  78. $data['title'] = "In Process";
  79. $data['projectss'] = Project::all();
  80. $data['groups'] = Group::where('user_id',0)->get();
  81. $data['users'] = User::where('active',1)->where('utype','<',3)->orderBy('name')->get();
  82. $data['in_process'] = Task::with(['user','proj_name'])->where('user_id','!=',0)->whereIn('status',[2,3,4,5])->orderBy('created_time')->paginate(25);
  83. return view('admin.task.in_process',$data);
  84. }
  85. public function add_task(Request $request){
  86. $name = $request->name;
  87. $proj_id = $request->proj_id;
  88. $p=Project::find($proj_id);
  89. $office_id=$p->office_id;
  90. $description = $request->description;
  91. $estimated_time = $request->estimated_time;
  92. $task_type = $request->task_type;
  93. $priority = $request->priority;
  94. $dead_date = $request->dead_line;
  95. //$dead_hour = $request->dead_hour;
  96. //$dead_min = $request->dead_min;
  97. $assigned_user = $request->user_id;
  98. $dead_line = $dead_date;
  99. date_default_timezone_set("Asia/Dhaka");
  100. $cdate = date('Y-m-d H:i:s');
  101. $sav = new Task;
  102. $sav->name = $name;
  103. $sav->type = $task_type;
  104. $sav->proj_id = $proj_id;
  105. $sav->priority = $priority;
  106. $sav->dead_line = $dead_line;
  107. $sav->description = $description;
  108. $sav->created_time = $cdate;
  109. $sav->created_by = Auth::user()->id;
  110. $sav->status = 1;
  111. $sav->user_id=$assigned_user;
  112. $sav->assign_by = Auth::user()->id;
  113. $sav->assign_time = $cdate;
  114. $sav->office_id = $office_id;
  115. $sav->save();
  116. $task_id = $sav->id;
  117. //send email to add user add with this proj
  118. $proj_user = AssignProject::where('proj_id',$proj_id)->get();
  119. $proj_namee = Project::find($proj_id)->name;
  120. foreach($proj_user as $pu){
  121. $data = [
  122. 'task_name' => $name,
  123. 'proj_name' => $proj_namee,
  124. 'user_name' => $pu->one_user->name,
  125. 'details' => $description,
  126. ];
  127. $subj = "A task has been created in ".$proj_namee.".";
  128. $this->send_email_new_task($pu->one_user->email,$subj,$data);
  129. }
  130. //activity log
  131. $msg = "Task: $name created.";
  132. $this->task_log($msg,$task_id,$proj_id); //call function
  133. //notification
  134. $projj_name = Project::find($proj_id)->name;
  135. $adminss = User::where('utype',1)->get();
  136. $post_by_name = Auth::user()->name;
  137. $post_by_id = Auth::user()->id;
  138. $msg = "Task:$name created in Proj:$projj_name by ".$post_by_name;
  139. foreach($adminss as $info){
  140. if($info->id != $post_by_id){
  141. $this->notification($info->id, $msg);
  142. }
  143. }
  144. $ass_usr = AssignProject::where('proj_id',$proj_id)->get();
  145. if(count($ass_usr) > 0){
  146. foreach($ass_usr as $info){
  147. $this->notification($info->user_id, $msg);
  148. }
  149. }
  150. return redirect()->back()->with('message','Success');
  151. }
  152. public function remove_task($id)
  153. {
  154. $del = Task::find($id);
  155. //activity log
  156. $msg = "Task:".$del->name." removed.";
  157. $this->project_log($msg,$del->proj_id); //call function
  158. //notification
  159. $projj_name = Project::find($del->proj_id)->name;
  160. $name = $del->name;
  161. $adminss = User::where('utype',1)->get();
  162. $post_by_name = Auth::user()->name;
  163. $post_by_id = Auth::user()->id;
  164. $msg = "Task:$name removed from Proj:$projj_name by ".$post_by_name;
  165. foreach($adminss as $info){
  166. if($info->id != $post_by_id){
  167. $this->notification($info->id, $msg);
  168. }
  169. }
  170. $ass_usr = AssignProject::where('proj_id',$del->proj_id)->get();
  171. if(count($ass_usr) > 0){
  172. foreach($ass_usr as $info){
  173. $this->notification($info->user_id, $msg);
  174. }
  175. }
  176. //end notification
  177. $del->delete();
  178. return redirect()->back()->with('data','Delete successfully !!');
  179. }
  180. public function change_tast_by_proj(Request $req)
  181. {
  182. $proj_id = $req->proj_id;
  183. $all = Task::where('proj_id',$proj_id)->get();
  184. $str = "";
  185. $str .="<select>";
  186. $str .="<option value='all'>All Tast</option>";
  187. foreach($all as $info){
  188. $str .="<option value=".$info->id.">".$info->name."</option>";
  189. }
  190. $str .="</select>";
  191. return $str;
  192. }
  193. public function user_list_for_task_assign(Request $req){
  194. $proj_id = $req->proj_id;
  195. $task_id = $req->task_id;
  196. $all = AssignProject::with('one_user')->where('proj_id',$proj_id)->get();
  197. $str = "";
  198. $str .="<select>";
  199. foreach($all as $info){
  200. $str .="<option value={$info->user_id}>".$info->one_user->name."</option>";
  201. }
  202. $str .="</select>";
  203. //getting status of this task
  204. $task_sta = Task::find($task_id);
  205. $task_status = $task_sta->status;
  206. $deadline =$task_sta->dead_line;
  207. if(!empty($task_sta->description))
  208. {
  209. $details = $task_sta->description;
  210. }
  211. else{
  212. $details = "No details...";
  213. }
  214. //tast already assigned user
  215. $str1 = $this->fetch_task_assign_user($task_id); //call function
  216. //activity log fetch
  217. $logss = ActivityLog::with('one_user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  218. $str_log = "";
  219. foreach($logss as $log){
  220. $str_log .="* $log->msg -".$log->one_user->name."<br>";
  221. }
  222. //comments fetch
  223. $str_comment = $this->fetch_comments($task_id);
  224. $data[0] = $str;
  225. $data[1] = $str1;
  226. $data[2] = $str_log;
  227. $data[3] = $str_comment;
  228. $data[4] = $task_status;
  229. $data[5] = $details;
  230. $data[6] = $deadline;
  231. return $data;
  232. }
  233. public function task_assign_to_user(Request $req){
  234. $task_id = $req->id_task_id;
  235. $estimated_time = $req->id_estimated_time;
  236. $user_id = $req->id_user_id;
  237. date_default_timezone_set("Asia/Dhaka");
  238. $cdate = date('Y-m-d H:i:s');
  239. $assign_by = Auth::user()->id;
  240. //dd($estimated_time);
  241. $sav = Task::find($task_id);
  242. $sav->user_id = $user_id;
  243. //fetch for activity log
  244. $proj_id = $sav->proj_id;
  245. $task_name = $sav->name;
  246. //send email to add user add with this proj
  247. $data = [
  248. 'task_name' => $sav->name,
  249. 'proj_name' => $sav->proj_name->name,
  250. 'user_name' => User::find($user_id)->name,
  251. 'estimated_time' => $estimated_time,
  252. 'assign_by' => User::find($assign_by)->name,
  253. ];
  254. $this->send_email_assign_task($sav->user->email,'Task assign to you',$data);
  255. $sav->assign_by = $assign_by;
  256. $sav->assign_time = $cdate;
  257. $sav->estimated_time = $estimated_time;
  258. $sav->save();
  259. //activity log
  260. $nam = User::find($user_id);
  261. $msg = "User: $nam->name added";
  262. $this->task_log($msg,$task_id,$proj_id); //call function
  263. //notification
  264. $projj_name = Project::find($proj_id)->name;
  265. $adminss = User::where('utype',1)->get();
  266. $post_by_name = Auth::user()->name;
  267. $post_by_id = Auth::user()->id;
  268. $msg = "$nam->name assigned in Task:$task_name of Proj:$projj_name by ".$post_by_name;
  269. foreach($adminss as $info){
  270. if($info->id != $post_by_id){
  271. $this->notification($info->id, $msg);
  272. }
  273. }
  274. $msg = "You assigned in Task:$task_name of Proj:$projj_name by ".$post_by_name;
  275. $this->notification($user_id, $msg);
  276. //---end notification
  277. $str = $this->fetch_task_assign_user($task_id);
  278. return $str;
  279. }
  280. public function remove_assign_task_user(Request $req){
  281. $id = $req->id; //here id and task id same
  282. $task_id = $req->task_id;
  283. $del = Task::find($id);
  284. //fetch proj_id for activity log
  285. $proj_id = $del->proj_id;
  286. //activity log
  287. $nam = User::find($del->user_id);
  288. $msg = "User: $nam->name removed";
  289. $this->task_log($msg,$task_id,$proj_id); //call function
  290. $del->user_id = 0;
  291. $del->save();
  292. $str = $this->fetch_task_assign_user($task_id); //call function
  293. return $str;
  294. }
  295. public function add_task_comment(Request $req){
  296. $comment = $req->comment;
  297. $task_id = $req->task_id;
  298. date_default_timezone_set("Asia/Dhaka");
  299. $assign_time = date('Y-m-d H:i:s');
  300. if(!empty($comment) && $task_id > 0){
  301. $sav = new Comment;
  302. $sav->comment = $comment;
  303. $sav->task_id = $task_id;
  304. $sav->date_time = $assign_time;
  305. $sav->post_by = Auth::user()->id;
  306. $sav->save();
  307. }
  308. //activity log
  309. $task_info = Task::find($task_id);
  310. $task_name = $task_info->name;
  311. $proj_id = $task_info->proj_id;
  312. $user_id = $task_info->user_id;
  313. $msg = "$comment -added to $task_name.";
  314. $this->task_log($msg,$task_id,$proj_id); //call function
  315. //notification
  316. $fetch_proj = Project::find($proj_id);
  317. $projj_name = $fetch_proj->name;
  318. $client_id = $fetch_proj->client_id;
  319. $adminss = User::where('utype',1)->get();
  320. $notification_post_by = Auth::user()->name;
  321. $noti_post_by_id = Auth::user()->id;
  322. $msg = "A comment added in Task:$task_name of Proj:$projj_name by ".$notification_post_by;
  323. foreach($adminss as $info){
  324. if($info->id != $noti_post_by_id){
  325. $this->notification($info->id, $msg);
  326. }
  327. }
  328. $this->notification($user_id, $msg);
  329. $this->notification($client_id, $msg);
  330. //--------end notification
  331. $str = $this->fetch_comments($task_id);
  332. return $str;
  333. }
  334. public function project_log($msg,$proj_id)
  335. {
  336. date_default_timezone_set("Asia/Dhaka");
  337. $assign_time = date('Y-m-d H:i:s');
  338. $sav = new ActivityLog;
  339. $sav->msg = $msg;
  340. $sav->proj_id = $proj_id;
  341. $sav->logged_user_id = Auth::user()->id;
  342. $sav->created_at = $assign_time;
  343. $sav->save();
  344. }
  345. public function fetch_task_assign_user($task_id)
  346. {
  347. $fetch = Task::with('user')->find($task_id);
  348. //echo $fetch->user_id;
  349. $str = "";
  350. if($fetch->user_id > 0){
  351. $test=date("F jS, Y h:i:s", strtotime($fetch->estimated_time));
  352. $str .="<input type='hidden' value=".$fetch->id.">";
  353. $str .= $fetch->user->name."<br/>Estimated time: ".$test." <span style='color:#FD4B39;cursor:pointer;' class='remove-task-assign-user'><i class='fa fa-times pull-right' aria-hidden='true'></i></span><br>";
  354. }
  355. else{
  356. $str .= "No user assigned";
  357. }
  358. return $str;
  359. }
  360. public function task_log($msg,$task_id,$proj_id)
  361. {
  362. date_default_timezone_set("Asia/Dhaka");
  363. $assign_time = date('Y-m-d H:i:s');
  364. $sav = new ActivityLog;
  365. $sav->msg = $msg;
  366. $sav->proj_id = $proj_id;
  367. $sav->task_id = $task_id;
  368. $sav->logged_user_id = Auth::user()->id;
  369. $sav->created_at = $assign_time;
  370. $sav->save();
  371. }
  372. public function fetch_comments($task_id){
  373. $fetch = Comment::with('user')->where('task_id',$task_id)->orderBy('id','DESC')->get();
  374. $str = "";
  375. if(count($fetch) > 0){
  376. foreach($fetch as $fet)
  377. {
  378. $immmgg = "assets/document/task/$fet->url";
  379. $str .="<input type='hidden' value=".$fet->id.">";
  380. $str .= $fet->comment ."-by ".$fet->user->name."<br/>";
  381. if(!empty($fet->url)){
  382. $str .="<br/><a target='_blank' href=".url("assets/document/task/$fet->url").">
  383. <img height='150' width='120' src='".asset($immmgg)."'></a>";
  384. }
  385. $str .="<span style='color:#FD4B39;cursor:pointer;margin-left:10px' data-id='".$fet->id."' class='edit_comment pull-right'><i class='fa fa-pencil-square-o' aria-hidden='true'></i></span>
  386. <span style='color:#FD4B39;cursor:pointer;' data-id='".$fet->id."' class='remove_comment'><i class='fa fa-times pull-right' aria-hidden='true'></i></span><br/>";
  387. }
  388. }
  389. return $str;
  390. }
  391. public function change_to_process(Request $req)
  392. {
  393. $task_id = $req->task_id;
  394. $status = $req->status;
  395. date_default_timezone_set("Asia/Dhaka");
  396. $t_time = date('Y-m-d H:i:s');
  397. $sav = Task::find($task_id);
  398. $task_name = $sav->name;
  399. $sav->status = $status;
  400. if($status == 2){
  401. $sav->start_time = $t_time;
  402. }
  403. elseif($status == 6){
  404. $sav->closing_time = $t_time;
  405. }
  406. //fetch proj_id for activity log
  407. $proj_id = $sav->proj_id;
  408. if($sav->save())
  409. {
  410. $text = "";
  411. if($status == 2){ $text = "in-process"; }
  412. elseif($status == 3){ $text = "completed"; }
  413. elseif($status == 4){ $text = "checking"; }
  414. elseif($status == 5){ $text = "bug-fixing"; }
  415. elseif($status == 6){ $text = "done"; }
  416. $msg = "$task_name -Change status to: $text";
  417. $this->task_log($msg,$task_id,$proj_id); //call function
  418. return "Yes";
  419. }
  420. else{
  421. return "No";
  422. }
  423. }
  424. public function send_email_new_task($uemail,$subj,$data){
  425. Mail::send('emails.new_task', $data, function ($message) use ($uemail,$subj)
  426. {
  427. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  428. $message->to($uemail)->subject($subj);
  429. });
  430. }
  431. public function send_email_assign_task($uemail,$subj,$data){
  432. Mail::send('emails.task_assign', $data, function ($message) use ($uemail,$subj)
  433. {
  434. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  435. $message->to($uemail)->subject($subj);
  436. });
  437. }
  438. public function notification($user_id,$msg){
  439. $sav = new Notification;
  440. $sav->user_id = $user_id;
  441. $sav->msg = $msg;
  442. $sav->status = 1;
  443. $sav->save();
  444. }
  445. public function company_task(Request $request, $typ){
  446. //dd($request->all());
  447. date_default_timezone_set("Asia/Dhaka");
  448. $company_id = $request->company_id;
  449. if($typ == 'manager_task'){
  450. $task_type = 1;
  451. $act_val = 'New Task created';
  452. }elseif($typ == 'manager_appointment'){
  453. $task_type = 2;
  454. $act_val = 'New Appointment created';
  455. }elseif($typ == 'manager_phone'){
  456. $task_type = 3;
  457. $act_val = 'New Call date created';
  458. }else{
  459. $task_type = 4;
  460. $act_val = 'An email Sent';
  461. }
  462. if($task_type != 4){
  463. $name = $request->name;
  464. $desc = $request->description;
  465. $priority = $request->priority;
  466. $date = $request->date;
  467. $cur_date = date('Y-m-d H:i:s');
  468. $company = CompanyModel::find($company_id);
  469. $manager_id = $company->account_manager;
  470. $manager = User::find($manager_id);
  471. if($manager_id != null){
  472. $sav = new Task;
  473. $sav->name = $name;
  474. $sav->type = 0;
  475. $sav->proj_id = 0;
  476. $sav->priority = $priority;
  477. $sav->dead_line = $date;
  478. $sav->description = $desc;
  479. $sav->created_time = $cur_date;
  480. $sav->created_by = Auth::user()->id;
  481. $sav->status = 1;
  482. $sav->user_id = $manager_id;
  483. $sav->assign_by = Auth::user()->id;
  484. $sav->assign_time = $cur_date;
  485. $sav->office_id = 1;
  486. $sav->task_type = $task_type;
  487. $sav->client_id = $company_id;
  488. $sav->save();
  489. $act = new CompanyActivity;
  490. $act->activity = $act_val;
  491. $act->company_id = $company_id;
  492. $act->date = $cur_date;
  493. $act->save();
  494. $data = [
  495. 'task_name' => $name,
  496. 'company_name' => $company->name,
  497. 'prio' => $priority,
  498. 'type' => $task_type,
  499. 'details' => $desc,
  500. 'date' => $date,
  501. 'manager_name' => $manager->name
  502. ];
  503. $subj = "New Task Added";
  504. $uemail = $manager->email;
  505. Mail::send('emails.company_task', $data, function ($message) use ($uemail,$subj)
  506. {
  507. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  508. $message->to($uemail)->subject($subj)->cc('helaldu@gmail.com');
  509. });
  510. }
  511. }else{
  512. $client = User::find($request->mail_to);
  513. $client_name = $client->name;
  514. $client_email = $client->email;
  515. $to = $client_email;
  516. $subject = $request->subject;
  517. $message = $request->message;
  518. $data = ['msg' => $message];
  519. Mail::send('emails.email_to_client', $data, function ($message) use ($to,$subject)
  520. {
  521. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  522. $message->to($to)->subject($subject)->cc('helaldu@gmail.com');
  523. });
  524. $act = new CompanyActivity;
  525. $act->activity = $act_val;
  526. $act->company_id = $company_id;
  527. $act->date = date('Y-m-d H:i:s');
  528. $act->save();
  529. }
  530. return redirect()->back()->with('message','Success');
  531. }
  532. }