2016-07-21 14:35:23 +02:00
|
|
|
<?php namespace App\Ninja\Repositories;
|
2016-01-06 15:23:58 +01:00
|
|
|
|
|
|
|
use DB;
|
|
|
|
use App\Models\Vendor;
|
2016-07-03 18:11:58 +02:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
// vendor
|
2016-01-06 15:23:58 +01:00
|
|
|
class VendorRepository extends BaseRepository
|
|
|
|
{
|
|
|
|
public function getClassName()
|
|
|
|
{
|
|
|
|
return 'App\Models\Vendor';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function all()
|
|
|
|
{
|
|
|
|
return Vendor::scope()
|
2016-05-04 14:16:49 +02:00
|
|
|
->with('user', 'vendor_contacts', 'country')
|
2016-01-06 15:23:58 +01:00
|
|
|
->withTrashed()
|
|
|
|
->where('is_deleted', '=', false)
|
|
|
|
->get();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function find($filter = null)
|
|
|
|
{
|
|
|
|
$query = DB::table('vendors')
|
|
|
|
->join('accounts', 'accounts.id', '=', 'vendors.account_id')
|
|
|
|
->join('vendor_contacts', 'vendor_contacts.vendor_id', '=', 'vendors.id')
|
|
|
|
->where('vendors.account_id', '=', \Auth::user()->account_id)
|
|
|
|
->where('vendor_contacts.is_primary', '=', true)
|
|
|
|
->where('vendor_contacts.deleted_at', '=', null)
|
|
|
|
->select(
|
|
|
|
DB::raw('COALESCE(vendors.currency_id, accounts.currency_id) currency_id'),
|
|
|
|
DB::raw('COALESCE(vendors.country_id, accounts.country_id) country_id'),
|
|
|
|
'vendors.public_id',
|
|
|
|
'vendors.name',
|
|
|
|
'vendor_contacts.first_name',
|
|
|
|
'vendor_contacts.last_name',
|
|
|
|
'vendors.created_at',
|
|
|
|
'vendors.work_phone',
|
2016-01-26 21:22:33 +01:00
|
|
|
'vendors.city',
|
2016-01-06 15:23:58 +01:00
|
|
|
'vendor_contacts.email',
|
|
|
|
'vendors.deleted_at',
|
2016-03-16 00:08:00 +01:00
|
|
|
'vendors.is_deleted',
|
|
|
|
'vendors.user_id'
|
2016-01-06 15:23:58 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!\Session::get('show_trash:vendor')) {
|
|
|
|
$query->where('vendors.deleted_at', '=', null);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($filter) {
|
|
|
|
$query->where(function ($query) use ($filter) {
|
|
|
|
$query->where('vendors.name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('vendor_contacts.first_name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('vendor_contacts.last_name', 'like', '%'.$filter.'%')
|
|
|
|
->orWhere('vendor_contacts.email', 'like', '%'.$filter.'%');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2016-01-19 14:01:19 +01:00
|
|
|
|
2016-07-21 14:35:23 +02:00
|
|
|
public function save($data, $vendor = null)
|
2016-01-06 15:23:58 +01:00
|
|
|
{
|
|
|
|
$publicId = isset($data['public_id']) ? $data['public_id'] : false;
|
|
|
|
|
2016-05-02 19:42:13 +02:00
|
|
|
if ($vendor) {
|
|
|
|
// do nothing
|
|
|
|
} elseif (!$publicId || $publicId == '-1') {
|
2016-01-06 15:23:58 +01:00
|
|
|
$vendor = Vendor::createNew();
|
|
|
|
} else {
|
2016-05-04 14:16:49 +02:00
|
|
|
$vendor = Vendor::scope($publicId)->with('vendor_contacts')->firstOrFail();
|
2016-05-02 19:42:13 +02:00
|
|
|
\Log::warning('Entity not set in vendor repo save');
|
2016-01-06 15:23:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$vendor->fill($data);
|
|
|
|
$vendor->save();
|
2016-07-21 14:35:23 +02:00
|
|
|
|
|
|
|
$first = true;
|
|
|
|
$vendorcontacts = isset($data['vendor_contact']) ? [$data['vendor_contact']] : $data['vendor_contacts'];
|
|
|
|
|
|
|
|
foreach ($vendorcontacts as $vendorcontact) {
|
|
|
|
$vendorcontact = $vendor->addVendorContact($vendorcontact, $first);
|
|
|
|
$first = false;
|
|
|
|
}
|
|
|
|
|
2016-01-06 15:23:58 +01:00
|
|
|
return $vendor;
|
|
|
|
}
|
|
|
|
}
|