Fixes to emailing cancel requests. Changes to email jobs with no site_id

This commit is contained in:
Deon George 2022-09-29 12:16:08 +10:00
parent 1667b8c1df
commit 2a19f14adb
13 changed files with 68 additions and 12 deletions

View File

@ -118,7 +118,7 @@ final class BroadbandTraffic implements ShouldQueue
// If we have no records // If we have no records
if (! $oo) { 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->service = $row[$o->getColumnKey('Login')];
$to->site_id = 1; // @todo This needs to be worked out a better way $to->site_id = 1; // @todo This needs to be worked out a better way

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\Service; use App\Models\Service;
@ -35,6 +36,8 @@ class CancelRequest extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->service->site);
switch (get_class($this->service->type)) { switch (get_class($this->service->type)) {
case 'App\Models\Service\Broadband': case 'App\Models\Service\Broadband':
$subject = sprintf('Cancel BROADBAND: %s',$this->service->type->service_address); $subject = sprintf('Cancel BROADBAND: %s',$this->service->type->service_address);

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\Service; use App\Models\Service;
@ -35,6 +36,8 @@ class ChangeRequest extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->service->site);
switch (get_class($this->service->type)) { switch (get_class($this->service->type)) {
case 'App\Models\Service\Broadband': case 'App\Models\Service\Broadband':
$subject = sprintf('Change BROADBAND: %s',$this->service->type->service_address); $subject = sprintf('Change BROADBAND: %s',$this->service->type->service_address);

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\Invoice; use App\Models\Invoice;
@ -32,6 +33,8 @@ class InvoiceEmail extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->invoice->site);
return $this return $this
->markdown('email.user.invoice') ->markdown('email.user.invoice')
->subject(sprintf( 'Invoice: %s - Total: $%s - Due: %s', ->subject(sprintf( 'Invoice: %s - Total: $%s - Due: %s',

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\Service; use App\Models\Service;
@ -35,6 +36,8 @@ class OrderRequest extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->service->site);
switch (get_class($this->service->type)) { switch (get_class($this->service->type)) {
case 'App\Models\Service\Broadband': case 'App\Models\Service\Broadband':
$subject = sprintf('Order BROADBAND: %s',$this->service->type->service_address); $subject = sprintf('Order BROADBAND: %s',$this->service->type->service_address);

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Config;
use App\Models\Service; use App\Models\Service;
@ -35,6 +36,8 @@ class OrderRequestApprove extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->service->site);
// @todo This is not consistent with Cancel/Change Request // @todo This is not consistent with Cancel/Change Request
switch ($this->service->category) { switch ($this->service->category) {
case 'BROADBAND': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address); case 'BROADBAND': $subject = sprintf('%s: %s',$this->service->category,$this->service->type->service_address);

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Config;
use App\Models\Service; use App\Models\Service;
@ -34,6 +35,8 @@ class OrderRequestReject extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->service->site);
return $this return $this
->markdown('email.admin.order.reject') ->markdown('email.admin.order.reject')
->subject(sprintf('Your order: #%s was rejected',$this->service->id)) ->subject(sprintf('Your order: #%s was rejected',$this->service->id))

View File

@ -5,6 +5,7 @@ namespace App\Mail;
use Illuminate\Bus\Queueable; use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\{Site,User,UserOauth}; use App\Models\{Site,User,UserOauth};
@ -35,6 +36,8 @@ class SocialLink extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->site);
return $this return $this
->markdown('email.system.social_link') ->markdown('email.system.social_link')
->subject('Link your Account') ->subject('Link your Account')

View File

@ -6,6 +6,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\User; use App\Models\User;
@ -32,6 +33,8 @@ class TestEmail extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$this->user->site);
return $this return $this
->markdown('email.system.test_email') ->markdown('email.system.test_email')
->subject('Just a test...') ->subject('Just a test...')

View File

@ -7,6 +7,7 @@ use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable; use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Config;
use App\Models\{Supplier,Site}; use App\Models\{Supplier,Site};
@ -35,11 +36,13 @@ class TrafficMismatch extends Mailable
*/ */
public function build() public function build()
{ {
Config::set('site',$x=Site::find(1)); // @todo To auto determine;
return $this return $this
->markdown('email.system.broadband_traffic_mismatch') ->markdown('email.system.broadband_traffic_mismatch')
->subject('Traffic Mismatch for '.$this->date) ->subject('Traffic Mismatch for '.$this->date)
->with([ ->with([
'site'=>Site::find(1), // @todo To auto determine 'site'=>$x,
'date'=>$this->date, 'date'=>$this->date,
'aso'=>$this->aso, 'aso'=>$this->aso,
]); ]);

View File

@ -17,6 +17,7 @@ class SiteScope implements Scope
*/ */
public function apply(Builder $builder, Model $model) 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); $builder->where($model->getTable().'.site_id',config('site')->site_id);
} }
} }

View File

@ -5,7 +5,6 @@ namespace App\Models;
use Carbon\Carbon; use Carbon\Carbon;
use Exception; use Exception;
use Illuminate\Database\Eloquent\Casts\AsCollection; use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Collection as DatabaseCollection;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo; use Illuminate\Database\Eloquent\Relations\BelongsTo;
@ -18,6 +17,7 @@ use Illuminate\Support\Facades\Auth;
use Symfony\Component\HttpKernel\Exception\HttpException; use Symfony\Component\HttpKernel\Exception\HttpException;
use Leenooks\Carbon as LeenooksCarbon; use Leenooks\Carbon as LeenooksCarbon;
use App\Models\Scopes\SiteScope;
use App\Interfaces\IDs; use App\Interfaces\IDs;
use App\Traits\ScopeServiceUserAuthorised; use App\Traits\ScopeServiceUserAuthorised;
use App\Traits\SiteID; use App\Traits\SiteID;
@ -325,7 +325,11 @@ class Service extends Model implements IDs
*/ */
public function account() 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() public function charges()
{ {
return $this->hasMany(Charge::class) 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) ->where('active','=',TRUE)
->orderBy('created_at'); ->orderBy('created_at');
} }
@ -354,6 +362,10 @@ class Service extends Model implements IDs
public function invoice_items($active=TRUE) public function invoice_items($active=TRUE)
{ {
$query = $this->hasMany(InvoiceItem::class) $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) ->where('item_type','=',0)
->orderBy('start_at'); ->orderBy('start_at');
@ -370,6 +382,10 @@ class Service extends Model implements IDs
public function invoices($active=TRUE) public function invoices($active=TRUE)
{ {
$query = $this->hasManyThrough(Invoice::class,InvoiceItem::class,NULL,'id',NULL,'invoice_id') $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') ->distinct('id')
->where('invoices.site_id','=',$this->site_id) ->where('invoices.site_id','=',$this->site_id)
->where('invoice_items.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() 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() 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() 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 */ /* SCOPES */

View File

@ -4,15 +4,15 @@ Please cancel the following...
@component('mail::table') @component('mail::table')
| Service | Details | | Service | Details |
| :---------- | :---------------- | | :---------- | :---------------- |
| Logged User | {{ Auth::user()->id }} | | Logged User | {{ Auth::user()->id ?? 'System' }} |
| Account | {{ $service->account->name }} | | Account | {{ $service->account->name }} |
| Service ID | {{ $service->sid }} | | Service ID | {{ $service->sid }} |
| Product | {{ $service->product->name }} | | Product | {{ $service->product->name }} |
@switch($service->category) @switch($service->category)
@case('Broadband') @case('broadband')
| Address | {{ $service->type->service_address }} | | Address | {{ $service->type->service_address }} |
@break; @break;
@case('Phone') @case('phone')
| Number | {{ $service->type->service_number }} | | Number | {{ $service->type->service_number }} |
| Supplier Details | {{ $service->order_info->join(':') }} | | Supplier Details | {{ $service->order_info->join(':') }} |
@break; @break;