2022-04-02 07:06:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
|
|
use Illuminate\Support\Collection;
|
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
use App\Http\Requests\ProductAddEdit;
|
2022-04-02 07:06:34 +00:00
|
|
|
use App\Models\{Product,ProductTranslate};
|
|
|
|
|
|
|
|
class ProductController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get a list of products that meet a type
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @return Collection
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
|
|
|
public function api_supplier_products(Request $request): Collection
|
|
|
|
{
|
|
|
|
switch ($request->type) {
|
|
|
|
case 'App\Models\Product\Broadband':
|
2022-04-19 07:07:39 +00:00
|
|
|
return Product\Broadband::select(['id','supplier_item_id'])
|
2022-04-02 07:06:34 +00:00
|
|
|
->with(['supplied.supplier_detail.supplier'])
|
|
|
|
->get()
|
2023-05-05 00:32:04 +00:00
|
|
|
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
|
2022-04-02 07:06:34 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2022-10-18 03:17:50 +00:00
|
|
|
case 'App\Models\Product\Domain':
|
|
|
|
return Product\Domain::select(['id','supplier_item_id'])
|
|
|
|
->with(['supplied.supplier_detail.supplier'])
|
|
|
|
->get()
|
2023-05-05 00:32:04 +00:00
|
|
|
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
|
2022-10-18 03:17:50 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2022-04-02 07:06:34 +00:00
|
|
|
case 'App\Models\Product\Email':
|
2022-04-19 07:07:39 +00:00
|
|
|
return Product\Email::select(['id','supplier_item_id'])
|
2022-04-02 07:06:34 +00:00
|
|
|
->with(['supplied.supplier_detail.supplier'])
|
|
|
|
->get()
|
2023-05-05 00:32:04 +00:00
|
|
|
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
|
2022-04-02 07:06:34 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2023-05-03 13:41:48 +00:00
|
|
|
case 'App\Models\Product\Generic':
|
|
|
|
return Product\Generic::select(['id','supplier_item_id'])
|
|
|
|
->with(['supplied.supplier_detail.supplier'])
|
|
|
|
->get()
|
2023-05-05 00:32:04 +00:00
|
|
|
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
|
2023-05-03 13:41:48 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2022-04-02 09:26:59 +00:00
|
|
|
case 'App\Models\Product\Host':
|
2022-04-19 07:07:39 +00:00
|
|
|
return Product\Host::select(['id','supplier_item_id'])
|
2022-04-02 09:26:59 +00:00
|
|
|
->with(['supplied.supplier_detail.supplier'])
|
|
|
|
->get()
|
2023-05-05 00:32:04 +00:00
|
|
|
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
|
2022-04-02 09:26:59 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2022-08-20 13:01:03 +00:00
|
|
|
case 'App\Models\Product\Phone':
|
|
|
|
return Product\Phone::select(['id','supplier_item_id'])
|
|
|
|
->with(['supplied.supplier_detail.supplier'])
|
|
|
|
->get()
|
2023-05-05 00:32:04 +00:00
|
|
|
->map(function($item) { return ['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)]; })
|
2022-08-20 13:01:03 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2022-04-02 07:06:34 +00:00
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown type: '.$request->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update a suppliers details
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Product $o
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function details(Request $request,Product $o)
|
|
|
|
{
|
2022-08-20 13:35:41 +00:00
|
|
|
if (! $o->exists && $request->name)
|
|
|
|
$o = Product::where('name',$request->name)->firstOrNew();
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
return view('product.details')
|
|
|
|
->with('breadcrumb',collect(['Products'=>url('a/product')]))
|
|
|
|
->with('o',$o);
|
|
|
|
}
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
public function details_addedit(ProductAddEdit $request,Product $o)
|
|
|
|
{
|
2022-10-18 12:23:45 +00:00
|
|
|
foreach ($request->except(['_token','submit','translate']) as $key => $item)
|
2022-08-20 13:35:41 +00:00
|
|
|
$o->{$key} = $item;
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
$o->active = (bool)$request->active;
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2023-05-04 12:17:42 +00:00
|
|
|
// Trim down the pricing array, remove null values
|
|
|
|
$o->pricing = $o->pricing->map(function($item) {
|
|
|
|
foreach ($item as $k=>$v) {
|
|
|
|
if (is_array($v)) {
|
|
|
|
$v = array_filter($v);
|
|
|
|
$item[$k] = $v;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $item;
|
|
|
|
});
|
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
try {
|
|
|
|
$o->save();
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect()->back()->withErrors($e->getMessage())->withInput();
|
|
|
|
}
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-10-18 12:23:45 +00:00
|
|
|
$o->load(['translate']);
|
|
|
|
$oo = $o->translate ?: new ProductTranslate;
|
|
|
|
foreach ($request->get('translate',[]) as $key => $item)
|
2022-08-20 13:35:41 +00:00
|
|
|
$oo->{$key} = $item;
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-10-18 12:23:45 +00:00
|
|
|
$o->translate()->save($oo);
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
return redirect()->back()
|
|
|
|
->with('success','Product saved');
|
2022-04-02 07:06:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage products for a site
|
|
|
|
*
|
|
|
|
* @note This method is protected by the routes
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function home()
|
|
|
|
{
|
2022-08-20 13:01:03 +00:00
|
|
|
return view('product.home');
|
2022-04-02 07:06:34 +00:00
|
|
|
}
|
|
|
|
}
|