2022-06-28 13:20:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
|
2022-08-19 05:12:56 +00:00
|
|
|
use Carbon\Carbon;
|
2022-06-30 13:51:20 +00:00
|
|
|
use Illuminate\Http\Request;
|
2024-08-18 03:58:18 +00:00
|
|
|
use Illuminate\Support\Arr;
|
2022-08-19 05:12:56 +00:00
|
|
|
|
2022-06-30 13:51:20 +00:00
|
|
|
use App\Http\Requests\{SupplierAddEdit,SupplierProductAddEdit};
|
2022-08-19 05:12:56 +00:00
|
|
|
use App\Jobs\ImportCosts;
|
2024-08-18 03:58:18 +00:00
|
|
|
use App\Models\{Cost,Supplier,SupplierDetail};
|
2022-06-28 13:20:56 +00:00
|
|
|
|
|
|
|
class SupplierController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Update a suppliers details
|
|
|
|
*
|
|
|
|
* @param SupplierAddEdit $request
|
|
|
|
* @param Supplier $o
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
|
|
|
public function addedit(SupplierAddEdit $request,Supplier $o)
|
|
|
|
{
|
2024-08-18 03:58:18 +00:00
|
|
|
foreach (Arr::except($request->validated(),['supplier_details','api_key','api_secret','submit']) as $key => $item)
|
2022-06-28 13:20:56 +00:00
|
|
|
$o->{$key} = $item;
|
|
|
|
|
|
|
|
$o->active = (bool)$request->active;
|
|
|
|
|
|
|
|
try {
|
|
|
|
$o->save();
|
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
return redirect()->back()->withErrors($e->getMessage())->withInput();
|
|
|
|
}
|
|
|
|
|
|
|
|
$o->load(['detail']);
|
|
|
|
$oo = $o->detail ?: new SupplierDetail;
|
|
|
|
|
|
|
|
foreach ($request->get('supplier_details',[]) as $key => $item)
|
|
|
|
$oo->{$key} = $item;
|
|
|
|
|
2022-08-05 14:22:22 +00:00
|
|
|
$oo->connections = $oo->connections->merge([
|
|
|
|
'api_key'=>$request->get('api_key'),
|
|
|
|
'api_secret'=>$request->get('api_secret'),
|
|
|
|
])->filter();
|
2022-06-28 13:20:56 +00:00
|
|
|
$o->detail()->save($oo);
|
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2022-08-05 14:22:22 +00:00
|
|
|
->with('success','Supplier Saved');
|
2022-06-28 13:20:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the suppliers invoice
|
|
|
|
*
|
|
|
|
* @param Cost $o
|
|
|
|
* @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
|
|
|
|
*/
|
|
|
|
public function cost(Cost $o)
|
|
|
|
{
|
|
|
|
// @todo Need to add the services that are active that are not on the bill for the supplier.
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.supplier.cost.view')
|
|
|
|
->with('o',$o);
|
2022-08-19 05:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cost_add(Supplier $o)
|
|
|
|
{
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.supplier.cost.add')
|
|
|
|
->with('o',$o);
|
2022-08-19 05:12:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function cost_submit(Request $request,Supplier $o)
|
|
|
|
{
|
|
|
|
$request->validate([
|
|
|
|
'file' => 'required|filled',
|
|
|
|
'billed_at' => 'required|date',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$filename = $request->file('file')->store('cost_import');
|
|
|
|
|
|
|
|
ImportCosts::dispatch(
|
|
|
|
config('site'),
|
|
|
|
$o,
|
|
|
|
Carbon::create($request->billed_at),
|
|
|
|
$filename,
|
|
|
|
)->onQueue('low');
|
|
|
|
|
2024-08-18 03:58:18 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
|
|
|
->with('success','File uploaded');
|
2022-06-30 13:51:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2024-08-18 03:58:18 +00:00
|
|
|
foreach (Arr::only($request->validated(),[
|
2022-06-30 13:51:20 +00:00
|
|
|
'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
|
2024-08-18 03:58:18 +00:00
|
|
|
foreach (Arr::only($request->validated(),['active','extra_shaped','extra_charged']) as $key => $value)
|
|
|
|
$oo->{$key} = ($value == 'on' ? 1 : 0);
|
2022-06-30 13:51:20 +00:00
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new \Exception('Unknown offering type:'.$request->offering_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
$oo->save();
|
|
|
|
|
2024-08-18 03:58:18 +00:00
|
|
|
return redirect()
|
|
|
|
->back()
|
2022-06-30 13:51:20 +00:00
|
|
|
->with('success','Saved');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
2024-08-18 03:58:18 +00:00
|
|
|
$o->load(['products.products.services']);
|
2022-06-30 13:51:20 +00:00
|
|
|
|
2024-07-04 05:03:11 +00:00
|
|
|
return view('theme.backend.adminlte.supplier.product.widget.'.$type)
|
2022-06-30 13:51:20 +00:00
|
|
|
->with('o',$id ? $o : NULL)
|
|
|
|
->withErrors($request->errors);
|
|
|
|
}
|
2022-06-28 13:20:56 +00:00
|
|
|
}
|