| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- <?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($product->promotion_price != "0"){
- $price = (int)$product->promotion_price;
- } else {
- $price = (int)$product->retail_price;
- }
-
- 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' => $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->company_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;
- }
- }
- }
|