diff --git a/app/Classes/External/Supplier.php b/app/Classes/External/Supplier.php new file mode 100644 index 0000000..bceca8b --- /dev/null +++ b/app/Classes/External/Supplier.php @@ -0,0 +1,115 @@ +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'); + } +} \ No newline at end of file diff --git a/app/Console/Commands/BroadbandTraffic.php b/app/Console/Commands/BroadbandTraffic.php new file mode 100644 index 0000000..998e81e --- /dev/null +++ b/app/Console/Commands/BroadbandTraffic.php @@ -0,0 +1,47 @@ +get() as $o) { + Job::dispatchNow($o); + } + } +} \ No newline at end of file diff --git a/app/Console/Commands/ServiceList.php b/app/Console/Commands/ServiceList.php index c61d2c1..7a16f9c 100644 --- a/app/Console/Commands/ServiceList.php +++ b/app/Console/Commands/ServiceList.php @@ -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, )); } } diff --git a/app/Http/Controllers/SearchController.php b/app/Http/Controllers/SearchController.php index 5740f3c..766cbb7 100644 --- a/app/Http/Controllers/SearchController.php +++ b/app/Http/Controllers/SearchController.php @@ -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; diff --git a/resources/views/theme/backend/adminlte/u/invoice.blade.php b/resources/views/theme/backend/adminlte/u/invoice.blade.php index 9902c9b..e328c94 100644 --- a/resources/views/theme/backend/adminlte/u/invoice.blade.php +++ b/resources/views/theme/backend/adminlte/u/invoice.blade.php @@ -19,7 +19,7 @@

{{ $so->site_name }} - Date: {{ $o->invoice_date}} + Date: {{ $o->invoice_date}}

@@ -50,10 +50,8 @@ @endif - {{-- col-sm-offset-2 not working here --}} -
-
+
@@ -131,12 +129,12 @@ --}}

- {{ $o->invoice_text }} + {!! $o->invoice_text !!}

-
+
Account:{{ $o->account->account_id }}
@@ -184,8 +182,8 @@
Print @if($o->id) - - + + Download PDF @endif
Subtotal: