Some product rework

This commit is contained in:
Deon George 2022-08-20 23:01:03 +10:00
parent 71b252843c
commit 8d920e1ba1
10 changed files with 256 additions and 232 deletions

View File

@ -43,6 +43,14 @@ class ProductController extends Controller
->sortBy('name')
->values();
case 'App\Models\Product\Phone':
return Product\Phone::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier_detail->supplier->name,$item->supplied->name)]; })
->sortBy('name')
->values();
default:
throw new \Exception('Unknown type: '.$request->type);
}
@ -91,7 +99,8 @@ class ProductController extends Controller
if (! $o->exists && $request->name)
$o = Product::where('name',$request->name)->firstOrNew();
return view('a.product.details')
return view('product.details')
->with('breadcrumb',collect(['Products'=>url('a/product')]))
->with('o',$o);
}
@ -103,6 +112,6 @@ class ProductController extends Controller
*/
public function home()
{
return view('a.product.home');
return view('product.home');
}
}

View File

@ -775,7 +775,7 @@ class Service extends Model implements IDs
*/
public function getNameShortAttribute()
{
return $this->type->getServiceNameAttribute() ?: 'SID:'.$this->sid;
return ($this->type && $this->type->getServiceNameAttribute()) ? $this->type->getServiceNameAttribute() : 'SID:'.$this->sid;
}
/**
@ -790,7 +790,7 @@ class Service extends Model implements IDs
*/
public function getNameDetailAttribute()
{
return $this->type->getServiceDescriptionAttribute();
return ($this->type && $this->type->getServiceDescriptionAttribute()) ? $this->type->getServiceDescriptionAttribute() : 'No Description';
}
/**

View File

@ -53,6 +53,10 @@ class SupplierDetail extends Model
$item = $this->broadbands->where('id',$id);
break;
case 'phone':
$item = $this->phones->where('id',$id);
break;
default:
throw new \Exception('Unknown type: '.$type);
}

View File

@ -1,70 +0,0 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Product
@endsection
@section('page_title')
Product
@endsection
@section('contentheader_title')
Product
@endsection
@section('contentheader_description')
@endsection
@section('main-content')
<div class="row">
<div class="col-12">
<div class="card card-dark">
<div class="card-header">
<h1 class="card-title">Product Configuration</h1>
</div>
<div class="card-body">
<form class="g-0 needs-validation" method="POST" enctype="multipart/form-data" role="form">
@csrf
<div class="row">
<div class="col-4">
<div class="form-group has-validation">
<label for="name">Product Name</label>
<select class="form-control form-control-border" id="name" name="supplier_id">
<option value=""></option>
<option value="">Add New</option>
@foreach(\App\Models\Product::get()->sortBy('name') as $o)
<option value="{{ $o->id }}">{{ $o->name }}</option>
@endforeach
</select>
<span class="invalid-feedback" role="alert">
@error('name')
{{ $message }}
@else
Product Name is required.
@enderror
</span>
<span class="input-helper">Product Name</span>
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@section('page-scripts')
@css(select2)
@js(select2,autofocus)
<script type="text/javascript">
$(document).ready(function() {
$('#name').select2()
.on('change',function(item) {
window.location.href = '{{ url('a/product/details') }}/'+item.target.value;
});
});
</script>
@endsection

View File

@ -1,155 +0,0 @@
<!-- $o = Product::class -->
<div class="row">
<div class="col-12">
<h3>Product Details</h3>
<hr>
@if(session()->has('success'))
<span class="ml-3 pt-0 pb-0 pr-1 pl-1 btn btn-outline-success"><small>{{ session()->get('success') }}</small></span>
@endif
<form class="g-0 needs-validation" method="POST" enctype="multipart/form-data" role="form">
@csrf
<div class="row">
<div class="col-4">
<div class="row">
<!-- Product Name -->
<div class="col-9">
<div class="form-group has-validation">
<label for="name">Product Name</label>
<input type="text" class="form-control form-control-border @error('description.name') is-invalid @enderror" id="name" name="description[name]" placeholder="Product Name" value="{{ old('description.name',$o->name) }}" required>
<span class="invalid-feedback" role="alert">
@error('description.name')
{{ $message }}
@else
Product Name required.
@enderror
</span>
</div>
</div>
<!-- Product Active -->
<div class="col-3">
<div class="form-group">
<div class="custom-control custom-switch custom-switch-off-danger custom-switch-on-success">
<input type="checkbox" class="custom-control-input" id="active" name="active" {{ old('active',$o->active) ? 'checked' : '' }}>
<label class="custom-control-label" for="active">Active</label>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<!-- Product Type -->
<div class="col-4">
<div class="form-group has-validation">
<label for="name">Product Type</label>
<select class="form-control form-control-border" id="model" name="model">
<option value=""></option>
@foreach($o->availableTypes() as $type)
<option value="{{ $type }}" @if($o->type && ($type == get_class($o->type)))selected @endif>{{ $type }}</option>
@endforeach
</select>
<span class="invalid-feedback" role="alert">
@error('model')
{{ $message }}
@else
Product Type required.
@enderror
</span>
</div>
</div>
</div>
<div class="row">
<!-- Supplied Product -->
<div class="col-4" id="supplier_product">
<div class="form-group has-validation">
<label for="name">Supplied Product</label>
<select class="form-control form-control-border" id="model_id" name="model_id">
</select>
<span class="invalid-feedback" role="alert">
@error('model_id')
{{ $message }}
@else
Supplied Product required.
@enderror
</span>
</div>
</div>
</div>
<div class="row">
<!-- Buttons -->
<div class="col-12">
<a href="{{ url('/home') }}" class="btn btn-danger">Cancel</a>
@can('wholesaler')
<button type="submit" name="submit" class="btn btn-success mr-0 float-right">@if ($o->exists)Save @else Add @endif</button>
@endcan
</div>
</div>
</form>
</div>
</div>
@section('page-scripts')
@css(select2)
@js(select2,autofocus)
<script type="text/javascript">
// Get a list of supplier items matching this type to populate model_id
function supplier_products(type,destination,selected) {
destination.prop('disabled',true);
$.ajax({
type: 'GET',
dataType: 'json',
data: {type: type},
cache: false,
url: '{{ url('api/a/supplier_products') }}',
timeout: 2000,
error: function(x) {
// @todo add a spinner
//spinner.toggleClass('d-none').toggleClass('fa-spin');
alert('Failed to submit');
},
success: function(data) {
destination
.empty()
.append($('<option>'))
.append(data.map(function(item,key) {
return new Option(item.name,item.id,selected && selected==item.id,selected && selected==item.id);
}))
.prop('disabled',false);
}
});
}
$(document).ready(function() {
$('#model').select2()
.on('change',function(item) {
if ($(this).val()) {
$('#supplier_product').show();
supplier_products($(this).val(),$('#model_id'));
} else {
$('#supplier_product').hide();
}
});
$('#model_id').select2();
// After we render the page, hide the supplier_product if this product has no model.
// We do this here, because adding d-none to the div results in the select2 input not presenting correctly
if (! $('#model').val())
$('#supplier_product').hide();
else
supplier_products($('#model').val(),$('#model_id'),{{ old('model_id',$o->model_id) }});
});
</script>
@endsection

View File

@ -33,11 +33,11 @@
<div class="card-body">
<div class="tab-content">
<div class="tab-pane fade active show" id="details" role="tabpanel">
@include('a.product.widgets.detail')
@include('product.widget.detail')
</div>
<div class="tab-pane fade" id="services" role="tabpanel">
@include('a.product.widgets.services')
@include('product.widget.services')
</div>
</div>
</div>

View File

@ -0,0 +1,61 @@
@extends('adminlte::layouts.app')
@section('htmlheader_title')
Product
@endsection
@section('page_title')
Product
@endsection
@section('contentheader_title')
Product
@endsection
@section('contentheader_description')
@endsection
@section('main-content')
<div class="row">
<div class="col-12">
<div class="card card-dark">
<div class="card-header">
<h1 class="card-title">Product Configuration</h1>
</div>
<div class="card-body">
<form class="g-0 needs-validation" method="POST" enctype="multipart/form-data" role="form">
@csrf
<div class="row">
<div class="col-12 col-sm-9 col-md-6 col-xl-5">
@include('adminlte::widget.form_select',[
'label'=>'Product',
'icon'=>'fas fa-list',
'id'=>'product_id',
'old'=>'product_id',
'name'=>'product_id',
'groupby'=>'active',
'options'=>\App\Models\Product::get()->sortBy(function($item) { return ($item->active ? '0' : '1').$item->name; })->transform(function($item) { return ['id'=>$item->id,'value'=>$item->name,'active'=>$item->active]; }),
'value'=>'',
])
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
@section('page-scripts')
@css(select2)
@js(select2,autofocus)
<script type="text/javascript">
$(document).ready(function() {
$('#product_id').on('change',function(item) {
window.location.href = '{{ url('a/product/details') }}/'+item.target.value;
});
});
</script>
@append

View File

@ -0,0 +1,140 @@
<!-- $o = Product::class -->
<div class="row">
<div class="col-12">
<h3>Product Details</h3>
<hr>
@if(session()->has('success'))
<span class="ml-3 pt-0 pb-0 pr-1 pl-1 btn btn-outline-success"><small>{{ session()->get('success') }}</small></span>
@endif
<form class="g-0 needs-validation" method="POST" enctype="multipart/form-data" role="form">
@csrf
<div class="row">
<!-- Product Name -->
<div class="col-12 col-sm-9 col-md-12 col-xl-6">
@include('adminlte::widget.form_text',[
'label'=>'Product Name',
'icon'=>'fas fa-atom',
'id'=>'name',
'old'=>'description.name',
'name'=>'description[name]',
'value'=>$o->name ?? '',
])
</div>
<!-- Active -->
<div class="col-3">
@include('adminlte::widget.form_toggle',[
'label'=>'Active',
'id'=>'active',
'old'=>'active',
'name'=>'active',
'value'=>$o->active ?? '',
])
</div>
</div>
<div class="row">
<!-- Product Type -->
<div class="col-12 col-sm-9 col-md-12 col-xl-6">
@include('adminlte::widget.form_select',[
'label'=>'Product Type',
'icon'=>'fas fa-list',
'id'=>'model',
'old'=>'model',
'name'=>'model',
'options'=>$o->availableTypes()->transform(function($item) { return ['id'=>$item,'value'=>$item]; }),
'value'=>get_class($o->type) ?? '',
])
</div>
</div>
<div class="row">
<!-- Supplied Product -->
<div class="col-12 col-sm-9 col-md-12 col-xl-6" id="supplier_product">
<div class="form-group">
<label for="model_id">Supplied Product</label>
<div class="input-group has-validation">
<div class="input-group-prepend">
<span class="input-group-text"><i class="fa-fw fas fa-shopping-cart"></i></span>
</div>
<select class="form-control @error('model_id') is-invalid @enderror" id="model_id" name="model_id"></select>
<span class="invalid-feedback" role="alert">
@error('model_id')
{{ $message }}
@enderror
</span>
</div>
</div>
</div>
</div>
<div class="row">
<!-- Buttons -->
<div class="col-12">
<a href="{{ url('/home') }}" class="btn btn-danger">Cancel</a>
@can('wholesaler')
<button type="submit" name="submit" class="btn btn-success mr-0 float-right">@if ($o->exists)Save @else Add @endif</button>
@endcan
</div>
</div>
</form>
</div>
</div>
@section('page-scripts')
@css(select2)
@js(select2,autofocus)
<script type="text/javascript">
// Get a list of supplier items matching this type to populate model_id
function supplier_products(type,destination,selected) {
destination.prop('disabled',true);
$.ajax({
type: 'GET',
dataType: 'json',
data: {type: type},
cache: false,
url: '{{ url('api/a/supplier_products') }}',
timeout: 2000,
error: function(x) {
// @todo add a spinner
//spinner.toggleClass('d-none').toggleClass('fa-spin');
alert('Failed to submit');
},
success: function(data) {
destination
.empty()
.append($('<option>'))
.append(data.map(function(item,key) {
return new Option(item.name,item.id,selected && selected==item.id,selected && selected==item.id);
}))
.prop('disabled',false);
}
});
}
$(document).ready(function() {
$('#model').on('change',function(item) {
if ($(this).val()) {
$('#supplier_product').show();
supplier_products($(this).val(),$('#model_id'));
} else {
$('#supplier_product').hide();
}
});
$('#model_id').select2();
// After we render the page, hide the supplier_product if this product has no model.
// We do this here, because adding d-none to the div results in the select2 input not presenting correctly
if (! $('#model').val())
$('#supplier_product').hide();
else
supplier_products($('#model').val(),$('#model_id'),{{ old('model_id',$o->model_id) }});
});
</script>
@append

View File

@ -0,0 +1,35 @@
<!-- $o = Product::class -->
<div class="row">
@if(count($o->services))
<div class="col-6">
<table class="table table-hover">
<thead>
<tr>
<th>ID</th>
<th>Date Start</th>
<th>Date Stop</th>
<th>Data Invoiced</th>
<th>Active</th>
<th class="text-right">Charge</th>
</tr>
</thead>
<tbody>
@foreach ($o->services as $so)
<tr>
<td><a href="{{ url('u/service',[$so->id]) }}">{{ $so->sid }}</a></td>
<td>{{ $so->start_at ? $so->start_at->format('Y-m-d') : '-' }}</td>
<td>{{ $so->stop_at ? $so->stop_at->format('Y-m-d') : '-' }}</td>
<td>{{ $so->invoice_to ? $so->invoice_to->format('Y-m-d') : '-' }}</td>
<td>{{ $so->active ? 'YES' : 'NO' }}</td>
<td class="text-right">{{ number_format($so->billing_charge,2) }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
@else
<p>No services use this product.</p>
@endif
</div>

View File

@ -62,7 +62,7 @@
'label'=>'Registrar Account',
'icon'=>'fas fa-user-circle',
'id'=>'registrar_account',
'old'=>'domain.registrar_account',
'old'=>'domain.registrar_account',
'name'=>'domain[registrar_account]',
'value'=>$o->registrar_account ?? '',
])