33namespace App \Http \Controllers \Workflow ;
44
55use Illuminate \Support \Str ;
6+ use App \Models \User ;
67use App \Models \Workflow \Leads ;
78use App \Services \LeadsKPIService ;
89use App \Traits \NextPreviousTrait ;
910use App \Services \SelectDataService ;
1011use App \Http \Controllers \Controller ;
12+ use Illuminate \Http \Request ;
1113use Illuminate \Support \Facades \Auth ;
1214use App \Models \Workflow \Opportunities ;
1315use App \Http \Requests \Workflow \UpdateLeadRequest ;
16+ use App \Models \Companies \CompaniesAddresses ;
17+ use App \Models \Companies \CompaniesContacts ;
1418
1519class LeadsController extends Controller
1620{
@@ -30,17 +34,169 @@ public function __construct(SelectDataService $SelectDataService,
3034 * @return \Illuminate\Contracts\View\View
3135 */
3236 public function index ()
33- {
34- // Using the OpportunitiesKPIService to retrieve KPI data
35- $ data ['leadsCountRate ' ] = $ this ->leadsKPIService ->getLeadsDataRate ();
36- $ leadByCompany = $ this ->leadsKPIService ->getLeadsByCompany ();
37- $ leadsCount = $ this ->leadsKPIService ->getLeadsCount ();
38- $ leadsCountByUser = $ this ->leadsKPIService ->getLeadsCountByUser ();
37+ {
38+ $ leadsCount = $ this ->leadsKPIService ->getLeadsCount ();
39+ $ dataRate = $ this ->leadsKPIService ->getLeadsDataRate ();
40+ $ leadByCompany = $ this ->leadsKPIService ->getLeadsByCompany ();
41+ $ leadsCountByUser = $ this ->leadsKPIService ->getLeadsCountByUser ();
3942 $ leadsCountByPriority = $ this ->leadsKPIService ->getLeadsCountByPriority ();
40- return view ('workflow/leads-index ' , array_merge (
41- compact (
42- 'leadByCompany ' , 'leadsCount ' , 'leadsCountByUser ' , 'leadsCountByPriority ' )
43- ))->with ('data ' , $ data );
43+
44+ return view ('workflow/leads-index ' , [
45+ 'kpi ' => ['count ' => $ leadsCount ],
46+ 'chart ' => $ dataRate ->map (fn ($ r ) => [
47+ 'statu ' => $ r ->statu ,
48+ 'count ' => $ r ->leadsCountRate ,
49+ ])->values ()->all (),
50+ 'byCompany ' => $ leadByCompany ->map (fn ($ o ) => [
51+ 'company_label ' => $ o ->companie ?->label ?? '— ' ,
52+ 'company_url ' => route ('companies.show ' , ['id ' => $ o ->companies_id ]),
53+ 'count ' => $ o ->count ,
54+ ])->values ()->all (),
55+ 'byPriority ' => $ leadsCountByPriority ->map (fn ($ r ) => [
56+ 'priority ' => $ r ->priority ,
57+ 'count ' => $ r ->leadsCount ,
58+ ])->values ()->all (),
59+ 'byUser ' => $ leadsCountByUser ->map (fn ($ leads ) => [
60+ 'user_name ' => $ leads ->first ()->UserManagement ?->name ?? '— ' ,
61+ 'counts ' => $ leads ->pluck ('total ' , 'statu ' )->all (),
62+ ])->values ()->all (),
63+ ]);
64+ }
65+
66+ // -------------------------------------------------------------------------
67+ // JSON endpoints for the React LeadsIndex component
68+ // -------------------------------------------------------------------------
69+
70+ public function listJson (Request $ request )
71+ {
72+ $ search = $ request ->get ('search ' , '' );
73+ $ statuses = array_filter (array_map ('intval ' , (array ) $ request ->get ('statuses ' , [])));
74+ $ priorities = array_filter (array_map ('intval ' , (array ) $ request ->get ('priorities ' , [])));
75+ $ sortField = $ request ->get ('sort ' , 'created_at ' );
76+ $ sortAsc = $ request ->boolean ('asc ' , false );
77+ $ companyId = $ request ->get ('company_id ' );
78+
79+ $ allowed = ['source ' , 'campaign ' , 'priority ' , 'statu ' , 'created_at ' , 'companie ' ];
80+ if (!in_array ($ sortField , $ allowed )) {
81+ $ sortField = 'created_at ' ;
82+ }
83+
84+ $ dir = $ sortAsc ? 'asc ' : 'desc ' ;
85+ $ query = Leads::with (['companie:id,label,code ' , 'contact:id,first_name,name ' , 'UserManagement:id,name ' ])
86+ ->when ($ search , fn ($ q ) => $ q ->where (fn ($ q2 ) =>
87+ $ q2 ->where ('source ' , 'like ' , '% ' .$ search .'% ' )
88+ ->orWhere ('campaign ' , 'like ' , '% ' .$ search .'% ' )
89+ ))
90+ ->when ($ statuses , fn ($ q ) => $ q ->whereIn ('statu ' , $ statuses ))
91+ ->when ($ priorities , fn ($ q ) => $ q ->whereIn ('priority ' , $ priorities ))
92+ ->when ($ companyId , fn ($ q ) => $ q ->where ('companies_id ' , $ companyId ));
93+
94+ match ($ sortField ) {
95+ 'companie ' => $ query ->orderByRaw ("(SELECT label FROM companies WHERE companies.id = leads.companies_id) {$ dir }" ),
96+ default => $ query ->orderBy ($ sortField , $ dir ),
97+ };
98+
99+ $ items = $ query ->paginate (15 );
100+
101+ return response ()->json ([
102+ 'data ' => $ items ->map (fn ($ l ) => [
103+ 'id ' => $ l ->id ,
104+ 'source ' => $ l ->source ,
105+ 'campaign ' => $ l ->campaign ,
106+ 'priority ' => $ l ->priority ,
107+ 'statu ' => $ l ->statu ,
108+ 'created_at ' => $ l ->created_at ?->format('d/m/Y ' ),
109+ 'companie ' => $ l ->companie ? ['id ' => $ l ->companie ->id , 'label ' => $ l ->companie ->label ] : null ,
110+ 'contact ' => $ l ->contact ? ['id ' => $ l ->contact ->id , 'name ' => trim ($ l ->contact ->first_name .' ' .$ l ->contact ->name )] : null ,
111+ 'user ' => $ l ->UserManagement ? ['id ' => $ l ->UserManagement ->id , 'name ' => $ l ->UserManagement ->name ] : null ,
112+ 'url ' => route ('leads.show ' , ['id ' => $ l ->id ]),
113+ ]),
114+ 'meta ' => [
115+ 'total ' => $ items ->total (),
116+ 'per_page ' => $ items ->perPage (),
117+ 'current_page ' => $ items ->currentPage (),
118+ 'last_page ' => $ items ->lastPage (),
119+ ],
120+ ]);
121+ }
122+
123+ public function kanbanJson ()
124+ {
125+ $ items = Leads::with (['companie:id,label ' , 'contact:id,first_name,name ' , 'UserManagement:id,name ' ])->get ();
126+
127+ return response ()->json ($ items ->map (fn ($ l ) => [
128+ 'id ' => $ l ->id ,
129+ 'source ' => $ l ->source ,
130+ 'campaign ' => $ l ->campaign ,
131+ 'priority ' => $ l ->priority ,
132+ 'statu ' => $ l ->statu ,
133+ 'companie ' => $ l ->companie ? ['id ' => $ l ->companie ->id , 'label ' => $ l ->companie ->label ] : null ,
134+ 'contact ' => $ l ->contact ? ['id ' => $ l ->contact ->id , 'name ' => trim ($ l ->contact ->first_name .' ' .$ l ->contact ->name )] : null ,
135+ 'user ' => $ l ->UserManagement ? ['id ' => $ l ->UserManagement ->id , 'name ' => $ l ->UserManagement ->name ] : null ,
136+ 'url ' => route ('leads.show ' , ['id ' => $ l ->id ]),
137+ ]));
138+ }
139+
140+ public function kanbanMoveJson (Request $ request , int $ id )
141+ {
142+ abort_unless (auth ()->check (), 403 );
143+ $ validated = $ request ->validate (['statu ' => 'required|integer|between:1,5 ' ]);
144+ $ lead = Leads::findOrFail ($ id );
145+ $ lead ->update (['statu ' => $ validated ['statu ' ]]);
146+ return response ()->json (['ok ' => true ]);
147+ }
148+
149+ public function storeJson (Request $ request )
150+ {
151+ abort_unless (auth ()->check (), 403 );
152+
153+ $ validated = $ request ->validate ([
154+ 'companies_id ' => 'required|integer ' ,
155+ 'companies_contacts_id ' => 'required|integer ' ,
156+ 'companies_addresses_id ' => 'required|integer ' ,
157+ 'user_id ' => 'required|integer ' ,
158+ 'source ' => 'required|string|max:255 ' ,
159+ 'priority ' => 'required|integer|between:1,4 ' ,
160+ 'campaign ' => 'nullable|string|max:255 ' ,
161+ 'comment ' => 'nullable|string ' ,
162+ ]);
163+
164+ $ lead = Leads::create ($ validated );
165+
166+ return response ()->json ([
167+ 'redirect ' => route ('leads.show ' , ['id ' => $ lead ->id ]),
168+ ], 201 );
169+ }
170+
171+ public function selectDataJson ()
172+ {
173+ return response ()->json ([
174+ 'companies ' => $ this ->SelectDataService ->getCompanies ()->map (fn ($ c ) => [
175+ 'id ' => $ c ->id ,
176+ 'label ' => $ c ->label ?? $ c ->last_name ,
177+ 'code ' => $ c ->code ,
178+ ]),
179+ 'users ' => User::select ('id ' , 'name ' )->get (),
180+ ]);
181+ }
182+
183+ public function addressesJson (int $ companyId )
184+ {
185+ return response ()->json (
186+ CompaniesAddresses::select ('id ' , 'label ' , 'adress ' )
187+ ->where ('companies_id ' , $ companyId )
188+ ->get ()
189+ );
190+ }
191+
192+ public function contactsJson (int $ companyId )
193+ {
194+ return response ()->json (
195+ CompaniesContacts::select ('id ' , 'first_name ' , 'name ' )
196+ ->where ('companies_id ' , $ companyId )
197+ ->get ()
198+ ->map (fn ($ c ) => ['id ' => $ c ->id , 'name ' => trim ($ c ->first_name .' ' .$ c ->name )])
199+ );
44200 }
45201
46202 /**
0 commit comments