<?php

namespace App\Http\Controllers\DS;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use File;
use Validator;
use PDF;
use Config;
Use Mail;

Use App\StaffOld;
Use App\Staff;
Use App\Model\StaffDetail;
Use App\Model\Company;

Use App\Model\OldBusiness;
Use App\Model\OldResidential;
Use App\Model\OldDocket;
Use App\Model\OrderHistory;
use App\Model\OldWorkOrder;

Use App\Model\Subscriber;
Use App\Model\Coverage;
Use App\Model\Form;
Use App\Model\PackageDetail;
Use App\Model\WorkOrder;
Use App\Model\Docket;
Use App\Model\FormStatus;

class TestController extends Controller {

	public function sortBy()
	{
		$i = 0;
		$arr = array();
		$docket=DocketLaravel::orderBy('created_at', 'desc')->get();
		$nested_data = array();

		foreach($docket as $a)
		{
			$customer = Subscriber::where('_id', $a->customer_id)->first();
			$business = Business::where('_id', $a->customer_id)->first();				

			if(!empty($customer))
			{
				$arr[]=$a;
			}

			if(!empty($business))
			{
				$arr[]=$a;
			};
		}

		foreach($arr as $b)
		{
			$i++; $n1 = '';

			$tempContractor = CompanyLaravel::where('_id', $b->contractor_id)->first();
			$contractor = $tempContractor->name;

			$tempInstaller = StaffDetailLaravel::where('_id', $b->installer_id)->first();

			$tempCustomer = Subscriber::where('_id', $b->customer_id)->first();
			$tempBusiness = Business::where('_id', $b->customer_id)->first();

			if(!empty($tempCustomer))
			{
				$building = $tempCustomer->building;
			}
			else
			{
				$building = $tempBusiness->company_name;
			}

			if(!empty($tempInstaller))
			{
				array_push($nested_data, array(
					'index' => $n1.$i,
					'docket_id' => $b->docket_id,
					'work_order_id' => $b->work_order_id,
					'nature_work' => $b->nature_work,
					'contractor_id' => $contractor,
					'installer_name' => $tempInstaller->name,
					'customer_id' => $building,
					'installer_id' => $tempInstaller->phone,
					'end_job' => $b->end_job,
					'rating' => $b->Rating1
				));
			}
			else
			{
				array_push($nested_data, array(
					'index' => $n1.$i,
					'docket_id' => $a->docket_id,
					'work_order_id' => $a->work_order_id,
					'nature_work' => $a->nature_work,
					'contractor_id' => $contractor,
					'installer_name' => '',
					'customer_id' => $building,
					'installer_id' => '',
					'end_job' => $a->end_job,
					'rating' => $a->Rating1
				));
			}	
		}

		foreach($nested_data as $c)
		{
			echo $c["end_job"]."<br>";
		}
	}

	function random_code($limit) {
		return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
	}
	

