osb/app/Http/Controllers/ProductController.php

127 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
2024-08-14 12:16:09 +00:00
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use App\Http\Requests\ProductAddEdit;
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
*/
2024-08-14 12:16:09 +00:00
public function api_supplied_products(Request $request): Collection
{
switch ($request->type) {
case Product\Broadband::class:
2022-04-19 07:07:39 +00:00
return Product\Broadband::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
2024-08-14 12:16:09 +00:00
->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)])
->sortBy('name')
->values();
case Product\Domain::class:
return Product\Domain::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
2024-08-14 12:16:09 +00:00
->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)])
->sortBy('name')
->values();
case Product\Email::class:
2022-04-19 07:07:39 +00:00
return Product\Email::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
2024-08-14 12:16:09 +00:00
->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)])
->sortBy('name')
->values();
case Product\Generic::class:
return Product\Generic::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
2024-08-14 12:16:09 +00:00
->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)])
->sortBy('name')
->values();
case Product\Host::class:
2022-04-19 07:07:39 +00:00
return Product\Host::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
2024-08-14 12:16:09 +00:00
->map(fn($item)=>['id'=>$item->id,'name'=>sprintf('%s: %s',$item->supplied->supplier->name,$item->supplied->name)])
->sortBy('name')
->values();
case Product\Phone::class:
2022-08-20 13:01:03 +00:00
return Product\Phone::select(['id','supplier_item_id'])
->with(['supplied.supplier_detail.supplier'])
->get()
2024-08-14 12:16:09 +00:00
->map(fn($item)=>['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();
default:
throw new \Exception('Unknown type: '.$request->type);
}
}
2024-08-14 12:16:09 +00:00
public function addedit(ProductAddEdit $request,Product $o)
{
2024-08-14 12:16:09 +00:00
foreach (Arr::except($request->validated(),['translate','accounting','pricing','active']) as $key => $item)
$o->{$key} = $item;
$o->active = (bool)$request->active;
// Trim down the pricing array, remove null values
2024-08-14 12:16:09 +00:00
$o->pricing = collect($request->validated('pricing'))
->map(function($item) {
foreach ($item as $k=>$v) {
if (is_array($v)) {
$v = array_filter($v);
$item[$k] = $v;
}
}
2024-08-14 12:16:09 +00:00
return $item;
});
try {
$o->save();
2024-08-14 12:16:09 +00:00
} catch (\Exception $e) {
2024-08-14 12:16:09 +00:00
return redirect()
->back()
->withErrors($e->getMessage())
->withInput();
}
$o->load(['translate']);
$oo = $o->translate ?: new ProductTranslate;
2024-08-14 12:16:09 +00:00
foreach ($request->validated('translate',[]) as $key => $item)
$oo->{$key} = $item;
$o->translate()->save($oo);
2024-08-14 12:16:09 +00:00
foreach ($request->validated('accounting',[]) as $k=>$v) {
$o->providers()->syncWithoutDetaching([
$k => [
'ref' => $v,
'site_id'=>$o->site_id,
],
]);
}
return redirect()
->back()
->with('success','Product saved');
}
}