id)->first(); if(!empty($wo)){ $form = Form::with('PackageDetail','Subscriber')->where('_id',$wo->_id)->first(); if(!empty($form->Subscriber) && !empty($form->PackageDetail)){ return $this->sendResponse($form, '/v3/main/work-order-pdf/'.$request->id); }else{ return $this->sendError('Package or subscriber not exist!', ''); } }else{ return $this->sendError('Work order not exist!', ''); } } /** * Reset Work Order * * @return json */ public function resetWorkOrder(Request $request){ $wo = WorkOrder::where('wo',$request->id)->first(); if(!empty($wo)){ $docket = Docket::where('work_order_id', $request->id)->first(); if(!empty($docket)){ $docket->forceDelete(); $wo->docket_id = ''; $wo->status = 'Pending Contractor'; $wo->save(); $form = Form::where('_id', $wo->_id)->first(); if(!empty($form)){ $formH = new FormStatus; $formH->form_id = $form->_id; $formH->status_id = 17; $formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000); $formH->status = 'Reset Work Order'; $formH->desc = 'This work order has been reset by customer service'; $form->formstatus()->save($formH); } return $this->sendResponse($wo, 'Success Reset Work Order. You can check work order under category Pending Contractor'); }else{ return $this->sendError('Docket not exist!', ''); } }else{ return $this->sendError('Work order not exist!', ''); } } /** * Delete Work Order * * @return json */ public function deleteWorkOrder(Request $request){ $wo = WorkOrder::where('wo',$request->id)->first(); if(!empty($wo)){ $wo->forceDelete(); return $this->sendResponse($wo, 'Success Delete Work Order. You can access the form in customer tab under application form and re-assign work order again'); }else{ return $this->sendError('Work order not exist!', ''); } } /** * Get Work Order Controlelr * * @return json */ public function getWorkOrderData($status,$company_id,$startdate,$enddate,$type_work,$package,$building,$dealer){ if($status == 'pending-contractor'){ $status = 'Pending Contractor'; }else if($status == 'pending-installer'){ $status = 'Pending Installer'; }else if($status == 'pending-non-prelaid'){ $status = 'Pending Non Prelaid'; }else if($status == 'success-non-prelaid'){ $status = 'Success Non Prelaid'; }else if($status == 'reschedule'){ $status = 'Reschedule'; }else if($status == 'suspend'){ $status = 'Suspend'; }else if($status == 'completed'){ $status = 'Completed'; }else if($status == 'cancelled'){ $status = 'Cancelled'; } if($startdate == '-' && $enddate == '-'){ if($company_id == 'null'){ if($package == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) { $q->with('Subscriber','PackageDetail')->with('Company'); }])->with('Company','Docket')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'B' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); }else if($package == 'B' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('status',$status)->get(); } } else if($company_id != 'null'){ if($package == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) { $q->with('Subscriber','PackageDetail')->with('Company'); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'B' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'B' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->where('contractor_id',$company_id)->where('status',$status)->get(); } } }else if($startdate != '-'){ $start = $startdate.' 00:00:00'; if($enddate != '-'){ $end = $enddate.' 23:59:59'; }else{ $end = $startdate.' 23:59:59'; } if($company_id == 'null'){ if($package == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) { $q->with('Subscriber','PackageDetail')->with('Company'); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'B' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); }else if($package == 'B' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('status',$status)->get(); } } else if($company_id != 'null'){ if($package == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) { $q->with('Subscriber','PackageDetail')->with('Company'); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building == 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'R' && $building != 'null' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer,$building){ $q->with(['Subscriber' => function($q) use($building) { $q->where('building_name', $building); }],'PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'B' && $dealer == 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); }else if($package == 'B' && $dealer != 'null'){ $wo = WorkOrder::with(['Form' => function($q) use($package,$dealer){ $q->with('Subscriber','PackageDetail')->with('Company')->where('type_application', $package)->where('company_id', $dealer); }])->with('Company')->with(['Staff' => function($w){ $w->with('StaffDetail'); }])->orderBy('dateTimeStart', 'DESC')->whereBetween('dateTimeStart', array($start, $end))->where('contractor_id',$company_id)->where('status',$status)->get(); } } } return $wo; } public function displayWorkOrder($status,$company_id,$startdate,$enddate,$type_work,$package,$building,$dealer){ $i = 0; $curr = Carbon::now()->getTimestamp(); $nested_data = array(); $wo = $this->getWorkOrderData($status,$company_id,$startdate,$enddate,$type_work,$package,$building,$dealer); // return $wo; if(!empty($wo)){ foreach ($wo as $key => $w) { $n1 = ''; $do = ''; $labelD = ''; $reg_time = Carbon::createFromFormat('Y-m-d H:i:s', $w->created_at); $expiry_date = $reg_time->addDays(3); $expiry_date = $expiry_date->getTimestamp(); if($curr < $expiry_date) { $n1 = 'New/'; } else{ $n1 = ''; } if(!empty($w->Form->Subscriber) && !empty($w->Form->PackageDetail)){ if(!empty($w->Form->Company)){ $com = explode(' ',trim($w->Form->Company->name)); $labelD = 'D/'.$com[0].'.'; }else{ $labelD = ''; } if(!empty($w->Company)){ $company = $w->Company->name; } $installer = ''; if(!empty($w->Staff)){ $installer = $w->Staff->StaffDetail->name; } $docket = ''; if($status == 'completed' || $status == 'cancelled'){ if(!empty($w->Docket)){ $docket = $w->Docket->docket_id; } } if($w->Form->PackageDetail->name == '1'){ $product = $w->Form->PackageDetail->name .'Gbps'; }else if($w->Form->PackageDetail->name == 'NA'){ $product = $w->Form->PackageDetail->name; }else { $product = $w->Form->PackageDetail->name .'Mbps'; } $unit = ''; $building = ''; $address = ''; if($w->Form->type_application == 'R'){ $building = $w->Form->Subscriber->building_name; $unit = $w->Form->Subscriber->unit_no; if($w->Form->Subscriber->street != ''){ $address = $w->Form->Subscriber->street; }else { $address = '-'; } }else if($w->Form->type_application == 'B'){ if($w->Form->Subscriber->unit_no == ''){ $unit = '-'; }else { $unit = $w->Form->Subscriber->unit_no; } $building = $w->Form->Subscriber->company_name; $address = $w->Form->Subscriber->street; } if($w->nature_work == $type_work){ $i++; array_push($nested_data, array( 'formT' => $n1.$i.$w->Form->type_application, 'service' => $w->nature_work, 'wo' => $w->wo, 'name' => $labelD.$w->Form->Subscriber->name, 'phone' => $w->Form->Subscriber->phone1, 'unit' => $unit, 'building' => $building, 'city' => $w->Form->Subscriber->city, 'package' => $product, 'contractor' => $company, 'installer' => $installer, 'date' => date("d/m/Y", strtotime($w->dateTimeStart)), 'time' => date("h:i A", strtotime($w->dateTimeStart)), 'status' => $w->status, 'docket' => $docket, 'action' => $w->wo, )); }else if($type_work == 'null'){ $i++; array_push($nested_data, array( 'formT' => $n1.$i.$w->Form->type_application, 'service' => $w->nature_work, 'wo' => $w->wo, 'name' => $labelD.$w->Form->Subscriber->name, 'phone' => $w->Form->Subscriber->phone1, 'unit' => $unit, 'building' => $building, 'city' => $w->Form->Subscriber->city, 'package' => $product, 'contractor' => $company, 'installer' => $installer, 'date' => date("d/m/Y", strtotime($w->dateTimeStart)), 'time' => date("h:i A", strtotime($w->dateTimeStart)), 'status' => $w->status, 'docket' => $docket, 'action' => $w->wo, )); } } } } return \DataTables::of($nested_data)->make(true); } public function updateStatusWorkOrderNonPrelaid(Request $request){ $wod = WorkOrder::where('wo', $request->id)->first(); if(!empty($wod)){ $wod->status = 'Success Non Prelaid'; $wod->save(); $form = Form::where('_id',$wod->_id)->first(); $formH = new FormStatus; $formH->form_id = $form->_id; $formH->status_id = 7; $formH->date = new \MongoDB\BSON\UTCDateTime(time()*1000); $formH->status = 'Success Non Prelaid'; $formH->desc = 'Prelaid successfully'; $form->formstatus()->save($formH); return $this->sendResponse($wod, 'Successfully confirm assign/reassign installer'); }else{ return $this->sendError('Work order id cant be found!', ''); } } }