getUser(); $user->requireAdmin(); $core->addBreadcrumb("/search", __("Search")); $request = Request::fromRequest(); $term = $request->string("term"); $results = []; if($term) { if(mb_strlen($term) < 3) { Session::addAlert("danger", __("Search term must be 3 or more characters long.")); } else { // References if(preg_match("/^([LGASD])(\d+)$/i", $term, $matches)) { $type = strtoupper($matches[1]); $number = $matches[2]; $document_type = null; switch($type) { case "L": break; case "G": $document_type = DocumentTypes::GOODS_IN; break; case "A": $document_type = DocumentTypes::ADVANCE_REPLACEMENT_UNIT; break; case "S": $document_type = DocumentTypes::SALES_ORDER; break; case "D": $document_type = DocumentTypes::DELIVERY_NOTE; break; } if($document_type === null) { $sale = Sale::getByNumber($number); if($sale) { $results[] = [ "type" => __("Lead"), "match_reason" => __("Lead reference match."), "name" => $sale->getTitle(), "url" => $sale->getUrl() ]; } } else { $document = Document::queryOne([ "number" => $number, "type" => $document_type, "dts_deleted" => null ]); if($document) { $results[] = [ "type" => $document->getDisplayType(), "match_reason" => __("%s reference match.", $document->getDisplayType()), "name" => $document->getTitle(), "url" => $document->getUrl() ]; } } } // Search sale and document numbers if(is_numeric($term)) { $documents = Document::query([ "number" => $term, "dts_deleted" => null ]); foreach($documents as $document) { $results[] = [ "type" => $document->getDisplayType(), "match_reason" => __("%s number match.", $document->getDisplayType()), "name" => $document->getTitle(), "url" => $document->getUrl() ]; } $sale = Sale::getByNumber($term); if($sale) { $results[] = [ "type" => __("Lead"), "match_reason" => __("Lead number match."), "name" => $sale->getTitle(), "url" => $sale->getUrl() ]; } } // Document items - match serial number $items = DocumentItem::queryFuzzy([ "serial_number" => $term, "dts_deleted" => null ]); foreach($items as $item) { $document = $item->getDocument(); $results[] = [ "type" => __("%s Item", $document->getDisplayType()), "match_reason" => __("%s item serial number match.", $document->getDisplayType()), "name" => $document->getTitle(), "url" => $document->getUrl() ]; } // Document items - match description $items = DocumentItem::queryFuzzy([ "description" => $term, "dts_deleted" => null ]); foreach($items as $item) { $document = $item->getDocument(); $results[] = [ "type" => __("%s Item", $document->getDisplayType()), "match_reason" => __("%s item description match.", $document->getDisplayType()), "name" => $document->getTitle(), "url" => $document->getUrl() ]; } // Lead notes $notes = SaleNote::queryFuzzy([ "content" => $term, "dts_deleted" => null ]); foreach($notes as $note) { $results[] = [ "type" => __("Lead Item"), "match_reason" => __("Lead note match."), "name" => $note->getSale()->getTitle(), "url" => $note->getSale()->getUrl() ]; } // Ticket comments $comments = TicketComment::queryFuzzy([ "content" => $term, "dts_deleted" => null ]); foreach($comments as $comment) { $results[] = [ "type" => __("Ticket / Comment"), "match_reason" => __("Ticket or ticket comment match."), "name" => $comment->getTicket()->getTitle(), "url" => $comment->getTicket()->getUrl() . "#ticket-comment-" . $comment->id ]; } // Customers $customers = Customer::queryFuzzy([ "name" => $term, "dts_deleted" => null ]); foreach($customers as $customer) { $results[] = [ "type" => __("Customer"), "match_reason" => __("Customer name match."), "name" => $customer->name, "url" => $customer->getUrl() ]; } // Locations $locations = Location::queryFuzzy([ "name" => $term, "dts_deleted" => null ]); foreach($locations as $location) { $results[] = [ "type" => __("Location"), "match_reason" => __("Location name match."), "name" => $location->getDisplayName(), "url" => $location->getUrl() ]; } // Products $products = Product::queryFuzzy([ "name" => $term, "dts_deleted" => null ]); foreach($products as $product) { $results[] = [ "type" => __("Product"), "match_reason" => __("Product name match."), "name" => $product->name, "url" => $product->getUrl() ]; } // Suppliers $suppliers = Supplier::queryFuzzy([ "name" => $term, "dts_deleted" => null ]); foreach($suppliers as $supplier) { $results[] = [ "type" => __("Supplier"), "match_reason" => __("Supplier name match."), "name" => $supplier->name, "url" => $supplier->getUrl() ]; } } } View::include("header"); View::include("search", compact("term", "results")); View::include("footer");