2022-04-02 07:06:34 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
|
|
|
use Illuminate\Http\Request;
|
2024-08-14 12:16:09 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2022-04-02 07:06:34 +00:00
|
|
|
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
|
|
|
|
*/
|
2024-08-14 12:16:09 +00:00
|
|
|
public function api_supplied_products(Request $request): Collection
|
2022-04-02 07:06:34 +00:00
|
|
|
{
|
|
|
|
switch ($request->type) {
|
2023-05-05 06:29:08 +00:00
|
|
|
case Product\Broadband::class:
|
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()
|
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-04-02 07:06:34 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2023-05-05 06:29:08 +00:00
|
|
|
case Product\Domain::class:
|
2022-10-18 03:17:50 +00:00
|
|
|
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)])
|
2022-10-18 03:17:50 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2023-05-05 06:29:08 +00:00
|
|
|
case Product\Email::class:
|
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()
|
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-04-02 07:06:34 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2023-05-05 06:29:08 +00:00
|
|
|
case Product\Generic::class:
|
2023-05-03 13:41:48 +00:00
|
|
|
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)])
|
2023-05-03 13:41:48 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2023-05-05 06:29:08 +00:00
|
|
|
case Product\Host::class:
|
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()
|
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-04-02 09:26:59 +00:00
|
|
|
->sortBy('name')
|
|
|
|
->values();
|
|
|
|
|
2023-05-05 06:29:08 +00:00
|
|
|
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();
|
|
|
|
|
2022-04-02 07:06:34 +00:00
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown type: '.$request->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-14 12:16:09 +00:00
|
|
|
public function addedit(ProductAddEdit $request,Product $o)
|
2022-08-20 13:35:41 +00:00
|
|
|
{
|
2024-08-14 12:16:09 +00:00
|
|
|
foreach (Arr::except($request->validated(),['translate','accounting','pricing','active']) 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
|
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;
|
|
|
|
}
|
2023-05-04 12:17:42 +00:00
|
|
|
}
|
|
|
|
|
2024-08-14 12:16:09 +00:00
|
|
|
return $item;
|
|
|
|
});
|
2023-05-04 12:17:42 +00:00
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
try {
|
|
|
|
$o->save();
|
2024-08-14 12:16:09 +00:00
|
|
|
|
2022-08-20 13:35:41 +00:00
|
|
|
} catch (\Exception $e) {
|
2024-08-14 12:16:09 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->withErrors($e->getMessage())
|
|
|
|
->withInput();
|
2022-08-20 13:35:41 +00:00
|
|
|
}
|
2022-04-02 07:06:34 +00:00
|
|
|
|
2022-10-18 12:23:45 +00:00
|
|
|
$o->load(['translate']);
|
|
|
|
$oo = $o->translate ?: new ProductTranslate;
|
2024-08-14 12:16:09 +00:00
|
|
|
foreach ($request->validated('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
|
|
|
|
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,
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
}
|
2023-05-12 10:09:51 +00:00
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2022-08-20 13:35:41 +00:00
|
|
|
->with('success','Product saved');
|
2022-04-02 07:06:34 +00:00
|
|
|
}
|
|
|
|
}
|