	//*********************************************************************** Check Work Order  & Form *******************************************************/
	public function checkAvailableWorkOrder(){

		$oldWork = WorkDetail::where('formT','R')->get()->toArray();
		$subs = Subscriber::where('dealer','=','')->get();

		$a = array();

		foreach($subs as $s){
			if(array_search($s->wo, array_column($oldWork, 'wo')) !== False) {
				$a[] = $s;
			} 
		}

		dd($a);
	}
	//*********************************************************************** Convert Form, Subscribers, Package Form, Work Order *******************************************************/
	public function convertFormAndRelation(){
		$newCoverage = Form::get()->toArray();
		$oldCoverage = OldResidential::all();
		$count = count($oldCoverage);
		$id = 0; $i = 0;
		foreach($oldCoverage as $c){

			if(array_search($c->_id, array_column($newCoverage, '_id')) !== False) {
			} else {

				if($c->dealer == ''){

					$sl = Form::create([
						'_id' => new \MongoDB\BSON\ObjectId($c->_id),
						'type_service' => $c->type_application,
						'type_application' => 'R',
						'status_payment' => $c->status,
						'status_email' => $c->status1,
						'remark_form' => $c->remarks,
						'dealer_id' => $c->dealer,
						'company_id' => $c->dealer_company,
						'created_at' => new \MongoDB\BSON\UTCDateTime($c->reg_time*1000)
					]);

					$su_id = '';
					do {
						$su_id = strtoupper('CBB-'.$this->random_code(6).'R');
					} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

					$sdl = new Subscriber();
					$sdl->subscriber_id = $su_id;
					$sdl->name = $c->name;
					$sdl->ic = $c->ic;
					$sdl->citizen = $c->citizen;
					$sdl->gender = $c->gender;
					$sdl->race = $c->race;
					$sdl->email = $c->email;
					$sdl->phone1 = $c->phone1;
					$sdl->phone2 = $c->phone2;
					$sdl->unit_no = $c->unit_no;
					$sdl->building_name = $c->building;
					$sdl->street = $c->street;
					$sdl->postcode = $c->postcode;
					$sdl->city = $c->city;
					$sdl->state = $c->state;
					$sdl->front_ic = str_replace("data","/document",$c->imagefront);
					$sdl->back_ic = str_replace("data","/document",$c->imageback);
					$sdl->signature = str_replace("data","/document",$c->signature);
					$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

					$packageD = new PackageDetail();
					$packageD->contract = $c->contract;
					$packageD->name = $c->package;
					$packageD->montly_fee = '';
					$packageD->voice_fee = $c->voice;
					$packageD->deposit = '';
					$packageD->upfront_payment = '';
					$packageD->rfs = $c->rfs;

					if($c->wo != ''){
						$getWo = OldWorkOrder::all();

						foreach($getWo as $w){
							if($w->wo == $c->wo){
								$woD = new WorkOrder();
								$woD->img_url = '/assets/img/activation_icon_nRead1.png';
								$woD->wo = $w->wo;
								$woD->nature_work = $w->nature_work;
								$woD->sub_category = $w->sub_category;
								$woD->dateTimeStart = $w->dateTimeStart;
								$woD->dateTimeEnd = $w->dateTimeEnd;
								$woD->contractor_id = $w->contractor_id;
								$woD->installer_id = $w->installer_id;
								$woD->onu = $w->onu;
								$woD->router = $w->router;
								$woD->need_phone = $w->n_phone;
								$woD->no_phone = $w->no_phone;
								$woD->pppoe_username = $w->pppoe_username;
								$woD->pppoe_password = '';
								$woD->docket_id = $w->docket;
								$woD->remarks_custservice = $w->remarks;
								$woD->remarks_installer = $w->remarks_inst;
								$woD->status = $w->status;
								$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
								$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

								$sl->workorder()->save($woD);
							}
						}
					}

					$sl->subscriber()->save($sdl);
					$sl->packagedetail()->save($packageD);

					$i++;

				}else {

					$sl = new Form();
					$sl->_id = new \MongoDB\BSON\ObjectId($c->_id);
					$sl->type_service = $c->type_application;
					$sl->type_application = 'R';
					$sl->status_payment = $c->status;
					$sl->status_email = $c->status1;
					$sl->remark_form = $c->remarks;
					$sl->dealer_id = $c->dealer;
					$sl->company_id = $c->dealer_company;
					$sl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

					// staff
					$staff = Staff::where('_id',$c->dealer)->first();
					// dd($c->dealer);
					$staff->form()->save($sl);

					$su_id = '';
					do {
						$su_id = strtoupper('CBB-'.$this->random_code(6).'R');
					} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

					$sdl = new Subscriber();
					$sdl->subscriber_id = $su_id;
					$sdl->name = $c->name;
					$sdl->ic = $c->ic;
					$sdl->citizen = $c->citizen;
					$sdl->gender = $c->gender;
					$sdl->race = $c->race;
					$sdl->email = $c->email;
					$sdl->phone1 = $c->phone1;
					$sdl->phone2 = $c->phone2;
					$sdl->unit_no = $c->unit_no;
					$sdl->building_name = $c->building;
					$sdl->street = $c->street;
					$sdl->postcode = $c->postcode;
					$sdl->city = $c->city;
					$sdl->state = $c->state;
					$sdl->front_ic = str_replace("data","/document",$c->imagefront);
					$sdl->back_ic = str_replace("data","/document",$c->imageback);
					$sdl->signature = str_replace("data","/document",$c->signature);
					$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

					$packageD = new PackageDetail();
					$packageD->contract = $c->contract;
					$packageD->name = $c->package;
					$packageD->montly_fee = '';
					$packageD->voice_fee = $c->voice;
					$packageD->deposit = '';
					$packageD->upfront_payment = '';
					$packageD->rfs = $c->rfs;

					if($c->wo != ''){
						$getWo = OldWorkOrder::all();

						foreach($getWo as $w){
							if($w->wo == $c->wo){
								$woD = new WorkOrder();
								$woD->img_url = '/assets/img/activation_icon_nRead1.png';
								$woD->wo = $w->wo;
								$woD->nature_work = $w->nature_work;
								$woD->sub_category = $w->sub_category;
								$woD->dateTimeStart = $w->dateTimeStart;
								$woD->dateTimeEnd = $w->dateTimeEnd;
								$woD->contractor_id = $w->contractor_id;
								$woD->installer_id = $w->installer_id;
								$woD->onu = $w->onu;
								$woD->router = $w->router;
								$woD->need_phone = $w->n_phone;
								$woD->no_phone = $w->no_phone;
								$woD->pppoe_username = $w->pppoe_username;
								$woD->pppoe_password = '';
								$woD->docket_id = $w->docket;
								$woD->remarks_custservice = $w->remarks;
								$woD->remarks_installer = $w->remarks_inst;
								$woD->status = $w->status;
								$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
								$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

								$sl->workorder()->save($woD);
							}
						}
					}

					$sl->subscriber()->save($sdl);
					$sl->packagedetail()->save($packageD);

					$id++;
				}
			}
		}

		dd('Semua- '.$count. ' -D ' .$id .' -ND '.$i);
	}

