Enable editing of supplier products and listing services connected to them
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\SupplierAddEdit;
|
||||
use Illuminate\Http\Request;
|
||||
use App\Http\Requests\{SupplierAddEdit,SupplierProductAddEdit};
|
||||
use App\Models\{Cost,Supplier,SupplierDetail};
|
||||
|
||||
class SupplierController extends Controller
|
||||
@@ -67,6 +68,111 @@ class SupplierController extends Controller
|
||||
return view('supplier.cost',['o'=>$o]);
|
||||
}
|
||||
|
||||
/**
|
||||
* New Product from a supplier
|
||||
*
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||||
*/
|
||||
public function product_add()
|
||||
{
|
||||
return view('supplier.product.addedit')
|
||||
->with('o',new Supplier)
|
||||
->with('oo',NULL);
|
||||
}
|
||||
|
||||
public function product_addedit(SupplierProductAddEdit $request,Supplier $o,int $id,string $type)
|
||||
{
|
||||
// Quick validation
|
||||
if ($type !== $request->offering_type)
|
||||
abort(500,'Type and offering type do not match');
|
||||
if ($o->exists && ($o->detail->id !== (int)$request->supplier_detail_id))
|
||||
abort(500,sprintf('Supplier [%d] and supplier_detail_id [%d] do not match',$o->detail->id,$request->supplier_detail_id));
|
||||
|
||||
switch ($request->offering_type) {
|
||||
case 'broadband':
|
||||
$oo = Supplier\Broadband::findOrNew($id);
|
||||
|
||||
// @todo these are broadband requirements - get them from the broadband class.
|
||||
foreach ($request->only([
|
||||
'supplier_detail_id',
|
||||
'product_id'.
|
||||
'product_desc',
|
||||
'base_cost',
|
||||
'setup_cost',
|
||||
'contract_term',
|
||||
'metric',
|
||||
'speed',
|
||||
'technology',
|
||||
'offpeak_start',
|
||||
'offpeak_end',
|
||||
'base_down_peak',
|
||||
'base_up_peak',
|
||||
'base_down_offpeak',
|
||||
'base_up_offpeak',
|
||||
'extra_down_peak',
|
||||
'extra_up_peak',
|
||||
'extra_down_offpeak',
|
||||
'extra_up_offpeak',
|
||||
]) as $key => $value)
|
||||
$oo->$key = $value;
|
||||
|
||||
// Our boolean values
|
||||
foreach ($request->only(['active','extra_shaped','extra_charged']) as $key => $value)
|
||||
$oo->$key = ($value == 'on' ? 1 : 0);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
throw new \Exception('Unknown offering type:'.$request->offering_type);
|
||||
}
|
||||
|
||||
$oo->save();
|
||||
|
||||
return redirect()->back()
|
||||
->with('success','Saved');
|
||||
}
|
||||
|
||||
/**
|
||||
* Edit a supplier product
|
||||
*
|
||||
* @param Supplier $o
|
||||
* @param int $id
|
||||
* @param string $type
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||||
*/
|
||||
public function product_view(Supplier $o,int $id,string $type)
|
||||
{
|
||||
$oo = $o->detail->find($type,$id);
|
||||
$oo->load(['products.product.services.product.type']);
|
||||
|
||||
return view('supplier.product.addedit')
|
||||
->with('o',$o)
|
||||
->with('oo',$oo);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the form for a specific product type
|
||||
*
|
||||
* @param Request $request
|
||||
* @param string $type
|
||||
* @param int|null $id
|
||||
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
||||
*/
|
||||
public function product_view_type(Request $request,string $type,int $id=NULL)
|
||||
{
|
||||
$o = $id ? Supplier::offeringTypeClass($type)->findOrFail($id) : NULL;
|
||||
|
||||
if ($request->old)
|
||||
$request->session()->flashInput($request->old);
|
||||
|
||||
if ($o)
|
||||
$o->load(['products.product.services']);
|
||||
|
||||
return view('supplier.product.widget.'.$type)
|
||||
->with('o',$id ? $o : NULL)
|
||||
->withErrors($request->errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* View a supplier.
|
||||
*
|
||||
|
60
app/Http/Requests/SupplierProductAddEdit.php
Normal file
60
app/Http/Requests/SupplierProductAddEdit.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
use App\Models\Supplier;
|
||||
|
||||
class SupplierProductAddEdit extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize()
|
||||
{
|
||||
return Auth::user()->isWholesaler();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function rules(Request $request)
|
||||
{
|
||||
// @todo these are broadband requirements - perhaps move them to the broadband class.
|
||||
// @todo Enhance the validation so that extra_* values are not accepted if base_* values are not included.
|
||||
return [
|
||||
'id' => 'required|nullable',
|
||||
'offering_type' => ['required',Rule::in(Supplier::offeringTypeKeys()->toArray())],
|
||||
'supplier_detail_id' => 'required|exists:supplier_details,id',
|
||||
'active' => 'sometimes|accepted',
|
||||
'extra_shaped' => 'sometimes|accepted',
|
||||
'extra_charged' => 'sometimes|accepted',
|
||||
'product_id' => 'required|string|min:2',
|
||||
'product_desc' => 'required|string|min:2',
|
||||
'base_cost' => 'required|numeric|min:.01',
|
||||
'setup_cost' => 'nullable|numeric',
|
||||
'contract_term' => 'nullable|numeric|min:1',
|
||||
'metric' => 'nullable|numeric|min:1',
|
||||
'speed' => 'nullable|string|max:64',
|
||||
'technology' => 'nullable|string|max:255',
|
||||
'offpeak_start' => 'nullable|date_format:H:i',
|
||||
'offpeak_end' => 'nullable|date_format:H:i',
|
||||
'base_down_peak' => 'nullable|numeric',
|
||||
'base_up_peak' => 'nullable|numeric',
|
||||
'base_down_offpeak' => 'nullable|numeric',
|
||||
'base_up_offpeak' => 'nullable|numeric',
|
||||
'extra_down_peak' => 'nullable|numeric',
|
||||
'extra_up_peak' => 'nullable|numeric',
|
||||
'extra_down_offpeak' => 'nullable|numeric',
|
||||
'extra_up_offpeak' => 'nullable|numeric',
|
||||
];
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user