projectController.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. namespace App\Http\Controllers\Admin;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\Http\Controllers\Controller;
  6. use App\Models\Project;
  7. use App\Models\Group;
  8. use App\Models\Task;
  9. use App\Models\AssignProject;
  10. use App\Models\ActivityLog;
  11. use App\Models\Notification;
  12. use App\Models\ProjAttachment;
  13. use App\User;
  14. use Auth;
  15. use Mail;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Storage;
  18. use Illuminate\Support\Facades\File;
  19. use App\Models\CompanyModel;
  20. class projectController extends Controller
  21. {
  22. public function index(){
  23. $data['title'] = "Projects";
  24. $data['login_user_id'] = Auth::user()->id;
  25. $data['userss'] = User::where('utype',3)->get();
  26. $data['client_list']=CompanyModel::where('status',2)->orderBy('name')->get();
  27. $data['projj'] = Project::with('client','tasks')->where('is_active',1)->orderBy('name')->get();
  28. return view('admin.projects',$data);
  29. /*
  30. 1=Created;
  31. No status will change for assign
  32. 2=in process;
  33. 3=Completed;
  34. 4=checking;
  35. 5=bugfixing;
  36. 5=done;
  37. */
  38. }
  39. public function add_file(Request $req){
  40. $file = $req->file('file');
  41. $proj_id = $req->proj_id;
  42. date_default_timezone_set("Asia/Dhaka");
  43. if ($req->hasFile('file')) {
  44. $extension = $file->getClientOriginalExtension();
  45. $os = array("jpg", "jpeg", "png", "pdf", "doc", "docx", "txt", "psd");
  46. if (in_array($extension, $os)){
  47. $name = $file->getClientOriginalName();
  48. $newFileName = $newFileName = "proj_".date('d_m_y_h_m_s').".".$extension;;
  49. $fileee = $file->move('assets/document/project/', $newFileName);
  50. //echo $newFileName;
  51. $cat = new ProjAttachment;
  52. $cat->url = $newFileName;
  53. $cat->uploader_id = Auth::user()->id;
  54. $cat->proj_id = $proj_id;
  55. $cat->name = $name;
  56. $cat->save();
  57. $msg = "Add document: $name";
  58. $this->project_log($msg,$proj_id); //call functiont
  59. //notification
  60. $projj_name = Project::find($proj_id)->name;
  61. $adminss = User::where('utype',1)->get();
  62. foreach($adminss as $info){
  63. $msg = "File:$name added in Proj:$projj_name by ".Auth::user()->name;
  64. $this->notification($info->id, $msg);
  65. }
  66. $ass_usr = AssignProject::where('proj_id',$proj_id)->get();
  67. if(count($ass_usr) > 0){
  68. foreach($ass_usr as $info){
  69. $msg = "File:$name added in Proj:$projj_name by ".Auth::user()->name;
  70. $this->notification($info->user_id, $msg);
  71. }
  72. }
  73. }
  74. }
  75. }
  76. public function add_project(Request $request){
  77. $proj_name = $request->proj_name;
  78. $client_id = $request->client_id;
  79. $description = $request->description;
  80. date_default_timezone_set("Asia/Dhaka");
  81. $cdate = date('Y-m-d H:i:s');
  82. if(empty($proj_name) || $client_id <= 0){
  83. return redirect()->back()->with('message','Fill up all fields');
  84. }
  85. $sav = new Project;
  86. $sav->name = $proj_name;
  87. $sav->client_id = $client_id;
  88. $sav->description = $description;
  89. $sav->created_time = $cdate;
  90. $sav->created_by = Auth::user()->id;
  91. $sav->save();
  92. $lastInsertedId = $sav->id;
  93. $msg = "Project:$proj_name created.";
  94. $this->project_log($msg,$lastInsertedId); //call function
  95. $noti = User::where('utype',1)->get();
  96. foreach($noti as $info){
  97. $msg = "Proj: $proj_name created by ".Auth::user()->name;
  98. $this->notification($info->id, $msg);
  99. }
  100. return redirect()->back()->with('message','Success');
  101. }
  102. public function remove_proj($id)
  103. {
  104. $del = Project::find($id);
  105. //proj_name for notification
  106. $proj_name = $del->name;
  107. $del->tasks()->delete();
  108. $del->documents()->delete();
  109. $del->proj_assign()->delete();
  110. $del->delete();
  111. //notification
  112. $noti = User::where('utype',1)->get();
  113. foreach($noti as $info)
  114. {
  115. $msg = "Proj: $proj_name removed by ".Auth::user()->name;
  116. $this->notification($info->id, $msg);
  117. }
  118. return redirect()->back()->with('data','Delete successfully !!');
  119. }
  120. public function remove_proj_document($id)
  121. {
  122. $del = ProjAttachment::find($id);
  123. //for notification
  124. $proj_id = $del->proj_id;
  125. $name = $del->name;
  126. if (File::exists('assets/document/project/'.$del->url) && !empty($del->url))
  127. {
  128. File::delete('assets/document/project/'.$del->url);
  129. $msg = "Document: $del->name removed.";
  130. $this->project_log($msg,$del->proj_id); //call function
  131. $del->delete();
  132. //notification
  133. $projj_name = Project::find($proj_id)->name;
  134. $adminss = User::where('utype',1)->get();
  135. foreach($adminss as $info){
  136. $msg = "File:$name removed from Proj:$projj_name by ".Auth::user()->name;
  137. $this->notification($info->id, $msg);
  138. }
  139. $ass_usr = AssignProject::where('proj_id',$proj_id)->get();
  140. if(count($ass_usr) > 0){
  141. foreach($ass_usr as $info){
  142. $msg = "File:$name removed from Proj:$projj_name by ".Auth::user()->name;
  143. $this->notification($info->user_id, $msg);
  144. }
  145. }
  146. }
  147. return redirect()->back()->with('data','Delete successfully !!');
  148. }
  149. public function project_details($id){
  150. $data['title'] = "Project details";
  151. $data['users'] = User::where('active',1)->get();
  152. $data['groups'] = Group::where('user_id',0)->get();
  153. $data['project_id'] = $id;
  154. $data['self'] = Project::find($id);
  155. $data['proj_assign_user'] = AssignProject::with('one_user')->where('active',1)->where('proj_id',$id)->get();
  156. $data['documents'] = ProjAttachment::where('proj_id',$id)->orderBy('id','desc')->get();
  157. $data['tasks'] = Task::where('status',1)->where('proj_id',$id)->get();
  158. $data['in_process'] = Task::where('status',2)->where('proj_id',$id)->get();
  159. $data['complete'] = Task::where('status',3)->where('proj_id',$id)->get();
  160. $data['check'] = Task::where('status',4)->where('proj_id',$id)->get();
  161. $data['bug'] = Task::where('status',5)->where('proj_id',$id)->get();
  162. $data['done'] = Task::where('status',6)->where('proj_id',$id)->get();
  163. $data['logss'] = ActivityLog::with('one_user')->where('proj_id',$id)->orderBy('created_at','DESC')->take(20)->get();
  164. return view('admin.project_details',$data);
  165. }
  166. public function assign_project(Request $req)
  167. {
  168. $proj_id = $req->proj_id;
  169. $type = $req->types;
  170. $group_id = $req->group_id;
  171. $user_id = $req->user_id;
  172. date_default_timezone_set("Asia/Dhaka");
  173. $assign_time = date('Y-m-d H:i:s');
  174. $fet = Project::find($proj_id);
  175. $proj_name = $fet->name;
  176. $subj = "You are added to ".$proj_name."." ;
  177. $fet_assign_by = User::find(Auth::user()->id);
  178. if($type == 1){
  179. $all = Group::where('depend_on',$group_id)->where('active','>',0)->get();
  180. foreach($all as $info)
  181. {
  182. $sav = new AssignProject;
  183. $sav->proj_id = $proj_id;
  184. $sav->user_id = $info->user_id;
  185. $sav->assign_time = $assign_time;
  186. $sav->assign_by = Auth::user()->id;
  187. $sav->active = 1;
  188. $sav->save();
  189. $fet_user_info = User::find($info->user_id);
  190. //send email after assign project
  191. $uemail = $fet_user_info->email;
  192. $data = [
  193. 'proj_name' => $proj_name,
  194. 'user_name' => $fet_user_info->name,
  195. 'assign_by' => $fet_assign_by->name,
  196. ];
  197. $this->send_email($uemail,$subj,$data); //call function
  198. $u_name = $this->get_user_name($info->user_id); //call function
  199. //activity log
  200. $msg = $u_name." assigned.";
  201. $this->project_log($msg,$proj_id); //call function
  202. //notification
  203. $adminss = User::where('utype',1)->get();
  204. $user_name = User::find($info->user_id)->name;
  205. foreach($adminss as $vals){
  206. $msg = "$user_name assigned in Proj:$proj_name by ".Auth::user()->name;
  207. $this->notification($vals->id, $msg);
  208. }
  209. $msg = "You assigned in Proj:$proj_name by ".Auth::user()->name;
  210. $this->notification($info->user_id, $msg);
  211. }
  212. }
  213. elseif($type == 2){
  214. $sav = new AssignProject;
  215. $sav->proj_id = $proj_id;
  216. $sav->user_id = $user_id;
  217. $sav->assign_time = $assign_time;
  218. $sav->assign_by = Auth::user()->id;
  219. $sav->active = 1;
  220. $sav->save();
  221. $fet_user_info = User::find($user_id);
  222. //send email after assign project
  223. $uemail = $fet_user_info->email;
  224. $data = [
  225. 'proj_name' => $proj_name,
  226. 'user_name' => $fet_user_info->name,
  227. 'assign_by' => $fet_assign_by->name,
  228. ];
  229. $this->send_email($uemail,$subj,$data); //call function
  230. $u_name = $this->get_user_name($user_id); //call function
  231. $msg = $u_name." assigned.";
  232. $this->project_log($msg,$proj_id); //call function
  233. //notification
  234. $adminss = User::where('utype',1)->get();
  235. $user_name = User::find($user_id)->name;
  236. foreach($adminss as $vals){
  237. $msg = "$user_name assigned in Proj:$proj_name by ".Auth::user()->name;
  238. $this->notification($vals->id, $msg);
  239. }
  240. $msg = "You assigned in Proj:$proj_name by ".Auth::user()->name;
  241. $this->notification($user_id, $msg);
  242. }
  243. else{
  244. return redirect()->back()->with('msg','Select type first..');
  245. }
  246. return redirect()->back()->with('msg','Add successfully!');
  247. }
  248. public function send_email($uemail,$subj,$data){
  249. Mail::send('emails.demo', $data, function ($message) use ($uemail,$subj)
  250. {
  251. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  252. $message->to($uemail)->subject($subj);
  253. });
  254. }
  255. public function remove_proj_user($id)
  256. {
  257. $del = AssignProject::find($id);
  258. $u_name = $this->get_user_name($del->user_id); //call function
  259. $msg = "$u_name removed.";
  260. $this->project_log($msg,$del->proj_id); //call function
  261. //notification
  262. $adminss = User::where('utype',1)->get();
  263. $proj_name = Project::find($del->proj_id)->name;
  264. foreach($adminss as $vals){
  265. $msg = "$u_name removed from Proj:$proj_name by ".Auth::user()->name;
  266. $this->notification($vals->id, $msg);
  267. }
  268. $msg = "You removed from Proj:$proj_name by ".Auth::user()->name;
  269. $this->notification($del->user_id, $msg);
  270. $del->delete();
  271. return redirect()->back();
  272. }
  273. public function project_log($msg,$proj_id)
  274. {
  275. date_default_timezone_set("Asia/Dhaka");
  276. $assign_time = date('Y-m-d H:i:s');
  277. $sav = new ActivityLog;
  278. $sav->msg = $msg;
  279. $sav->proj_id = $proj_id;
  280. $sav->logged_user_id = Auth::user()->id;
  281. $sav->created_at = $assign_time;
  282. $sav->save();
  283. }
  284. public function get_user_name($id){
  285. $fetch = User::find($id);
  286. return $fetch->name;
  287. }
  288. public function make_pm(Request $req)
  289. {
  290. $id = $req->id;
  291. $status = $req->pm_status;
  292. $upd = AssignProject::find($id);
  293. $upd->pm_status = $status;
  294. $upd->save();
  295. //notification
  296. $projj_name = Project::find($upd->proj_id)->name;
  297. $adminss = User::where('utype',1)->get();
  298. $user_name = User::find($upd->user_id)->name;
  299. if($status == 1)
  300. {
  301. $msg = "$user_name assigned as a PM in Proj:$projj_name by ".Auth::user()->name;
  302. }
  303. else
  304. {
  305. $msg = "$user_name lost PM position from Proj:$projj_name by ".Auth::user()->name;
  306. }
  307. foreach($adminss as $info){
  308. $this->notification($info->id, $msg);
  309. }
  310. $ass_usr = AssignProject::where('proj_id',$upd->proj_id)->get();
  311. if(count($ass_usr) > 0){
  312. foreach($ass_usr as $info){
  313. $this->notification($info->user_id, $msg);
  314. }
  315. }
  316. return "PM status changed successfully!";
  317. }
  318. public function notification($user_id,$msg){
  319. $sav = new Notification;
  320. $sav->user_id = $user_id;
  321. $sav->msg = $msg;
  322. $sav->status = 1;
  323. $sav->save();
  324. }
  325. public function updateDrag($id,$leveltransfer=''){
  326. $new_value;
  327. $msg;
  328. $start_time;
  329. $end_time;
  330. $prv_value=DB::table('task')->where('id','=',$id)->first();
  331. if($prv_value->status==1 && $leveltransfer=='no'){
  332. $new_value= 2;
  333. $msg="In Progress !";
  334. date_default_timezone_set("Asia/Dhaka");
  335. $start_time = date('Y-m-d H:i:s');
  336. } else if($prv_value->status==2 && $leveltransfer=='no'){
  337. $new_value= 1;
  338. $msg="Created !";
  339. } else if($prv_value->status==3 && $leveltransfer=='no'){
  340. $new_value= 6;
  341. $msg="Done !";
  342. $end_time = date('Y-m-d H:i:s');
  343. } else if($prv_value->status==6 && $leveltransfer=='no'){
  344. $new_value= 3;
  345. $msg="Completed !";
  346. $end_time="0000-00-00 00:00:00";
  347. } else if($prv_value->status==2 && $leveltransfer=='yes'){
  348. $new_value= 3;
  349. $msg="Completed !";
  350. }
  351. if($new_value==2){
  352. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'start_time'=>$start_time));
  353. } else if($new_value ==6){
  354. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'closing_time'=>$end_time));
  355. } else {
  356. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value));
  357. }
  358. return $prv_value->name." STATUS UPDATED TO ".$msg;
  359. }
  360. public function change_task_status($id)
  361. {
  362. $new_value;
  363. $msg;
  364. $start_time;
  365. $end_time;
  366. $prv_value=DB::table('task')->where('id','=',$id)->first();
  367. if($prv_value->status==1)
  368. {
  369. $new_value= 2;
  370. date_default_timezone_set("Asia/Dhaka");
  371. $start_time = date('Y-m-d H:i:s');
  372. }
  373. else if($prv_value->status > 1 && $prv_value->status < 6)
  374. {
  375. $new_value= 6;
  376. $end_time = date('Y-m-d H:i:s');
  377. }
  378. if($new_value==2){
  379. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'start_time'=>$start_time));
  380. } else if($new_value ==6){
  381. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'closing_time'=>$end_time));
  382. }
  383. return redirect()->back();
  384. }
  385. }