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

DocketController.php 26KB

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