search.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php /** @noinspection PhpUnhandledExceptionInspection */
  2. namespace App;
  3. use App\Enums\DocumentTypes;
  4. $core = Core::getInstance();
  5. $user = $core->getUser();
  6. $user->requireAdmin();
  7. $core->addBreadcrumb("/search", __("Search"));
  8. $request = Request::fromRequest();
  9. $term = $request->string("term");
  10. $results = [];
  11. if($term) {
  12. if(mb_strlen($term) < 3) {
  13. Session::addAlert("danger", __("Search term must be 3 or more characters long."));
  14. }
  15. else {
  16. // References
  17. if(preg_match("/^([LGASD])(\d+)$/i", $term, $matches)) {
  18. $type = strtoupper($matches[1]);
  19. $number = $matches[2];
  20. $document_type = null;
  21. switch($type) {
  22. case "L":
  23. break;
  24. case "G":
  25. $document_type = DocumentTypes::GOODS_IN;
  26. break;
  27. case "A":
  28. $document_type = DocumentTypes::ADVANCE_REPLACEMENT_UNIT;
  29. break;
  30. case "S":
  31. $document_type = DocumentTypes::SALES_ORDER;
  32. break;
  33. case "D":
  34. $document_type = DocumentTypes::DELIVERY_NOTE;
  35. break;
  36. }
  37. if($document_type === null) {
  38. $sale = Sale::getByNumber($number);
  39. if($sale) {
  40. $results[] = [
  41. "type" => __("Lead"),
  42. "match_reason" => __("Lead reference match."),
  43. "name" => $sale->getTitle(),
  44. "url" => $sale->getUrl()
  45. ];
  46. }
  47. }
  48. else {
  49. $document = Document::queryOne([
  50. "number" => $number,
  51. "type" => $document_type,
  52. "dts_deleted" => null
  53. ]);
  54. if($document) {
  55. $results[] = [
  56. "type" => $document->getDisplayType(),
  57. "match_reason" => __("%s reference match.", $document->getDisplayType()),
  58. "name" => $document->getTitle(),
  59. "url" => $document->getUrl()
  60. ];
  61. }
  62. }
  63. }
  64. // Search sale and document numbers
  65. if(is_numeric($term)) {
  66. $documents = Document::query([
  67. "number" => $term,
  68. "dts_deleted" => null
  69. ]);
  70. foreach($documents as $document) {
  71. $results[] = [
  72. "type" => $document->getDisplayType(),
  73. "match_reason" => __("%s number match.", $document->getDisplayType()),
  74. "name" => $document->getTitle(),
  75. "url" => $document->getUrl()
  76. ];
  77. }
  78. $sale = Sale::getByNumber($term);
  79. if($sale) {
  80. $results[] = [
  81. "type" => __("Lead"),
  82. "match_reason" => __("Lead number match."),
  83. "name" => $sale->getTitle(),
  84. "url" => $sale->getUrl()
  85. ];
  86. }
  87. }
  88. // Document items - match serial number
  89. $items = DocumentItem::queryFuzzy([
  90. "serial_number" => $term,
  91. "dts_deleted" => null
  92. ]);
  93. foreach($items as $item) {
  94. $document = $item->getDocument();
  95. $results[] = [
  96. "type" => __("%s Item", $document->getDisplayType()),
  97. "match_reason" => __("%s item serial number match.", $document->getDisplayType()),
  98. "name" => $document->getTitle(),
  99. "url" => $document->getUrl()
  100. ];
  101. }
  102. // Document items - match description
  103. $items = DocumentItem::queryFuzzy([
  104. "description" => $term,
  105. "dts_deleted" => null
  106. ]);
  107. foreach($items as $item) {
  108. $document = $item->getDocument();
  109. $results[] = [
  110. "type" => __("%s Item", $document->getDisplayType()),
  111. "match_reason" => __("%s item description match.", $document->getDisplayType()),
  112. "name" => $document->getTitle(),
  113. "url" => $document->getUrl()
  114. ];
  115. }
  116. // Lead notes
  117. $notes = SaleNote::queryFuzzy([
  118. "content" => $term,
  119. "dts_deleted" => null
  120. ]);
  121. foreach($notes as $note) {
  122. $results[] = [
  123. "type" => __("Lead Item"),
  124. "match_reason" => __("Lead note match."),
  125. "name" => $note->getSale()->getTitle(),
  126. "url" => $note->getSale()->getUrl()
  127. ];
  128. }
  129. // Ticket comments
  130. $comments = TicketComment::queryFuzzy([
  131. "content" => $term,
  132. "dts_deleted" => null
  133. ]);
  134. foreach($comments as $comment) {
  135. $results[] = [
  136. "type" => __("Ticket / Comment"),
  137. "match_reason" => __("Ticket or ticket comment match."),
  138. "name" => $comment->getTicket()->getTitle(),
  139. "url" => $comment->getTicket()->getUrl() . "#ticket-comment-" . $comment->id
  140. ];
  141. }
  142. // Customers
  143. $customers = Customer::queryFuzzy([
  144. "name" => $term,
  145. "dts_deleted" => null
  146. ]);
  147. foreach($customers as $customer) {
  148. $results[] = [
  149. "type" => __("Customer"),
  150. "match_reason" => __("Customer name match."),
  151. "name" => $customer->name,
  152. "url" => $customer->getUrl()
  153. ];
  154. }
  155. // Locations
  156. $locations = Location::queryFuzzy([
  157. "name" => $term,
  158. "dts_deleted" => null
  159. ]);
  160. foreach($locations as $location) {
  161. $results[] = [
  162. "type" => __("Location"),
  163. "match_reason" => __("Location name match."),
  164. "name" => $location->getDisplayName(),
  165. "url" => $location->getUrl()
  166. ];
  167. }
  168. // Products
  169. $products = Product::queryFuzzy([
  170. "name" => $term,
  171. "dts_deleted" => null
  172. ]);
  173. foreach($products as $product) {
  174. $results[] = [
  175. "type" => __("Product"),
  176. "match_reason" => __("Product name match."),
  177. "name" => $product->name,
  178. "url" => $product->getUrl()
  179. ];
  180. }
  181. // Suppliers
  182. $suppliers = Supplier::queryFuzzy([
  183. "name" => $term,
  184. "dts_deleted" => null
  185. ]);
  186. foreach($suppliers as $supplier) {
  187. $results[] = [
  188. "type" => __("Supplier"),
  189. "match_reason" => __("Supplier name match."),
  190. "name" => $supplier->name,
  191. "url" => $supplier->getUrl()
  192. ];
  193. }
  194. }
  195. }
  196. View::include("header");
  197. View::include("search", compact("term", "results"));
  198. View::include("footer");