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 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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::groupBy('building_name')->orderBy('building_name')->get();
  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. if($w->installer_id != ''){
  185. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->first();
  186. $installer = $installer->StaffDetail->name;
  187. }
  188. if(empty($product)){
  189. $product = 'R Mbps';
  190. }else {
  191. $product = $product->package_name;
  192. }
  193. array_push($nested_data, array(
  194. 'formT' => $n1.$i.$w->Form->type_application,
  195. 'service' => $w->nature_work,
  196. 'wo' => $w->wo,
  197. 'name' => $name,
  198. 'phone' => $form->Subscriber->phone1,
  199. 'unit' => $unit,
  200. 'building' => $building,
  201. 'city' => $form->Subscriber->city,
  202. 'package' => $product,
  203. 'contractor' => $company->name,
  204. 'installer' => $installer,
  205. 'date' => date("d/m/Y", strtotime($w->dateTimeStart)),
  206. 'time' => date("h:i A", strtotime($w->dateTimeStart)),
  207. 'status' => $w->status,
  208. 'action' => $w->wo,
  209. ));
  210. }
  211. }
  212. return \DataTables::of($nested_data)->make(true);
  213. }
  214. public function getAllPendingNonPrelaid($year,$month,$day){
  215. $id = Auth::guard('contractor')->id();
  216. $user = Staff::with('StaffDetail')->find($id);
  217. $i = 0; $n1 = '';
  218. $curr = Carbon::now()->getTimestamp();
  219. $nested_data = array();
  220. if($year == 'null' && $month == 'null' && $day == 'null'){
  221. $wo = WorkOrder::with('Form')->where('status','Pending Non Prelaid')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->get();
  222. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  223. $timestamp = $year."-01-01 00:00:00.000Z";
  224. $masa = strtotime($timestamp);
  225. $go = Carbon::createFromTimestamp($masa);
  226. $go2 = Carbon::createFromTimestamp($masa);
  227. $end_year = $go2->endOfYear();
  228. $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();
  229. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  230. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  231. $masa = strtotime($timestamp);
  232. $go = Carbon::createFromTimestamp($masa);
  233. $go2 = Carbon::createFromTimestamp($masa);
  234. $end_year = $go2->endOfMonth();
  235. $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();
  236. }
  237. if(!empty($wo)){
  238. foreach ($wo as $key => $w) {
  239. $i++;
  240. $n1 = '';
  241. $reg_time = $w->updated_at;
  242. $expiry_date = $reg_time->addDays(3);
  243. $expiry_date = $expiry_date->getTimestamp();
  244. if($curr < $expiry_date) {
  245. $n1 = "New/";
  246. }
  247. $installer = '';
  248. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->Form->_id)->first();
  249. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$w->Form->type_application)->first();
  250. $company = Company::where('_id',$w->contractor_id)->first();
  251. if($w->installer_id != ''){
  252. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->first();
  253. if(!empty($installer)){
  254. $installer = $installer->StaffDetail->name;
  255. }
  256. }
  257. $building = ''; $unit = '-'; $name = '';
  258. if($form->type_application == 'R'){
  259. $building = $form->Subscriber->building_name;
  260. $unit = $form->Subscriber->unit_no;
  261. $name = $form->Subscriber->name;
  262. }else if($form->type_application == 'B'){
  263. if($form->Subscriber->unit_no != ''){
  264. $unit = '-';
  265. }else {
  266. $unit = $form->Subscriber->unit_no;
  267. }
  268. $building = '-';
  269. $name = $form->Subscriber->company_name;
  270. }
  271. if(empty($product)){
  272. $product = 'R Mbps';
  273. }else {
  274. $product = $product->package_name;
  275. }
  276. $date = ''; $time = '';
  277. if($w->dateTimeStart == ''){
  278. $date = '';
  279. $time = '';
  280. }else {
  281. $date = date("d/m/Y", strtotime($w->dateTimeStart));
  282. $time = date("h:i A", strtotime($w->dateTimeStart));
  283. }
  284. array_push($nested_data, array(
  285. 'formT' => $n1.$i.$w->Form->type_application,
  286. 'service' => $w->nature_work,
  287. 'wo' => $w->wo,
  288. 'name' => $name,
  289. 'phone' => $form->Subscriber->phone1,
  290. 'unit' => $unit,
  291. 'building' => $building,
  292. 'city' => $form->Subscriber->city,
  293. 'package' => $product,
  294. 'contractor' => $company->name,
  295. 'installer' => $installer,
  296. 'date' => $date,
  297. 'time' => $time,
  298. 'status' => $w->status,
  299. 'action' => $w->wo,
  300. ));
  301. }
  302. }
  303. return \DataTables::of($nested_data)->make(true);
  304. }
  305. public function getAllPendingInstaller($year,$month,$day){
  306. $id = Auth::guard('contractor')->id();
  307. $user = Staff::with('StaffDetail')->find($id);
  308. $i = 0; $n1 = '';
  309. $curr = Carbon::now()->getTimestamp();
  310. $nested_data = array();
  311. if($year == 'null' && $month == 'null' && $day == 'null'){
  312. $wo = WorkOrder::with('Form')->where('status','Pending Installer')->where('contractor_id',$user->StaffDetail->company_id)->orderBy('updated_at', 'desc')->get();
  313. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  314. $timestamp = $year."-01-01 00:00:00.000Z";
  315. $masa = strtotime($timestamp);
  316. $go = Carbon::createFromTimestamp($masa);
  317. $go2 = Carbon::createFromTimestamp($masa);
  318. $end_year = $go2->endOfYear();
  319. $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();
  320. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  321. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  322. $masa = strtotime($timestamp);
  323. $go = Carbon::createFromTimestamp($masa);
  324. $go2 = Carbon::createFromTimestamp($masa);
  325. $end_year = $go2->endOfMonth();
  326. $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();
  327. }
  328. if(!empty($wo)){
  329. foreach ($wo as $key => $w) {
  330. $i++;
  331. $n1 = '';
  332. $reg_time = $w->updated_at;
  333. $expiry_date = $reg_time->addDays(3);
  334. $expiry_date = $expiry_date->getTimestamp();
  335. if($curr < $expiry_date) {
  336. $n1 = "New/";
  337. }
  338. $installer = '';
  339. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->Form->_id)->first();
  340. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$w->Form->type_application)->first();
  341. $company = Company::where('_id',$w->contractor_id)->first();
  342. if($w->installer_id != ''){
  343. $installer = Staff::with('StaffDetail')->where('_id',$w->installer_id)->first();
  344. if(!empty($installer)){
  345. $installer = $installer->StaffDetail->name;
  346. }
  347. }
  348. $building = ''; $unit = '-'; $name = '';
  349. if($form->type_application == 'R'){
  350. $building = $form->Subscriber->building_name;
  351. $unit = $form->Subscriber->unit_no;
  352. $name = $form->Subscriber->name;
  353. }else if($form->type_application == 'B'){
  354. if($form->Subscriber->unit_no != ''){
  355. $unit = '-';
  356. }else {
  357. $unit = $form->Subscriber->unit_no;
  358. }
  359. $building = '-';
  360. $name = $form->Subscriber->company_name;
  361. }
  362. if(empty($product)){
  363. $product = 'R Mbps';
  364. }else {
  365. $product = $product->package_name;
  366. }
  367. array_push($nested_data, array(
  368. 'formT' => $n1.$i.$w->Form->type_application,
  369. 'service' => $w->nature_work,
  370. 'wo' => $w->wo,
  371. 'name' => $name,
  372. 'phone' => $form->Subscriber->phone1,
  373. 'unit' => $unit,
  374. 'building' => $building,
  375. 'city' => $form->Subscriber->city,
  376. 'package' => $product,
  377. 'contractor' => $company->name,
  378. 'installer' => $installer,
  379. 'date' => date("d/m/Y", strtotime($w->dateTimeStart)),
  380. 'time' => date("h:i A", strtotime($w->dateTimeStart)),
  381. 'status' => $w->status,
  382. 'action' => $w->wo,
  383. ));
  384. }
  385. }
  386. return \DataTables::of($nested_data)->make(true);
  387. }
  388. public function getAllCompleted($year,$month,$day){
  389. $id = Auth::guard('contractor')->id();
  390. $user = Staff::with('StaffDetail')->find($id);
  391. $i = 0; $n1 = '';
  392. $curr = Carbon::now()->getTimestamp();
  393. $nested_data = array();
  394. if($year == 'null' && $month == 'null' && $day == 'null'){
  395. $doc = Docket::with('WorkOrder')->orderBy('updated_at', 'desc')->get();
  396. }else if($year != 'null' && $month == 'null' && $day == 'null'){
  397. $timestamp = $year."-01-01 00:00:00.000Z";
  398. $masa = strtotime($timestamp);
  399. $go = Carbon::createFromTimestamp($masa);
  400. $go2 = Carbon::createFromTimestamp($masa);
  401. $end_year = $go2->endOfYear();
  402. $doc = Docket::with('WorkOrder')->orderBy('updated_at', 'desc')->whereBetween('dateTimeStart', [$go, $end_year])->get();
  403. }else if($year != 'null' && $month != 'null' && $day == 'null'){
  404. $timestamp = $year."-".$month."-01 00:00:00.000Z";
  405. $masa = strtotime($timestamp);
  406. $go = Carbon::createFromTimestamp($masa);
  407. $go2 = Carbon::createFromTimestamp($masa);
  408. $end_year = $go2->endOfMonth();
  409. $doc = Docket::with('WorkOrder')->orderBy('updated_at', 'desc')->whereBetween('dateTimeStart', [$go, $end_year])->get();
  410. }
  411. if(!empty($doc)){
  412. foreach ($doc as $key => $w) {
  413. $i++;
  414. $n1 = '';
  415. if(!empty($w->updated_at)) {
  416. $reg_time = $w->updated_at;
  417. $expiry_date = $reg_time->addDays(3);
  418. $expiry_date = $expiry_date->getTimestamp();
  419. if($curr < $expiry_date) {
  420. $n1 = "New/";
  421. }
  422. }else {
  423. $n1 = '';
  424. }
  425. $installer = '';
  426. if(!empty($w->WorkOrder->_id)){
  427. $form = Form::with('Subscriber','PackageDetail')->where('_id',$w->WorkOrder->_id)->first();
  428. $product = Product::where('speed',$form->PackageDetail->name)->where('formT',$form->type_application)->first();
  429. $company = Company::where('_id',$w->WorkOrder->contractor_id)->first();
  430. if($w->WorkOrder->installer_id != ''){
  431. $installer = Staff::with('StaffDetail')->withTrashed()->where('_id',$w->WorkOrder->installer_id)->first();
  432. $installer = $installer->StaffDetail->name;
  433. }
  434. if(empty($product)){
  435. $product = 'R Mbps';
  436. }else {
  437. $product = $product->package_name;
  438. }
  439. $building = ''; $unit = '-'; $name = '';
  440. if($form->type_application == 'R'){
  441. $building = $form->Subscriber->building_name;
  442. $unit = $form->Subscriber->unit_no;
  443. $name = $form->Subscriber->name;
  444. }else if($form->type_application == 'B'){
  445. if($form->Subscriber->unit_no != ''){
  446. $unit = '-';
  447. }else {
  448. $unit = $form->Subscriber->unit_no;
  449. }
  450. $building = '-';
  451. $name = $form->Subscriber->company_name;
  452. }
  453. if($w->WorkOrder->contractor_id == $user->StaffDetail->company_id){
  454. array_push($nested_data, array(
  455. 'formT' => $n1.$i.$form->type_application,
  456. 'service' => $w->nature_work,
  457. 'wo' => $w->WorkOrder->wo,
  458. 'name' => $name,
  459. 'phone' => $form->Subscriber->phone1,
  460. 'unit' => $unit,
  461. 'building' => $building,
  462. 'city' => $form->Subscriber->city,
  463. 'package' => $product,
  464. 'contractor' => $company->name,
  465. 'installer' => $installer,
  466. 'date' => date("d/m/Y", strtotime($w->end_job)),
  467. 'time' => date("h:i A", strtotime($w->end_job)),
  468. 'status' => $w->WorkOrder->status,
  469. 'docket' => $w->docket_id,
  470. 'action' => $w->WorkOrder->wo,
  471. ));
  472. }
  473. }
  474. }
  475. }
  476. return \DataTables::of($nested_data)->make(true);
  477. }
  478. public function generateWorkOrderPDF($wo){
  479. $id = Auth::guard('contractor')->id();
  480. $user = Staff::with('StaffDetail')->find($id);
  481. $wo = WorkOrder::where('wo',$wo)->first();
  482. $form = Form::with('PackageDetail','Subscriber')->where('_id',$wo->_id)->first();
  483. $product = Product::where('formT',$form->type_application)->where('speed',$form->PackageDetail->name)->first();
  484. if(empty($form->customer_category)){
  485. $category = '';
  486. }else {
  487. $category = $form->customer_category;
  488. }
  489. if(empty($product)){
  490. if($form->PackageDetail->name == "30"){
  491. $product = '30Mbps';
  492. }else{
  493. $product = 'RMbps';
  494. }
  495. }else {
  496. $product = $product->package_name;
  497. }
  498. $created_by = Staff::with('StaffDetail')->where("_id",$wo->created_by)->first();
  499. if(empty($created_by)){
  500. $created_by = '';
  501. }
  502. $created_at = Carbon::parse($wo->created_at)->toDateTimeString();
  503. $address = '';
  504. if($form->type_application == 'R'){
  505. if($form->Subscriber->street != ''){
  506. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->street. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  507. }else {
  508. $address = $form->Subscriber->unit_no. ' , '.$form->Subscriber->building_name. ' , '.$form->Subscriber->postcode. ' , '.$form->Subscriber->city. ' , '.$form->Subscriber->state;
  509. }
  510. }else if($form->type_application == 'B'){
  511. if ($form->Subscriber->unit_no != '') {
  512. $unit = $form->Subscriber->unit_no;
  513. } else {
  514. $unit = ' ';
  515. }
  516. if ($form->Subscriber->building_name == null) {
  517. $address = $unit . ' , ' . $form->Subscriber->street . ' , ' . $form->Subscriber->postcode . ' , ' . $form->Subscriber->city . ' , ' . $form->Subscriber->state;
  518. } else {
  519. $address = $unit . ' , ' . $form->Subscriber->street . ' , ' . $form->Subscriber->building_name . ' , ' . $form->Subscriber->postcode . ' , ' . $form->Subscriber->city . ' , ' . $form->Subscriber->state;
  520. }
  521. }
  522. $pdf = PDF::loadView('pdf.workorder-pdf',compact('wo','form','product','created_at','created_by','address', 'category'));
  523. $pdf->setPaper('A4', 'potrait');
  524. return $pdf->stream();
  525. }
  526. public function updateWorkOrder(Request $request){
  527. $id = Auth::guard('contractor')->id();
  528. $user = Staff::with('StaffDetail')->find($id);
  529. $wo = WorkOrder::where('wo',$request->work_order)->first();
  530. if(!empty($wo)){
  531. $wo->nature_work = $request->task;
  532. $wo->pppoe_username = $request->pppoe_username;
  533. $wo->onu = $request->onu_ont;
  534. $wo->router = $request->router;
  535. $wo->n_phone = $request->n_phone;
  536. $wo->no_phone = $request->no_phone;
  537. $wo->remarks_custservice = $request->remark;
  538. $wo->created_by = $user->_id;
  539. $wo->save();
  540. return redirect('/contractor/work-order/list');
  541. // return redirect()->back()->with('success_msg', 'Success! Update work order '.$request->get('work_order'));
  542. }else {
  543. return redirect()->back()->with('error_msg', 'Cant update work order '.$request->get('work_order'));
  544. }
  545. }
  546. public function rescheduleWorkOrder(Request $request){
  547. $wod = WorkOrder::where('wo',$request->wo)->first();
  548. if(!empty($wod)){
  549. $wod->dateTimeStart = $request->start;
  550. $wod->dateTimeEnd = $request->end;
  551. $wod->installer_id = $request->installer;
  552. $wod->status = $request->type_work;
  553. $wod->save();
  554. return 'success';
  555. }else{
  556. return 'false';
  557. }
  558. }
  559. public function cancelCreateWorkOrder(Request $request){
  560. $wod = WorkOrder::where('wo',$request->wo)->first();
  561. if(!empty($wod)){
  562. $wod->delete();
  563. return 'true';
  564. }else{
  565. return 'false';
  566. }
  567. }
  568. public function updateWorkOrderReschedule(Request $request)
  569. {
  570. $data = array();
  571. $wod = WorkOrder::where('wo',$request->wo)->first();
  572. $form = Form::where('_id',$wod->_id)->first();
  573. $installer = Staff::with('StaffDetail')->where('_id',$wod->installer_id)->withTrashed()->first();
  574. $name = '';
  575. if (!empty($installer)){
  576. $name = $installer->StaffDetail->name;
  577. }
  578. if(!empty($wod)){
  579. if($wod->status == 'Pending Contractor'){
  580. $wod->status = $request->type_work;
  581. $wod->installer_id = $request->installer;
  582. $wod->save();
  583. $formH = new FormStatus;
  584. $formH->form_id = $form->_id;
  585. $formH->status_id = 6;
  586. $formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000);
  587. $formH->status = 'Pending Installer';
  588. $formH->desc = 'This work order been assigned to particular installer ('.$name.')';
  589. $form->formstatus()->save($formH);
  590. array_push($data, array(
  591. 'result' => 'yes',
  592. ));
  593. }else {
  594. $wod->status = $request->type_work;
  595. $wod->installer_id = $request->installer;
  596. $wod->save();
  597. array_push($data, array(
  598. 'result' => 'yes',
  599. ));
  600. }
  601. }else{
  602. array_push($data, array(
  603. 'result' => 'no',
  604. ));
  605. }
  606. return response()->json($data);
  607. }
  608. public function updateStatusWorkOrder($wo){
  609. $wod = WorkOrder::where('wo',$wo)->first();
  610. if(!empty($wod)){
  611. $wod->status = 'Success Non Prelaid';
  612. $wod->save();
  613. $form = Form::where('_id',$wod->_id)->first();
  614. $formH = new FormStatus;
  615. $formH->form_id = $form->_id;
  616. $formH->status_id = 7;
  617. $formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000);
  618. $formH->status = 'Success Prelaid';
  619. $formH->desc = 'Prelaid successfully';
  620. $form->formstatus()->save($formH);
  621. return redirect('/contractor/work-order/list');
  622. }
  623. }
  624. }