	public function convertFormAndRelationBusiness(){
		$newCoverage = Form::get()->toArray();
		$oldCoverage = OldBusiness::all();
		$count = count($oldCoverage);
		$id = 0; $i = 0;
		foreach($oldCoverage as $c){

			if(array_search($c->_id, array_column($newCoverage, '_id')) !== False) {
			} else {

				if($c->dealer == ''){

					$sl = Form::create([
						'_id' => new \MongoDB\BSON\ObjectId($c->_id),
						'type_service' => $c->type_application,
						'type_application' => 'B',
						'status_payment' => $c->status,
						'status_email' => $c->status1,
						'remark_form' => $c->remarks,
						'dealer_id' => $c->dealer,
						'company_id' => $c->dealer_company,
						'created_at' => new \MongoDB\BSON\UTCDateTime($c->reg_time*1000)
					]);

					$su_id = '';
					do {
						$su_id = strtoupper('CBB-'.$this->random_code(6).'B');
					} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

					$sdl = new Subscriber();
					$sdl->subscriber_id = $su_id;
					$sdl->company_name = $c->company_name;
					$sdl->company_reg = $c->company_reg;
					$sdl->company_num = $c->company_num;
					$sdl->company_fax = $c->company_fax;
					$sdl->name = $c->name;
					$sdl->ic = $c->ic;
					$sdl->designation = $c->designation;
					$sdl->gender = $c->gender;
					$sdl->race = $c->race;
					$sdl->email = $c->email;
					$sdl->phone1 = $c->phone1;
					$sdl->phone2 = $c->phone2;
					$sdl->unit_no = '';
					$sdl->street = $c->address;
					$sdl->postcode = $c->postcode;
					$sdl->city = $c->city;
					$sdl->state = $c->state;
					$sdl->front_ic = str_replace("data","/document",$c->imagefront);
					$sdl->back_ic = str_replace("data","/document",$c->imageback);
					$sdl->form24_49 = str_replace("data","/document",$c->formA_B);
					$sdl->form9_44 = str_replace("data","/document",$c->formD);
					$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

					$packageD = new PackageDetail();
					$packageD->contract = $c->contract;
					$packageD->name = $c->package;
					$packageD->montly_fee = '';
					$packageD->voice_fee = $c->voice;
					$packageD->deposit = '';
					$packageD->upfront_payment = '';
					$packageD->rfs = $c->rfs;

					if($c->wo != ''){
						$getWo = OldWorkOrder::all();

						foreach($getWo as $w){
							if($w->wo == $c->wo){
								$woD = new WorkOrder();
								$woD->img_url = '/assets/img/activation_icon_nRead1.png';
								$woD->wo = $w->wo;
								$woD->nature_work = $w->nature_work;
								$woD->sub_category = $w->sub_category;
								$woD->dateTimeStart = $w->dateTimeStart;
								$woD->dateTimeEnd = $w->dateTimeEnd;
								$woD->contractor_id = $w->contractor_id;
								$woD->installer_id = $w->installer_id;
								$woD->onu = $w->onu;
								$woD->router = $w->router;
								$woD->need_phone = $w->n_phone;
								$woD->no_phone = $w->no_phone;
								$woD->pppoe_username = $w->pppoe_username;
								$woD->pppoe_password = '';
								$woD->docket_id = $w->docket;
								$woD->remarks_custservice = $w->remarks;
								$woD->remarks_installer = $w->remarks_inst;
								$woD->status = $w->status;
								$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
								$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

								$sl->workorder()->save($woD);
							}
						}
					}

					$sl->subscriber()->save($sdl);
					$sl->packagedetail()->save($packageD);

					$i++;

				}else {

					$sl = new Form();
					$sl->_id = new \MongoDB\BSON\ObjectId($c->_id);
					$sl->type_service = $c->type_application;
					$sl->type_application = 'B';
					$sl->status_payment = $c->status;
					$sl->status_email = $c->status1;
					$sl->remark_form = $c->remarks;
					$sl->dealer_id = $c->dealer;
					$sl->company_id = $c->dealer_company;
					$sl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

					// staff
					$staff = Staff::where('_id',$c->dealer)->first();
					// dd($c->dealer);
					$staff->form()->save($sl);

					$su_id = '';
					do {
						$su_id = strtoupper('CBB-'.$this->random_code(6).'B');
					} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

					$sdl = new Subscriber();
					$sdl->subscriber_id = $su_id;
					$sdl->company_name = $c->company_name;
					$sdl->company_reg = $c->company_reg;
					$sdl->company_num = $c->company_num;
					$sdl->company_fax = $c->company_fax;
					$sdl->name = $c->name;
					$sdl->ic = $c->ic;
					$sdl->designation = $c->designation;
					$sdl->gender = $c->gender;
					$sdl->race = $c->race;
					$sdl->email = $c->email;
					$sdl->phone1 = $c->phone1;
					$sdl->phone2 = $c->phone2;
					$sdl->unit_no = '';
					$sdl->street = $c->address;
					$sdl->postcode = $c->postcode;
					$sdl->city = $c->city;
					$sdl->state = $c->state;
					$sdl->front_ic = str_replace("data","/document",$c->imagefront);
					$sdl->back_ic = str_replace("data","/document",$c->imageback);
					$sdl->form24_49 = str_replace("data","/document",$c->formA_B);
					$sdl->form9_44 = str_replace("data","/document",$c->formD);
					$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

					$packageD = new PackageDetail();
					$packageD->contract = $c->contract;
					$packageD->name = $c->package;
					$packageD->montly_fee = '';
					$packageD->voice_fee = $c->voice;
					$packageD->deposit = '';
					$packageD->upfront_payment = '';
					$packageD->rfs = $c->rfs;

					if($c->wo != ''){
						$getWo = OldWorkOrder::all();

						foreach($getWo as $w){
							if($w->wo == $c->wo){
								$woD = new WorkOrder();
								$woD->img_url = '/assets/img/activation_icon_nRead1.png';
								$woD->wo = $w->wo;
								$woD->nature_work = $w->nature_work;
								$woD->sub_category = $w->sub_category;
								$woD->dateTimeStart = $w->dateTimeStart;
								$woD->dateTimeEnd = $w->dateTimeEnd;
								$woD->contractor_id = $w->contractor_id;
								$woD->installer_id = $w->installer_id;
								$woD->onu = $w->onu;
								$woD->router = $w->router;
								$woD->need_phone = $w->n_phone;
								$woD->no_phone = $w->no_phone;
								$woD->pppoe_username = $w->pppoe_username;
								$woD->pppoe_password = '';
								$woD->docket_id = $w->docket;
								$woD->remarks_custservice = $w->remarks;
								$woD->remarks_installer = $w->remarks_inst;
								$woD->status = $w->status;
								$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
								$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

								$sl->workorder()->save($woD);
							}
						}
					}

					$sl->subscriber()->save($sdl);
					$sl->packagedetail()->save($packageD);

					$id++;
				}
			}
		}

		dd('Semua- '.$count. ' -D ' .$id .' -ND '.$i);
	}

