From 1c4cb6f38c94a63991a9c8792b3a32a5924a1f09 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 26 Jul 2024 14:45:43 +1000 Subject: [PATCH] Fix usage_broadband, since our usage data is a float --- app/Console/Commands/BroadbandTraffic.php | 25 ++++++++++---- app/Jobs/BroadbandTraffic.php | 34 ++++++++++--------- app/Models/Rtm.php | 7 ++++ ...24_07_26_142217_usage_broadband_floats.php | 27 +++++++++++++++ routes/console.php | 4 +-- 5 files changed, 73 insertions(+), 24 deletions(-) create mode 100644 database/migrations/2024_07_26_142217_usage_broadband_floats.php diff --git a/app/Console/Commands/BroadbandTraffic.php b/app/Console/Commands/BroadbandTraffic.php index 89cb6be..079a34d 100644 --- a/app/Console/Commands/BroadbandTraffic.php +++ b/app/Console/Commands/BroadbandTraffic.php @@ -3,6 +3,7 @@ namespace App\Console\Commands; use Illuminate\Console\Command; +use Illuminate\Database\Eloquent\ModelNotFoundException; use App\Jobs\BroadbandTraffic as Job; use App\Models\Supplier; @@ -15,7 +16,7 @@ class BroadbandTraffic extends Command * @var string */ protected $signature = 'broadband:traffic:import'. - ' {--s|supplier= : Supplier Name}'; + ' {supplier? : Supplier Name}'; /** * The console command description. @@ -31,14 +32,26 @@ class BroadbandTraffic extends Command */ public function handle() { - if ($this->option('supplier')) { - $o = Supplier::where('name','ilike',$this->option('supplier'))->singleOrFail(); + if ($this->argument('supplier')) { + try { + $o = Supplier::active() + ->where('name','ilike',$this->argument('supplier')) + ->sole(); - Job::dispatchSync($o->id); - return; + } catch (ModelNotFoundException $e) { + $this->error(sprintf('Supplier [%s] not found',$this->argument('supplier'))); + + return self::FAILURE; + } + + Job::dispatchSync($o->name); + + return self::SUCCESS; } foreach (Supplier::active()->get() as $o) - Job::dispatchSync($o->id); + Job::dispatchSync($o->name); + + return self::SUCCESS; } } \ No newline at end of file diff --git a/app/Jobs/BroadbandTraffic.php b/app/Jobs/BroadbandTraffic.php index b79a953..b1bea77 100644 --- a/app/Jobs/BroadbandTraffic.php +++ b/app/Jobs/BroadbandTraffic.php @@ -5,24 +5,22 @@ namespace App\Jobs; use Carbon\Carbon; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Database\Eloquent\Model; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Arr; -use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; use App\Classes\External\Supplier as ExternalSupplier; use App\Mail\TrafficMismatch; -use App\Models\Supplier; +use App\Models\{Rtm,Supplier}; use App\Models\Service\Broadband as ServiceBroadband; use App\Models\Usage\Broadband as UsageBroadband; /** * Class BroadbandTraffic - * Read and update the traffic for an Broadband supplier + * Read and update the traffic for a Broadband supplier * * @package App\Jobs */ @@ -32,15 +30,11 @@ final class BroadbandTraffic implements ShouldQueue private const LOGKEY = 'JBT'; - protected int $sid; // The supplier we are updating from private const class_prefix = 'App\Classes\External\Supplier\\'; private const traffic = 'broadband'; - public function __construct(int $sid) - { - $this->sid = $sid; - } + public function __construct(private string $supplier) {} /** * Execute the job. @@ -50,12 +44,19 @@ final class BroadbandTraffic implements ShouldQueue */ public function handle() { - $so = Supplier::findOrFail($this->sid); + $so = Supplier::active() + ->where('name','ilike',$this->supplier) + ->sole(); + + // Wholesaler email + $ro = Rtm::where('parent_id',NULL)->sole(); + Log::info(sprintf('%s:Importing Broadband Traffic from [%s]',self::LOGKEY,$so->name)); if ((! $connection=$so->detail->connections->get('broadband')) || (count(array_intersect(array_keys($connection),ExternalSupplier::traffic_connection_keys)) !== 3)) throw new \Exception('No or missing connection details for:'.self::traffic); + // Count of updated records $u = 0; // Load our class for this supplier @@ -95,15 +96,16 @@ final class BroadbandTraffic implements ShouldQueue try { $date = Carbon::createFromFormat('Y-m-d',$row[$o->getColumnKey('Date')]); + // Find the right service dependent on the dates we supplied the service $oo = ServiceBroadband::where('service_username',$row[$o->getColumnKey('Login')]) - ->select(DB::raw('service_broadband.*')) - ->join('services','services.id','=','service_id') + ->select('service_broadband.*') + ->join('services',['service_broadband.service_id'=>'services.id']) ->where('services.start_at','<=',$date) - ->where(function($query) use ($date) { + ->where(fn($query)=> $query->whereNULL('services.stop_at') - ->orWhere('services.stop_at','<=',$date); - }) + ->orWhere('services.stop_at','>=',$date) + ) ->single(); $to = new UsageBroadband; @@ -149,7 +151,7 @@ final class BroadbandTraffic implements ShouldQueue if ($u) { if ($so->trafficMismatch($date)->count()) - Mail::to('deon@graytech.net.au') // @todo To change + Mail::to($ro->owner->email) ->send(new TrafficMismatch($so,$date)); } } diff --git a/app/Models/Rtm.php b/app/Models/Rtm.php index 56739c9..9f9d2c6 100644 --- a/app/Models/Rtm.php +++ b/app/Models/Rtm.php @@ -25,6 +25,13 @@ class Rtm extends Model return $this->hasMany(self::class,'parent_id'); } + public function owner() + { + return $this->hasOneThrough(User::class,Account::class,'id','id','account_id','user_id'); + } + + /* METHODS */ + /** * Return all the children RTM records that this record is parent of * diff --git a/database/migrations/2024_07_26_142217_usage_broadband_floats.php b/database/migrations/2024_07_26_142217_usage_broadband_floats.php new file mode 100644 index 0000000..5f94ce5 --- /dev/null +++ b/database/migrations/2024_07_26_142217_usage_broadband_floats.php @@ -0,0 +1,27 @@ +timezone('Australia/Melbourne') - ->dailyAt('10:00'); + ->dailyAt('10:00'); \ No newline at end of file