Skip to content

Commit

Permalink
added customer and vendor scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Apr 7, 2020
1 parent d94bf5d commit a2b17ea
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 37 deletions.
8 changes: 4 additions & 4 deletions app/Abstracts/Listeners/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,17 @@ public function getCategories($types)

public function getCustomers()
{
return $this->getContacts($this->getCustomerTypes());
return $this->getContacts('customer');
}

public function getVendors()
{
return $this->getContacts($this->getVendorTypes());
return $this->getContacts('vendor');
}

public function getContacts($types)
public function getContacts($type)
{
return Contact::type($types)->orderBy('name')->pluck('name', 'id')->toArray();
return Contact::$type()->orderBy('name')->pluck('name', 'id')->toArray();
}

public function applyDateFilter($event)
Expand Down
7 changes: 2 additions & 5 deletions app/Http/Controllers/Common/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,9 @@
use App\Models\Purchase\Bill;
use App\Models\Sale\Invoice;
use App\Models\Common\Item;
use App\Traits\Contacts;

class Search extends Controller
{
use Contacts;

/**
* Display a listing of the resource.
*
Expand Down Expand Up @@ -82,7 +79,7 @@ public function index()
}
}*/

$customers = Contact::type($this->getCustomerTypes())->enabled()->usingSearchString($keyword)->get();
$customers = Contact::customer()->enabled()->usingSearchString($keyword)->get();

if ($customers->count()) {
foreach ($customers as $customer) {
Expand Down Expand Up @@ -124,7 +121,7 @@ public function index()
}
}*/

$vendors = Contact::type($this->getVendorTypes())->enabled()->usingSearchString($keyword)->get();
$vendors = Contact::vendor()->enabled()->usingSearchString($keyword)->get();

if ($vendors->count()) {
foreach ($vendors as $vendor) {
Expand Down
11 changes: 5 additions & 6 deletions app/Http/Controllers/Purchases/Bills.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
use App\Traits\Contacts;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Purchases;
Expand All @@ -29,7 +28,7 @@

class Bills extends Controller
{
use Contacts, Currencies, DateTime, Purchases, Uploads;
use Currencies, DateTime, Purchases, Uploads;

/**
* Display a listing of the resource.
Expand All @@ -40,7 +39,7 @@ public function index()
{
$bills = Bill::with(['contact', 'items', 'histories', 'transactions'])->collect(['billed_at'=> 'desc']);

$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('expense')->enabled()->orderBy('name')->pluck('name', 'id');

Expand All @@ -66,7 +65,7 @@ public function show(Bill $bill)

$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();

$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('expense')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down Expand Up @@ -97,7 +96,7 @@ public function show(Bill $bill)
*/
public function create()
{
$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();

Expand Down Expand Up @@ -193,7 +192,7 @@ public function import(ImportRequest $request)
*/
public function edit(Bill $bill)
{
$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();

Expand Down
9 changes: 4 additions & 5 deletions app/Http/Controllers/Purchases/Payments.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use App\Models\Common\Contact;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Traits\Contacts;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Utilities\Modules;

class Payments extends Controller
{
use Contacts, Currencies, DateTime;
use Currencies, DateTime;

/**
* Display a listing of the resource.
Expand All @@ -33,7 +32,7 @@ public function index()
{
$payments = Transaction::type('expense')->with(['account', 'category', 'contact'])->isNotTransfer()->collect(['paid_at'=> 'desc']);

$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('expense')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down Expand Up @@ -67,7 +66,7 @@ public function create()

$currency = Currency::where('code', $account_currency_code)->first();

$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('expense')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down Expand Up @@ -155,7 +154,7 @@ public function edit(Transaction $payment)

$currency = Currency::where('code', $payment->currency_code)->first();

$vendors = Contact::type($this->getVendorTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$vendors = Contact::vendor()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('expense')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Purchases/Vendors.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Vendors extends Controller
*/
public function index()
{
$vendors = Contact::type($this->getVendorTypes())->collect();
$vendors = Contact::vendor()->collect();

return view('purchases.vendors.index', compact('vendors'));
}
Expand Down
5 changes: 1 addition & 4 deletions app/Http/Controllers/Sales/Customers.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,19 @@
use App\Models\Common\Contact;
use App\Models\Sale\Invoice;
use App\Models\Setting\Currency;
use App\Traits\Contacts;
use Date;
use Illuminate\Http\Request as BaseRequest;

class Customers extends Controller
{
use Contacts;

/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
$customers = Contact::type($this->getCustomerTypes())->collect();
$customers = Contact::customer()->collect();

return view('sales.customers.index', compact('customers'));
}
Expand Down
11 changes: 5 additions & 6 deletions app/Http/Controllers/Sales/Invoices.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
use App\Notifications\Sale\Invoice as Notification;
use App\Traits\Contacts;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Sales;
Expand All @@ -30,7 +29,7 @@

class Invoices extends Controller
{
use Contacts, Currencies, DateTime, Sales;
use Currencies, DateTime, Sales;

/**
* Display a listing of the resource.
Expand All @@ -41,7 +40,7 @@ public function index()
{
$invoices = Invoice::with(['contact', 'items', 'histories', 'transactions'])->collect(['invoice_number'=> 'desc']);

$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');

Expand All @@ -67,7 +66,7 @@ public function show(Invoice $invoice)

$account_currency_code = Account::where('id', setting('default.account'))->pluck('currency_code')->first();

$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down Expand Up @@ -100,7 +99,7 @@ public function show(Invoice $invoice)
*/
public function create()
{
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();

Expand Down Expand Up @@ -196,7 +195,7 @@ public function import(ImportRequest $request)
*/
public function edit(Invoice $invoice)
{
$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();

Expand Down
9 changes: 4 additions & 5 deletions app/Http/Controllers/Sales/Revenues.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use App\Models\Common\Contact;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Traits\Contacts;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Utilities\Modules;

class Revenues extends Controller
{
use Contacts, Currencies, DateTime;
use Currencies, DateTime;

/**
* Display a listing of the resource.
Expand All @@ -33,7 +32,7 @@ public function index()
{
$revenues = Transaction::type('income')->with(['account', 'category', 'contact'])->isNotTransfer()->collect(['paid_at'=> 'desc']);

$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down Expand Up @@ -67,7 +66,7 @@ public function create()

$currency = Currency::where('code', $account_currency_code)->first();

$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down Expand Up @@ -155,7 +154,7 @@ public function edit(Transaction $revenue)

$currency = Currency::where('code', $revenue->currency_code)->first();

$customers = Contact::type($this->getCustomerTypes())->enabled()->orderBy('name')->pluck('name', 'id');
$customers = Contact::customer()->enabled()->orderBy('name')->pluck('name', 'id');

$categories = Category::type('income')->enabled()->orderBy('name')->pluck('name', 'id');

Expand Down
25 changes: 24 additions & 1 deletion app/Models/Common/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use App\Abstracts\Model;
use Bkwld\Cloner\Cloneable;
use App\Traits\Contacts;
use App\Traits\Currencies;
use App\Traits\Media;
use Illuminate\Notifications\Notifiable;

class Contact extends Model
{
use Cloneable, Currencies, Media, Notifiable;
use Cloneable, Contacts, Currencies, Media, Notifiable;

protected $table = 'contacts';

Expand Down Expand Up @@ -79,6 +80,28 @@ public function scopeType($query, $types)
return $query->whereIn($this->table . '.type', (array) $types);
}

/**
* Scope to include only vendors.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeVendor($query)
{
return $query->whereIn($this->table . '.type', (array) $this->getVendorTypes());
}

/**
* Scope to include only customers.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeCustomer($query)
{
return $query->whereIn($this->table . '.type', (array) $this->getCustomerTypes());
}

public function scopeEmail($query, $email)
{
return $query->where('email', '=', $email);
Expand Down

0 comments on commit a2b17ea

Please sign in to comment.