	public function staffConvert(){

		$old_staff = StaffOld::where('_id','5c74a263ded8f67c660b8b94')->get();
		$company = CompanyLaravel::where('_id','5b42b935ee0dc2b707473b76')->first();
		$instList = array();

		foreach($old_staff as $inst){
			$sl = StaffLaravel::create([
				'_id' => new \MongoDB\BSON\ObjectId($inst->_id),
				'email' => $inst->email,
				'password' => $inst->password,
				'roles_access' => $inst->position,
				'company_id' => '5b42b935ee0dc2b707473b76',
				'last_login_at' => '',
				'last_login_ip'=> '',
				'created_at' => new \MongoDB\BSON\UTCDateTime($inst->reg_time*1000)
			]);

			$sdl = new StaffDetailLaravel();
			$sdl->_id = new \MongoDB\BSON\ObjectId($inst->_id);
			$sdl->name = $inst->name;
			$sdl->ic = $inst->ic;
			$sdl->email = $inst->email;
			$sdl->phone = $inst->phone;
			$sdl->company_id = '5b42b935ee0dc2b707473b76';
			$sdl->color = $inst->color;
			$sdl->position = $inst->position;
			$sdl->password = $inst->password;
			$sdl->user_pic = $inst->user_pic;
			$sdl->created_at = new \MongoDB\BSON\UTCDateTime($inst->reg_time*1000);

			$company->stafflaravel()->save($sl);
			$company->save();
			$sl->staffdetaillaravel()->save($sdl);
		}

		dd($old_staff);
	}

	public function dealerConvert(){
		$oldDealer = DealerDetail::get();
		$newStaff = StaffLaravel::all()->toArray();

		$a = array();
		foreach ( $oldDealer as $o ) {
			if(array_search($o->_id, array_column($newStaff, '_id')) !== False) {

			} else {

				$sl = StaffLaravel::create([
					'_id' => new \MongoDB\BSON\ObjectId($o->_id),
					'email' => $o->email,
					'password' => $o->password,
					'roles_access' => $o->position,
					'company_id' => $o->company_id,
					'last_login_at' => '',
					'last_login_ip'=> '',
					'created_at' => $o->created_at,
					'updated_at' => $o->updated_at
				]);

				$sdl = new StaffDetailLaravel();
				$sdl->_id = new \MongoDB\BSON\ObjectId($o->_id);
				$sdl->name = $o->name;
				$sdl->ic = $o->ic;
				$sdl->email = $o->email;
				$sdl->phone = $o->phone;
				$sdl->company_id = $o->company_id;
				$sdl->color = $o->color;
				$sdl->position = $o->position;
				$sdl->password = $o->password;
				$sdl->user_pic = $o->user_pic;
				$sdl->created_at = $o->created_at;

				$company = CompanyLaravel::where('_id',$o->company_id)->first();
				$company->stafflaravel()->save($sl);
				$company->save();
				$sl->staffdetaillaravel()->save($sdl);

				$a[] = $o;
			}
		}

		dd($a);
	}

	public function test1()
	{
		// $sub = SubscribersLaravel::where('subscriber_id','CBB-DNC0KQB')->first();
		// echo $sub->

		// $destinationPathIcon = '/assets/document';
		// if(!File::exists(public_path().$destinationPathIcon))
		// {
		// 	echo "folder takde woi";
		// }
		// else
		// 	echo "folder ada";

		$sub = SubscribersLaravel::all();
		$i=0;
		$arr = array();
		foreach($sub as $s)
		{
			if(!empty($s->front_ic))
			{	
				if(strpos($s->form24_49, '/document/business/'.$s->_id) !== false)
				{
					$sb = SubscribersLaravel::where('subscriber_id', $s->subscriber_id)->first();
					// $sb->front_ic = str_replace('/document/business/'.$sb->_id,"/document/".$s->subscriber_id,$sb->front_ic);
					// $sb->back_ic = str_replace('/document/business/'.$sb->_id,"/document/".$s->subscriber_id,$sb->back_ic); 
					// $sb->signature = str_replace('/document/business/'.$sb->_id,"/document/".$s->subscriber_id,$sb->signature);
					$sb->form24_49 = str_replace('/document/business/'.$sb->_id,"/document/".$s->subscriber_id,$sb->form24_49);
					$sb->form9_44 = str_replace('/document/business/'.$sb->_id,"/document/".$s->subscriber_id,$sb->form9_44);
					// $sb->back = "/document/".$s->subscriber_id."/backic.jpeg";
					// $sb->save();
					$arr[] = $s;
				}
			}		
		}
		dd($arr);
	}

	public function updateHistory()
	{
		$form = Form::all()->toArray();
		$oh = OrderHistory::all();
		$i = 0;
		if(!empty($oh)){
			foreach ($oh as $o){
				if(array_search($o->_id, array_column($form, '_id')) !== False) {
					$fm = Form::where('_id', $o->_id)->first();
					if(empty($fm)){}
						else
						{
							foreach($o->details as $d)
							{
								$stat = new FormStatus();
								$stat->form_id = $o->_id;
								$stat->status_id = $d['id'];
								$stat->date = new \MongoDB\BSON\UTCDateTime($d['date']*1000);
								$stat->status = $d['status'];
								$stat->desc = $d['desc'];
								$stat->created_at = new \MongoDB\BSON\UTCDateTime($d['date']*1000);
								$fm->formstatus()->save($stat);
								$i++;
							}
						}
					}
				}
			}

			dd($i);
		}

		public function updateWorkOrder(){
			$wo = WorkOrder::all()->toArray();
			$oldWork = OldWorkOrder::all();

			$i = 0;
			foreach($oldWork as $o) {

				$curr = WorkOrder::where('wo',$o->wo)->first();
				if(!empty($curr)){
					$curr->nature_work = $o->nature_work;
					$curr->sub_category = $o->sub_category;
					$curr->dateTimeStart = $o->dateTimeStart;
					$curr->dateTimeEnd = $o->dateTimeEnd;
					$curr->contractor_id = $o->contractor_id;
					$curr->installer_id = $o->installer_id;
					$curr->onu = $o->onu;
					$curr->router = $o->router;
					$curr->need_phone = $o->n_phone;
					$curr->no_phone = $o->no_phone;
					$curr->pppoe_username = $o->pppoe_username;
					$curr->pppoe_password = '';
					$curr->docket_id = $o->docket;
					$curr->remarks_custservice = $o->remarks;
					$curr->remarks_installer = $o->remarks_inst;
					$curr->status = $o->status;
					$curr->save();

					$i++;
				}
			}

			dd($i);
		}

