Пример #1
0
 /**
  * Get Companies options for 'select' dropdown in Vuejs format
  */
 public function getCompanies(Request $request, $company_id, $trade_id, $site_id)
 {
     $company_list = Auth::user()->company->companyList('1')->pluck('id')->toArray();
     if ($company_id == 'match-trade' || $trade_id == 'match-trade') {
         //if ($trade_id == 'match-trade')
         // All authorised companies that have the given trade_id
         $companies = Company::select(['companys.id', 'companys.name'])->join('company_trade', 'companys.id', '=', 'company_trade.company_id')->where('companys.status', '1')->where('company_trade.trade_id', $trade_id)->whereIn('companys.id', $company_list)->orderBy('name')->get();
     } else {
         if ($company_id == 'all') {
             // All authorised companies
             $companies = Company::where('status', '1')->whereIn('id', $company_list)->orderBy('name')->get();
         } else {
             // All authorised companies except the given company_id
             $companies = Company::where('status', '1')->whereIn('id', $company_list)->where('id', '<>', $company_id)->orderBy('name')->get();
         }
     }
     // Unique array of companies currently on planner for given site_id
     $companiesOnPlanner = SitePlanner::distinct()->select('entity_id')->where('site_id', $site_id)->where('entity_type', 'c')->groupBy('entity_id')->lists('entity_id')->toArray();
     $array = [];
     $array[] = ['value' => '', 'text' => 'Select company'];
     if ($company_id == 'match-trade') {
         $array[] = ['value' => 'gen', 'text' => 'Unassigned (Generic)'];
     }
     // Create array in specific Vuejs 'select' format.
     foreach ($companies as $company) {
         $text = $company->name;
         if (in_array($company->id, $companiesOnPlanner)) {
             $text = '<b>' . $company->name . '</b>';
         }
         $array[] = ['value' => $company->id, 'text' => $text, 'name' => $company->name];
     }
     return $array;
 }