CompanyController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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\CompanyModel;
  7. use App\Models\Remarks;
  8. use App\Models\FileUpload;
  9. use App\Models\CompanyActivity;
  10. use App\Models\ServiceList;
  11. use App\Models\CompanyService;
  12. use App\Models\ServiceAttachment;
  13. use App\Models\Invoice;
  14. use App\Models\AddressBookModel;
  15. use Mail;
  16. use File;
  17. use App\User;
  18. use DB;
  19. class CompanyController extends Controller
  20. {
  21. public function index(Request $req)
  22. {
  23. $data['cname']='';
  24. $data['services']=ServiceList::orderBy('name','asc')->get();
  25. $query=(new CompanyModel)->newQuery();
  26. if($req->has('cname'))
  27. {
  28. $cname=$req->get('cname');
  29. $data['cname']=$cname;
  30. if(!empty($cname))
  31. {
  32. $query->where('name','like',"%{$cname}%");
  33. }
  34. }
  35. $data['all'] = $query->with(['mtask'])->orderBy('name')->paginate(10);
  36. $data['title'] = "Companies";
  37. return view('admin.companies',$data);
  38. }
  39. function change_status(Request $req)
  40. {
  41. $id=$req->get('id');
  42. $status=$req->get('status');
  43. $company=CompanyModel::find($id);
  44. $company->status=$status;
  45. $company->save();
  46. return json_encode(['ok'=>true]);
  47. }
  48. public function create()
  49. {
  50. //
  51. }
  52. public function store(Request $request)
  53. {
  54. $name = $request->name;
  55. $industry = $request->industry;
  56. $service_required = $request->service_required;
  57. $current_condition = $request->current_condition;
  58. $mob1 = $request->mob1;
  59. $mob2 = $request->mob2;
  60. $email1 = $request->email1;
  61. $email2 = $request->email2;
  62. $web = $request->web;
  63. $details = $request->details;
  64. $address = $request->address;
  65. $remarks = $request->remarks;
  66. $display = $request->display==1?1:0;
  67. if(!empty($name) && !empty($mob1) && !empty($email1)){
  68. $sav = new CompanyModel;
  69. $sav->name = $name;
  70. $sav->industry = $industry;
  71. $sav->service_required = $service_required;
  72. $sav->current_condition = $current_condition;
  73. $sav->mob1 = $mob1;
  74. $sav->mob2 = $mob2;
  75. $sav->email1 = $email1;
  76. $sav->email2 = $email2;
  77. $sav->web = $web;
  78. $sav->details = $details;
  79. $sav->address = $address;
  80. $sav->remarks = $remarks;
  81. $sav->display = $display;
  82. $sav->save();
  83. }
  84. return redirect()->back()->with('msg', 'Data added successfully!');
  85. }
  86. public function show($id) // show is using as destroy :(
  87. {
  88. CompanyModel::find($id)->delete();
  89. return redirect()->back();
  90. }
  91. function get_service($id)
  92. {
  93. $data['service']=CompanyService::with(['services','company'])->where('id',$id)->first();
  94. $company_id=$data['service']->company_id;
  95. $data['company'] = CompanyModel::where('display',1)->get();
  96. $data['company_services']=CompanyService::with(['services','attachments'])->where('company_id',$company_id)->get();
  97. $data['filess'] = FileUpload::all();
  98. $data['services']=ServiceList::orderBy('name','asc')->get();
  99. $data['all'] = CompanyModel::with('contact_persons')->find($company_id);
  100. $data['title'] = "Service Details";
  101. return view('admin.company_info.service_details',$data);
  102. }
  103. function file_upload_view($id)
  104. {
  105. $data['service']=CompanyService::with(['services'])->find($id);
  106. return view('admin.company_info.service_docs_upload',$data);
  107. }
  108. function save_upload(Request $req)
  109. {
  110. $this->validate($req,[
  111. 'file' => 'required|max:10000'
  112. ]);
  113. $service_id=$req->get('hdn_service_id');
  114. $title=$req->get('title');
  115. $file_name = time().'.'.$req->file('file')->getClientOriginalExtension();
  116. $req->file('file')->move(
  117. base_path() . '/assets/uploads/service/', $file_name
  118. );
  119. $att=new ServiceAttachment();
  120. $att->service_id=$service_id;
  121. $att->title=$title;
  122. $att->file_name=$file_name;
  123. $att->save();
  124. return redirect()->back()->with('msg','File uploaded successfully !!');
  125. //return redirect('admin/view_company/'.$req->get('hdn_com_id'))->with('msg','File uploaded successfully !!');
  126. }
  127. /**
  128. * assign service to company
  129. * @param Request $req
  130. * @return json
  131. */
  132. function assign_service(Request $req)
  133. {
  134. $com_id=$req->get('com_id');
  135. $service=$req->get('service');
  136. $contact_person=$req->get('contact_person');
  137. $contact_number=$req->get('contact_number');
  138. $deployement_unit=$req->get('deployement_unit');
  139. $po_details=$req->get('po_details');
  140. $renewal_cycle=$req->get('renewal_cycle');
  141. $free_session=$req->get('free_session');
  142. $start_date=$req->get('start_date');
  143. $com_service=new CompanyService();
  144. $com_service->service_id=$service;
  145. $com_service->company_id=$com_id;
  146. $com_service->contact_person=$contact_person;
  147. $com_service->contact_number=$contact_number;
  148. $com_service->deployment_unit_details=$deployement_unit;
  149. $com_service->po_details=$po_details;
  150. $com_service->renewal_cycle=$renewal_cycle;
  151. $com_service->free_session=$free_session;
  152. $com_service->start_date=$start_date;
  153. $com_service->save();
  154. $this->log_activity($com_id,'New service assigned !');
  155. return json_encode(['ok'=>true,'msg'=>'Service assigned to this company successfully !']);
  156. }
  157. public function edit($id)
  158. {
  159. $val = CompanyModel::find($id);
  160. $data['id'] = $id;
  161. $data['name'] = $val->name;
  162. $data['industry'] = $val->industry;
  163. $data['service_required'] = $val->service_required;
  164. $data['current_condition'] = $val->current_condition;
  165. $data['mob1'] = $val->mob1;
  166. $data['mob2'] = $val->mob2;
  167. $data['email1'] = $val->email1;
  168. $data['email2'] = $val->email2;
  169. $data['web'] = $val->web;
  170. $data['details'] = $val->details;
  171. $data['address'] = $val->address;
  172. $data['remarks'] = $val->remarks;
  173. $data['display'] = $val->display;
  174. return $data;
  175. }
  176. public function update(Request $request, $id)
  177. {
  178. //
  179. }
  180. public function update_company(Request $request){
  181. $id = $request->id;
  182. $name = $request->name;
  183. $industry = $request->industry;
  184. $service_required = $request->service_required;
  185. $current_condition = $request->current_condition;
  186. $mob1 = $request->mob1;
  187. $mob2 = $request->mob2;
  188. $email1 = $request->email1;
  189. $email2 = $request->email2;
  190. $web = $request->web;
  191. $details = $request->details;
  192. $address = $request->address;
  193. $remarks = $request->remarks;
  194. $display = $request->display==1?1:0;
  195. if(!empty($name) && !empty($mob1) && !empty($email1)){
  196. $sav = CompanyModel::find($id);
  197. $sav->name = $name;
  198. $sav->industry = $industry;
  199. $sav->service_required = $service_required;
  200. $sav->current_condition = $current_condition;
  201. $sav->mob1 = $mob1;
  202. $sav->mob2 = $mob2;
  203. $sav->email1 = $email1;
  204. $sav->email2 = $email2;
  205. $sav->web = $web;
  206. $sav->details = $details;
  207. $sav->address = $address;
  208. $sav->remarks = $remarks;
  209. $sav->display = $display;
  210. $sav->save();
  211. }
  212. $this->log_activity($id,'Company information updated !');
  213. return redirect()->back()->with('msg', 'Data updated successfully!');
  214. }
  215. function edit_company_info($id)
  216. {
  217. $data['company']=CompanyModel::find($id);
  218. return view('admin.company_info.edit_company',$data);
  219. }
  220. public function change_company_info(Request $request){
  221. $id = $request->id;
  222. $name = $request->com_name;
  223. $industry = $request->industry;
  224. $service_required = $request->req_service;
  225. $current_condition = $request->current_condition;
  226. $mob1 = $request->mobile1;
  227. $mob2 = $request->mobile2;
  228. $email1 = $request->email1;
  229. $email2 = $request->email2;
  230. $web = $request->web;
  231. $details = $request->details;
  232. $address = $request->address;
  233. $remarks = $request->remark;
  234. $display = $request->display==1?1:0;
  235. if(!empty($name) && !empty($mob1) && !empty($email1))
  236. {
  237. $sav = CompanyModel::find($id);
  238. $sav->name = $name;
  239. $sav->industry = $industry;
  240. $sav->service_required = $service_required;
  241. $sav->current_condition = $current_condition;
  242. $sav->mob1 = $mob1;
  243. $sav->mob2 = $mob2;
  244. $sav->email1 = $email1;
  245. $sav->email2 = $email2;
  246. $sav->web = $web;
  247. $sav->details = $details;
  248. $sav->address = $address;
  249. $sav->remarks = $remarks;
  250. $sav->display = $display;
  251. $sav->save();
  252. }
  253. $this->log_activity($id,'Company information updated !');
  254. return redirect()->back()->with('msg', 'Data updated successfully!');
  255. }
  256. public function destroy($id)
  257. {
  258. }
  259. public function view_company($id){
  260. $data['title'] = "Company Details";
  261. $data['company'] = CompanyModel::where('display',1)->get();
  262. $data['company_services']=CompanyService::with(['services'])->where('company_id',$id)->get();
  263. $data['filess'] = FileUpload::all();
  264. $data['services']=ServiceList::orderBy('name','asc')->get();
  265. $data['all'] = CompanyModel::with('contact_persons')->find($id);
  266. $data['invoice'] = Invoice::with(['items','company'])->where('client_id',$id)->get();
  267. $data['cilents'] = User::where('company',$id)->get();
  268. $data['managers_all'] = User::where('utype',2)->get();
  269. $company = CompanyModel::find($id);
  270. $data['company_id'] = $id;
  271. //var_dump($company->account_manager);
  272. $data['manager'] = User::find($company->account_manager);
  273. return view('admin.company_info.indi_company',$data);
  274. }
  275. public function save_remarks(Request $req)
  276. {
  277. date_default_timezone_set("Asia/Dhaka");
  278. $id = $req->company_id;
  279. $company_service_id=$req->has('company_service_id')?$req->get('company_service_id'):0;
  280. $remarks = $req->remarks;
  281. $date = date("Y-m-d h:i");
  282. if($remarks != '0')
  283. {
  284. $sav = new Remarks;
  285. $sav->remarks = $remarks;
  286. $sav->company_id = $id;
  287. $sav->company_service_id=$company_service_id;
  288. $sav->date = $date;
  289. $sav->save();
  290. }
  291. $all=false;
  292. if($company_service_id==0)
  293. {
  294. $all = Remarks::with(['user'])->where('company_id',$id)->orderBy('id','DESC')->get();
  295. }
  296. else
  297. {
  298. $all = Remarks::with(['user'])->where('company_id',$id)->where('company_service_id',$company_service_id)->orderBy('id','DESC')->get();
  299. }
  300. $str = "";
  301. return $this->fetch_remarks($all,$str); //call function
  302. //return $str;
  303. }
  304. public function remove_remarks(Request $req)
  305. {
  306. $id = $req->id;
  307. $company_id = $req->company_id;
  308. Remarks::find($id)->delete();
  309. $this->log_activity($company_id,'remanrks deleted');
  310. $all = Remarks::where('company_id',$company_id)->orderBy('id','DESC')->get();
  311. $str = "";
  312. return $this->fetch_remarks($all,$str); //call function
  313. //return $str;
  314. }
  315. /**
  316. * log activity in company info
  317. * @param integer $company_id
  318. * @param string $activity
  319. * @return void
  320. */
  321. function log_activity($company_id=0,$activity='')
  322. {
  323. date_default_timezone_set("Asia/Dhaka");
  324. $id = $company_id;
  325. $date = date("Y-m-d H:i:s");
  326. $sav = new CompanyActivity;
  327. $sav->activity = $activity;
  328. $sav->company_id = $company_id;
  329. $sav->date = $date;
  330. $sav->user_id=\Auth::user()->id;
  331. $sav->save();
  332. }
  333. public function save_activity(Request $req)
  334. {
  335. date_default_timezone_set("Asia/Dhaka");
  336. $id = $req->company_id;
  337. $val = $req->val;
  338. $date = date("Y-m-d h:i");
  339. if($val != '0')
  340. {
  341. $sav = new CompanyActivity;
  342. $sav->activity = $val;
  343. $sav->company_id = $id;
  344. $sav->date = $date;
  345. $sav->save();
  346. }
  347. $all = CompanyActivity::where('company_id',$id)->orderBy('id','DESC')->get();
  348. $str = "";
  349. return $this->fetch_activity($all,$str); //call function
  350. //return $str;
  351. }
  352. public function remove_activity(Request $req)
  353. {
  354. $id = $req->id;
  355. $company_id = $req->company_id;
  356. CompanyActivity::find($id)->delete();
  357. $all = CompanyActivity::where('company_id',$company_id)->orderBy('id','DESC')->get();
  358. $str = "";
  359. return $this->fetch_activity($all,$str); //call function
  360. }
  361. public function fetch_remarks($all,$str){
  362. if(count($all) > 0){
  363. $str .="<ul class='list-group'>";
  364. foreach($all as $info){
  365. $str .="<li class='list-group-item'>";
  366. $str.="<div class='col-lg-11 col-md-11 col-sm-12 col-xs-10'>";
  367. $str .="$info->remarks";
  368. $user_name=$info->user?$info->user->name:'';
  369. $str .="<p class='remarks-footer'><i class='fa fa-user'></i> {$user_name} &nbsp;&nbsp;&nbsp; <i class='fa fa-clock-o'></i> &nbsp;".date("d-M-Y H:i", strtotime($info->date)).'</p>';
  370. $str .="</div>";
  371. $str.="<div class='col-lg-1 col-md-1 col-sm-12 col-xs-10'>";
  372. $str.="<button style='color:red;' class='btn btn-xs remove-remarks' data-id='{$info->id}'><i class='fa fa-trash-o'></i></button>";
  373. $str.="</div><div class='clearfix'></div>";
  374. $str.="</li>";
  375. }
  376. $str .="</ul>";
  377. }
  378. else{
  379. $str .="No remarks available ...";
  380. }
  381. return $str;
  382. }
  383. public function fetch_activity($all,$str){
  384. if(count($all) > 0){
  385. $str .="<table class='table'>";
  386. foreach($all as $info){
  387. $str .="<tr>";
  388. $str .="<td>$info->activity</td>";
  389. $str .="<td>".date("d-M-Y H:i", strtotime($info->date))."</td>";
  390. $str .="<td><button style='color:red;' class='btn btn-xs remove-activity' data-id='{$info->id}'><i class='fa fa-trash-o'></i></button></td>";
  391. $str .="</tr>";
  392. }
  393. $str .="</table>";
  394. }
  395. else{
  396. $str .="No activity available ...";
  397. }
  398. return $str;
  399. }
  400. public function send_email_to_company(Request $req){
  401. $file_name = "";
  402. $email1 = $req->email1;
  403. $email2 = $req->email2;
  404. $attachment_id = $req->attachment_id;
  405. if($attachment_id > 0){
  406. $att = FileUpload::find($attachment_id);
  407. $file_name = $att->url;
  408. }
  409. $emailss[0] = $email1;
  410. if(!empty($email2))
  411. {
  412. $emailss[1] = $email2;
  413. }
  414. $subj = $req->subj;
  415. $data['email_body'] = $req->email_body;
  416. Mail::send('emails.email_to_company', $data, function ($message) use($subj,$emailss,$file_name){
  417. $message->from('m.revinr@gmail.com', 'Revinr Hr');
  418. if(File::exists('assets/document/files/'.$file_name) && !empty($file_name)){
  419. $message->attach("assets/document/files/$file_name");
  420. }
  421. $message->subject($subj);
  422. $message->to($emailss)->cc('helaldu@gmail.com');
  423. });
  424. if( count(Mail::failures()) > 0 )
  425. {
  426. return "error";
  427. }
  428. else
  429. {
  430. return "success";
  431. }
  432. //return "$email1 - $email2 - $subj - $email_body";
  433. }
  434. public function company_user($id){
  435. $company_address = AddressBookModel::find($id);
  436. $cp_id=$company_address->id;
  437. $name = $company_address->name;
  438. $email = $company_address->email1;
  439. $mob = $company_address->mob1;
  440. $address = 'N/A';
  441. $company = $company_address->company_id;
  442. $website = 'N/A';
  443. $user_type = 3;
  444. $defult_password='revinr123';
  445. $password = bcrypt($defult_password);
  446. $email_check = User::where('email',$email)->first();
  447. if (count($email_check) <=0) {
  448. $sav = new User;
  449. $sav->designation= $company_address->designation;
  450. $sav->name = $name;
  451. $sav->email = $email;
  452. $sav->mob = $mob;
  453. $sav->address = $address;
  454. $sav->company = $company;
  455. $sav->website = $website;
  456. $sav->utype = $user_type;
  457. $sav->password = $password;
  458. $sav->active = 1;
  459. if($sav->save()){
  460. $data=[
  461. 'user_name'=>$name,
  462. 'password'=>$defult_password,
  463. ];
  464. $subject="Resgistration as User";
  465. DB::table('address_book')->where('id','=',$cp_id)->update(array('system_user'=>1));
  466. Mail::send('emails.register', $data, function ($message) use ($email ,$subject)
  467. {
  468. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  469. $message->to($email )->subject($subject);
  470. });
  471. return redirect()->back()->with('msg', 'User Created Successfully');
  472. } else {
  473. return redirect()->back()->with('msg', 'Something Went Wrong Please Try Again');
  474. }
  475. }
  476. else{
  477. return redirect()->back()->with('msg', 'Email Already Use.');
  478. }
  479. }
  480. public function company_manager(Request $req){
  481. $company = $req->company;
  482. $user = User::find($req->user_id);
  483. $email = $user->email;
  484. $data = CompanyModel::find($company);
  485. $data->account_manager = $req->user_id;
  486. if($data->update()){
  487. $res=[
  488. 'user_name'=>$user->name,
  489. 'company_name'=>$data->name,
  490. 'contact'=>$data->mob1,
  491. ];
  492. $subject="Added as Account Manager";
  493. Mail::send('emails.account_manager', $res, function ($message) use ($email ,$subject)
  494. {
  495. $message->from('m.revinr@gmail.com', 'Revinr Task Management');
  496. $message->to($email )->subject($subject);
  497. });
  498. return "Manager Created Successfully";
  499. } else {
  500. return "Something Went Wrong Please Try Again !";
  501. }
  502. }
  503. }