		public function updateDocket(){

			$i = 0;
			$wo = WorkOrder::all();
			foreach ($wo as $key => $w) {
				if($w->status == 'Completed'){

					$latest = Docket::where('work_order_id',$w->wo)->first();
					if(empty($latest)){
						$doc = OldDocket::where('work_order_id',$w->wo)->first();
						if(!empty($doc)){
							$i++;
							$curr =  WorkOrder::where('wo',$doc->work_order_id)->first();
							$docket = new Docket();
							$docket->work_order_id = $doc->work_order_id;
							$docket->docket_id = $doc->docket_id;
							$docket->nature_work = $doc->nature_work;
							$docket->installer_id = $doc->installer_id;
							$docket->end_job = $doc->end_job;
							$docket->router_serial_number = $doc->router_serial_number;
							$docket->mac_router = $doc->mac_router;
							$docket->modem_sn = $doc->modem_sn;
							$docket->type_testing = $doc->type_testing;
							$docket->cable_read = $doc->cable_read;
							$docket->condition = $doc->condition;
							$docket->Note = $doc->Note;
							$docket->Rating1 = $doc->Rating1;
							$docket->Rating2 = $doc->Rating2;
							$docket->Rating3 = $doc->Rating3;
							$docket->customer_signature = $doc->customer_signature;
							$curr->docket()->save($docket);
						}
					}
				}
			}

			dd($i);
		}

