<?php

namespace App\Modules\Sugarcrm\Controllers\Api\V1;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\BaseController;

class OpportunitiesControllerREST  extends BaseController
{
  public function getMyOpportunities(){
    //dd(Auth::user()->username);
          // Ready to submit the data
//    $sugar = app('SugarRest');
    $results =  DB::connection('crm')->table('opportunities')->select('opportunities.name', 'opportunities.date_closed', 'opportunities.id', 'users.user_name', 'opportunities.amount', 'opportunities.sales_stage', 'opportunities.next_step', 'accounts.name as accountName')
      ->join('users', 'opportunities.assigned_user_id', '=', 'users.id')
      ->join('accounts_opportunities', 'opportunities.id', '=', 'accounts_opportunities.opportunity_id')
      ->join('accounts', 'accounts_opportunities.account_id', '=', 'accounts.id')
      ->where('opportunities.deleted', 0)
      ->where('opportunities.sales_stage', '!=', 'Closed Lost')
      ->where('opportunities.sales_stage', '!=', 'Closed Won')
      ->where('users.user_name', Auth::user()->username)
      ->get();
     
    return true;
#    return collect($results)->each(function ($opportunity) {
#      $opportunity->url = "http://crm/index.php?module=Opportunities&return_module=Opportunities&action=DetailView&record=".$opportunity->id;
#    });  

    }
}
 
