ApiController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Requests;
  5. use App\User;
  6. use App\Models\Project;
  7. use App\Models\Task;
  8. use App\Models\AssignProject;
  9. use Hash;
  10. use Mail;
  11. use DB;
  12. use App\Models\FundRequest;
  13. use App\Models\AccountHead;
  14. use App\Models\Attendance;
  15. use Excel;
  16. use Auth;
  17. class ApiController extends Controller
  18. {
  19. public function get_all_users()
  20. {
  21. $users = User::all();
  22. $new_array=array('users'=>$users);
  23. return json_encode($new_array,JSON_PRETTY_PRINT);
  24. }
  25. public function get_all_projects()
  26. {
  27. $projects = Project::all();
  28. $new_array=array('projects'=>$projects);
  29. return json_encode($new_array,JSON_PRETTY_PRINT);
  30. }
  31. public function get_all_tasks()
  32. {
  33. $tasks=Task::with(['user','proj_name'])->where('user_id','!=',0)->where('created_by','!=',0)->where('assign_by','!=',0)->orderBy('created_time')->get();
  34. foreach($tasks as $task)
  35. {
  36. if($task->status == 1)
  37. {
  38. $status = "Created";
  39. }
  40. else if($task->status == 2)
  41. {
  42. $status = "In process";
  43. }
  44. else if($task->status == 3)
  45. {
  46. $status = "Completed";
  47. }
  48. else if($task->status == 4)
  49. {
  50. $status = "Checking";
  51. }
  52. else if($task->status == 5)
  53. {
  54. $status = "Bugfixing";
  55. }
  56. else if($task->status == 6)
  57. {
  58. $status = "done";
  59. }
  60. $task_array[] = array(
  61. 'name' => $task['name'],
  62. 'description' => $task['description'],
  63. 'project_name' => @$task->proj_name->name,
  64. 'user_name' =>$task->user->name,
  65. 'created_by' => $task->user->name,
  66. 'assign_by' =>$task->user->name,
  67. 'created_time' => $task['created_time'],
  68. 'dead_line' => $task['dead_line'],
  69. 'priority' => $task['priority'],
  70. 'assign_time' => $task['assign_time'],
  71. 'start_time' => $task['start_time'],
  72. 'closing_time' => $task['closing_time'],
  73. 'estimated_time'=> $task['estimated_time'],
  74. 'status' => $status,
  75. );
  76. }
  77. $new_array=array('tasks'=>$task_array);
  78. return json_encode($new_array,JSON_PRETTY_PRINT);
  79. }
  80. public function deadline_missed()
  81. {
  82. $deadline_missed=Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status','!=',6)->whereDate('dead_line', '<', date('Y-m-d'))->orderBy('created_time')->get();
  83. //dd($deadline_missed);
  84. foreach($deadline_missed as $task)
  85. {
  86. if($task->status == 1)
  87. {
  88. $status = "Created";
  89. }
  90. else if($task->status == 2)
  91. {
  92. $status = "In process";
  93. }
  94. else if($task->status == 3)
  95. {
  96. $status = "Completed";
  97. }
  98. else if($task->status == 4)
  99. {
  100. $status = "Checking";
  101. }
  102. else if($task->status == 5)
  103. {
  104. $status = "Bugfixing";
  105. }
  106. else if($task->status == 6)
  107. {
  108. $status = "done";
  109. }
  110. $task_array[] = array(
  111. 'task_id' => $task['id'],
  112. 'name' => $task['name'],
  113. 'description' => $task['description'],
  114. 'project_name' => @$task->proj_name->name,
  115. 'user_name' => $task->user->name,
  116. 'created_by' => $task->user->name,
  117. 'assign_by' => $task->user->name,
  118. 'created_time' => $task['created_time'],
  119. 'dead_line' => $task['dead_line'],
  120. 'priority' => $task['priority'],
  121. 'assign_time' => $task['assign_time'],
  122. 'start_time' => $task['start_time'],
  123. 'closing_time' => $task['closing_time'],
  124. 'estimated_time'=> $task['estimated_time'],
  125. 'status' => $status,
  126. );
  127. }
  128. $new_array=array('deadline_missed'=>$task_array);
  129. return json_encode($new_array,JSON_PRETTY_PRINT);
  130. }
  131. public function no_work_today()
  132. {
  133. $all_users=User::where('utype','=',2)->pluck('name','id');
  134. $working_today = Task::where('user_id','!=',0)->whereDate('start_time', '=', date('Y-m-d'))->orwhereDate('closing_time', '=', date('Y-m-d'))->whereNotIn('status',[2,3,4,5])->orderBy('created_time')->pluck('user_id');
  135. $active_users=array();
  136. foreach($working_today as $row){
  137. if($row !=0){
  138. if (!in_array($row,$active_users))
  139. {
  140. array_push($active_users,$row);
  141. }
  142. }
  143. }
  144. $inactive_users=array();
  145. foreach($all_users as $i=>$value){
  146. if (!in_array($i,$active_users))
  147. {
  148. array_push($inactive_users,$value);
  149. }
  150. }
  151. $new_array=array('not_working_today'=>$inactive_users);
  152. return json_encode($new_array,JSON_PRETTY_PRINT);
  153. }
  154. public function create_task(Request $request)
  155. {
  156. $name = $request->name;
  157. $proj_id = $request->proj_id;
  158. $description = $request->description;
  159. $estimated_time = $request->dead_line;
  160. $task_type = 1;
  161. $priority = $request->priority;
  162. $dead_date = $request->dead_line;
  163. $assigned_user = $request->user_id;
  164. $dead_line = $dead_date;
  165. date_default_timezone_set("Asia/Dhaka");
  166. $cdate = date('Y-m-d H:i:s');
  167. $sav = new Task;
  168. $sav->name = $name;
  169. $sav->type = $task_type;
  170. $sav->proj_id = $proj_id;
  171. $sav->priority = $priority;
  172. $sav->dead_line = $dead_line;
  173. $sav->description = $description;
  174. $sav->created_time = $cdate;
  175. $sav->created_by = 2;
  176. $sav->status = 1;
  177. $sav->user_id=$assigned_user;
  178. $sav->assign_by = 2;
  179. $sav->assign_time = $cdate;
  180. $sav->save();
  181. $task_id = $sav->id;
  182. $proj_user = AssignProject::where('proj_id',$proj_id)->get();
  183. /*
  184. $proj_namee = Project::find($proj_id)->name;
  185. foreach($proj_user as $pu){
  186. $data = [
  187. 'task_name' => $name,
  188. 'proj_name' => $proj_namee,
  189. 'user_name' => $pu->one_user->name,
  190. 'details' => $description,
  191. ];
  192. $subj = "A task has been created in ".$proj_namee.".";
  193. $this->send_email_new_task($pu->one_user->email,$subj,$data);
  194. }*/
  195. if($task_id !='' && $task_id !=0){
  196. $new_array=array('success_message'=>'Successfully Saved');
  197. return json_encode($new_array,JSON_PRETTY_PRINT);
  198. }
  199. else
  200. {
  201. $new_array=array('unsucessful_message'=>'Unsuccessful Attempt!Try Again!');
  202. return json_encode($new_array,JSON_PRETTY_PRINT);
  203. }
  204. }
  205. public function send_email_new_task($uemail,$subj,$data)
  206. {
  207. Mail::send('emails.new_task', $data, function ($message) use ($uemail,$subj)
  208. {
  209. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  210. $message->to($uemail)->subject($subj);
  211. });
  212. }
  213. public function change_task_status($id)
  214. {
  215. $new_value;
  216. $msg;
  217. $start_time;
  218. $end_time;
  219. $prv_value=DB::table('task')->where('id','=',$id)->first();
  220. if($prv_value->status >= 1 && $prv_value->status < 6)
  221. {
  222. $new_value= 6;
  223. $end_time = date('Y-m-d H:i:s');
  224. }
  225. if($new_value ==6){
  226. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'closing_time'=>$end_time));
  227. }
  228. /*if($prv_value->status==1)
  229. {
  230. $new_value= 2;
  231. date_default_timezone_set("Asia/Dhaka");
  232. $start_time = date('Y-m-d H:i:s');
  233. }
  234. else if($prv_value->status > 1 && $prv_value->status < 6)
  235. {
  236. $new_value= 6;
  237. $end_time = date('Y-m-d H:i:s');
  238. }
  239. if($new_value==2){
  240. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'start_time'=>$start_time));
  241. } else if($new_value ==6){
  242. DB::table('task')->where('id','=',$id)->update(array('status'=>$new_value,'closing_time'=>$end_time));
  243. }*/
  244. $new_array=array('success_message'=>'Successfully Status Changed');
  245. return json_encode($new_array,JSON_PRETTY_PRINT);
  246. }
  247. public function fund_purpose()
  248. {
  249. $fund_purpose = AccountHead::where('type_id',2)->where('parent_head',24)->orderBy('name','asc')->get();
  250. foreach($fund_purpose as $fund)
  251. {
  252. $fund_p[] = array(
  253. 'id' => $fund->id,
  254. 'name' => $fund->name,
  255. );
  256. }
  257. $new_array=array('fund_purpose'=>$fund_p);
  258. return json_encode($new_array,JSON_PRETTY_PRINT);
  259. }
  260. public function fund_save(Request $req)
  261. {
  262. $this->validate($req,[
  263. 'purpose'=>'required',
  264. 'amount'=>'required|numeric'
  265. ]);
  266. $fund=new FundRequest();
  267. $fund->purpose=$req->get('purpose');
  268. $fund->description=$req->get('description');
  269. $fund->amount=$req->get('amount');
  270. $fund->due_date=$req->get('due_date',date('Y-m-d H:i:s'));
  271. $fund->request_by=2;
  272. $fund->is_approved=0;
  273. $fund->save();
  274. if($fund->id > 0)
  275. {
  276. $new_array=array('success_message'=>'Successfully Saved');
  277. return json_encode($new_array,JSON_PRETTY_PRINT);
  278. }
  279. }
  280. public function get_entry_time_today()
  281. {
  282. date_default_timezone_set("Asia/Dhaka");
  283. $entry_time = Attendance::whereDate('attendance_time', '=', date('Y-m-d'))->where('status','=',0)->orderBy('attendance_time','desc')->get();
  284. $new_array=array('entry_time'=>$entry_time);
  285. return json_encode($new_array,JSON_PRETTY_PRINT);
  286. }
  287. public function get_exit_time_yesterday()
  288. {
  289. $exit_time = Attendance::whereDate('attendance_time', '=', date('Y-m-d',strtotime('yesterday')))->where('status','=',1)->orderBy('attendance_time','desc')->groupBy('attendance_time')->get();
  290. $new_array=array('exit_time'=>$exit_time);
  291. return json_encode($new_array,JSON_PRETTY_PRINT);
  292. }
  293. public function get_month_attendance_report()
  294. {
  295. $entry_time = Attendance::whereDate('attendance_time', '=', date('Y-m-d'))->where('status','=',0)->orderBy('attendance_time','desc')->groupBy('attendance_time')->get();
  296. $exit_time = Attendance::whereDate('attendance_time', '=', date('Y-m-d',strtotime('yesterday')))->where('status','=',1)->orderBy('attendance_time','desc')->groupBy('attendance_time')->get();
  297. $title = "User Attendace";
  298. $all_dates = array();
  299. $dates = Attendance::pluck('attendance_time');
  300. $all_users = array();
  301. $users = Attendance::pluck('user_id');
  302. $a=strtotime('9:30 am');
  303. $c=date('H:i:s a', $a);
  304. $exact_time= $c;
  305. foreach($users as $user)
  306. {
  307. $new_val = $user;
  308. if(!in_array($new_val,$all_users))
  309. {
  310. array_push($all_users, $new_val);
  311. }
  312. }
  313. $late_array = array();
  314. foreach($all_users as $d)
  315. {
  316. if($d !=6 && $d !=10){
  317. $late_count=0;
  318. $test_entry_time=Attendance::select('attendance_time','user_name')->where('user_id','=',$d)->where('status','=',0)->whereMonth('attendance_time', '>=', date('m'))->get();
  319. $absent_count=Attendance::where('user_id','=',$d)->whereMonth('attendance_time', '=', date('m'))->where('absent_count','=',1)->count();
  320. foreach ($test_entry_time as $value1) {
  321. $u_name=$value1->user_name;
  322. $et=date('H:i:s a',strtotime($value1->attendance_time));
  323. if($et>$exact_time)
  324. {
  325. $late_count++;
  326. }
  327. }
  328. $new_array=array('user_id'=>$d,'user_name'=>@$u_name,'late_count'=>$late_count,'absent_count'=>$absent_count);
  329. array_push($late_array, $new_array);
  330. }
  331. }
  332. $new_array=array('late_absent_record'=>$late_array);
  333. return json_encode($new_array,JSON_PRETTY_PRINT);
  334. }
  335. function get_month_report($id)
  336. {
  337. $entry_times = Attendance::where('user_id','=',$id)->where('status','=',0)->whereMonth('attendance_time', '=', date('m'))->orderBy('attendance_time','desc')->groupBy('attendance_time')->get();
  338. $exit_times = Attendance::where('user_id','=',$id)->where('status','=',1)->whereMonth('attendance_time', '=', date('m'))->orderBy('attendance_time','desc')->groupBy('attendance_time')->get();
  339. foreach($entry_times as $report)
  340. {
  341. $entry_array[] = array(
  342. 'user_id'=>@$report->user_id,
  343. 'user_name'=>$report->user_name,
  344. 'time'=>$report->attendance_time
  345. );
  346. }
  347. foreach($exit_times as $report)
  348. {
  349. $exit_array[] = array(
  350. 'user_id'=>@$report->user_id,
  351. 'user_name'=>$report->user_name,
  352. 'time'=>$report->attendance_time
  353. );
  354. }
  355. $report_array=array('entry_array'=>@$entry_array,'exit_array'=>@$exit_array);
  356. $new_array=array('month_attendance_report'=>@$report_array);
  357. return json_encode(array('month_attendance_report'=>$report_array),JSON_PRETTY_PRINT);
  358. }
  359. function get_fund_requests()
  360. {
  361. $fund_reqs = FundRequest::with(['acc_head','user'])->where('is_approved',0)
  362. ->orderBY('id','desc')->get();
  363. foreach($fund_reqs as $req)
  364. {
  365. $id = $req->id;
  366. $purpose = $req->acc_head?$req->acc_head->name:'';
  367. $description = $req->description;
  368. $created_at = $req->created_at;
  369. $user = $req->user?$req->user->name:'';
  370. $due_date = $req->due_date;
  371. $amount = $req->amount;
  372. $req_array[] = array(
  373. 'id'=>$id,
  374. 'created_by'=>$user,
  375. 'purpose'=>$purpose,
  376. 'description'=>$description,
  377. 'created_at'=>$created_at,
  378. 'due_date'=>$due_date,
  379. 'amount'=>$amount
  380. );
  381. }
  382. $new_array=array('fund_requests'=>$req_array);
  383. return json_encode($new_array,JSON_PRETTY_PRINT);
  384. }
  385. function change_fund_request_status($id,$value)
  386. {
  387. if($value==1)
  388. {
  389. DB::table('fund_request')->where('id','=',$id)->update(array('is_approved'=>$value));
  390. $new_array=array('message'=>'Fund Request Approved');
  391. return json_encode($new_array,JSON_PRETTY_PRINT);
  392. }
  393. else
  394. {
  395. DB::table('fund_request')->where('id','=',$id)->delete();
  396. $new_array=array('message'=>'Fund Request Declined');
  397. return json_encode($new_array,JSON_PRETTY_PRINT);
  398. }
  399. }
  400. public function get_pending_tasks()
  401. {
  402. $pending_tasks=Task::with(['user','proj_name'])->where('user_id','!=',0)->where('status','!=',6)->orderBy('created_time')->get();
  403. foreach($pending_tasks as $task)
  404. {
  405. $task_array[] = array(
  406. 'task_id' => $task['id'],
  407. 'name' => $task['name'],
  408. 'description' => $task['description'],
  409. 'project_name' => @$task->proj_name->name,
  410. 'user_name' => $task->user->name,
  411. 'created_by' => $task->user->name,
  412. 'assign_by' => $task->user->name,
  413. 'created_time' => $task['created_time'],
  414. 'dead_line' => $task['dead_line'],
  415. 'priority' => $task['priority'],
  416. 'assign_time' => $task['assign_time'],
  417. 'start_time' => $task['start_time'],
  418. 'closing_time' => $task['closing_time'],
  419. 'estimated_time' => $task['estimated_time'],
  420. );
  421. }
  422. $new_array=array('pending_tasks'=>$task_array);
  423. return json_encode($new_array,JSON_PRETTY_PRINT);
  424. }
  425. public function get_in_progress_tasks(){
  426. $in_progress_tasks=Task::with(['user','proj_name'])->where('user_id','!=',0)->whereIn('status',[2,3,4,5])->orderBy('created_time')->get();
  427. foreach($in_progress_tasks as $task)
  428. {
  429. $task_array[] = array(
  430. 'task_id' =>$task['id'],
  431. 'name' =>$task['name'],
  432. 'description' => $task['description'],
  433. 'project_name' => @$task->proj_name->name,
  434. 'user_name' =>$task->user->name,
  435. 'created_by' => $task->user->name,
  436. 'assign_by' =>$task->user->name,
  437. 'created_time' => $task['created_time'],
  438. 'dead_line' => $task['dead_line'],
  439. 'priority' => $task['priority'],
  440. 'assign_time' => $task['assign_time'],
  441. 'start_time' => $task['start_time'],
  442. 'closing_time' => $task['closing_time'],
  443. 'estimated_time'=> $task['estimated_time'],
  444. );
  445. }
  446. $new_array=array('in_progress_tasks'=>$task_array);
  447. return json_encode($new_array,JSON_PRETTY_PRINT);
  448. }
  449. public function leave_req(Request $request)
  450. {
  451. $purpose= $request->purpose;
  452. $apply_to= 2;
  453. $from= $request->get('from',date('Y-m-d H:i:s'));
  454. $to= $request->get('to',date('Y-m-d H:i:s'));
  455. $description = $request->description;
  456. $approval = 0;
  457. $created_at = date('Y-m-d H:i:s');
  458. $user_id = $request->user_id;
  459. DB::table('leave_req')->insert(array(
  460. 'purpose'=>$purpose,
  461. 'apply_to'=>$apply_to,
  462. 'from'=>$from,
  463. 'to'=>$to,
  464. 'description'=>$description,
  465. 'approval'=>$approval,
  466. 'created_at'=>$created_at,
  467. 'user_id'=>$user_id,
  468. ));
  469. $new_array=array('create_message'=>'Leave Request Approved');
  470. return json_encode($new_array,JSON_PRETTY_PRINT);
  471. }
  472. public function get_leave_purposes(){
  473. $leave_type = DB::table('leave_type_head')->where('is_published','=',1)->get();
  474. foreach($leave_type as $leave)
  475. {
  476. $purposes[] = array('id'=>$leave->id,'name'=>$leave->name);
  477. }
  478. $new_array=array('leave_purposes'=>$purposes);
  479. return json_encode($new_array,JSON_PRETTY_PRINT);
  480. }
  481. public function get_leave_requests()
  482. {
  483. $all = DB::table('leave_req')->where('approval','=',0)->get();
  484. foreach($all as $leave){
  485. $user= User::select('name')->where('id','=',$leave->user_id)->first();
  486. $purpose=DB::table('leave_type_head')->select('name')->where('id','=',$leave->purpose)->first();
  487. $from=$leave->from;
  488. $to=$leave->to;
  489. $description=$leave->description;
  490. $leave_array[] = array(
  491. 'id'=>$leave->id,
  492. 'user'=>$user->name,
  493. 'purpose'=>$purpose->name,
  494. 'from'=>$from,
  495. 'to'=>$to,
  496. 'description'=>$description,
  497. 'status'=>$leave->approval
  498. );
  499. }
  500. $new_array=array('leave_requests'=>$leave_array);
  501. return json_encode($new_array,JSON_PRETTY_PRINT);
  502. }
  503. public function leave_accept(Request $request)
  504. {
  505. $id = $request->id;
  506. $status = $request->status;
  507. if($status ==0)
  508. {
  509. DB::table('leave_req')->where('id','=',$id)->delete();
  510. $new_array=array('accept_message'=>'Leave Request Discarded');
  511. return json_encode($new_array,JSON_PRETTY_PRINT);
  512. }
  513. else
  514. {
  515. DB::table('leave_req')->where('id','=',$id)->update(array('approval'=>$status));
  516. $new_array=array('accept_message'=>'Leave Request Approved');
  517. return json_encode($new_array,JSON_PRETTY_PRINT);
  518. }
  519. }
  520. public function login(Request $request)
  521. {
  522. $email=$request->email;
  523. $password=$request->password;
  524. if(Auth::attempt(['email'=>$email,'password'=>$password]))
  525. {
  526. $user=Auth::user();
  527. $info= array(
  528. 'login_message'=>'Successfully Logged In',
  529. 'user_id'=>$user->id,
  530. 'user_name'=>$user->name,
  531. 'email'=>$user->email,
  532. 'user_type'=>$user->utype
  533. );
  534. $new_array= array('login_info'=>$info);
  535. return json_encode($new_array,JSON_PRETTY_PRINT);
  536. }
  537. else
  538. {
  539. $info= array('login_message'=>'Email or Password Is Invalid. Try Again !');
  540. $new_array= array('login_info'=>$info);
  541. return json_encode($new_array,JSON_PRETTY_PRINT);
  542. }
  543. }
  544. }