		public function checkResidential(){
		// $newCoverage = Form::all();
		// $oldCoverage = OldResidential::all()->toArray();

		// $i = 0; $j = 0; $empty = array();
		// foreach ($newCoverage as $key => $a) {
		// 	if(array_search($a->_id, array_column($oldCoverage, '_id')) !== False) {
		// 	}else {
		// 		$empty[] = $a;
		// 	}
		// }		

		// foreach ($empty as $key => $a) {
		// 	dd($a['_id']);
		// }


		// if($c->dealer == ''){

		// 	$sl = Form::create([
		// 		'_id' => new \MongoDB\BSON\ObjectId($c->_id),
		// 		'type_service' => $c->type_application,
		// 		'type_application' => 'B',
		// 		'status_payment' => $c->status,
		// 		'status_email' => $c->status1,
		// 		'remark_form' => $c->remarks,
		// 		'dealer_id' => $c->dealer,
		// 		'company_id' => $c->dealer_company,
		// 		'created_at' => new \MongoDB\BSON\UTCDateTime($c->reg_time*1000)
		// 	]);

		// 	$su_id = '';
		// 	do {
		// 		$su_id = strtoupper('CBB-'.$this->random_code(6).'B');
		// 	} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

		// 	$sdl = new Subscriber();
		// 	$sdl->subscriber_id = $su_id;
		// 	$sdl->company_name = $c->company_name;
		// 	$sdl->company_reg = $c->company_reg;
		// 	$sdl->company_num = $c->company_num;
		// 	$sdl->company_fax = $c->company_fax;
		// 	$sdl->name = $c->name;
		// 	$sdl->ic = $c->ic;
		// 	$sdl->designation = $c->designation;
		// 	$sdl->gender = $c->gender;
		// 	$sdl->race = $c->race;
		// 	$sdl->email = $c->email;
		// 	$sdl->phone1 = $c->phone1;
		// 	$sdl->phone2 = $c->phone2;
		// 	$sdl->unit_no = '';
		// 	$sdl->street = $c->address;
		// 	$sdl->postcode = $c->postcode;
		// 	$sdl->city = $c->city;
		// 	$sdl->state = $c->state;
		// 	$sdl->front_ic = str_replace("data","/document",$c->imagefront);
		// 	$sdl->back_ic = str_replace("data","/document",$c->imageback);
		// 	$sdl->form24_49 = str_replace("data","/document",$c->formA_B);
		// 	$sdl->form9_44 = str_replace("data","/document",$c->formD);
		// 	$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

		// 	$packageD = new PackageDetail();
		// 	$packageD->contract = $c->contract;
		// 	$packageD->name = $c->package;
		// 	$packageD->montly_fee = '';
		// 	$packageD->voice_fee = $c->voice;
		// 	$packageD->deposit = '';
		// 	$packageD->upfront_payment = '';
		// 	$packageD->rfs = $c->rfs;

		// 	if($c->wo != ''){
		// 		$getWo = OldWorkOrder::all();

		// 		foreach($getWo as $w){
		// 			if($w->wo == $c->wo){
		// 				$woD = new WorkOrder();
		// 				$woD->img_url = '/assets/img/activation_icon_nRead1.png';
		// 				$woD->wo = $w->wo;
		// 				$woD->nature_work = $w->nature_work;
		// 				$woD->sub_category = $w->sub_category;
		// 				$woD->dateTimeStart = $w->dateTimeStart;
		// 				$woD->dateTimeEnd = $w->dateTimeEnd;
		// 				$woD->contractor_id = $w->contractor_id;
		// 				$woD->installer_id = $w->installer_id;
		// 				$woD->onu = $w->onu;
		// 				$woD->router = $w->router;
		// 				$woD->need_phone = $w->n_phone;
		// 				$woD->no_phone = $w->no_phone;
		// 				$woD->pppoe_username = $w->pppoe_username;
		// 				$woD->pppoe_password = '';
		// 				$woD->docket_id = $w->docket;
		// 				$woD->remarks_custservice = $w->remarks;
		// 				$woD->remarks_installer = $w->remarks_inst;
		// 				$woD->status = $w->status;
		// 				$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
		// 				$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

		// 				$sl->workorder()->save($woD);
		// 			}
		// 		}
		// 	}

		// 	$sl->subscriber()->save($sdl);
		// 	$sl->packagedetail()->save($packageD);

		// 	$i++;

		// }else {

		// 	$sl = new Form();
		// 	$sl->_id = new \MongoDB\BSON\ObjectId($c->_id);
		// 	$sl->type_service = $c->type_application;
		// 	$sl->type_application = 'B';
		// 	$sl->status_payment = $c->status;
		// 	$sl->status_email = $c->status1;
		// 	$sl->remark_form = $c->remarks;
		// 	$sl->dealer_id = $c->dealer;
		// 	$sl->company_id = $c->dealer_company;
		// 	$sl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

		// 			// staff
		// 	$staff = Staff::where('_id',$c->dealer)->first();
		// 			// dd($c->dealer);
		// 	$staff->form()->save($sl);

		// 	$su_id = '';
		// 	do {
		// 		$su_id = strtoupper('CBB-'.$this->random_code(6).'B');
		// 	} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

		// 	$sdl = new Subscriber();
		// 	$sdl->subscriber_id = $su_id;
		// 	$sdl->company_name = $c->company_name;
		// 	$sdl->company_reg = $c->company_reg;
		// 	$sdl->company_num = $c->company_num;
		// 	$sdl->company_fax = $c->company_fax;
		// 	$sdl->name = $c->name;
		// 	$sdl->ic = $c->ic;
		// 	$sdl->designation = $c->designation;
		// 	$sdl->gender = $c->gender;
		// 	$sdl->race = $c->race;
		// 	$sdl->email = $c->email;
		// 	$sdl->phone1 = $c->phone1;
		// 	$sdl->phone2 = $c->phone2;
		// 	$sdl->unit_no = '';
		// 	$sdl->street = $c->address;
		// 	$sdl->postcode = $c->postcode;
		// 	$sdl->city = $c->city;
		// 	$sdl->state = $c->state;
		// 	$sdl->front_ic = str_replace("data","/document",$c->imagefront);
		// 	$sdl->back_ic = str_replace("data","/document",$c->imageback);
		// 	$sdl->form24_49 = str_replace("data","/document",$c->formA_B);
		// 	$sdl->form9_44 = str_replace("data","/document",$c->formD);
		// 	$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

		// 	$packageD = new PackageDetail();
		// 	$packageD->contract = $c->contract;
		// 	$packageD->name = $c->package;
		// 	$packageD->montly_fee = '';
		// 	$packageD->voice_fee = $c->voice;
		// 	$packageD->deposit = '';
		// 	$packageD->upfront_payment = '';
		// 	$packageD->rfs = $c->rfs;

		// 	if($c->wo != ''){
		// 		$getWo = OldWorkOrder::all();

		// 		foreach($getWo as $w){
		// 			if($w->wo == $c->wo){
		// 				$woD = new WorkOrder();
		// 				$woD->img_url = '/assets/img/activation_icon_nRead1.png';
		// 				$woD->wo = $w->wo;
		// 				$woD->nature_work = $w->nature_work;
		// 				$woD->sub_category = $w->sub_category;
		// 				$woD->dateTimeStart = $w->dateTimeStart;
		// 				$woD->dateTimeEnd = $w->dateTimeEnd;
		// 				$woD->contractor_id = $w->contractor_id;
		// 				$woD->installer_id = $w->installer_id;
		// 				$woD->onu = $w->onu;
		// 				$woD->router = $w->router;
		// 				$woD->need_phone = $w->n_phone;
		// 				$woD->no_phone = $w->no_phone;
		// 				$woD->pppoe_username = $w->pppoe_username;
		// 				$woD->pppoe_password = '';
		// 				$woD->docket_id = $w->docket;
		// 				$woD->remarks_custservice = $w->remarks;
		// 				$woD->remarks_installer = $w->remarks_inst;
		// 				$woD->status = $w->status;
		// 				$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
		// 				$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

		// 				$sl->workorder()->save($woD);
		// 			}
		// 		}
		// 	}

		// 	$sl->subscriber()->save($sdl);
		// 	$sl->packagedetail()->save($packageD);

		// 	$id++;
		// }

			$data = array();
			$oldCoverage = OldResidential::all();
			foreach($oldCoverage as $c){
				$fm = Form::with('Subscriber')->where('_id',$c->_id)->first();
				if($fm === null){
					$data[] = $c;

				// if($c->dealer == ''){

				// 	$sl = Form::create([
				// 		'_id' => new \MongoDB\BSON\ObjectId($c->_id),
				// 		'type_service' => $c->type_application,
				// 		'type_application' => 'R',
				// 		'status_payment' => $c->status,
				// 		'status_email' => $c->status1,
				// 		'remark_form' => $c->remarks,
				// 		'dealer_id' => $c->dealer,
				// 		'company_id' => $c->dealer_company,
				// 		'created_at' => new \MongoDB\BSON\UTCDateTime($c->reg_time*1000)
				// 	]);

				// 	$su_id = '';
				// 	do {
				// 		$su_id = strtoupper('CBB-'.$this->random_code(6).'R');
				// 	} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

				// 	$sdl = new Subscriber();
				// 	$sdl->subscriber_id = $su_id;
				// 	$sdl->name = $c->name;
				// 	$sdl->ic = $c->ic;
				// 	$sdl->citizen = $c->citizen;
				// 	$sdl->gender = $c->gender;
				// 	$sdl->race = $c->race;
				// 	$sdl->email = $c->email;
				// 	$sdl->phone1 = $c->phone1;
				// 	$sdl->phone2 = $c->phone2;
				// 	$sdl->unit_no = $c->unit_no;
				// 	$sdl->building_name = $c->building;
				// 	$sdl->street = $c->street;
				// 	$sdl->postcode = $c->postcode;
				// 	$sdl->city = $c->city;
				// 	$sdl->state = $c->state;
				// 	$sdl->front_ic = str_replace("data","/document",$c->imagefront);
				// 	$sdl->back_ic = str_replace("data","/document",$c->imageback);
				// 	$sdl->signature = str_replace("data","/document",$c->signature);
				// 	$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

				// 	$packageD = new PackageDetail();
				// 	$packageD->contract = $c->contract;
				// 	$packageD->name = $c->package;
				// 	$packageD->montly_fee = '';
				// 	$packageD->voice_fee = $c->voice;
				// 	$packageD->deposit = '';
				// 	$packageD->upfront_payment = '';
				// 	$packageD->rfs = $c->rfs;

				// 	if($c->wo != ''){
				// 		$getWo = OldWorkOrder::all();

				// 		foreach($getWo as $w){
				// 			if($w->wo == $c->wo){
				// 				$woD = new WorkOrder();
				// 				$woD->img_url = '/assets/img/activation_icon_nRead1.png';
				// 				$woD->wo = $w->wo;
				// 				$woD->nature_work = $w->nature_work;
				// 				$woD->sub_category = $w->sub_category;
				// 				$woD->dateTimeStart = $w->dateTimeStart;
				// 				$woD->dateTimeEnd = $w->dateTimeEnd;
				// 				$woD->contractor_id = $w->contractor_id;
				// 				$woD->installer_id = $w->installer_id;
				// 				$woD->onu = $w->onu;
				// 				$woD->router = $w->router;
				// 				$woD->need_phone = $w->n_phone;
				// 				$woD->no_phone = $w->no_phone;
				// 				$woD->pppoe_username = $w->pppoe_username;
				// 				$woD->pppoe_password = '';
				// 				$woD->docket_id = $w->docket;
				// 				$woD->remarks_custservice = $w->remarks;
				// 				$woD->remarks_installer = $w->remarks_inst;
				// 				$woD->status = $w->status;
				// 				$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
				// 				$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

				// 				$sl->workorder()->save($woD);
				// 			}
				// 		}
				// 	}

				// 	$sl->subscriber()->save($sdl);
				// 	$sl->packagedetail()->save($packageD);

				// 	$i++;

				// }else {

				// 	$sl = new Form();
				// 	$sl->_id = new \MongoDB\BSON\ObjectId($c->_id);
				// 	$sl->type_service = $c->type_application;
				// 	$sl->type_application = 'R';
				// 	$sl->status_payment = $c->status;
				// 	$sl->status_email = $c->status1;
				// 	$sl->remark_form = $c->remarks;
				// 	$sl->dealer_id = $c->dealer;
				// 	$sl->company_id = $c->dealer_company;
				// 	$sl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

				// 	// staff
				// 	$staff = Staff::where('_id',$c->dealer)->first();
				// 	// dd($c->dealer);
				// 	$staff->form()->save($sl);

				// 	$su_id = '';
				// 	do {
				// 		$su_id = strtoupper('CBB-'.$this->random_code(6).'R');
				// 	} while (Subscriber::where("subscriber_id", "=", $su_id)->first() instanceof Subscriber);

				// 	$sdl = new Subscriber();
				// 	$sdl->subscriber_id = $su_id;
				// 	$sdl->name = $c->name;
				// 	$sdl->ic = $c->ic;
				// 	$sdl->citizen = $c->citizen;
				// 	$sdl->gender = $c->gender;
				// 	$sdl->race = $c->race;
				// 	$sdl->email = $c->email;
				// 	$sdl->phone1 = $c->phone1;
				// 	$sdl->phone2 = $c->phone2;
				// 	$sdl->unit_no = $c->unit_no;
				// 	$sdl->building_name = $c->building;
				// 	$sdl->street = $c->street;
				// 	$sdl->postcode = $c->postcode;
				// 	$sdl->city = $c->city;
				// 	$sdl->state = $c->state;
				// 	$sdl->front_ic = str_replace("data","/document",$c->imagefront);
				// 	$sdl->back_ic = str_replace("data","/document",$c->imageback);
				// 	$sdl->signature = str_replace("data","/document",$c->signature);
				// 	$sdl->created_at = new \MongoDB\BSON\UTCDateTime($c->reg_time*1000);

				// 	$packageD = new PackageDetail();
				// 	$packageD->contract = $c->contract;
				// 	$packageD->name = $c->package;
				// 	$packageD->montly_fee = '';
				// 	$packageD->voice_fee = $c->voice;
				// 	$packageD->deposit = '';
				// 	$packageD->upfront_payment = '';
				// 	$packageD->rfs = $c->rfs;

				// 	if($c->wo != ''){
				// 		$getWo = OldWorkOrder::all();

				// 		foreach($getWo as $w){
				// 			if($w->wo == $c->wo){
				// 				$woD = new WorkOrder();
				// 				$woD->img_url = '/assets/img/activation_icon_nRead1.png';
				// 				$woD->wo = $w->wo;
				// 				$woD->nature_work = $w->nature_work;
				// 				$woD->sub_category = $w->sub_category;
				// 				$woD->dateTimeStart = $w->dateTimeStart;
				// 				$woD->dateTimeEnd = $w->dateTimeEnd;
				// 				$woD->contractor_id = $w->contractor_id;
				// 				$woD->installer_id = $w->installer_id;
				// 				$woD->onu = $w->onu;
				// 				$woD->router = $w->router;
				// 				$woD->need_phone = $w->n_phone;
				// 				$woD->no_phone = $w->no_phone;
				// 				$woD->pppoe_username = $w->pppoe_username;
				// 				$woD->pppoe_password = '';
				// 				$woD->docket_id = $w->docket;
				// 				$woD->remarks_custservice = $w->remarks;
				// 				$woD->remarks_installer = $w->remarks_inst;
				// 				$woD->status = $w->status;
				// 				$woD->created_by = str_replace(array( 'ObjectId(', ')' ), '', $w->created_by);
				// 				$woD->created_at = new \MongoDB\BSON\UTCDateTime($w->reg_time*1000);

				// 				$sl->workorder()->save($woD);
				// 			}
				// 		}
				// 	}

				// 	$sl->subscriber()->save($sdl);
				// 	$sl->packagedetail()->save($packageD);
				// }
				}
			}

			dd($data);
		}

