Skip to content

Instantly share code, notes, and snippets.

@jongravois
Created October 26, 2020 16:25
Show Gist options
  • Save jongravois/cfb3dd9d3164e88371d6d833e33d8b1e to your computer and use it in GitHub Desktop.
Save jongravois/cfb3dd9d3164e88371d6d833e33d8b1e to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Livewire\Modeler;
use App\Http\Livewire\DataTable\WithSorting;
use App\Models\AcModel;
use App\Models\Aircraft;
use App\Models\CorporateDelegate;
use App\Models\PartsModeled;
use App\Models\User;
use App\Models\UserPref;
use App\Notifications\ModelSentToIC;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
use Livewire\WithPagination;
class ModelerHome extends Component
{
use WithPagination, WithSorting;
public $search = '';
protected $queryString = ['status'];
public AcModel $single;
public $showEditModal = false;
public $showUploadModal = false;
public $status = 'active';
public $perPage = 10;
public $cntAll;
public $cntActive;
public $cntArchived;
public $cntLocked;
public function rules()
{
return [
'single.aircraft_id' => 'sometimes',
'single.title' => 'sometimes',
'single.aircraft' => 'sometimes',
'single.serial_no' => 'sometimes',
'single.line_no' => 'sometimes',
'single.is_full_airframe' => 'sometimes',
'single.description' => 'sometimes',
'single.apu' => 'sometimes',
'single.engines' => 'sometimes',
'single.engine_count' => 'sometimes',
'single.owner' => 'sometimes',
'single.operator' => 'sometimes',
'single.model_date' => 'sometimes',
'single.first_run' => 'sometimes'
];
} // end function
protected $customAttributes = [];
public $columns;
protected $listeners = [
'refreshTable' => 'getRowsProperty'
];
public function mount()
{
$prefs = UserPref::whereUserId(auth()->id())
->whereResource('modeler')
->first();
if(!$prefs) {
$prefs = UserPref::create([
'resource' => 'modeler',
'user_id' => auth()->id(),
'properties' => $this->getColumns()
]);
} // end if
$this->columns = $prefs->properties;
$this->single = $this->makeBlankModel();
} // end function
public function render()
{
$counts = AcModel::toBase()
->selectRaw("count(case when status = 'active' then 1 end) as cntActive")
->selectRaw("count(case when status = 'archived' then 1 end) as cntArchived")
->selectRaw("count(case when status = 'locked' then 1 end) as cntLocked")
->first();
$this->cntAll = DB::table('ac_models')->count();
$this->cntActive = $counts->cntActive;
$this->cntArchived = $counts->cntArchived;
$this->cntLocked = $counts->cntLocked;
return view('livewire.modeler.modeler-home', [
'models' => $this->rows
]);
} //end function
public function archive(AcModel $model)
{
$model->status = 'archived';
$model->save();
} // end function
public function changeStatus($status)
{
$this->status = $status;
} // end function
public function delete($id)
{
$model = AcModel::with('parts')->find($id);
$model->components()->delete();
$model->delete();
$this->dispatchBrowserEvent('deleted');
} // end function
public function edit($id)
{
$this->redirect("/modeler/creator/{$id}");
} // end function
public function export($id)
{
} // end function
public function getColumns()
{
return [
'title' => [
'label' => 'Tail/Title',
'field' => 'title',
'visible' => true
],
'aircraft' => [
'label' => 'Aircraft',
'field' => 'aircraft',
'visible' => true
],
'is_full_airframe' => [
'label' => 'Full',
'field' => 'is_full_airframe',
'visible' => true
],
'line_no' => [
'label' => 'Line',
'field' => 'line_no',
'visible' => true
],
'serial_no' => [
'label' => 'MSN',
'field' => 'serial_no',
'visible' => true
],
'apu' => [
'label' => 'APU',
'field' => 'apu',
'visible' => true
],
'engines' => [
'label' => 'Engines',
'field' => 'engines',
'visible' => true
],
'total_count' => [
'label' => 'C#',
'field' => 'total_count',
'visible' => true
],
'total_value' => [
'label' => 'Total Value',
'field' => 'total_value',
'visible' => true
],
'cat1_sum' => [
'label' => 'Cat1',
'field' => 'cat1_sum',
'visible' => true
],
'cat2_sum' => [
'label' => 'Cat2',
'field' => 'cat2_sum',
'visible' => true
],
'cat3_sum' => [
'label' => 'Cat3',
'field' => 'cat3_sum',
'visible' => true
],
'owner' => [
'label' => 'Owner',
'field' => 'owner',
'visible' => true
],
'model_date' => [
'label' => 'Modeled',
'field' => 'model_date',
'visible' => true
],
];
} // end function
public function makeBlankModel()
{
return AcModel::make([
'is_full_airframe' => true,
'was_uploaded' => false,
'first_run' => true,
'model_date' => Carbon::today('America/Chicago')
]);
} // end function
public function getRowsQueryProperty()
{
return AcModel::fullSearch($this->search)
->when($this->status === 'locked', function($query) {
$query->where('status', 'locked');
})->when($this->status === 'archived', function($query) {
$query->where('status', 'archived');
})->when($this->status === 'active', function($query) {
$query->where('status', 'active');
})->latest();
} // end function
public function getRowsProperty()
{
return $this->rowsQuery->paginate($this->perPage);
} // end function
public function run(AcModel $model)
{
if($model->status !== 'active') {
$this->dispatchBrowserEvent('toast', [
'type' => 'error',
'msg' => 'Model is not active'
]);
} // end if
$model->updateModelValues();
} // end function
public function save()
{
if(! $this->single->user_id) {
$this->single->user_id = auth()->id();
} // end if
if(!$this->single->tail || $this->single->tail != $this->single->title) {
$this->single->tail = $this->single->title;
} // end if
$this->single->save();
$this->single = $this->makeBlankModel();
$this->showEditModal = false;
} // end function
public function sendToIC(AcModel $model)
{
$model->status = 'locked';
$model->to_ic = Carbon::today('America/Chicago');
$model->save();
$delegate = CorporateDelegate::whereRole('inventory_controller')->first();
$delegate->notify(new ModelSentToIC($model));
} // end function
public function toggleAirframe($id)
{
$model = AcModel::find($id);
$model->is_full_airframe = !$model->is_full_airframe;
$model->save();
} // end function
public function unarchive(AcModel $model)
{
$model->status = 'active';
$model->save();
} // end function
public function updatedColumns()
{
$record = UserPref::whereUserId(auth()->id())
->whereResource('modeler')
->first();
if($record) {
$record->properties = $this->columns;
$record->save();
} else {
UserPref::create([
'resource' => 'modeler',
'user_id' => auth()->id(),
'properties' => $this->getColumns()
]);
} // end if
} // end function
public function updatedSingleTitle()
{
$aircraft = Aircraft::whereRegistration($this->single->title)->first();
if($aircraft) {
$this->single->aircraft_id = $aircraft->id;
$this->single->tail = $aircraft->registration;
$this->single->aircraft = $aircraft->series;
$this->single->serial_no = $aircraft->serial_number;
$this->single->line_no = $aircraft->line_number;
$this->single->owner = $aircraft->owner;
$this->single->operator = $aircraft->operator;
$this->single->apu = $aircraft->apu_sub_series;
$this->single->engines = $aircraft->engine_master_series;
$this->single->engine_count = $aircraft->number_of_engines;
$this->single->model_date = Carbon::today('America/Chicago');
} // end if
} // end function
public function upload(AcModel $model)
{
if( $this->single->isNot($model)) {
$this->single = $model;
} // end if
$this->showUploadModal = true;
} // end function
public function view($id)
{
$this->redirect("/modeler/single/{$id}");
} // end function
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment