您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

ContractorController.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851
  1. <?php
  2. namespace App\Http\Controllers\Contractor;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. use Illuminate\Support\Facades\Auth;
  6. use Illuminate\Support\Facades\Log;
  7. use Carbon\Carbon;
  8. use Validator;
  9. use PDF;
  10. use App\Staff;
  11. use App\Model\StaffDetail;
  12. use App\Model\Form;
  13. use App\Model\Subscriber;
  14. use App\Model\Coverage;
  15. use App\Model\FormStatus;
  16. use App\Model\WorkOrder;
  17. use App\Model\Docket;
  18. Use App\Model\DealerClaim;
  19. use App\Model\Commission;
  20. use App\Model\Company;
  21. class ContractorController extends Controller
  22. {
  23. //
  24. // public function get_guard(){
  25. // if(Auth::guard('contractor')->check())
  26. // {return "contractor";}
  27. // }
  28. // public function showFormDLogin(){
  29. // return view('login_cs_co');
  30. // }
  31. public function encryptPassword($action, $string) {
  32. $output = false;
  33. $encrypt_method = "AES-256-CBC";
  34. $secret_key = '28472B4B6250655368566D5970337336';
  35. $secret_iv = md5(md5($string)); //'This is my secret iv';
  36. // hash
  37. $key = hash('sha256', $secret_key);
  38. $iv = substr(hash('sha256', $secret_iv), 0, 16);
  39. if ( $action == 'encrypt' ) {
  40. $output = openssl_encrypt($string, $encrypt_method, $key, 0, $iv);
  41. $output = base64_encode($output);
  42. } else if( $action == 'decrypt' ) {
  43. $output = openssl_decrypt(base64_decode($string), $encrypt_method, $key, 0, $iv);
  44. }
  45. return $output;
  46. }
  47. // public function requestLogin(Request $request) {
  48. // $this->validate($request, [
  49. // 'email' => 'required|email'
  50. // // 'password' => 'required|min:6'
  51. // ]);
  52. // $user = '';
  53. // try {
  54. // $message = trans('messages.invalid_login_credentials');
  55. // $rememberMe = false;
  56. // $user = StaffLaravel::where('email', $request->email)->where('roles_access','Contractor')->first();
  57. // if (!empty($user)) {
  58. // if($this->encryptPassword('encrypt', $request->password) == $user->password){
  59. // // Update last_login & last ip
  60. // $user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
  61. // $user->last_login_ip = $request->getClientIp();
  62. // $user->save();
  63. // if($user->roles_access == "Contractor"){
  64. // Auth::guard('contractor')->loginUsingId($user->_id, $rememberMe);
  65. // return redirect('/Contractor');
  66. // }
  67. // }
  68. // }
  69. // } catch (\Exception $e) {
  70. // Log::error(__CLASS__ . "::" . __METHOD__ . " " . $e->getMessage() . "on line" . $e->getLine());
  71. // }
  72. // return redirect('/login/dashboard/admin')->with('error_msg', $message);
  73. // }
  74. // public function logout(){
  75. // // Get Current member id
  76. // $id = Auth::guard('contractor')->id();
  77. // if (Auth::guard('contractor')->check()) {
  78. // $user = StaffLaravel::find($id);
  79. // // Update last_login & last ip
  80. // $user->last_login_at = Carbon::now(new \DateTimeZone('Asia/Kuala_Lumpur'))->toDateTimeString();
  81. // $user->last_login_ip = \Request::getClientIp();
  82. // $user->save();
  83. // // Proceed to Log Out
  84. // Auth::guard('contractor')->logout();
  85. // // return redirect('/login/dashboard');
  86. // return redirect('http://cbblaravel.swisslink.com.my/login/dashboard/admin');
  87. // }
  88. // }
  89. /************************************************************************************* Next Step *************************************************************************************/
  90. public function viewContractorTeam()
  91. {
  92. $id = Auth::guard('contractor')->id();
  93. $user = Staff::with('StaffDetail')->find($id);
  94. $pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  95. $sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  96. $rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
  97. $ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
  98. $cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());
  99. return view('contractor.view_team',compact('user','pp','sp','rs','ss','cm','pc','pi'));
  100. }
  101. public function viewEditContractorTeam($i_id)
  102. {
  103. $id = Auth::guard('contractor')->id();
  104. $user = Staff::with('StaffDetail')->find($id);
  105. $pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  106. $sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  107. $rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
  108. $ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
  109. $cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());
  110. $staff = Staff::with('StaffDetail')->where('_id',$i_id)->first();
  111. return view('contractor.edit_contractor',compact('user','pp','sp','rs','ss','cm','pc','pi','staff'));
  112. }
  113. public function getContractorWork()
  114. {
  115. $id = Auth::guard('contractor')->id();
  116. $user = Staff::with('StaffDetailLaravel')->find($id);
  117. $curr = Carbon::now()->getTimestamp();
  118. $work_detail= WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->get();
  119. $i = 0;
  120. $nested_data = array();
  121. if(!empty($work_detail)){
  122. foreach ($work_detail as $a)
  123. {
  124. $i++; $y = ''; $n1 = '';
  125. $reg_time = $a->created_at;
  126. $expiry_date = $reg_time->addDays(3);
  127. $expiry_date = $expiry_date->getTimestamp();
  128. if($curr < $expiry_date) {
  129. $n1 = "New/";
  130. }
  131. $formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
  132. $installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
  133. $contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();
  134. $inst_name = '';
  135. if(!empty($installer)){
  136. $inst_name = $installer->name;
  137. }
  138. array_push($nested_data, array(
  139. 'index' => $i.$n1,
  140. 'nature_work' => $a->nature_work,
  141. 'wo'=>$a->wo,
  142. 'customer_name' => $formDetail->SubscribersLaravel->name,
  143. 'customer_phone' => $formDetail->SubscribersLaravel->phone1,
  144. 'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
  145. 'customer_building' => $formDetail->SubscribersLaravel->building_name,
  146. 'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
  147. 'customer_city' => $formDetail->SubscribersLaravel->city,
  148. 'contractor_id' => $contractor->name,
  149. 'installer_id' => $inst_name,
  150. 'dateTimeEnd' => $a->dateTimeEnd,
  151. 'status' => $a->status,
  152. 'action' => $a->id,
  153. ));
  154. }
  155. }
  156. return \DataTables::of($nested_data)->make(true);
  157. }
  158. public function filterContractorWork($year, $month, $status)
  159. {
  160. $id = Auth::guard('contractor')->id();
  161. $user = StaffLaravel::with('StaffDetailLaravel')->find($id);
  162. $curr = Carbon::now()->getTimestamp();
  163. if($year == 'null' && $month == 'null' & $status !='null')
  164. {
  165. $nested_data = array();
  166. $work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->where('status', $status)->get();
  167. $i = 0;
  168. $nested_data = array();
  169. if(!empty($work_detail)){
  170. foreach ($work_detail as $a)
  171. {
  172. $i++; $y = ''; $n1 = '';
  173. $reg_time = $a->created_at;
  174. $expiry_date = $reg_time->addDays(3);
  175. $expiry_date = $expiry_date->getTimestamp();
  176. if($curr < $expiry_date) {
  177. $n1 = "New/";
  178. }
  179. $formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
  180. $installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
  181. $contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();
  182. $inst_name = '';
  183. if(!empty($installer)){
  184. $inst_name = $installer->name;
  185. }
  186. array_push($nested_data, array(
  187. 'index' => $i.$n1,
  188. 'nature_work' => $a->nature_work,
  189. 'wo'=>$a->wo,
  190. 'customer_name' => $formDetail->SubscribersLaravel->name,
  191. 'customer_phone' => $formDetail->SubscribersLaravel->phone1,
  192. 'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
  193. 'customer_building' => $formDetail->SubscribersLaravel->building_name,
  194. 'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
  195. 'customer_city' => $formDetail->SubscribersLaravel->city,
  196. 'contractor_id' => $contractor->name,
  197. 'installer_id' => $inst_name,
  198. 'dateTimeEnd' => $a->dateTimeEnd,
  199. 'status' => $a->status,
  200. 'action' => $a->id,
  201. ));
  202. }
  203. }
  204. }
  205. if($year=='null' && $month == 'null' & $status == 'null')
  206. {
  207. $nested_data = array();
  208. $work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->get();
  209. $i = 0;
  210. $nested_data = array();
  211. if(!empty($work_detail)){
  212. foreach ($work_detail as $a)
  213. {
  214. $i++; $y = ''; $n1 = '';
  215. $reg_time = $a->created_at;
  216. $expiry_date = $reg_time->addDays(3);
  217. $expiry_date = $expiry_date->getTimestamp();
  218. if($curr < $expiry_date) {
  219. $n1 = "New/";
  220. }
  221. $formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
  222. $installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
  223. $contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();
  224. $inst_name = '';
  225. if(!empty($installer)){
  226. $inst_name = $installer->name;
  227. }
  228. array_push($nested_data, array(
  229. 'index' => $i.$n1,
  230. 'nature_work' => $a->nature_work,
  231. 'wo'=>$a->wo,
  232. 'customer_name' => $formDetail->SubscribersLaravel->name,
  233. 'customer_phone' => $formDetail->SubscribersLaravel->phone1,
  234. 'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
  235. 'customer_building' => $formDetail->SubscribersLaravel->building_name,
  236. 'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
  237. 'customer_city' => $formDetail->SubscribersLaravel->city,
  238. 'contractor_id' => $contractor->name,
  239. 'installer_id' => $inst_name,
  240. 'dateTimeEnd' => $a->dateTimeEnd,
  241. 'status' => $a->status,
  242. 'action' => $a->id,
  243. ));
  244. }
  245. }
  246. }
  247. if($year!='null' && $month!='nulll' && $status !='null')
  248. {
  249. $nested_data = array();
  250. $start_month = $year."-".$month."-01 00:00:00";
  251. $end_month = $year."-".$month."-32 23:59:59";
  252. $work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->where('status',$status)->get();
  253. $i = 0;
  254. $nested_data = array();
  255. if(!empty($work_detail)){
  256. foreach ($work_detail as $a)
  257. {
  258. $i++; $y = ''; $n1 = '';
  259. $reg_time = $a->created_at;
  260. $expiry_date = $reg_time->addDays(3);
  261. $expiry_date = $expiry_date->getTimestamp();
  262. if($curr < $expiry_date) {
  263. $n1 = "New/";
  264. }
  265. $formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
  266. $installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
  267. $contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();
  268. $inst_name = '';
  269. if(!empty($installer)){
  270. $inst_name = $installer->name;
  271. }
  272. array_push($nested_data, array(
  273. 'index' => $i.$n1,
  274. 'nature_work' => $a->nature_work,
  275. 'wo'=>$a->wo,
  276. 'customer_name' => $formDetail->SubscribersLaravel->name,
  277. 'customer_phone' => $formDetail->SubscribersLaravel->phone1,
  278. 'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
  279. 'customer_building' => $formDetail->SubscribersLaravel->building_name,
  280. 'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
  281. 'customer_city' => $formDetail->SubscribersLaravel->city,
  282. 'contractor_id' => $contractor->name,
  283. 'installer_id' => $inst_name,
  284. 'dateTimeEnd' => $a->dateTimeEnd,
  285. 'status' => $a->status,
  286. 'action' => $a->id,
  287. ));
  288. }
  289. }
  290. }
  291. if($year!='null' && $month!='null' && $status =='null')
  292. {
  293. $nested_data = array();
  294. $start_month = $year."-".$month."-01 00:00:00";
  295. $end_month = $year."-".$month."-32 23:59:59";
  296. $work_detail = WorkOrderLaravel::with('FormLaravel')->where('contractor_id',$user->StaffDetailLaravel->company_id)->orderBy('dateTimeStart', 'desc')->whereBetween('dateTimeStart', [$start_month, $end_month])->get();
  297. $i = 0;
  298. $nested_data = array();
  299. if(!empty($work_detail)){
  300. foreach ($work_detail as $a)
  301. {
  302. $i++; $y = ''; $n1 = '';
  303. $reg_time = $a->created_at;
  304. $expiry_date = $reg_time->addDays(3);
  305. $expiry_date = $expiry_date->getTimestamp();
  306. if($curr < $expiry_date) {
  307. $n1 = "New/";
  308. }
  309. $formDetail = FormLaravel::with('SubscribersLaravel','PackageDetailLaravel')->first();
  310. $installer = StaffDetailLaravel::where('_id', $a->installer_id)->where('position','Installer')->first();
  311. $contractor = CompanyLaravel::where('_id', $a->contractor_id)->first();
  312. $inst_name = '';
  313. if(!empty($installer)){
  314. $inst_name = $installer->name;
  315. }
  316. array_push($nested_data, array(
  317. 'index' => $i.$n1,
  318. 'nature_work' => $a->nature_work,
  319. 'wo'=>$a->wo,
  320. 'customer_name' => $formDetail->SubscribersLaravel->name,
  321. 'customer_phone' => $formDetail->SubscribersLaravel->phone1,
  322. 'customer_unit' => $formDetail->SubscribersLaravel->unit_no,
  323. 'customer_building' => $formDetail->SubscribersLaravel->building_name,
  324. 'customer_postcode' => $formDetail->SubscribersLaravel->postcode,
  325. 'customer_city' => $formDetail->SubscribersLaravel->city,
  326. 'contractor_id' => $contractor->name,
  327. 'installer_id' => $inst_name,
  328. 'dateTimeEnd' => $a->dateTimeEnd,
  329. 'status' => $a->status,
  330. 'action' => $a->id,
  331. ));
  332. }
  333. }
  334. }
  335. return \DataTables::of($nested_data)->make(true);
  336. }
  337. public function filterContractorDocket($year, $month)
  338. {
  339. $year = $year;
  340. $month = $month;
  341. $nested_data = array();
  342. $arr = array();
  343. $i=0;
  344. if($year=='null' && $month=='null')
  345. {
  346. $docket= DocketLaravel::orderBy('created_at', 'desc')->get();
  347. if(!empty($docket))
  348. {
  349. foreach ($docket as $a)
  350. {
  351. $customer = Subscriber::where('_id', $a->customer_id)->first();
  352. $business = Business::where('_id', $a->customer_id)->first();
  353. if(!empty($customer))
  354. {
  355. $arr[]=$a;
  356. }
  357. if(!empty($business))
  358. {
  359. $arr[]=$a;
  360. }
  361. }
  362. }
  363. foreach($arr as $b)
  364. {
  365. $i++; $n1 = '';
  366. $tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
  367. $contractor = $tempContractor->name;
  368. $tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();
  369. $tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
  370. $tempBusiness = Business::where('_id', $b->customer_id)->first();
  371. if(!empty($tempCustomer))
  372. {
  373. $building = $tempCustomer->building;
  374. }
  375. else
  376. {
  377. $building = $tempBusiness->company_name;
  378. }
  379. if(!empty($tempInstaller))
  380. {
  381. array_push($nested_data, array(
  382. 'index' => $n1.$i,
  383. 'docket_id' => $b->docket_id,
  384. 'work_order_id' => $b->work_order_id,
  385. 'nature_work' => $b->nature_work,
  386. 'contractor_id' => $contractor,
  387. 'installer_name' => $tempInstaller->name,
  388. 'customer_id' => $building,
  389. 'installer_id' => $tempInstaller->phone,
  390. 'end_job' => $b->end_job,
  391. 'rating' => $b->Rating1
  392. ));
  393. }
  394. else
  395. {
  396. array_push($nested_data, array(
  397. 'index' => $n1.$i,
  398. 'docket_id' => $a->docket_id,
  399. 'work_order_id' => $a->work_order_id,
  400. 'nature_work' => $a->nature_work,
  401. 'contractor_id' => $contractor,
  402. 'installer_name' => '',
  403. 'customer_id' => $building,
  404. 'installer_id' => '',
  405. 'end_job' => $a->end_job,
  406. 'rating' => $a->Rating1
  407. ));
  408. }
  409. }
  410. }
  411. elseif($year!='' && $month=='null')
  412. {
  413. $timestamp = $year."-01-01 00:00:00.000Z";
  414. $masa = strtotime($timestamp);
  415. $go = Carbon::createFromTimestamp($masa);
  416. $go2 = Carbon::createFromTimestamp($masa);
  417. $end_year = $go2->endOfYear();
  418. // $time = Carbon::createFromTimeString($timestamp);
  419. // echo $month;
  420. $docket = DocketLaravel::whereBetween('created_at', [$go, $end_year])->get();
  421. if(!empty($docket))
  422. {
  423. foreach ($docket as $a)
  424. {
  425. $customer = Subscriber::where('_id', $a->customer_id)->first();
  426. $business = Business::where('_id', $a->customer_id)->first();
  427. if(!empty($customer))
  428. {
  429. $arr[]=$a;
  430. }
  431. if(!empty($business))
  432. {
  433. $arr[]=$a;
  434. }
  435. }
  436. }
  437. foreach($arr as $b)
  438. {
  439. $i++; $n1 = '';
  440. $tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
  441. $contractor = $tempContractor->name;
  442. $tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();
  443. $tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
  444. $tempBusiness = Business::where('_id', $b->customer_id)->first();
  445. if(!empty($tempCustomer))
  446. {
  447. $building = $tempCustomer->building;
  448. }
  449. else
  450. {
  451. $building = $tempBusiness->company_name;
  452. }
  453. if(!empty($tempInstaller))
  454. {
  455. array_push($nested_data, array(
  456. 'index' => $n1.$i,
  457. 'docket_id' => $b->docket_id,
  458. 'work_order_id' => $b->work_order_id,
  459. 'nature_work' => $b->nature_work,
  460. 'contractor_id' => $contractor,
  461. 'installer_name' => $tempInstaller->name,
  462. 'customer_id' => $building,
  463. 'installer_id' => $tempInstaller->phone,
  464. 'end_job' => $b->end_job,
  465. 'rating' => $b->Rating1
  466. ));
  467. }
  468. else
  469. {
  470. array_push($nested_data, array(
  471. 'index' => $n1.$i,
  472. 'docket_id' => $a->docket_id,
  473. 'work_order_id' => $a->work_order_id,
  474. 'nature_work' => $a->nature_work,
  475. 'contractor_id' => $contractor,
  476. 'installer_name' => '',
  477. 'customer_id' => $building,
  478. 'installer_id' => '',
  479. 'end_job' => $a->end_job,
  480. 'rating' => $a->Rating1
  481. ));
  482. }
  483. }
  484. }
  485. elseif($year!='' && $month !='')
  486. {
  487. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  488. $masa = strtotime($timestamp);
  489. $go = Carbon::createFromTimestamp($masa);
  490. $go2 = Carbon::createFromTimestamp($masa);
  491. $end_year = $go2->endOfMonth();
  492. // $time = Carbon::createFromTimeString($timestamp);
  493. $docket = DocketLaravel::whereBetween('created_at', [$go, $end_year])->get();
  494. if(!empty($docket))
  495. {
  496. foreach ($docket as $a)
  497. {
  498. $customer = Subscriber::where('_id', $a->customer_id)->first();
  499. $business = Business::where('_id', $a->customer_id)->first();
  500. if(!empty($customer))
  501. {
  502. $arr[]=$a;
  503. }
  504. if(!empty($business))
  505. {
  506. $arr[]=$a;
  507. }
  508. }
  509. }
  510. foreach($arr as $b)
  511. {
  512. $i++; $n1 = '';
  513. $tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
  514. $contractor = $tempContractor->name;
  515. $tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();
  516. $tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
  517. $tempBusiness = Business::where('_id', $b->customer_id)->first();
  518. if(!empty($tempCustomer))
  519. {
  520. $building = $tempCustomer->building;
  521. }
  522. else
  523. {
  524. $building = $tempBusiness->company_name;
  525. }
  526. if(!empty($tempInstaller))
  527. {
  528. array_push($nested_data, array(
  529. 'index' => $n1.$i,
  530. 'docket_id' => $b->docket_id,
  531. 'work_order_id' => $b->work_order_id,
  532. 'nature_work' => $b->nature_work,
  533. 'contractor_id' => $contractor,
  534. 'installer_name' => $tempInstaller->name,
  535. 'customer_id' => $building,
  536. 'installer_id' => $tempInstaller->phone,
  537. 'end_job' => $b->end_job,
  538. 'rating' => $b->Rating1
  539. ));
  540. }
  541. // else
  542. // {
  543. // array_push($nested_data, array(
  544. // 'index' => $n1.$i,
  545. // 'docket_id' => $a->docket_id,
  546. // 'work_order_id' => $a->work_order_id,
  547. // 'nature_work' => $a->nature_work,
  548. // 'contractor_id' => $contractor,
  549. // 'installer_name' => '',
  550. // 'customer_id' => $building,
  551. // 'installer_id' => '',
  552. // 'end_job' => $a->end_job,
  553. // 'rating' => $a->Rating1
  554. // ));
  555. // }
  556. }
  557. }
  558. return \DataTables::of($nested_data)->make(true);
  559. }
  560. public function getContractorTeam()
  561. {
  562. $id = Auth::guard('contractor')->id();
  563. $user = Staff::with('StaffDetail')->find($id);
  564. $agent = Staff::with('Company','StaffDetail')->where('roles_access','Installer')->orderBy('updated_at','desc')->get();
  565. $i = 0;
  566. $nested_data = array();
  567. $curr = Carbon::now()->getTimestamp();
  568. foreach ($agent as $a)
  569. {
  570. $n1 = '';
  571. $reg_time = $a->updated_at;
  572. $expiry_date = $reg_time->addDays(3);
  573. $expiry_date = $expiry_date->getTimestamp();
  574. if($curr < $expiry_date) {
  575. $n1 = "New/";
  576. }
  577. if($a->company_id == $user->StaffDetail->company_id){
  578. $i++;
  579. array_push($nested_data, array(
  580. 'index' => $n1.$i,
  581. 'user_pic' => $a->StaffDetail->user_pic,
  582. 'name' => $a->StaffDetail->name,
  583. 'ic' => $a->StaffDetail->ic,
  584. 'email' => $a->StaffDetail->email,
  585. 'phone' => $a->StaffDetail->phone,
  586. 'company_id' => $a->Company->name,
  587. 'position' => $a->StaffDetail->position,
  588. 'action' => $a->_id
  589. ));
  590. }
  591. }
  592. return \DataTables::of($nested_data)->make(true);
  593. }
  594. public function addContractorTeam()
  595. {
  596. $id = Auth::guard('contractor')->id();
  597. $user = Staff::with('StaffDetail')->find($id);
  598. $pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  599. $sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  600. $rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
  601. $ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
  602. $cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());
  603. return view('contractor.add_contractor',compact('user','pp','sp','rs','ss','cm','pc','pi'));
  604. }
  605. public function requestAddContractorTeam(Request $request)
  606. {
  607. $validator = Validator::make($request->all(), [
  608. 'password' => 'min:8|confirmed',
  609. ]);
  610. if ($validator->fails()) {
  611. return redirect()->back()->withInput()->withErrors($validator);
  612. }
  613. $staff = Staff::where('email', $request->get('email'))->first();
  614. if(empty($staff))
  615. {
  616. $id = Auth::guard('contractor')->id();
  617. $user = Staff::with('StaffDetail')->find($id);
  618. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  619. $dealerD = new StaffDetail;
  620. $dealerD->name = $request->get('aname');
  621. $dealerD->ic = $request->get('ic');
  622. $dealerD->email = $request->get('email');
  623. $dealerD->phone = $request->get('phone');
  624. $dealerD->company_id = $user->StaffDetail->company_id;
  625. $dealerD->color = '';
  626. $dealerD->position = 'Installer';
  627. $dealerD->password = $pass;
  628. $dealerD->user_pic = 'assets/avatar/user.png';
  629. // Save Login Dealer
  630. $loginD = new Staff;
  631. $loginD->email = $request->get('email');
  632. $loginD->password = $pass;
  633. $loginD->roles_access = 'Installer';
  634. $loginD->company_id = $user->StaffDetail->company_id;
  635. $loginD->last_login_at = '';
  636. $loginD->last_login_ip = '';
  637. // $loginData = [
  638. // 'email' => $request->get('email'),
  639. // 'password' => $pass,
  640. // 'roles_access' => 'Sales',
  641. // 'last_login_at' => '',
  642. // 'last_login_ip' => '',
  643. // ];
  644. $company = Company::where('_id', $user->StaffDetail->company_id)->first();
  645. $company->staff()->save($loginD);
  646. $loginD->staffdetail()->save($dealerD);
  647. return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
  648. }
  649. else
  650. {
  651. return redirect()->back()->withInput()->withErrors('Email already exist!');
  652. }
  653. }
  654. public function requestEditInstaller(Request $request)
  655. {
  656. $staff = Staff::with('StaffDetail')->where('_id', $request->get('_id'))->first();
  657. if(!empty($staff))
  658. {
  659. $id = Auth::guard('contractor')->id();
  660. $user = Staff::with('StaffDetail')->find($id);
  661. if($request->get('password') != ''){
  662. $validator = Validator::make($request->all(), [
  663. 'password' => 'min:8',
  664. ]);
  665. if ($validator->fails()) {
  666. return redirect()->back()->withInput()->withErrors($validator);
  667. }
  668. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  669. $staff->email = $request->get('email');
  670. $staff->password = $pass;
  671. $staff->save();
  672. $staff->StaffDetail->name = $request->get('aname');
  673. $staff->StaffDetail->ic = $request->get('ic');
  674. $staff->StaffDetail->email = $request->get('email');
  675. $staff->StaffDetail->phone = $request->get('phone');
  676. $staff->StaffDetail->save();
  677. return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
  678. }else {
  679. $staff->email = $request->get('email');
  680. $staff->save();
  681. $staff->StaffDetail->name = $request->get('aname');
  682. $staff->StaffDetail->ic = $request->get('ic');
  683. $staff->StaffDetail->email = $request->get('email');
  684. $staff->StaffDetail->phone = $request->get('phone');
  685. $staff->StaffDetail->save();
  686. return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
  687. }
  688. }
  689. else
  690. {
  691. return redirect()->back()->withInput()->withErrors('Email already exist!');
  692. }
  693. }
  694. public function deleteInstaller(Request $request){
  695. $staff = Staff::where('_id',$request->id)->first();
  696. info("delete installer" . $request->id);
  697. if(!empty($staff)){
  698. $st = StaffDetail::where('email',$staff->email)->first();
  699. $wo = WorkOrder::where('installer_id', $request->id)->where('status', 'like', '%Pending%')->get();
  700. foreach ($wo as $w) {
  701. $w->status = "Pending Contractor";
  702. $w->installer_id = "";
  703. $w->save();
  704. }
  705. if(!empty($st)){
  706. $st->delete();
  707. $staff->delete();
  708. }
  709. return 'true';
  710. }else {
  711. return 'false';
  712. }
  713. }
  714. }