		public function subscriberID(){

			$final = array();
			$data = Form::with('PackageDetail','Subscriber')->where('type_service','!=','Rectification')->where('type_application','B')->withTrashed()->get();
			foreach ($data as $key => $d) {
				$created_at = $d->created_at->format('m/Y'); 
				// if(strpos($created_at,'03/2019') !== false){
					// if(($d->Subscriber->email != '<b>Package Development</b>' || $d->Subscriber->ic != '<b>Package Development</b>') && $d->PackageDetail->name == "10"){ 
						if($d->PackageDetail->name == '1' && $d->PackageDetail->contract == '12'){
							// if($d->Subscriber->citizen == 'Malaysian'){
							// 	$d->PackageDetail->montly_fee = '115.54';
							// 	$d->PackageDetail->deposit = '200';
							// 	$d->PackageDetail->upfront_payment = '0';
							// 	$d->PackageDetail->save();
							// }else {
								$d->PackageDetail->montly_fee = '3,708.94';
								$d->PackageDetail->deposit = '3,499';
								$d->PackageDetail->upfront_payment = '300';
								$d->PackageDetail->save();
							// }
							$final[] = $d->_id;
						}

						// if($d->PackageDetail->name == 'R'){
						// 	$final[] = $d->_id;
						// 	$d->type_service = 'Rectification';
						// 	$d->status_payment = 'paid';
						// 	$d->status_email = 'verified';
						// 	$d->save();
						// }
					// }
				// }
				// 	// if(($d->Subscriber->email == '<b>Package Development</b>' || $d->Subscriber->ic == '<b>Package Development</b>') && $d->PackageDetail->contract == ""){
				// 	// 	$d->PackageDetail->contract = "24";
				// 	// 	$d->PackageDetail->save();

				// 	// 	$final[] = $d->_id;
				// 	// }
				// }

				// if(!empty($d->PackageDetail->voice_fee)){
				// 	if($d->PackageDetail->voice_fee == 0){
				// 		$d->PackageDetail->voice_fee = 'No';
				// 		$d->PackageDetail->save();
				// 		$final[] = $d->_id;
				// 	}
				// }
			}

			dd($final);
		}

