diff --git a/app/Http/Controllers/SupplierController.php b/app/Http/Controllers/SupplierController.php index fdd4925..bad5b77 100644 --- a/app/Http/Controllers/SupplierController.php +++ b/app/Http/Controllers/SupplierController.php @@ -73,6 +73,20 @@ class SupplierController extends Controller ->with('success','File uploaded'); } + public function product_new(SupplierProductAddEdit $request) + { + $o = Supplier::offeringTypeClass($request->validated('offering_type')); + + foreach (Arr::except($request->validated(),['id','offering_type']) as $key => $value) + $o->{$key} = $value; + + $o->save(); + + return redirect() + ->back() + ->with('success','Saved'); + } + public function product_addedit(SupplierProductAddEdit $request,Supplier $o,int $id,string $type) { // Quick validation @@ -88,7 +102,7 @@ class SupplierController extends Controller // @todo these are broadband requirements - get them from the broadband class. foreach (Arr::only($request->validated(),[ 'supplier_detail_id', - 'product_id'. + 'product_id', 'product_desc', 'base_cost', 'setup_cost', @@ -107,7 +121,7 @@ class SupplierController extends Controller 'extra_down_offpeak', 'extra_up_offpeak', ]) as $key => $value) - $oo->$key = $value; + $oo->{$key} = $value; // Our boolean values foreach (Arr::only($request->validated(),['active','extra_shaped','extra_charged']) as $key => $value) @@ -115,6 +129,17 @@ class SupplierController extends Controller break; + case 'phone': + $oo = Supplier\Phone::findOrNew($id); + + foreach (Arr::except($oo->validation(),[ + 'id', + 'offering_type', + ]) as $key => $value) + $oo->{$key} = $request->validated($key); + + break; + default: throw new \Exception('Unknown offering type:'.$request->offering_type); } diff --git a/app/Http/Requests/SupplierProductAddEdit.php b/app/Http/Requests/SupplierProductAddEdit.php index 87e456a..b6036df 100644 --- a/app/Http/Requests/SupplierProductAddEdit.php +++ b/app/Http/Requests/SupplierProductAddEdit.php @@ -5,56 +5,28 @@ 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 - */ - public function rules(Request $request) + /** + * Determine if the user is authorized to make this request. + * + * @return bool + */ + public function authorize() { - // @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', - ]; + return Auth::user()->isWholesaler(); } -} + + /** + * Get the validation rules that apply to the request. + * + * @return array + */ + public function rules(Request $request): array + { + return Supplier::offeringTypeClass($request->offering_type ?? '')->validation(); + } +} \ No newline at end of file diff --git a/app/Models/Supplier/Broadband.php b/app/Models/Supplier/Broadband.php index 8572552..fa06c02 100644 --- a/app/Models/Supplier/Broadband.php +++ b/app/Models/Supplier/Broadband.php @@ -148,4 +148,27 @@ class Broadband extends Type return $result; } + + public function validation(): array + { + // @todo Enhance the validation so that extra_* values are not accepted if base_* values are not included. + return array_merge(parent::validation(), + [ + 'extra_shaped' => 'sometimes|accepted', + 'extra_charged' => 'sometimes|accepted', + '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', + ]); + } } \ No newline at end of file diff --git a/app/Models/Supplier/Type.php b/app/Models/Supplier/Type.php index 108b4e7..67e9c0e 100644 --- a/app/Models/Supplier/Type.php +++ b/app/Models/Supplier/Type.php @@ -4,9 +4,10 @@ namespace App\Models\Supplier; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Arr; +use Illuminate\Validation\Rule; use Leenooks\Traits\ScopeActive; -use App\Models\{Invoice,SupplierDetail}; +use App\Models\{Invoice, Supplier, SupplierDetail}; use App\Traits\SiteID; abstract class Type extends Model @@ -87,4 +88,23 @@ abstract class Type extends Model { return $this->supplier_detail->supplier; } + + public function validation(): array + { + return [ + 'id' => [ + 'nullable', + 'numeric', + sprintf('exists:%s,id',static::getTable()) + ], + 'offering_type' => ['required',Rule::in(Supplier::offeringTypeKeys()->toArray())], + 'supplier_detail_id' => 'required|exists:supplier_details,id', + 'active' => 'sometimes|accepted', + 'base_cost' => 'required|numeric|min:0', + 'setup_cost' => 'nullable|numeric|min:0', + 'contract_term' => 'nullable|numeric|min:1', + 'product_id' => 'required|string|min:2', + 'product_desc' => 'required|string|min:2', + ]; + } } \ No newline at end of file diff --git a/app/Models/SupplierDetail.php b/app/Models/SupplierDetail.php index 1661ed1..557cecc 100644 --- a/app/Models/SupplierDetail.php +++ b/app/Models/SupplierDetail.php @@ -46,7 +46,7 @@ class SupplierDetail extends Model * @return Type * @throws \Exception */ - public function find(string $type,int $id): Type + public function supplier_product(string $type,int $id): Type { switch ($type) { case 'broadband': diff --git a/resources/views/theme/backend/adminlte/supplier/product/addedit.blade.php b/resources/views/theme/backend/adminlte/supplier/product/addedit.blade.php index e4d23c0..843c51f 100644 --- a/resources/views/theme/backend/adminlte/supplier/product/addedit.blade.php +++ b/resources/views/theme/backend/adminlte/supplier/product/addedit.blade.php @@ -5,8 +5,10 @@ @php if(isset($spo)) { - $oo = $spo->detail?->find(request()->route()->parameter('type'),request()->route()->parameter('id')); + $oo = $spo->detail?->supplier_product(request()->route()->parameter('type'),request()->route()->parameter('id')); $oo?->load(['products.products.services']); + + $breadcrumb = [$spo->name=>route('supplier.details',['spo'=>$spo->id])]; } @endphp @@ -55,7 +57,11 @@ if(isset($spo)) { -
+
+ @if(($x=old('offering_type')) && ((! isset($oo)) || (! $oo->exists))) + @include('theme.backend.adminlte.supplier.product.widget.'.$x,['o'=>NULL]) + @endif +
@@ -153,26 +159,26 @@ if(isset($spo)) { @section('page-scripts')