<?php

namespace App\Http\Controllers\DS;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;

use App\Model\Staff;
use App\Model\StaffDetail;
use App\Model\Company;
Use App\Model\Commission;

use App\Model\Form;
use App\Model\PackageDetail;
use App\Model\Subscriber;
use App\Model\WorkOrder;
use App\Model\Docket;
use App\Model\DealerClaim;
use App\Model\DealerClaim2;
use App\Model\Product;

class DealerClaimController extends Controller
{
	public function getCompleted(){

		$cdata = array(); $finalData = array();

		$dealerclaim = DealerClaim::get()->toArray();

		$form = Form::with('Subscriber','PackageDetail','WorkOrder')->get();
		foreach($form as $f){
			if(!empty($f->dealer_id)){
				if(!empty($f->WorkOrder)){
					if($f->WorkOrder->status == 'Completed' && $f->WorkOrder->nature_work == 'Activation'){
						$cdata[] = $f->_id;
					}
				}
			}
		}

		$notExist = array();
		foreach($cdata as $c){
			if(array_search($c, array_column($dealerclaim, 'customer_id')) !== False) {
			} else {
				$notExist[] = $c;

				$formD = Form::with('Subscriber','PackageDetail','WorkOrder')->where('_id',$c)->first();
				$product = Product::where('speed', $formD->PackageDetail->name)->where('formT',$formD->type_application)->first();
				$docket = Docket::where('work_order_id',$formD->WorkOrder->wo)->first();

				if(!empty($docket->docket_id)){
					$subsData = [
						'formT' => $formD->type_application,
						'contract' => $formD->PackageDetail->contract,
						'submission_dt' => Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur')->toDateTimeString(),
						'submission_my' => date('m/Y', strtotime(Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur'))),
						'wo' => $formD->WorkOrder->wo,
						'customer_id' => $formD->_id,
						'package' => $formD->PackageDetail->name,
						'retail_price' => (int)$product->retail_price,
						'docket' => $docket->docket_id,
						'activated_dt' => $docket->end_job,
						'activated_my' => date('m/Y', strtotime($docket->end_job)),
						'claim_dt' => '',
						'total_form_by_month' => '',
						'agent' => $formD->dealer_id,
						'dealer' => $formD->company_id,
					];

					DealerClaim::create($subsData);
				}
			}
		}
		return $notExist;
	}

	public function getCompleted2(){

		$cdata = array(); $finalData = array();

		$dealerclaim = DealerClaim2::get()->toArray();

		$form = Form::with('Subscriber','PackageDetail','WorkOrder')->get();
		foreach($form as $f){
			if(!empty($f->dealer_id)){
				if(!empty($f->WorkOrder)){
					if($f->WorkOrder->status == 'Completed' && $f->WorkOrder->nature_work == 'Activation'){
						$cdata[] = $f->_id;
					}
				}
			}
		}

		$notExist = array();
		foreach($cdata as $c){
			if(array_search($c, array_column($dealerclaim, 'customer_id')) !== False) {
			} else {
				$notExist[] = $c;

				$formD = Form::with('Subscriber','PackageDetail','WorkOrder')->where('_id',$c)->first();
				$product = Product::where('speed', $formD->PackageDetail->name)->where('formT',$formD->type_application)->withTrashed()->first();
				$docket = Docket::where('work_order_id',$formD->WorkOrder->wo)->first();

				if(!empty($docket->docket_id)){
					$subsData = [
						'formT' => $formD->type_application,
						'contract' => $formD->PackageDetail->contract,
						'submission_dt' => Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur')->toDateTimeString(),
						'submission_my' => date('m/Y', strtotime(Carbon::parse($formD->created_at)->setTimezone('Asia/Kuala_Lumpur'))),
						'wo' => $formD->WorkOrder->wo,
						'customer_id' => $formD->_id,
						'package' => $formD->PackageDetail->name,
						'retail_price' => (int)$product->retail_price,
						'docket' => $docket->docket_id,
						'activated_dt' => $docket->end_job,
						'activated_my' => date('m/Y', strtotime($docket->end_job)),
						'claim_dt' => '',
						'total_form_by_month' => '',
						'agent' => $formD->dealer_id,
						'dealer' => $formD->company_id,
					];

					DealerClaim2::create($subsData);
				}
			}
		}
		return $notExist;
	}

	public function storeDealerClaim(){

		$data = $this->getCompleted();
		return $data;
	}

	public function storeDealerClaim2(){

		$data = $this->getCompleted2();
		return $data;
	}

	public function getDealerCompany2(){
		$dataD = $this->storeDealerClaim2();
		dd($dataD);
	} 


	public function test1(){
		$d = DealerClaim::where('formT' == 'R')->all();

		
		$data12 = array(); $data24 = array(); $final = array();
		foreach($d as $dc){
			if($dc->activated_my == $mY){
				if($dc->contract == "12"){
					$data12[] = $dc;
				}else if($dc->contract == "24"){
					$data24[] = $dc;
				}
			}
		}

		$countD24 = count($data24);
		$countD12 = count($data12);

		$comm = Commission::where('formT','R')->get();
		$commV12 = 0; $commV24 = 0;
		$comm12 = ''; $comm24 = '';

		foreach($comm as $c){
			if($c->contract =='12'){
				if(strpos($c->ranges, '>') !== false) {

					$pieces = explode(">", $c->ranges);

					if($countD12 >= intval($pieces[1])){
						$commV12 = floatval($c->comm)/100;
						$comm12 = $c->comm.'%';
					}
				}
				if(strpos($c->ranges,'-') !== false){

					$pieces = explode("-", $c->ranges);

					if($countD12 >= intval($pieces[0]) && $countD12 <= intval($pieces[1])){
						$commV12 = floatval($c->comm)/100;
						$comm12 = $c->comm.'%';
					}

				}

			}else if($c->contract =='24'){
					// Contract => 24
				if($countD24 != 0){
					if(strpos($c->ranges, '>') !== false) {

						$pieces = explode(">", $c->ranges);

						if($countD24 > intval($pieces[1])){
							$commV24 = floatval($c->comm)/100;
							$comm24 = $c->comm.'%';
						}
					}
					if(strpos($c->ranges,'-') !== false){

						$pieces = explode("-", $c->ranges);

						if($countD24 >= intval($pieces[0]) && $countD24 <= intval($pieces[1])){
							$commV24 = floatval($c->comm)/100;
							$comm24 = $c->comm.'%';
						}
					}
				}
			}
		}

		foreach ($data12 as $key => $d) {

			$customer = Subscriber::where('_id', $d['customer_id'])->first();
			$address = $customer->unit_no.', '.$customer->building.', '.$customer->street.', '.$customer->postcode.', '.$customer->city.', '.$customer->state;

			$final[] = array(
				'index' => $d['formT'],
				'contract' => $d['contract'],
				'wo' => $d['wo'],
				'activation' => $d['activated_dt'],
				'name' => $customer->name,
				'address' => $address,
				'package' => $d['package'],
				'retail_price' => 'RM '.$d['retail_price'],
				'incentives1' => $comm12,
				'incentives2' => '',
				'total' => 'RM '.number_format($d['retail_price']*$commV12,2),
				'claim' => $d['claim_dt'],
			);
		}

		foreach ($data24 as $key => $d) {

			$customer = Subscriber::where('_id', $d['customer_id'])->first();
			$address = $customer->unit_no.', '.$customer->building.', '.$customer->street.', '.$customer->postcode.', '.$customer->city.', '.$customer->state;

			$final[] = array(
				'index' => $d['formT'],
				'contract' => $d['contract'],
				'wo' => $d['wo'],
				'activation' => $d['activated_dt'],
				'name' => $customer->name,
				'address' => $address,
				'package' => $d['package'],
				'retail_price' => 'RM '.$d['retail_price'],
				'incentives1' => $comm24,
				'incentives2' => '',
				'total' => 'RM '.number_format($d['retail_price']*$commV24,2),
				'claim' => $d['claim_dt'],
			);
		}

		dd($final);
	}


	public function getDealerCompany(){
		$dataD = $this->storeDealerClaim();
		dd($dataD);
	} 

	public function updateIncentives(){
		$data_not_activated = array();
		$dealerclaim = DealerClaim::get();
		foreach ($dealerclaim as $key => $d) {
			$wo = WorkOrder::where('wo',$d->wo)->first();
			if(!empty($wo)){
				if($wo->nature_work !== 'Activation'){
					$data_not_activated[] = $d;
				}
			}
		}
		dd($data_not_activated);
	}

}
