144 lines
4.9 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
2018-06-10 02:48:51 +03:00
namespace App\Http\Controllers\Common;
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
use App\Abstracts\Http\Controller;
2020-05-09 16:44:58 +03:00
use App\Events\Common\GlobalSearched;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Account;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Transaction;
use App\Models\Common\Contact;
2019-12-31 15:49:09 +03:00
use App\Models\Purchase\Bill;
use App\Models\Sale\Invoice;
2018-06-10 02:48:51 +03:00
use App\Models\Common\Item;
2017-09-14 22:21:00 +03:00
class Search extends Controller
{
/**
* Display a listing of the resource.
*
* @return Response
*/
2019-11-16 10:21:14 +03:00
public function index()
2017-09-14 22:21:00 +03:00
{
2020-05-09 16:44:58 +03:00
$search = new \stdClass();
$search->results = [];
$search->keyword = request('keyword');
2017-09-14 22:21:00 +03:00
2020-05-09 16:44:58 +03:00
$accounts = Account::enabled()->usingSearchString($search->keyword)->get();
2017-09-14 22:21:00 +03:00
if ($accounts->count()) {
foreach ($accounts as $account) {
2020-05-09 16:44:58 +03:00
$search->results[] = (object) [
2017-09-14 22:21:00 +03:00
'id' => $account->id,
'name' => $account->name,
'type' => trans_choice('general.accounts', 1),
2019-11-16 10:21:14 +03:00
'color' => '#55588b',
2020-05-09 16:44:58 +03:00
'href' => route('accounts.edit', $account->id),
2017-09-14 22:21:00 +03:00
];
}
}
2020-05-09 16:44:58 +03:00
$items = Item::enabled()->usingSearchString($search->keyword)->get();
2017-09-14 22:21:00 +03:00
if ($items->count()) {
foreach ($items as $item) {
2020-05-09 16:44:58 +03:00
$search->results[] = (object) [
2017-09-14 22:21:00 +03:00
'id' => $item->id,
'name' => $item->name,
'type' => trans_choice('general.items', 1),
2019-11-16 10:21:14 +03:00
'color' => '#efad32',
2020-05-09 16:44:58 +03:00
'href' => route('items.edit', $item->id),
2017-09-14 22:21:00 +03:00
];
}
}
2020-05-09 16:44:58 +03:00
$invoices = Invoice::usingSearchString($search->keyword)->get();
2017-09-14 22:21:00 +03:00
if ($invoices->count()) {
foreach ($invoices as $invoice) {
2020-05-09 16:44:58 +03:00
$search->results[] = (object) [
2017-09-14 22:21:00 +03:00
'id' => $invoice->id,
2019-11-16 10:21:14 +03:00
'name' => $invoice->invoice_number . ' - ' . $invoice->contact_name,
2017-09-14 22:21:00 +03:00
'type' => trans_choice('general.invoices', 1),
2019-11-16 10:21:14 +03:00
'color' => '#6da252',
2020-05-09 16:44:58 +03:00
'href' => route('invoices.show', $invoice->id),
2017-09-14 22:21:00 +03:00
];
}
2019-11-16 10:21:14 +03:00
}/*
2020-05-03 11:15:56 +03:00
$income_transactions = Transaction::income()->usingSearchString($keyword)->get();
2017-09-14 22:21:00 +03:00
if ($income_transactions->count()) {
foreach ($income_transactions as $transaction) {
2019-11-16 10:21:14 +03:00
$results[] = (object)[
'id' => $transaction->id,
'name' => $transaction->contact_name,
2019-11-16 10:21:14 +03:00
'type' => trans_choice('general.revenues', 1),
'color' => '#00c0ef',
2019-12-31 15:49:09 +03:00
'href' => url('sales/revenues/' . $transaction->id),
2019-11-16 10:21:14 +03:00
];
}
}*/
2017-09-14 22:21:00 +03:00
2020-05-09 16:44:58 +03:00
$customers = Contact::customer()->enabled()->usingSearchString($search->keyword)->get();
2017-09-14 22:21:00 +03:00
if ($customers->count()) {
foreach ($customers as $customer) {
2020-05-09 16:44:58 +03:00
$search->results[] = (object) [
2017-09-14 22:21:00 +03:00
'id' => $customer->id,
'name' => $customer->name,
'type' => trans_choice('general.customers', 1),
2019-11-16 10:21:14 +03:00
'color' => '#328aef',
2020-05-09 16:44:58 +03:00
'href' => route('customers.show', $customer->id),
2017-09-14 22:21:00 +03:00
];
}
}
2020-05-09 16:44:58 +03:00
$bills = Bill::usingSearchString($search->keyword)->get();
2017-09-14 22:21:00 +03:00
if ($bills->count()) {
foreach ($bills as $bill) {
2020-05-09 16:44:58 +03:00
$search->results[] = (object) [
2017-09-14 22:21:00 +03:00
'id' => $bill->id,
2019-11-16 10:21:14 +03:00
'name' => $bill->bill_number . ' - ' . $bill->contact_name,
2017-09-14 22:21:00 +03:00
'type' => trans_choice('general.bills', 1),
2019-11-16 10:21:14 +03:00
'color' => '#ef3232',
2020-05-09 16:44:58 +03:00
'href' => route('bills.show', $bill->id),
2017-09-14 22:21:00 +03:00
];
}
}
2019-11-16 10:21:14 +03:00
/*
2020-05-03 11:15:56 +03:00
$payments = Transaction::expense()->usingSearchString($keyword)->get();
2017-09-14 22:21:00 +03:00
2019-11-16 10:21:14 +03:00
if ($revenues->count()) {
foreach ($revenues as $revenue) {
$results[] = (object)[
'id' => $revenue->id,
'name' => $revenue->contact_name,
'type' => trans_choice('general.revenues', 1),
'color' => '#00c0ef',
2019-12-31 15:49:09 +03:00
'href' => url('sales/revenues/' . $revenue->id),
2019-11-16 10:21:14 +03:00
];
}
}*/
2017-09-14 22:21:00 +03:00
2020-05-09 16:44:58 +03:00
$vendors = Contact::vendor()->enabled()->usingSearchString($search->keyword)->get();
2017-09-14 22:21:00 +03:00
if ($vendors->count()) {
foreach ($vendors as $vendor) {
2020-05-09 16:44:58 +03:00
$search->results[] = (object) [
2017-09-14 22:21:00 +03:00
'id' => $vendor->id,
'name' => $vendor->name,
'type' => trans_choice('general.vendors', 1),
2019-11-16 10:21:14 +03:00
'color' => '#efef32',
2020-05-09 16:44:58 +03:00
'href' => route('vendors.show', $vendor->id),
2017-09-14 22:21:00 +03:00
];
}
}
2020-05-09 16:44:58 +03:00
event(new GlobalSearched($search));
return response()->json((object) $search->results);
2017-09-14 22:21:00 +03:00
}
}