@@ -176,42 +176,15 @@ public function getTicketCollectionFrom($ticketList)
176176 });
177177 }
178178
179- if (isset ($ search_fields ['attachment_name ' ])) {
180- $ collection ->where (function ($ query ) use ($ search_fields ) {
181- $ query ->whereHas ('attachments ' , function ($ q1 ) use ($ search_fields ) {
182- $ q1 ->where ('original_filename ' , 'like ' , '% ' .$ search_fields ['attachment_name ' ].'% ' )
183- ->orWhere ('new_filename ' , 'like ' , '% ' .$ search_fields ['attachment_name ' ].'% ' )
184- ->orWhere ('description ' , 'like ' , '% ' .$ search_fields ['attachment_name ' ].'% ' );
185- })
186- ->orWhereHas ('comments ' , function ($ q2 ) use ($ search_fields ) {
187- $ q2 ->whereHas ('attachments ' , function ($ q3 ) use ($ search_fields ) {
188- $ q3 ->where ('original_filename ' , 'like ' , '% ' .$ search_fields ['attachment_name ' ].'% ' )
189- ->orWhere ('new_filename ' , 'like ' , '% ' .$ search_fields ['attachment_name ' ].'% ' )
190- ->orWhere ('description ' , 'like ' , '% ' .$ search_fields ['attachment_name ' ].'% ' );
191- });
192- });
193- });
179+ if (isset ($ search_fields ['attachment_name ' ])){
180+ $ collection ->whereIn ('panichd_tickets.id ' , $ this ->listTicketsWhereAttachmentHas ($ search_fields ['attachment_name ' ]));
194181 }
195182
196183 if (isset ($ search_fields ['any_text_field ' ])) {
197- // Coincidence in Ticket / Comment attachments
198- $ attachments_col = Attachment::where ('original_filename ' , 'like ' , '% ' . $ search_fields ['any_text_field ' ] . '% ' )
199- ->orWhere ('new_filename ' , 'like ' , '% ' . $ search_fields ['any_text_field ' ] . '% ' )
200- ->orWhere ('description ' , 'like ' , '% ' . $ search_fields ['any_text_field ' ] . '% ' )
201- ->with ('ticket ' , 'comment.ticket ' )->get ();
202-
203- $ a_ticket_ids = [];
204- foreach ($ attachments_col as $ att ){
205- if (!is_null ($ att ->ticket )){
206- $ a_ticket_ids [] = $ att ->ticket ->id ;
207-
208- }elseif (!is_null ($ att ->comment ) and !is_null ($ att ->comment ->ticket )){
209- $ a_ticket_ids [] = $ att ->comment ->ticket ->id ;
210- }
211- }
184+ $ a_attachment_tickets = $ this ->listTicketsWhereAttachmentHas ($ search_fields ['any_text_field ' ]);
212185
213186 // Coincidence in any ticket field
214- $ collection ->where (function ($ query ) use ($ search_fields , $ a_ticket_ids ) {
187+ $ collection ->where (function ($ query ) use ($ search_fields , $ a_attachment_tickets ) {
215188 $ query ->where ('subject ' , 'like ' , '% ' .$ search_fields ['any_text_field ' ].'% ' )
216189 ->orWhere ('content ' , 'like ' , '% ' .$ search_fields ['any_text_field ' ].'% ' )
217190 ->orWhere ('intervention ' , 'like ' , '% ' .$ search_fields ['any_text_field ' ].'% ' )
@@ -220,7 +193,7 @@ public function getTicketCollectionFrom($ticketList)
220193 })
221194
222195 // Attachment with coincidence with "any_text_field"
223- ->orWhereIn ('panichd_tickets.id ' , $ a_ticket_ids );
196+ ->orWhereIn ('panichd_tickets.id ' , $ a_attachment_tickets );
224197 });
225198 }
226199 }
0 commit comments