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, 'projectT' => $formD->project_type, '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, 'invoice_no' => $this->generateInvoiceNumber($formD->type_application,$formD->dealer_id,date('m', strtotime($docket->end_job)),date('Y', strtotime($docket->end_job))), ]; 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(); // $mY=''; // $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); } public function generateInvoiceNumber($formT,$dealer,$month,$year){ $claim = DealerClaim::where('formT',$formT)->where('dealer',$dealer)->where('activated_my',$month.'/'.$year)->first(); if(!empty($claim->invoice_no)){ return $claim->invoice_no; }else{ //get latest invoice number $invoice = DealerClaim::where('formT',$formT)->withTrashed()->latest()->first(); //sanitize invoice number to return only integer $num = filter_var($invoice['invoice_no'], FILTER_SANITIZE_NUMBER_INT); //compulsory increment to store new invoice number, WHILE number is found, keep looping do{ (int)$num += 1; //add 0s at the fornt if number to complete 9 digit if ((strlen((string) $num)) == 2) { $num = '0000000'.$num; }elseif ((strlen((string)$num)) == 3){ $num = '000000'.$num; }elseif ((strlen((string)$num )) == 4) { $num = '00000'.$num; }elseif ((strlen((string)$num )) == 5) { $num = '0000'.$num; }elseif ((strlen((string)$num )) == 6) { $num = '000'.$num; }elseif ((strlen((string)$num )) == 7) { $num = '00'.$num; }elseif ((strlen((string)$num )) == 8) { $num = '0'.$num; } }while(!empty(DealerClaim::where('formT', $formT)->where('invoice_no', $formT.$num)->first())); $num = $formT.$num; return $num; } } }