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

DocketController.php 27KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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. if ($form->Subscriber->building_name != null) {
  320. $building = $form->Subscriber->building_name;
  321. } else {
  322. $building = $form->Subscriber->company_name;
  323. }
  324. }
  325. $reg_time = $d->created_at;
  326. $expiry_date = $reg_time->addDays(3);
  327. $expiry_date = $expiry_date->getTimestamp();
  328. if($curr < $expiry_date) {
  329. $n1 = "New/";
  330. }
  331. array_push($tempD, array(
  332. 'index' => $n1.$i,
  333. 'docket_id' => $d->docket_id,
  334. 'work_order_id' => $d->work_order_id,
  335. 'nature_work' => $d->nature_work,
  336. 'contractor_id' => $company->name,
  337. 'installer_name' => $installer->StaffDetail->name,
  338. 'customer_id' => $building,
  339. 'installer_id' => $installer->StaffDetail->phone,
  340. 'end_job' => date('d/m/Y H:i', strtotime($d->end_job)),
  341. 'rating' => round((($d->Rating1 + $d->Rating2 + $d->Rating3)/15) * 100)."%",
  342. 'action' => $d->docket_id.'/'.$d->work_order_id
  343. ));
  344. }
  345. }
  346. }
  347. }
  348. foreach($tempD as $t){
  349. array_push($nested_data, array(
  350. 'index' => $t['index'],
  351. 'docket_id' => $t['docket_id'],
  352. 'work_order_id' => $t['work_order_id'],
  353. 'nature_work' => $t['nature_work'],
  354. 'contractor_id' => $t['contractor_id'],
  355. 'installer_name' => $t['installer_name'],
  356. 'customer_id' => $t['customer_id'],
  357. 'installer_id' => $t['installer_id'],
  358. 'end_job' => $t['end_job'],
  359. 'rating' =>$t['rating'],
  360. 'action' =>$t['action'],
  361. ));
  362. }
  363. return \DataTables::of($nested_data)->make(true);
  364. }
  365. public function filterContractorDocket($year, $month)
  366. {
  367. $year = $year;
  368. $month = $month;
  369. $nested_data = array();
  370. $arr = array();
  371. $i=0;
  372. if($year=='null' && $month=='null')
  373. {
  374. $docket= DocketLaravel::orderBy('created_at', 'desc')->get();
  375. if(!empty($docket))
  376. {
  377. foreach ($docket as $a)
  378. {
  379. $customer = Subscriber::where('_id', $a->customer_id)->first();
  380. $business = Business::where('_id', $a->customer_id)->first();
  381. if(!empty($customer))
  382. {
  383. $arr[]=$a;
  384. }
  385. if(!empty($business))
  386. {
  387. $arr[]=$a;
  388. }
  389. }
  390. }
  391. foreach($arr as $b)
  392. {
  393. $i++; $n1 = '';
  394. $tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
  395. $contractor = $tempContractor->name;
  396. $tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();
  397. $tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
  398. $tempBusiness = Business::where('_id', $b->customer_id)->first();
  399. if(!empty($tempCustomer))
  400. {
  401. $building = $tempCustomer->building;
  402. }
  403. else
  404. {
  405. $building = $tempBusiness->company_name;
  406. }
  407. if(!empty($tempInstaller))
  408. {
  409. array_push($nested_data, array(
  410. 'index' => $n1.$i,
  411. 'docket_id' => $b->docket_id,
  412. 'work_order_id' => $b->work_order_id,
  413. 'nature_work' => $b->nature_work,
  414. 'contractor_id' => $contractor,
  415. 'installer_name' => $tempInstaller->name,
  416. 'customer_id' => $building,
  417. 'installer_id' => $tempInstaller->phone,
  418. 'end_job' => $b->end_job,
  419. 'rating' => $b->Rating1
  420. ));
  421. }
  422. else
  423. {
  424. array_push($nested_data, array(
  425. 'index' => $n1.$i,
  426. 'docket_id' => $a->docket_id,
  427. 'work_order_id' => $a->work_order_id,
  428. 'nature_work' => $a->nature_work,
  429. 'contractor_id' => $contractor,
  430. 'installer_name' => '',
  431. 'customer_id' => $building,
  432. 'installer_id' => '',
  433. 'end_job' => $a->end_job,
  434. 'rating' => $a->Rating1
  435. ));
  436. }
  437. }
  438. }
  439. elseif($year!='' && $month=='null')
  440. {
  441. $timestamp = $year."-01-01 00:00:00.000Z";
  442. $masa = strtotime($timestamp);
  443. $go = Carbon::createFromTimestamp($masa);
  444. $go2 = Carbon::createFromTimestamp($masa);
  445. $end_year = $go2->endOfYear();
  446. // $time = Carbon::createFromTimeString($timestamp);
  447. // echo $month;
  448. $docket = DocketLaravel::whereBetween('created_at', [$go, $end_year])->get();
  449. if(!empty($docket))
  450. {
  451. foreach ($docket as $a)
  452. {
  453. $customer = Subscriber::where('_id', $a->customer_id)->first();
  454. $business = Business::where('_id', $a->customer_id)->first();
  455. if(!empty($customer))
  456. {
  457. $arr[]=$a;
  458. }
  459. if(!empty($business))
  460. {
  461. $arr[]=$a;
  462. }
  463. }
  464. }
  465. foreach($arr as $b)
  466. {
  467. $i++; $n1 = '';
  468. $tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
  469. $contractor = $tempContractor->name;
  470. $tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();
  471. $tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
  472. $tempBusiness = Business::where('_id', $b->customer_id)->first();
  473. if(!empty($tempCustomer))
  474. {
  475. $building = $tempCustomer->building;
  476. }
  477. else
  478. {
  479. $building = $tempBusiness->company_name;
  480. }
  481. if(!empty($tempInstaller))
  482. {
  483. array_push($nested_data, array(
  484. 'index' => $n1.$i,
  485. 'docket_id' => $b->docket_id,
  486. 'work_order_id' => $b->work_order_id,
  487. 'nature_work' => $b->nature_work,
  488. 'contractor_id' => $contractor,
  489. 'installer_name' => $tempInstaller->name,
  490. 'customer_id' => $building,
  491. 'installer_id' => $tempInstaller->phone,
  492. 'end_job' => $b->end_job,
  493. 'rating' => $b->Rating1
  494. ));
  495. }
  496. else
  497. {
  498. array_push($nested_data, array(
  499. 'index' => $n1.$i,
  500. 'docket_id' => $a->docket_id,
  501. 'work_order_id' => $a->work_order_id,
  502. 'nature_work' => $a->nature_work,
  503. 'contractor_id' => $contractor,
  504. 'installer_name' => '',
  505. 'customer_id' => $building,
  506. 'installer_id' => '',
  507. 'end_job' => $a->end_job,
  508. 'rating' => $a->Rating1
  509. ));
  510. }
  511. }
  512. }
  513. elseif($year!='' && $month !='')
  514. {
  515. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  516. $masa = strtotime($timestamp);
  517. $go = Carbon::createFromTimestamp($masa);
  518. $go2 = Carbon::createFromTimestamp($masa);
  519. $end_year = $go2->endOfMonth();
  520. // $time = Carbon::createFromTimeString($timestamp);
  521. $docket = DocketLaravel::whereBetween('created_at', [$go, $end_year])->get();
  522. if(!empty($docket))
  523. {
  524. foreach ($docket as $a)
  525. {
  526. $customer = Subscriber::where('_id', $a->customer_id)->first();
  527. $business = Business::where('_id', $a->customer_id)->first();
  528. if(!empty($customer))
  529. {
  530. $arr[]=$a;
  531. }
  532. if(!empty($business))
  533. {
  534. $arr[]=$a;
  535. }
  536. }
  537. }
  538. foreach($arr as $b)
  539. {
  540. $i++; $n1 = '';
  541. $tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
  542. $contractor = $tempContractor->name;
  543. $tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();
  544. $tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
  545. $tempBusiness = Business::where('_id', $b->customer_id)->first();
  546. if(!empty($tempCustomer))
  547. {
  548. $building = $tempCustomer->building;
  549. }
  550. else
  551. {
  552. $building = $tempBusiness->company_name;
  553. }
  554. if(!empty($tempInstaller))
  555. {
  556. array_push($nested_data, array(
  557. 'index' => $n1.$i,
  558. 'docket_id' => $b->docket_id,
  559. 'work_order_id' => $b->work_order_id,
  560. 'nature_work' => $b->nature_work,
  561. 'contractor_id' => $contractor,
  562. 'installer_name' => $tempInstaller->name,
  563. 'customer_id' => $building,
  564. 'installer_id' => $tempInstaller->phone,
  565. 'end_job' => $b->end_job,
  566. 'rating' => $b->Rating1
  567. ));
  568. }
  569. // else
  570. // {
  571. // array_push($nested_data, array(
  572. // 'index' => $n1.$i,
  573. // 'docket_id' => $a->docket_id,
  574. // 'work_order_id' => $a->work_order_id,
  575. // 'nature_work' => $a->nature_work,
  576. // 'contractor_id' => $contractor,
  577. // 'installer_name' => '',
  578. // 'customer_id' => $building,
  579. // 'installer_id' => '',
  580. // 'end_job' => $a->end_job,
  581. // 'rating' => $a->Rating1
  582. // ));
  583. // }
  584. }
  585. }
  586. return \DataTables::of($nested_data)->make(true);
  587. }
  588. public function getContractorTeam()
  589. {
  590. $id = Auth::guard('contractor')->id();
  591. $user = StaffLaravel::with('StaffDetailLaravel')->find($id);
  592. $agent = StaffLaravel::with('CompanyLaravel','StaffDetailLaravel')->where('roles_access','Installer')->get();
  593. $i = 0;
  594. $nested_data = array();
  595. foreach ($agent as $a)
  596. {
  597. $i++; $n1 = '';
  598. if($a->company_id == $user->StaffDetailLaravel->company_id){
  599. array_push($nested_data, array(
  600. 'index' => $n1.$i,
  601. 'user_pic' => $a->StaffDetailLaravel->user_pic,
  602. 'name' => $a->StaffDetailLaravel->name,
  603. 'ic' => $a->StaffDetailLaravel->ic,
  604. 'email' => $a->StaffDetailLaravel->email,
  605. 'phone' => $a->StaffDetailLaravel->phone,
  606. 'company_id' => $a->CompanyLaravel->name,
  607. 'position' => $a->StaffDetailLaravel->position,
  608. 'action' => $a->_id
  609. ));
  610. }
  611. }
  612. return \DataTables::of($nested_data)->make(true);
  613. }
  614. public function addContractorTeam()
  615. {
  616. return view('contractor.add_contractor');
  617. }
  618. public function requestAddContractorTeam(Request $request)
  619. {
  620. $validator = Validator::make($request->all(), [
  621. 'password' => 'min:8|confirmed',
  622. ]);
  623. if ($validator->fails()) {
  624. return redirect()->back()->withInput()->withErrors($validator);
  625. }
  626. $user = StaffLaravel::where('email', $request->get('email'))->first();
  627. if(empty($user))
  628. {
  629. $pass = $this->encryptPassword('encrypt', $request->get('password'));
  630. $dealerD = new StaffDetailLaravel;
  631. $dealerD->name = $request->get('aname');
  632. $dealerD->ic = $request->get('ic');
  633. $dealerD->email = $request->get('email');
  634. $dealerD->phone = $request->get('phone');
  635. $dealerD->company_id = $request->get('vendor');
  636. $dealerD->color = '';
  637. $dealerD->position = 'Installer';
  638. $dealerD->password = $pass;
  639. $dealerD->user_pic = 'assets/avatar/user.png';
  640. // Save Login Dealer
  641. $loginD = new StaffLaravel;
  642. $loginD->email = $request->get('email');
  643. $loginD->password = $pass;
  644. $loginD->roles_access = 'Installer';
  645. $loginD->company_id = $request->get('vendor');
  646. $loginD->last_login_at = '';
  647. $loginD->last_login_ip = '';
  648. // $loginData = [
  649. // 'email' => $request->get('email'),
  650. // 'password' => $pass,
  651. // 'roles_access' => 'Sales',
  652. // 'last_login_at' => '',
  653. // 'last_login_ip' => '',
  654. // ];
  655. $company = CompanyLaravel::where('_id', $request->get('vendor'))->first();
  656. $company->stafflaravel()->save($loginD);
  657. $loginD->staffdetaillaravel()->save($dealerD);
  658. return redirect()->back()->with('success_msg', '<strong>Success!</strong> register <strong>'.$request->get('email').'</strong>');
  659. }
  660. else
  661. {
  662. return redirect()->back()->withInput()->withErrors('Email already exist!');
  663. }
  664. }
  665. public function generateDocketPDF($do)
  666. {
  667. $id = Auth::guard('contractor')->id();
  668. $user = Staff::with('StaffDetail')->find($id);
  669. $docket = Docket::with('WorkOrder')->where('docket_id',$do)->first();
  670. if(!empty($docket)){
  671. $form = Form::with('Subscriber','PackageDetail')->where('_id',$docket->WorkOrder->_id)->first();
  672. $address = '';
  673. if($form->type_application == 'R'){
  674. if($form->Subscriber->street != ''){
  675. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  676. }else {
  677. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  678. }
  679. }else if($form->type_application == 'B'){
  680. if ($form->Subscriber->unit_no != '') {
  681. $unit = $form->Subscriber->unit_no;
  682. } else {
  683. $unit = ' ';
  684. }
  685. if ($form->Subscriber->building_name == null) {
  686. $address = $unit . ' , ' . $form->Subscriber->street . ' , ' . $form->Subscriber->postcode . ' , ' . $form->Subscriber->city . ' , ' . $form->Subscriber->state;
  687. } else {
  688. $address = $unit . ' , ' . $form->Subscriber->street . ' , ' . $form->Subscriber->building_name . ' , ' . $form->Subscriber->postcode . ' , ' . $form->Subscriber->city . ' , ' . $form->Subscriber->state;
  689. }
  690. }
  691. if (empty($form->customer_category)){
  692. $category = '';
  693. }else {
  694. $category = $form->customer_category;
  695. }
  696. $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();
  697. $installer = Staff::with('StaffDetail')->where('_id',$docket->installer_id)->withTrashed()->first();
  698. if(empty($product)){
  699. $product = 'RMbps';
  700. }else {
  701. $product = $product->package_name;
  702. }
  703. $dateTime = Carbon::parse($docket->WorkOrder->dateTimeStart)->toDateTimeString();
  704. $edateTime = Carbon::parse($docket->end_job)->toDateTimeString();
  705. $pdf = PDF::loadView('pdf.docket-pdf',compact('docket','form','address','product','installer','dateTime','edateTime', 'category'));
  706. $pdf->setPaper('A4', 'potrait');
  707. return $pdf->stream();
  708. }
  709. }
  710. }