Some invoice layout fixes, start on traffic collection, update console command service:list
This commit is contained in:
115
app/Classes/External/Supplier.php
vendored
Normal file
115
app/Classes/External/Supplier.php
vendored
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace App\Classes\External;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
abstract class Supplier
|
||||
{
|
||||
protected $o = NULL;
|
||||
protected $_columns = [];
|
||||
|
||||
public function __construct(Model $o)
|
||||
{
|
||||
$this->o = $o;
|
||||
$this->_columns = collect();
|
||||
}
|
||||
|
||||
/**
|
||||
* Connect and pull down traffic data
|
||||
*
|
||||
* @param array $args
|
||||
* @return mixed
|
||||
*/
|
||||
protected function connect(array $args=[])
|
||||
{
|
||||
if ($x=$this->mustPause()) {
|
||||
Log::error('API Throttle, waiting .',['m'=>__METHOD__]);
|
||||
sleep($x);
|
||||
}
|
||||
|
||||
$result = Cache::remember('Supplier:'.$this->o->id,86400,function() {
|
||||
$client = $this->getClient();
|
||||
|
||||
$result = $client->request('POST',null,[
|
||||
'query'=>[
|
||||
$this->login_user_field=>$this->o->stats_username,
|
||||
$this->login_pass_field=>$this->o->stats_password,
|
||||
$this->date_field=>$this->o->stats_lastupdate,
|
||||
],
|
||||
/*
|
||||
'headers' => [
|
||||
'Accept'=>'application/json',
|
||||
],
|
||||
*/
|
||||
]);
|
||||
|
||||
$api_remain = Arr::get($result->getHeader('X-RateLimit-Remaining'),0);
|
||||
$api_reset = Arr::get($result->getHeader('X-RateLimit-Reset'),0);
|
||||
|
||||
if ($api_remain == 0) {
|
||||
Log::error('API Throttle.',['m'=>__METHOD__]);
|
||||
Cache::put('api_throttle',$api_reset,now()->addSeconds($api_reset));
|
||||
}
|
||||
|
||||
return $result->getBody()->getContents();
|
||||
|
||||
});
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the API HTTP client
|
||||
* @return Client
|
||||
*/
|
||||
protected function getClient(): Client
|
||||
{
|
||||
return new Client(['base_uri'=>$this->o->stats_url]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the expected columns from a supplier traffic import
|
||||
*
|
||||
* @param string $line
|
||||
* @param Collection $expect
|
||||
* @return Collection
|
||||
*/
|
||||
protected function getColumns(string $line,Collection $expect): Collection
|
||||
{
|
||||
$fields = collect(explode(',',$line))->filter();
|
||||
$this->_columns = $expect;
|
||||
|
||||
if ($this->_columns->diff($fields)->count()) {
|
||||
abort('500','Missing columns in data: '.join('|',$this->_columns->diff($fields)->toArray()));
|
||||
}
|
||||
|
||||
return $fields->intersect($this->_columns);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the key ID for a column
|
||||
*
|
||||
* @param string $key
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getColumnKey(string $key)
|
||||
{
|
||||
return $this->_columns->search($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* If the supplier has API throttling...
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
protected function mustPause()
|
||||
{
|
||||
return Cache::get('api_throttle');
|
||||
}
|
||||
}
|
47
app/Console/Commands/BroadbandTraffic.php
Normal file
47
app/Console/Commands/BroadbandTraffic.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
use App\Jobs\BroadbandTraffic as Job;
|
||||
use App\Models\AdslSupplier;
|
||||
|
||||
class BroadbandTraffic extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'broadband:traffic:import';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Input Broadband Traffic from Suppliers';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function handle()
|
||||
{
|
||||
foreach (AdslSupplier::active()->get() as $o) {
|
||||
Job::dispatchNow($o);
|
||||
}
|
||||
}
|
||||
}
|
@@ -15,7 +15,10 @@ class ServiceList extends Command
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'service:list';
|
||||
protected $signature = 'service:list '.
|
||||
'{--a|active : Active Only}'.
|
||||
'{--category= : Category}'.
|
||||
'{--f|fix : Fix start_date}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
@@ -45,15 +48,46 @@ class ServiceList extends Command
|
||||
Log::debug('- SQL',['sql'=>$query->sql,'binding'=>$query->bindings]);
|
||||
});
|
||||
|
||||
foreach (Service::active()->get() as $o) {
|
||||
$this->info(sprintf('|%10s|%-6s|%-20s|%-50s|%8s|%14s|%10s|',
|
||||
$this->warn(sprintf('|%10s|%-6s|%-20s|%-50s|%8s|%14s|%10s|%10s|%10s|%10s|%10s|',
|
||||
'ID',
|
||||
'CAT',
|
||||
'Product',
|
||||
'Name',
|
||||
'active',
|
||||
'status',
|
||||
'invoice next',
|
||||
'start date',
|
||||
'stop date',
|
||||
'connect date',
|
||||
'first invoice'
|
||||
));
|
||||
|
||||
foreach (Service::all() as $o) {
|
||||
if ($this->option('active') AND ! $o->isActive())
|
||||
continue;
|
||||
|
||||
if ($this->option('category') AND $o->product->category !== $this->option('category'))
|
||||
continue;
|
||||
|
||||
$c = $o->invoice_items->filter(function($item) {return $item->item_type === 0; })->sortby('date_start')->first();
|
||||
|
||||
if ($this->option('fix') AND ! $o->date_start AND $c AND $c->date_start AND $o->type AND $o->type->service_connect_date AND $c->date_start->format('Y-m-d') == $o->type->service_connect_date->format('Y-m-d')) {
|
||||
$o->date_start = $o->type->service_connect_date;
|
||||
$o->save();
|
||||
}
|
||||
|
||||
$this->info(sprintf('|%10s|%-6s|%-20s|%-50s|%8s|%14s|%10s|%10s|%10s|%10s|%10s|',
|
||||
$o->sid,
|
||||
$o->product->category,
|
||||
$o->product_name,
|
||||
$o->name_short,
|
||||
$o->active ? 'active' : 'inactive',
|
||||
$o->status,
|
||||
$o->invoice_next ? $o->invoice_next : NULL,
|
||||
$o->invoice_next ? $o->invoice_next->format('Y-m-d') : NULL,
|
||||
$o->date_start ? $o->date_start->format('Y-m-d') : NULL,
|
||||
$o->date_end ? $o->date_end->format('Y-m-d') : NULL,
|
||||
($o->type AND $o->type->service_connect_date) ? $o->type->service_connect_date->format('Y-m-d') : NULL,
|
||||
$c ? $c->date_start->format('Y-m-d') : NULL,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
@@ -34,7 +34,7 @@ class SearchController extends Controller
|
||||
->orderBy('firstname')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['name'=>sprintf('US:%s %s',$o->aid,$o->name),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
|
||||
$result->push(['name'=>sprintf('%s %s',$o->aid,$o->name),'value'=>'/u/home/'.$o->id,'category'=>'Users']);
|
||||
}
|
||||
|
||||
# Look for Account
|
||||
@@ -43,7 +43,7 @@ class SearchController extends Controller
|
||||
->orderBy('company')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['name'=>sprintf('AC:%s %s',$o->aid,$o->company),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
|
||||
$result->push(['name'=>sprintf('%s %s',$o->aid,$o->company),'value'=>'/u/home/'.$o->user_id,'category'=>'Accounts']);
|
||||
}
|
||||
|
||||
# Look for a Service
|
||||
@@ -52,7 +52,7 @@ class SearchController extends Controller
|
||||
->orderBy('id')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['name'=>sprintf('SV:%s (%s)',$o->name,$o->sid),'value'=>'/u/service/'.$o->id,'category'=>'Services']);
|
||||
$result->push(['name'=>sprintf('%s (%s)',$o->name,$o->sid),'value'=>'/u/service/'.$o->id,'category'=>'Services']);
|
||||
}
|
||||
|
||||
# Look for an Invoice
|
||||
@@ -61,7 +61,7 @@ class SearchController extends Controller
|
||||
->orderBy('id')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['name'=>sprintf('IN:%s #%s',$o->account->name,$o->invoice_id),'value'=>'/u/invoice/'.$o->id,'category'=>'Invoices']);
|
||||
$result->push(['name'=>sprintf('%s #%s',$o->account->name,$o->invoice_id),'value'=>'/u/invoice/'.$o->id,'category'=>'Invoices']);
|
||||
}
|
||||
|
||||
# Look for an ADSL/NBN Service
|
||||
@@ -70,7 +70,7 @@ class SearchController extends Controller
|
||||
->orderBy('service_number')
|
||||
->limit(10)->get() as $o)
|
||||
{
|
||||
$result->push(['name'=>sprintf('SV:%s (%s)',$o->name,$o->service->sid),'value'=>'/u/service/'.$o->id,'category'=>'Broadband']);
|
||||
$result->push(['name'=>sprintf('%s (%s)',$o->name,$o->service->sid),'value'=>'/u/service/'.$o->id,'category'=>'Broadband']);
|
||||
}
|
||||
|
||||
return $result;
|
||||
|
Reference in New Issue
Block a user