You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WorkOrderController.php 25KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
  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 Carbon\Carbon;
  7. use Validator;
  8. use PDF;
  9. use App\Staff;
  10. use App\Model\StaffDetail;
  11. use App\Model\Form;
  12. use App\Model\Subscriber;
  13. use App\Model\WorkOrder;
  14. use App\Model\PackageDetail;
  15. use App\Model\Company;
  16. use App\Model\Coverage;
  17. use App\Model\Product;
  18. use App\Model\Docket;
  19. use App\Model\FormStatus;
  20. class WorkOrderController extends Controller
  21. {
  22. function random_code($limit) {
  23. return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
  24. }
  25. // function createWorkID($limit){
  26. // $number = WorkOrder::select('id','wo')->orderBy('created_at','DESC')->first();
  27. // $numberOnly = str_replace('WO-','',$number->wo);
  28. // $numberOnly++;
  29. // return $numberOnly;
  30. // $allowedNumbers = range(0, 9);
  31. // shuffle($allowedNumbers);
  32. // $digits = array_rand($allowedNumbers, $limit);
  33. // $number = '';
  34. // foreach($digits as $d){
  35. // $number .= $allowedNumbers[$d];
  36. // }
  37. // $unique_id = $number;
  38. // return $unique_id;
  39. // }
  40. public function viewContractorWork()
  41. {
  42. $id = Auth::guard('contractor')->id();
  43. $user = Staff::with('StaffDetail')->find($id);
  44. $pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  45. $sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  46. $rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
  47. $ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
  48. $cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());
  49. $pc = count(WorkOrder::where('status','Pending Contractor')->where('contractor_id',$user->StaffDetail->company_id)->get());
  50. $pi = count(WorkOrder::where('status','Pending Installer')->where('contractor_id',$user->StaffDetail->company_id)->get());
  51. return view('contractor.view_wo', compact('user','pp','sp','rs','ss','cm','pc','pi'));
  52. }
  53. public function editContractorWork($wo)
  54. {
  55. $id = Auth::guard('contractor')->id();
  56. $user = Staff::with('StaffDetail')->find($id);
  57. $pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  58. $sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  59. $rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
  60. $ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
  61. $cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());
  62. $w = WorkOrder::where('wo',$wo)->first();
  63. $form = Form::with('Subscriber','WorkOrder')->where('_id',$w->_id)->first();
  64. $coverage = Coverage::all();
  65. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->first();
  66. if(empty($installer)){
  67. $installer = '';
  68. }else {
  69. $installer = $installer->StaffDetail->name;
  70. }
  71. $company = Company::where('_id',$w->contractor_id)->first();
  72. return view('contractor.edit_wo', compact('user','pp','sp','rs','ss','cm','pc','pi','form','coverage','installer','company'));
  73. }
  74. public function updateCustomerDetail(Request $request){
  75. $form = Subscriber::with('Form')->where('subscriber_id',$request->subscriber_id)->first();
  76. $phone = ''; $street = ''; $fax = '';
  77. if($request->phone2 != null){
  78. $phone = $request->phone2;
  79. }
  80. if($form->Form->type_application == 'R'){
  81. if($request->street != null){
  82. $street = $request->street;
  83. }
  84. $cov = Coverage::where('building_name',$request->building)->first();
  85. $form->name = $request->name;
  86. $form->ic = $request->ic;
  87. $form->email = $request->email;
  88. $form->phone1 = $request->phone1;
  89. $form->phone2 = $phone;
  90. $form->unit_no = $request->unit_no;
  91. $form->building_name = $cov->building_name;
  92. $form->street = $street;
  93. $form->city = $cov->city;
  94. $form->postcode = $cov->postcode;
  95. $form->state = $cov->state;
  96. $form->save();
  97. return redirect()->back()->with('success_msg', 'Success! Update customer '.$request->get('name'));
  98. }else if($form->Form->type_application == 'B'){
  99. if($request->company_fax != null){
  100. $fax = $request->company_fax;
  101. }
  102. $form->company_reg = $request->company_reg;
  103. $form->company_num = $request->company_num;
  104. $form->company_fax = $fax;
  105. $form->company_name = $request->company_name;
  106. $form->unit_no = $request->unit_no;
  107. $form->street = $request->address;
  108. $form->city = $request->city;
  109. $form->postcode = $request->postcode;
  110. $form->state = $request->state;
  111. $form->name = $request->name;
  112. $form->ic = $request->ic;
  113. $form->designation = $request->designation;
  114. $form->email = $request->email;
  115. $form->phone1 = $request->phone1;
  116. $form->phone2 = $phone;
  117. $form->save();
  118. return redirect()->back()->with('success_msg', 'Success! Update customer '.$request->get('name'));
  119. }
  120. }
  121. public function viewRescheduleCalendar($wo){
  122. $id = Auth::guard('contractor')->id();
  123. $user = Staff::with('StaffDetail')->find($id);
  124. $pp = count(WorkOrder::where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  125. $sp = count(WorkOrder::where('status','Success Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->get());
  126. $rs = count(WorkOrder::where('status','Reschedule')->where('contractor_id',$user->StaffDetail->company_id)->get());
  127. $ss = count(WorkOrder::where('status','Suspend')->where('contractor_id',$user->StaffDetail->company_id)->get());
  128. $cm = count(WorkOrder::where('status','Completed')->where('contractor_id',$user->StaffDetail->company_id)->get());
  129. $installer = StaffDetail::where('company_id',$user->StaffDetail->company_id)->where('position','Installer')->get();
  130. $today = Carbon::today();
  131. return view('contractor.reschedule_work_order', compact('user','pp','sp','rs','ss','cm','company','wo','today','installer'));
  132. }
  133. public function getAllPendingContractor($year,$month,$day){
  134. $id = Auth::guard('contractor')->id();
  135. $user = Staff::with('StaffDetail')->find($id);
  136. $i = 0; $n1 = '';
  137. $curr = Carbon::now()->getTimestamp();
  138. $nested_data = array();
  139. if($month == 'null' && $day == 'null'){
  140. $wo = WorkOrder::with('Form')->where('status','Pending Contractor')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->get();
  141. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  142. $timestamp = $year."-01-01 00:00:00.000Z";
  143. $masa = strtotime($timestamp);
  144. $go = Carbon::createFromTimestamp($masa);
  145. $go2 = Carbon::createFromTimestamp($masa);
  146. $end_year = $go2->endOfYear();
  147. $wo = WorkOrder::with('Form')->where('status','Pending Contractor')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
  148. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  149. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  150. $masa = strtotime($timestamp);
  151. $go = Carbon::createFromTimestamp($masa);
  152. $go2 = Carbon::createFromTimestamp($masa);
  153. $end_year = $go2->endOfMonth();
  154. $wo = WorkOrder::with('Form')->where('status','Pending Contractor')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
  155. }
  156. if(!empty($wo)){
  157. foreach ($wo as $key => $w) {
  158. $i++;
  159. $n1 = '';
  160. $reg_time = $w->updated_at;
  161. $expiry_date = $reg_time->addDays(3);
  162. $expiry_date = $expiry_date->getTimestamp();
  163. if($curr < $expiry_date) {
  164. $n1 = "New/";
  165. }
  166. $installer = '';
  167. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->Form->_id)->first();
  168. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$w->Form->type_application)->first();
  169. $company = Company::where('_id',$w->contractor_id)->first();
  170. $building = ''; $unit = '-'; $name = '';
  171. if($form->type_application == 'R'){
  172. $building = $form->Subscriber->building_name;
  173. $unit = $form->Subscriber->unit_no;
  174. $name = $form->Subscriber->name;
  175. }else if($form->type_application == 'B'){
  176. if($form->Subscriber->unit_no != ''){
  177. $unit = '-';
  178. }else {
  179. $unit = $form->Subscriber->unit_no;
  180. }
  181. $building = '-';
  182. $name = $form->Subscriber->company_name;
  183. }
  184. info('contractor242' . $w->installer_id . $w->wo);
  185. if($w->installer_id != ''){
  186. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->withTrashed()->first();
  187. $installer = $installer->StaffDetail->name;
  188. }
  189. if(empty($product)){
  190. $product = 'R Mbps';
  191. }else {
  192. $product = $product->package_name;
  193. }
  194. array_push($nested_data, array(
  195. 'formT' => $n1.$i.$w->Form->type_application,
  196. 'service' => $w->nature_work,
  197. 'wo' => $w->wo,
  198. 'name' => $name,
  199. 'phone' => $form->Subscriber->phone1,
  200. 'unit' => $unit,
  201. 'building' => $building,
  202. 'city' => $form->Subscriber->city,
  203. 'package' => $product,
  204. 'contractor' => $company->name,
  205. 'installer' => $installer,
  206. 'date' => date("d/m/Y", strtotime($w->dateTimeStart)),
  207. 'time' => date("h:i A", strtotime($w->dateTimeStart)),
  208. 'status' => $w->status,
  209. 'action' => $w->wo,
  210. ));
  211. }
  212. }
  213. return \DataTables::of($nested_data)->make(true);
  214. }
  215. public function getAllPendingNonPrelaid($year,$month,$day){
  216. $id = Auth::guard('contractor')->id();
  217. $user = Staff::with('StaffDetail')->find($id);
  218. $i = 0; $n1 = '';
  219. $curr = Carbon::now()->getTimestamp();
  220. $nested_data = array();
  221. if($year == 'null' && $month == 'null' && $day == 'null'){
  222. $wo = WorkOrder::with('Form')->where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->get();
  223. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  224. $timestamp = $year."-01-01 00:00:00.000Z";
  225. $masa = strtotime($timestamp);
  226. $go = Carbon::createFromTimestamp($masa);
  227. $go2 = Carbon::createFromTimestamp($masa);
  228. $end_year = $go2->endOfYear();
  229. $wo = WorkOrder::with('Form')->where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
  230. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  231. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  232. $masa = strtotime($timestamp);
  233. $go = Carbon::createFromTimestamp($masa);
  234. $go2 = Carbon::createFromTimestamp($masa);
  235. $end_year = $go2->endOfMonth();
  236. $wo = WorkOrder::with('Form')->where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
  237. }
  238. if(!empty($wo)){
  239. foreach ($wo as $key => $w) {
  240. $i++;
  241. $n1 = '';
  242. $reg_time = $w->updated_at;
  243. $expiry_date = $reg_time->addDays(3);
  244. $expiry_date = $expiry_date->getTimestamp();
  245. if($curr < $expiry_date) {
  246. $n1 = "New/";
  247. }
  248. $installer = '';
  249. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->Form->_id)->first();
  250. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$w->Form->type_application)->first();
  251. $company = Company::where('_id',$w->contractor_id)->first();
  252. if($w->installer_id != ''){
  253. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->first();
  254. if(!empty($installer)){
  255. $installer = $installer->StaffDetail->name;
  256. }
  257. }
  258. $building = ''; $unit = '-'; $name = '';
  259. if($form->type_application == 'R'){
  260. $building = $form->Subscriber->building_name;
  261. $unit = $form->Subscriber->unit_no;
  262. $name = $form->Subscriber->name;
  263. }else if($form->type_application == 'B'){
  264. if($form->Subscriber->unit_no != ''){
  265. $unit = '-';
  266. }else {
  267. $unit = $form->Subscriber->unit_no;
  268. }
  269. $building = '-';
  270. $name = $form->Subscriber->company_name;
  271. }
  272. if(empty($product)){
  273. $product = 'R Mbps';
  274. }else {
  275. $product = $product->package_name;
  276. }
  277. $date = ''; $time = '';
  278. if($w->dateTimeStart == ''){
  279. $date = '';
  280. $time = '';
  281. }else {
  282. $date = date("d/m/Y", strtotime($w->dateTimeStart));
  283. $time = date("h:i A", strtotime($w->dateTimeStart));
  284. }
  285. array_push($nested_data, array(
  286. 'formT' => $n1.$i.$w->Form->type_application,
  287. 'service' => $w->nature_work,
  288. 'wo' => $w->wo,
  289. 'name' => $name,
  290. 'phone' => $form->Subscriber->phone1,
  291. 'unit' => $unit,
  292. 'building' => $building,
  293. 'city' => $form->Subscriber->city,
  294. 'package' => $product,
  295. 'contractor' => $company->name,
  296. 'installer' => $installer,
  297. 'date' => $date,
  298. 'time' => $time,
  299. 'status' => $w->status,
  300. 'action' => $w->wo,
  301. ));
  302. }
  303. }
  304. return \DataTables::of($nested_data)->make(true);
  305. }
  306. public function getAllPendingInstaller($year,$month,$day){
  307. $id = Auth::guard('contractor')->id();
  308. $user = Staff::with('StaffDetail')->find($id);
  309. $i = 0; $n1 = '';
  310. $curr = Carbon::now()->getTimestamp();
  311. $nested_data = array();
  312. if($year == 'null' && $month == 'null' && $day == 'null'){
  313. $wo = WorkOrder::with('Form')->where('status','Pending Installer')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->get();
  314. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  315. $timestamp = $year."-01-01 00:00:00.000Z";
  316. $masa = strtotime($timestamp);
  317. $go = Carbon::createFromTimestamp($masa);
  318. $go2 = Carbon::createFromTimestamp($masa);
  319. $end_year = $go2->endOfYear();
  320. $wo = WorkOrder::with('Form')->where('status','Pending Installer')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
  321. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  322. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  323. $masa = strtotime($timestamp);
  324. $go = Carbon::createFromTimestamp($masa);
  325. $go2 = Carbon::createFromTimestamp($masa);
  326. $end_year = $go2->endOfMonth();
  327. $wo = WorkOrder::with('Form')->where('status','Pending Installer')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->whereBetween('created_at', [$go, $end_year])->get();
  328. }
  329. if(!empty($wo)){
  330. foreach ($wo as $key => $w) {
  331. $i++;
  332. $n1 = '';
  333. $reg_time = $w->updated_at;
  334. $expiry_date = $reg_time->addDays(3);
  335. $expiry_date = $expiry_date->getTimestamp();
  336. if($curr < $expiry_date) {
  337. $n1 = "New/";
  338. }
  339. $installer = '';
  340. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->Form->_id)->first();
  341. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$w->Form->type_application)->first();
  342. $company = Company::where('_id',$w->contractor_id)->first();
  343. if($w->installer_id != ''){
  344. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->first();
  345. if(!empty($installer)){
  346. $installer = $installer->StaffDetail->name;
  347. }
  348. }
  349. $building = ''; $unit = '-'; $name = '';
  350. if($form->type_application == 'R'){
  351. $building = $form->Subscriber->building_name;
  352. $unit = $form->Subscriber->unit_no;
  353. $name = $form->Subscriber->name;
  354. }else if($form->type_application == 'B'){
  355. if($form->Subscriber->unit_no != ''){
  356. $unit = '-';
  357. }else {
  358. $unit = $form->Subscriber->unit_no;
  359. }
  360. $building = '-';
  361. $name = $form->Subscriber->company_name;
  362. }
  363. if(empty($product)){
  364. $product = 'R Mbps';
  365. }else {
  366. $product = $product->package_name;
  367. }
  368. array_push($nested_data, array(
  369. 'formT' => $n1.$i.$w->Form->type_application,
  370. 'service' => $w->nature_work,
  371. 'wo' => $w->wo,
  372. 'name' => $name,
  373. 'phone' => $form->Subscriber->phone1,
  374. 'unit' => $unit,
  375. 'building' => $building,
  376. 'city' => $form->Subscriber->city,
  377. 'package' => $product,
  378. 'contractor' => $company->name,
  379. 'installer' => $installer,
  380. 'date' => date("d/m/Y", strtotime($w->dateTimeStart)),
  381. 'time' => date("h:i A", strtotime($w->dateTimeStart)),
  382. 'status' => $w->status,
  383. 'action' => $w->wo,
  384. ));
  385. }
  386. }
  387. return \DataTables::of($nested_data)->make(true);
  388. }
  389. public function getAllCompleted($year,$month,$day){
  390. $id = Auth::guard('contractor')->id();
  391. $user = Staff::with('StaffDetail')->find($id);
  392. $i = 0; $n1 = '';
  393. $curr = Carbon::now()->getTimestamp();
  394. $nested_data = array();
  395. if($year == 'null' && $month == 'null' && $day == 'null'){
  396. $doc = Docket::with('WorkOrder')->orderBy('updated_at', 'desc')->get();
  397. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  398. $timestamp = $year."-01-01 00:00:00.000Z";
  399. $masa = strtotime($timestamp);
  400. $go = Carbon::createFromTimestamp($masa);
  401. $go2 = Carbon::createFromTimestamp($masa);
  402. $end_year = $go2->endOfYear();
  403. $doc = Docket::with('WorkOrder')->orderBy('updated_at', 'desc')->whereBetween('dateTimeStart', [$go, $end_year])->get();
  404. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  405. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  406. $masa = strtotime($timestamp);
  407. $go = Carbon::createFromTimestamp($masa);
  408. $go2 = Carbon::createFromTimestamp($masa);
  409. $end_year = $go2->endOfMonth();
  410. $doc = Docket::with('WorkOrder')->orderBy('updated_at', 'desc')->whereBetween('dateTimeStart', [$go, $end_year])->get();
  411. }
  412. if(!empty($doc)){
  413. foreach ($doc as $key => $w) {
  414. $i++;
  415. $n1 = '';
  416. if(!empty($w->updated_at)) {
  417. $reg_time = $w->updated_at;
  418. $expiry_date = $reg_time->addDays(3);
  419. $expiry_date = $expiry_date->getTimestamp();
  420. if($curr < $expiry_date) {
  421. $n1 = "New/";
  422. }
  423. }else {
  424. $n1 = '';
  425. }
  426. $installer = '';
  427. if(!empty($w->WorkOrder->_id)){
  428. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->WorkOrder->_id)->first();
  429. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$form->type_application)->first();
  430. $company = Company::where('_id',$w->WorkOrder->contractor_id)->first();
  431. if($w->WorkOrder->installer_id != ''){
  432. $installer = Staff::with('StaffDetail')->withTrashed()->where('_id',$w->WorkOrder->installer_id)->first();
  433. $installer = $installer->StaffDetail->name;
  434. }
  435. if(empty($product)){
  436. $product = 'R Mbps';
  437. }else {
  438. $product = $product->package_name;
  439. }
  440. $building = ''; $unit = '-'; $name = '';
  441. if($form->type_application == 'R'){
  442. $building = $form->Subscriber->building_name;
  443. $unit = $form->Subscriber->unit_no;
  444. $name = $form->Subscriber->name;
  445. }else if($form->type_application == 'B'){
  446. if($form->Subscriber->unit_no != ''){
  447. $unit = '-';
  448. }else {
  449. $unit = $form->Subscriber->unit_no;
  450. }
  451. $building = '-';
  452. $name = $form->Subscriber->company_name;
  453. }
  454. if($w->WorkOrder->contractor_id == $user->StaffDetail->company_id){
  455. array_push($nested_data, array(
  456. 'formT' => $n1.$i.$form->type_application,
  457. 'service' => $w->nature_work,
  458. 'wo' => $w->WorkOrder->wo,
  459. 'name' => $name,
  460. 'phone' => $form->Subscriber->phone1,
  461. 'unit' => $unit,
  462. 'building' => $building,
  463. 'city' => $form->Subscriber->city,
  464. 'package' => $product,
  465. 'contractor' => $company->name,
  466. 'installer' => $installer,
  467. 'date' => date("d/m/Y", strtotime($w->end_job)),
  468. 'time' => date("h:i A", strtotime($w->end_job)),
  469. 'status' => $w->WorkOrder->status,
  470. 'docket' => $w->docket_id,
  471. 'action' => $w->WorkOrder->wo,
  472. ));
  473. }
  474. }
  475. }
  476. }
  477. return \DataTables::of($nested_data)->make(true);
  478. }
  479. public function generateWorkOrderPDF($wo){
  480. $id = Auth::guard('contractor')->id();
  481. $user = Staff::with('StaffDetail')->find($id);
  482. $wo = WorkOrder::where('wo',$wo)->first();
  483. $form = Form::with('PackageDetail','Subscriber')->where('_id',$wo->_id)->first();
  484. $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();
  485. if(empty($product)){
  486. if($form->PackageDetail->name == "30"){
  487. $product = '30Mbps';
  488. }else{
  489. $product = 'RMbps';
  490. }
  491. }else {
  492. $product = $product->package_name;
  493. }
  494. $created_by = Staff::with('StaffDetail')->where("_id",$wo->created_by)->first();
  495. if(empty($created_by)){
  496. $created_by = '';
  497. }
  498. $created_at = Carbon::parse($wo->created_at)->toDateTimeString();
  499. $address = '';
  500. if($form->type_application == 'R'){
  501. if($form->Subscriber->street != ''){
  502. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  503. }else {
  504. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  505. }
  506. }else if($form->type_application == 'B'){
  507. if($form->Subscriber->unit_no != ''){
  508. $address = $form->Subscriber->unit_no. ', '.$form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  509. }else {
  510. $address = $form->Subscriber->company_name. ', '.$form->Subscriber->street. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  511. }
  512. }
  513. $pdf = PDF::loadView('pdf.workorder-pdf',compact('wo','form','product','created_at','created_by','address'));
  514. $pdf->setPaper('A4', 'potrait');
  515. return $pdf->stream();
  516. }
  517. public function updateWorkOrder(Request $request){
  518. $id = Auth::guard('contractor')->id();
  519. $user = Staff::with('StaffDetail')->find($id);
  520. $wo = WorkOrder::where('wo',$request->work_order)->first();
  521. if(!empty($wo)){
  522. $wo->nature_work = $request->task;
  523. $wo->pppoe_username = $request->pppoe_username;
  524. $wo->onu = $request->onu_ont;
  525. $wo->router = $request->router;
  526. $wo->n_phone = $request->n_phone;
  527. $wo->no_phone = $request->no_phone;
  528. $wo->remarks_custservice = $request->remark;
  529. $wo->created_by = $user->_id;
  530. $wo->save();
  531. return redirect('/contractor/work-order/list');
  532. // return redirect()->back()->with('success_msg', 'Success! Update work order '.$request->get('work_order'));
  533. }else {
  534. return redirect()->back()->with('error_msg', 'Cant update work order '.$request->get('work_order'));
  535. }
  536. }
  537. public function rescheduleWorkOrder(Request $request){
  538. $wod = WorkOrder::where('wo',$request->wo)->first();
  539. if(!empty($wod)){
  540. $wod->dateTimeStart = $request->start;
  541. $wod->dateTimeEnd = $request->end;
  542. $wod->installer_id = $request->installer;
  543. $wod->status = $request->type_work;
  544. $wod->save();
  545. return 'success';
  546. }else{
  547. return 'false';
  548. }
  549. }
  550. public function cancelCreateWorkOrder(Request $request){
  551. $wod = WorkOrder::where('wo',$request->wo)->first();
  552. if(!empty($wod)){
  553. $wod->delete();
  554. return 'true';
  555. }else{
  556. return 'false';
  557. }
  558. }
  559. public function updateWorkOrderReschedule(Request $request)
  560. {
  561. $data = array();
  562. $wod = WorkOrder::where('wo',$request->wo)->first();
  563. $form = Form::where('_id',$wod->_id)->first();
  564. $installer = Staff::with('StaffDetail')->where('_id',$wod->installer_id)->first();
  565. $name = '';
  566. if (!empty($installer)){
  567. $name = $installer->StaffDetail->name;
  568. }
  569. if(!empty($wod)){
  570. if($wod->status == 'Pending Contractor'){
  571. $wod->status = $request->type_work;
  572. $wod->installer_id = $request->installer;
  573. $wod->save();
  574. $formH = new FormStatus;
  575. $formH->form_id = $form->_id;
  576. $formH->status_id = 6;
  577. $formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000);
  578. $formH->status = 'Pending Installer';
  579. $formH->desc = 'This work order been assigned to particular installer ('.$name.')';
  580. $form->formstatus()->save($formH);
  581. array_push($data, array(
  582. 'result' => 'yes',
  583. ));
  584. }else {
  585. $wod->status = $request->type_work;
  586. $wod->installer_id = $request->installer;
  587. $wod->save();
  588. array_push($data, array(
  589. 'result' => 'yes',
  590. ));
  591. }
  592. }else{
  593. array_push($data, array(
  594. 'result' => 'no',
  595. ));
  596. }
  597. return response()->json($data);
  598. }
  599. public function updateStatusWorkOrder($wo){
  600. $wod = WorkOrder::where('wo',$wo)->first();
  601. if(!empty($wod)){
  602. $wod->status = 'Success Non Prelaid';
  603. $wod->save();
  604. $form = Form::where('_id',$wod->_id)->first();
  605. $formH = new FormStatus;
  606. $formH->form_id = $form->_id;
  607. $formH->status_id = 7;
  608. $formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000);
  609. $formH->status = 'Success Prelaid';
  610. $formH->desc = 'Prelaid successfully';
  611. $form->formstatus()->save($formH);
  612. return redirect('/contractor/work-order/list');
  613. }
  614. }
  615. }