Untitled

mail@pastecode.io avatar
unknown
plain_text
25 days ago
19 kB
2
Indexable
Never
public function providerDetails($id, Request $request,AccountingServices $AccountingServices){
      
        if ($request->ajax()) {
            $request['provider'] =array($id);
            $data = $AccountingServices->getAccounting($request, self::ELEMENTS_PER_PAGE);
            foreach ($data['accounting'] as $key => $value) {
                if(is_array(json_decode($value->vehicles, true))){
                $getVehicles = Vehicles::whereIn('id', json_decode($value->vehicles, true))->get();
                    $data['accounting'][$key]['vehicle'] = $getVehicles;
                }else{
                    $data['accounting'][$key]['vehicle'] = array();
                }
                $getNote = AccountingNote::select(DB::raw('accounting_note.*,users.first_name,users.last_name,users.profile_picture'))->leftJoin('users', 'accounting_note.user_id', '=', 'users.id')->where('accounting_note.accounting_id', '=', $value->id)->orderBy('accounting_note.id', 'desc')->get();
                $data['accounting'][$key]['notes'] = $getNote;

                $payment_details=PaymentDetails::select(DB::raw("GROUP_CONCAT(payment_id) as payment_ids"))->where('accounting_id',$value->id)->first();
                $payments=Payment::whereIn('id',explode(",",$payment_details->payment_ids))->get();
                $data['accounting'][$key]['payments'] = $payments;
            }

            $view = \View::make('admin.accounting.viewAccountingPartial', $data);
                $html = $view->render();
                return response()->json(['error' => false, 'message' => 'success', 'html' => $html]);
        }else{
            $atwho = $sale_note = $purchase_note =array();
              $allNote=array();

              
              $provider =  ProviderExpanse::select(DB::raw('provider_for_expanse.*,provider_type.type_name as category_name'))
                    ->leftJoin('provider_type','provider_for_expanse.category','=','provider_type.id')->where('provider_for_expanse.id','=',$id)->first();
                if(empty($provider)){
           
                return redirect('viewProvider')->with('error', 'Provider not found');
                }        
              $accounting = Accounting::select(DB::raw('accounting.*,provider_for_expanse.provider_name,provider_type.type_name as category_name'))
                    ->leftJoin('provider_for_expanse','accounting.provider_id','=','provider_for_expanse.id')
                    ->leftJoin('provider_type','accounting.category_id','=','provider_type.id')->where('accounting.provider_id','=',$id)->orderBy('invoice_date','asc')->with('paymentDetails')

                    ->get();

              
               $accounting_count = $accounting->count();
               $unionQuery1 = null;     
               $unionQuery2 = null;     
              foreach($accounting as $key => $value){
                 if(is_array(json_decode($value->vehicles, true))){
                    $getVehicles = Vehicles::whereIn('id', json_decode($value->vehicles, true))->get();
                    $accounting[$key]['vehicle']= $getVehicles;
                }else{
                    $accounting[$key]['vehicle']= array();
                }
                 

                $getNoteP=AccountingNote::
                    leftJoin('users','accounting_note.user_id','=','users.id')
                    ->where('accounting_note.accounting_id','=',$value->id)
                    ->orderBy('accounting_note.id','desc')
                    ->select(
                        DB::raw('users.profile_picture'),                        
                        DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS full_name"),
                        DB::raw("'' AS title"),
                        DB::raw("'purchase' AS type"),
                        DB::raw("accounting_note.note AS note"),
                        DB::raw("accounting_note.created_at AS created_at")                        
                    );

                if ($unionQuery1 === null) {
                    $unionQuery1 = $getNoteP;
                } else {
                    $unionQuery1->union($getNoteP);
                }    
                
                    

                $getNoteS=AccountingNote::
                    leftJoin('users','accounting_note.user_id','=','users.id')
                    ->where('accounting_note.accounting_id','=',$value->id)
                    ->orderBy('accounting_note.id','desc')
                    ->select(  
                        DB::raw('users.profile_picture'),                            
                        DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS full_name"),
                        DB::raw("'' AS title"),
                        DB::raw("'sale' AS type"),
                        DB::raw("accounting_note.note AS note"),
                        DB::raw("accounting_note.created_at AS created_at")                        
                    );        

                 $accounting[$key]['notes']=$getNoteP->get();

                if ($unionQuery2 === null) {
                    $unionQuery2 = $getNoteS;
                } else {
                    $unionQuery2->union($getNoteS);
                }

                 /*$payment_details=PaymentDetails::select(DB::raw("payment_details.*,payment.status,payment.payment_date,payment.merged_pdf,payment.payment_proof,payment.cancel_proof"))->leftJoin('payment','payment_details.payment_id','=','payment.id')->where('accounting_id',$value->id)->get();*/
                 //$payments=Payment::whereIn('id',explode(",",$payment_details->payment_ids))->get();
                 //$accounting[$key]['payments'] = $payment_details;

                 /*if($value->type == 'sale' || $value->type == 'bank_deposit'){
                    array_push($sale_note, $getNoteS);
                 }

                 if($value->type == 'expense'){                    
                    array_push($purchase_note, $getNoteP);
                 }*/

              }

              if(isset($unionQuery2)){
                $new_purchase_note = $unionQuery2->get();  
              }
              if(isset($unionQuery2)){
              
                $new_sale_note = $unionQuery2->get(); 
              }             /*
              foreach ($purchase_note as $pkey => $pvalue) {
                 foreach ($pvalue as $ppkey => $ppvalue) {
                   array_push($new_purchase_note, $ppvalue);

                   array_push($allNote,array('first_name'=>$ppvalue->first_name,'last_name'=>$ppvalue->last_name,'note'=>$ppvalue->note,'created_at'=>$ppvalue->created_at,"title"=>"","type" => 'purchase'));
                 }
              }
              foreach ($sale_note as $skey => $svalue) {
                 foreach ($svalue as $sskey => $ssvalue) {
                   array_push($new_sale_note, $ssvalue);

                   array_push($allNote,array('first_name'=>$ssvalue->first_name,'last_name'=>$ssvalue->last_name,'note'=>$ssvalue->note,'created_at'=>$ssvalue->created_at,"title"=>"","type" => 'sale'));
                 }
              }*/

              

              $totals = Accounting::select(
                    DB::raw('SUM(CASE WHEN accounting.type = "sale" THEN accounting.subtotal ELSE 0 END) - SUM(CASE WHEN accounting.type = "expense" THEN accounting.subtotal ELSE 0 END) AS subtotal'),
                    DB::raw('SUM(CASE WHEN accounting.type = "sale" THEN accounting.gst ELSE 0 END) - SUM(CASE WHEN accounting.type = "expense" THEN accounting.gst ELSE 0 END) AS gst'),
                    DB::raw('SUM(CASE WHEN accounting.type = "sale" THEN accounting.pst ELSE 0 END) - SUM(CASE WHEN accounting.type = "expense" THEN accounting.pst ELSE 0 END) AS pst'),
                    DB::raw('SUM(CASE WHEN accounting.type = "sale" THEN accounting.total ELSE 0 END) - SUM(CASE WHEN accounting.type = "expense" THEN accounting.total ELSE 0 END) AS total')
                )
                ->where('accounting.provider_id', '=', $id)
                ->first();

              
              
               $vehicle=Vehicles::select('id','make','stock','model','year')->get();
               $client=Client::select('id','first_name','last_name')->get();
               $category=TaskCategory::select('id','category_name')->where('status','active')->get();
               
               $saleCategory = SaleCategory::select('id','category_name')->where('status', 'active')->get();     
               //$staff=User::where('user_type','=','staff')->where('status','=','active')->get();
               $providers=ProviderExpanse::select('id','provider_name')->where('status','active')->get();
               $staff = User::select('id','first_name','last_name',DB::raw("CONCAT(first_name, ' ', last_name) AS full_name"))->where('user_type','staff')->where('status', '=', 'active')->get();

                $atwho = $staff->pluck('full_name')->toArray();

                //$vehicleLocation=VehicleLocation::all();
                //$vehicleType=Vehicles::select(DB::raw('vehicle_type'))->distinct('vehicle_type')->get();
                //$vehicleStatus=VehicleStatus::where('status','active')->orderBy('priority', 'asc')->get();
                //$make = Vehicles::select(DB::raw('make'))->distinct('make')->get();
                //$model = Vehicles::select(DB::raw('model'))->distinct('model')->get();
                //$clientType=ClientCategory::where('status', '=', 'active')->get();
                //$clientStatus=ClientStatus::where('status', '=', 'active')->get();
                
                
                //$allvehicle=Vehicles::all();
               //-------------------------------------Task-----------------------------------------// 

                
                 $task = Task::
                    select('task.id')
                    //select(DB::raw('task.*,task_status.status_name,task_status.badge_color as status_badge,task_category.category_name,task_category.badge_color as category_badge,vehicles.make,vehicles.model,vehicles.year,vehicles.stock'))
                    //->leftJoin('task_status','task.status','=','task_status.id')
                    //->leftJoin('task_category','task.category','=','task_category.id')
                    //->leftJoin('users','task.assign_to','=','users.id')
                    //->leftJoin('clients','task.client','=','clients.id')
                    //->leftJoin('provider_for_expanse','task.provider_id','=','provider_for_expanse.id')
                    //->leftJoin('vehicles','task.vehicle','=','vehicles.id')
                    ->where('task.provider_id','=',$id);
                    if(\Auth::user()->user_type == 'staff'){
                        $v=\Auth::user()->id;

                         $task->where(function ($query) use ($v){
                            $query->where('task.assign_to','like', '%"'.$v.'"%');
                            $query->orWhere('task.inform','=', $v);
                            $query->orWhere('task.created_by_staff','=', $v);
                            $query->orWhere('task.staff_id','like', '%"'.$v.'"%');
                            $query->orWhere('task.user_id','=', $v);
                        });
                        
                    }
                    $task = $task->count();
                    //dd($task);
                    /*foreach($task as $key => $value){
              
                        $getAssignStaff=User::whereIn('id',json_decode($value->assign_to,true))->get();
                        $task[$key]['assign_staff']=$getAssignStaff;    
                        $getTaskNote=TaskNote::select(DB::raw('task_note.*,users.first_name,users.last_name,users.profile_picture'))->leftJoin('users','task_note.user_id','=','users.id')->where('task_note.task_id','=',$value->id)->orderBy('task_note.id','desc')->get();
                          $task[$key]['notes']=$getTaskNote;
                    }*/

               //-------------------------------------Communication-----------------------------------------//
                $email_logs=EmailSmsLogs::select(DB::raw('email_sms_logs.*,staff.first_name as staff_first_name,staff.last_name as staff_last_name,staff.profile_picture as staff_profile_picture, admin.first_name as admin_first_name,admin.last_name as admin_last_name,admin.profile_picture as admin_profile_picture, provider_for_expanse.provider_name'))
                ->leftJoin('users as admin', 'admin.id', '=', 'email_sms_logs.user_id')
                ->leftJoin('users as staff', 'staff.id', '=', 'email_sms_logs.staff_id')
                ->leftJoin('provider_for_expanse', 'provider_for_expanse.id', '=', 'email_sms_logs.provider_id')
                ->where('email_sms_logs.type','=','email')
                ->where('email_sms_logs.provider_id','=',$id)->orderBy('email_sms_logs.id', 'desc')->get();

                $sms_logs=EmailSmsLogs::select(DB::raw('email_sms_logs.*,staff.first_name as staff_first_name,staff.last_name as staff_last_name,staff.profile_picture as staff_profile_picture, admin.first_name as admin_first_name,admin.last_name as admin_last_name,admin.profile_picture as admin_profile_picture, provider_for_expanse.provider_name'))
                ->leftJoin('users as admin', 'admin.id', '=', 'email_sms_logs.user_id')
                ->leftJoin('users as staff', 'staff.id', '=', 'email_sms_logs.staff_id')
                ->leftJoin('provider_for_expanse', 'provider_for_expanse.id', '=', 'email_sms_logs.provider_id')
                ->where('email_sms_logs.type','=','sms')
                ->where('email_sms_logs.provider_id','=',$id)->orderBy('email_sms_logs.id', 'asc')->get();

                $email_logs_counts=EmailSmsLogs::where('provider_id','=',$id)->where('type','=','email')->where('is_read','=','0')->count();
                $sms_logs_counts=EmailSmsLogs::where('provider_id','=',$id)->where('type','=','sms')->where('is_read','=','0')->count();
                //-------------------------------------others Details-----------------------------------------//

                $document=ProviderDocuments::select(DB::raw('provider_documents.*,users.first_name,users.last_name'))
                ->where('provider_documents.provider_id','=',$id)
                ->leftJoin('users','provider_documents.user_id','=','users.id')
                ->orderBy('provider_documents.id','DESC')->get(); 

                //-----------------------------Call Log---------------------------//
                  
                  $callLog=CallLog::select(DB::raw('call_logs.*,users.first_name,users.last_name'))->leftJoin('users','call_logs.user_id','=','users.id')->where('call_logs.provider_id','=',$id)->orderBy('call_logs.id','desc')->get();  

                  //-------------------------------------Notes-----------------------------------------//
                    
                    $note=ProviderNote::leftJoin('users','provider_note.user_id','=','users.id')
                        ->where('provider_note.provider_id','=',$id)
                        ->orderBy('provider_note.id','desc')
                        ->select(
                            DB::raw('users.profile_picture'),
                            DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS full_name"),
                            DB::raw("'' AS title"),
                            DB::raw("'provider' AS type"),
                            DB::raw("provider_note.note AS note"),
                            DB::raw("provider_note.created_at AS created_at")
                        );
                    /*foreach($note as $key => $value){
                      array_push($allNote,array('first_name'=>$value->first_name,'last_name'=>$value->last_name,'note'=>$value->note,'created_at'=>$value->created_at,"title"=>"","type" => 'provider'));
                    }*/
                    $taskNote=TaskNote::leftJoin('users','task_note.user_id','=','users.id')
                        ->leftJoin('task','task_note.task_id','=','task.id')
                        ->where('task.provider_id','=',$id)
                        ->orderBy('task_note.id','desc')
                        ->select(
                            DB::raw('users.profile_picture'),
                            DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS full_name"),
                            DB::raw("task.title AS title"),
                            DB::raw("'task' AS type"),
                            DB::raw("task_note.note AS note"),
                            DB::raw("task_note.created_at AS created_at")
                        );
                    
                    /*foreach($taskNote as $key => $value){
                        if(auth()->user()->can('view_provider_task')){
                            array_push($allNote,array('first_name'=>$value->first_name,'last_name'=>$value->last_name,'note'=>$value->note,'created_at'=>$value->created_at,"title"=>$value->title,"type" => 'task'));
                        }
                      
                    }*/
                    
                    $appoinmentNote=AppoinmentNote::
                        leftJoin('users','appoinment_note.user_id','=','users.id')
                        ->leftJoin('appoinment','appoinment_note.appoinment_id','=','appoinment.id')->where('appoinment.client','=',$id)
                        ->orderBy('appoinment_note.id','desc')
                        ->select(
                            DB::raw('users.profile_picture'),
                            DB::raw("CONCAT(users.first_name, ' ', users.last_name) AS full_name"),
                            DB::raw("'' AS title"),
                            DB::raw("'appoinment' AS type"),
                            DB::raw("appoinment_note.note AS note"),
                            DB::raw("appoinment_note.created_at AS created_at")
                        );                   
                    



            
                if($unionQuery2 !== null && $unionQuery1 !==null){

                    
                    $allNote = $note->union($unionQuery2)->union($unionQuery1)->union($taskNote)->union($appoinmentNote)->orderBy('created_at', 'desc')->get()->toArray();
                    
                } elseif($unionQuery2 !== null && $unionQuery1 ==null) {
                    $allNote = $note->union($unionQuery2)->union($taskNote)->union($appoinmentNote)->orderBy('created_at', 'desc')->get()->toArray();
                } elseif($unionQuery2 == null && $unionQuery1 !==null) {
                    $allNote = $note->union($unionQuery1)->union($taskNote)->union($appoinmentNote)->orderBy('created_at', 'desc')->get()->toArray();
                } else {
                    $allNote = $note->union($taskNote)->union($appoinmentNote)->orderBy('created_at', 'desc')->get()->toArray();
                }
Leave a Comment