diff --git a/app/Jobs/BroadbandTraffic.php b/app/Jobs/BroadbandTraffic.php index 3325572..930224f 100644 --- a/app/Jobs/BroadbandTraffic.php +++ b/app/Jobs/BroadbandTraffic.php @@ -118,7 +118,7 @@ final class BroadbandTraffic implements ShouldQueue // If we have no records if (! $oo) { - Log::error(sprintf('%s:Too many services return for [%s]',self::LOGKEY,$row[$o->getColumnKey('Login')]),['date'=>$date,'count'=>$oo->count()]); + Log::error(sprintf('%s:None or too many services return for [%s]',self::LOGKEY,$row[$o->getColumnKey('Login')]),['date'=>$date]); $to->service = $row[$o->getColumnKey('Login')]; $to->site_id = 1; // @todo This needs to be worked out a better way diff --git a/app/Mail/CancelRequest.php b/app/Mail/CancelRequest.php index 33b8417..f2619e3 100644 --- a/app/Mail/CancelRequest.php +++ b/app/Mail/CancelRequest.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\Service; @@ -35,6 +36,8 @@ class CancelRequest extends Mailable */ public function build() { + Config::set('site',$this->service->site); + switch (get_class($this->service->type)) { case 'App\Models\Service\Broadband': $subject = sprintf('Cancel BROADBAND: %s',$this->service->type->service_address); diff --git a/app/Mail/ChangeRequest.php b/app/Mail/ChangeRequest.php index d7cb383..afbee33 100644 --- a/app/Mail/ChangeRequest.php +++ b/app/Mail/ChangeRequest.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\Service; @@ -35,6 +36,8 @@ class ChangeRequest extends Mailable */ public function build() { + Config::set('site',$this->service->site); + switch (get_class($this->service->type)) { case 'App\Models\Service\Broadband': $subject = sprintf('Change BROADBAND: %s',$this->service->type->service_address); diff --git a/app/Mail/InvoiceEmail.php b/app/Mail/InvoiceEmail.php index 1b29f4e..ce89fd9 100644 --- a/app/Mail/InvoiceEmail.php +++ b/app/Mail/InvoiceEmail.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\Invoice; @@ -32,6 +33,8 @@ class InvoiceEmail extends Mailable */ public function build() { + Config::set('site',$this->invoice->site); + return $this ->markdown('email.user.invoice') ->subject(sprintf( 'Invoice: %s - Total: $%s - Due: %s', diff --git a/app/Mail/OrderRequest.php b/app/Mail/OrderRequest.php index f4a1e0a..4e1c57b 100644 --- a/app/Mail/OrderRequest.php +++ b/app/Mail/OrderRequest.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\Service; @@ -35,6 +36,8 @@ class OrderRequest extends Mailable */ public function build() { + Config::set('site',$this->service->site); + switch (get_class($this->service->type)) { case 'App\Models\Service\Broadband': $subject = sprintf('Order BROADBAND: %s',$this->service->type->service_address); diff --git a/app/Mail/OrderRequestApprove.php b/app/Mail/OrderRequestApprove.php index a373f2a..48208b5 100644 --- a/app/Mail/OrderRequestApprove.php +++ b/app/Mail/OrderRequestApprove.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Support\Facades\Config; use App\Models\Service; @@ -35,6 +36,8 @@ class OrderRequestApprove extends Mailable */ public function build() { + Config::set('site',$this->service->site); + // @todo This is not consistent with Cancel/Change Request switch ($this->service->category) { case 'BROADBAND': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address); diff --git a/app/Mail/OrderRequestReject.php b/app/Mail/OrderRequestReject.php index 6df72d3..c49042e 100644 --- a/app/Mail/OrderRequestReject.php +++ b/app/Mail/OrderRequestReject.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Support\Facades\Config; use App\Models\Service; @@ -34,6 +35,8 @@ class OrderRequestReject extends Mailable */ public function build() { + Config::set('site',$this->service->site); + return $this ->markdown('email.admin.order.reject') ->subject(sprintf('Your order: #%s was rejected',$this->service->id)) diff --git a/app/Mail/SocialLink.php b/app/Mail/SocialLink.php index 9fb50ab..d8ef032 100644 --- a/app/Mail/SocialLink.php +++ b/app/Mail/SocialLink.php @@ -5,6 +5,7 @@ namespace App\Mail; use Illuminate\Bus\Queueable; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\{Site,User,UserOauth}; @@ -35,6 +36,8 @@ class SocialLink extends Mailable */ public function build() { + Config::set('site',$this->site); + return $this ->markdown('email.system.social_link') ->subject('Link your Account') diff --git a/app/Mail/TestEmail.php b/app/Mail/TestEmail.php index a52602f..b2cb1f6 100644 --- a/app/Mail/TestEmail.php +++ b/app/Mail/TestEmail.php @@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\User; @@ -32,6 +33,8 @@ class TestEmail extends Mailable */ public function build() { + Config::set('site',$this->user->site); + return $this ->markdown('email.system.test_email') ->subject('Just a test...') @@ -40,4 +43,4 @@ class TestEmail extends Mailable 'user'=>$this->user, ]); } -} +} \ No newline at end of file diff --git a/app/Mail/TrafficMismatch.php b/app/Mail/TrafficMismatch.php index cea1d17..7e3d85e 100644 --- a/app/Mail/TrafficMismatch.php +++ b/app/Mail/TrafficMismatch.php @@ -7,6 +7,7 @@ use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Mail\Mailable; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Config; use App\Models\{Supplier,Site}; @@ -35,13 +36,15 @@ class TrafficMismatch extends Mailable */ public function build() { + Config::set('site',$x=Site::find(1)); // @todo To auto determine; + return $this ->markdown('email.system.broadband_traffic_mismatch') ->subject('Traffic Mismatch for '.$this->date) ->with([ - 'site'=>Site::find(1), // @todo To auto determine + 'site'=>$x, 'date'=>$this->date, 'aso'=>$this->aso, ]); } -} +} \ No newline at end of file diff --git a/app/Models/Scopes/SiteScope.php b/app/Models/Scopes/SiteScope.php index 549a88b..475dfa7 100644 --- a/app/Models/Scopes/SiteScope.php +++ b/app/Models/Scopes/SiteScope.php @@ -17,6 +17,7 @@ class SiteScope implements Scope */ public function apply(Builder $builder, Model $model) { + // @todo Need to only do this, if the original query doesnt already have a where condition with a site_id $builder->where($model->getTable().'.site_id',config('site')->site_id); } } \ No newline at end of file diff --git a/app/Models/Service.php b/app/Models/Service.php index fe0ae9b..4d987fe 100644 --- a/app/Models/Service.php +++ b/app/Models/Service.php @@ -5,7 +5,6 @@ namespace App\Models; use Carbon\Carbon; use Exception; use Illuminate\Database\Eloquent\Casts\AsCollection; -use Illuminate\Database\Eloquent\Collection as DatabaseCollection; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; @@ -18,6 +17,7 @@ use Illuminate\Support\Facades\Auth; use Symfony\Component\HttpKernel\Exception\HttpException; use Leenooks\Carbon as LeenooksCarbon; +use App\Models\Scopes\SiteScope; use App\Interfaces\IDs; use App\Traits\ScopeServiceUserAuthorised; use App\Traits\SiteID; @@ -325,7 +325,11 @@ class Service extends Model implements IDs */ public function account() { - return $this->belongsTo(Account::class); + return $this->belongsTo(Account::class) + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }); } /** @@ -346,6 +350,10 @@ class Service extends Model implements IDs public function charges() { return $this->hasMany(Charge::class) + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }) ->where('active','=',TRUE) ->orderBy('created_at'); } @@ -354,6 +362,10 @@ class Service extends Model implements IDs public function invoice_items($active=TRUE) { $query = $this->hasMany(InvoiceItem::class) + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }) ->where('item_type','=',0) ->orderBy('start_at'); @@ -370,6 +382,10 @@ class Service extends Model implements IDs public function invoices($active=TRUE) { $query = $this->hasManyThrough(Invoice::class,InvoiceItem::class,NULL,'id',NULL,'invoice_id') + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }) ->distinct('id') ->where('invoices.site_id','=',$this->site_id) ->where('invoice_items.site_id','=',$this->site_id) @@ -390,7 +406,11 @@ class Service extends Model implements IDs */ public function orderedby() { - return $this->belongsTo(Account::class); + return $this->belongsTo(Account::class) + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }); } /** @@ -400,7 +420,11 @@ class Service extends Model implements IDs */ public function product() { - return $this->belongsTo(Product::class); + return $this->belongsTo(Product::class) + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }); } /** @@ -410,7 +434,11 @@ class Service extends Model implements IDs */ public function type() { - return $this->morphTo(null,'model','id','service_id'); + return $this->morphTo(null,'model','id','service_id') + ->when($this->site_id,function($q) { + return $q->where('site_id', $this->site_id) + ->withoutGlobalScope(SiteScope::class); + }); } /* SCOPES */ diff --git a/resources/views/email/admin/service/cancel.blade.php b/resources/views/email/admin/service/cancel.blade.php index 8447691..7c15174 100644 --- a/resources/views/email/admin/service/cancel.blade.php +++ b/resources/views/email/admin/service/cancel.blade.php @@ -4,15 +4,15 @@ Please cancel the following... @component('mail::table') | Service | Details | | :---------- | :---------------- | -| Logged User | {{ Auth::user()->id }} | +| Logged User | {{ Auth::user()->id ?? 'System' }} | | Account | {{ $service->account->name }} | | Service ID | {{ $service->sid }} | | Product | {{ $service->product->name }} | @switch($service->category) -@case('Broadband') +@case('broadband') | Address | {{ $service->type->service_address }} | @break; -@case('Phone') +@case('phone') | Number | {{ $service->type->service_number }} | | Supplier Details | {{ $service->order_info->join(':') }} | @break;