Site ID is now optional for some console commands

This commit is contained in:
Deon George 2025-05-16 11:05:49 +10:00
parent 3fc676fa26
commit 48d7968d0c
2 changed files with 46 additions and 30 deletions

View File

@ -10,32 +10,39 @@ use App\Models\{Site,Supplier,User};
class SupplierAccountSync extends Command class SupplierAccountSync extends Command
{ {
/** /**
* The name and signature of the console command. * The name and signature of the console command.
* *
* @var string * @var string
*/ */
protected $signature = 'supplier:account:sync' protected $signature = 'supplier:account:sync'
.' {siteid : Site ID}'
.' {supplier : Supplier Name}' .' {supplier : Supplier Name}'
.' {--f|forceprod : Force Prod API on dev environment}'; .' {--f|forceprod : Force Prod API on dev environment}'
.' {--s|site : Site ID}';
/** /**
* The console command description. * The console command description.
* *
* @var string * @var string
*/ */
protected $description = 'Sync accounts with a supplier'; protected $description = 'Sync accounts with a supplier';
/** /**
* Execute the console command. * Execute the console command.
* *
* @return int * @return int
*/ * @note Suppliers are now associated with accounts, so this needs to be updated
public function handle() */
{ public function handle()
Config::set('site',Site::findOrFail($this->argument('siteid'))); {
$so = Supplier::where('name',$this->argument('supplier'))->singleOrFail(); Config::set(
'site',
$this->option('site')
? Site::findOrFail($this->option('site'))
: Site::where('url',config('app.url'))->sole()
);
$so = Supplier::where('name','ilike',strtolower($this->argument('supplier')))->sole();
foreach ($so->API($this->option('forceprod'))->getCustomers(['fetchall'=>true]) as $customer) { foreach ($so->API($this->option('forceprod'))->getCustomers(['fetchall'=>true]) as $customer) {
// Check if we have this customer already (by ID) // Check if we have this customer already (by ID)
@ -43,7 +50,6 @@ class SupplierAccountSync extends Command
$this->info(sprintf('User already linked (%s:%s)',$customer->id,$customer->email)); $this->info(sprintf('User already linked (%s:%s)',$customer->id,$customer->email));
} elseif ($x=User::where('email',$customer->email)->single()) { } elseif ($x=User::where('email',$customer->email)->single()) {
//dump($x->suppliers->first());
if ($x->suppliers->count()) { if ($x->suppliers->count()) {
$this->alert(sprintf('User [%d:%s] already linked to this supplier with ID (%s)',$customer->id,$customer->email,$x->suppliers->first()->pivot->id)); $this->alert(sprintf('User [%d:%s] already linked to this supplier with ID (%s)',$customer->id,$customer->email,$x->suppliers->first()->pivot->id));
@ -63,5 +69,7 @@ class SupplierAccountSync extends Command
$this->error(sprintf('User doesnt exist with email (%s:%s)',$customer->id,$customer->email)); $this->error(sprintf('User doesnt exist with email (%s:%s)',$customer->id,$customer->email));
} }
} }
}
return self::SUCCESS;
}
} }

View File

@ -3,9 +3,10 @@
namespace App\Console\Commands; namespace App\Console\Commands;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use App\Models\{Site,Supplier};
use App\Jobs\SupplierDomainSync as Job; use App\Jobs\SupplierDomainSync as Job;
use App\Models\{Site,Supplier};
class SupplierDomainSync extends Command class SupplierDomainSync extends Command
{ {
@ -15,9 +16,9 @@ class SupplierDomainSync extends Command
* @var string * @var string
*/ */
protected $signature = 'supplier:domain:sync' protected $signature = 'supplier:domain:sync'
.' {siteid : Site ID}' .' {supplier : Supplier Name}'
.' {supplier : Supplier Name}' .' {--f|forceprod : Force Prod API on dev environment}'
.' {--f|forceprod : Force Prod API on dev environment}'; .' {--s|site : Site ID}';
/** /**
* The console command description. * The console command description.
@ -33,8 +34,15 @@ class SupplierDomainSync extends Command
*/ */
public function handle() public function handle()
{ {
Config::set(
'site',
($x=$this->option('site')
? Site::findOrFail($this->option('site'))
: Site::where('url',config('app.url'))->sole())
);
$so = Supplier::where('name',$this->argument('supplier'))->singleOrFail(); $so = Supplier::where('name',$this->argument('supplier'))->singleOrFail();
Job::dispatchSync(Site::findOrFail($this->argument('siteid')),$so,$this->option('forceprod')); return Job::dispatchSync($x,$so,$this->option('forceprod'));
} }
} }