		public function convertStaffDb()
		{
		// $s = OldResidential::first();
			return $this->subscriberID();
		// return $this->checkResidential();
		// return $this->test2();
		// return $this->updateWorkOrder();
		// return $this->updateDocket();
		// return $this->updateHistory();
		// return $this->staffConvert();
		// return $this->convertFormAndRelation();
		// return $this->convertFormAndRelationBusiness();
		// return $this->dealerConvert();

		// return $this->addWotoExistingForm();

		// $a = array(); $count = 0;
		// $oldDocket = Docket::all();
		// $newWork = WorkOrderLaravel::where('status','Completed')->get()->toArray();

		// $i = 0;
		// foreach ( $oldDocket as $o ) {
		// 	if(array_search($o->work_order_id, array_column($newWork, 'wo')) !== False) {

		// 		// $a[] = $o->docket_id.' - '.$o->work_order_id;
		// 		$a[] = $o->docket_id;

		// 		$xNw = DocketLaravel::where('work_order_id',$o->work_order_id)->first();

		// 		if(empty($xNw)){

		// 			$testDate = $o->end_job;
		// 			$date = str_replace('/','-',$testDate);
		// 			$timestamp = strtotime($date);
		// 			//Convert timestamp to carbon format for created_at
		// 			$go = Carbon::createFromTimestamp($timestamp).".000Z";
		// 			$time = Carbon::createFromTimeString($go);

		// 			$wo = WorkOrderLaravel::where('wo',$o->work_order_id)->first();

		// 			$do = new DocketLaravel();
		// 			$do->work_order_id = $o->work_order_id;
		// 			$do->docket_id = $o->docket_id;
		// 			$do->nature_work = $o->nature_work;
		// 			$do->installer_id = $o->installer_id;
		// 			$do->end_job = $time->toDateTimeString();
		// 			$do->router_serial_number = $o->router_serial_number;
		// 			$do->mac_router = $o->mac_router;
		// 			$do->cable_read = $o->cable_read;
		// 			$do->condition = $o->condition;
		// 			$do->Note = $o->Note;
		// 			$do->Rating1 = $o->Rating1;
		// 			$do->Rating2 = $o->Rating2;
		// 			$do->Rating3 = $o->Rating3;
		// 			$do->customer_signature = $o->customer_signature;
		// 			$do->created_at= $time;

		// 			$wo->docketlaravel()->save($do);

		// 			$i++;
		// 		}
		// 	}
		// }

		// // $xNw1 = WorkOrderLaravel::with('FormLaravel')->get();
		// // $fm = FormLaravel::with('WorkOrderLaravel','SubscribersLaravel')->first();
		// // $docket = DocketLaravel::where('work_order_id',$fm->WorkOrderLaravel->wo)->first();

		// // $withoutDuplicates = array_unique($a);
		// // $duplicates = array_diff($a, $withoutDuplicates);

		// // dd(count($newWork) .'-'.$i);

		// $doc = DocketLaravel::all();
		// dd(count($doc));

		}
	}