sarga/packages/Webkul/Admin/src/Http/Controllers/Customer/CustomerGroupController.php

138 lines
3.8 KiB
PHP
Raw Normal View History

2018-10-22 05:47:42 +00:00
<?php
namespace Webkul\Admin\Http\Controllers\Customer;
use Webkul\Admin\DataGrids\CustomerGroupDataGrid;
2018-10-22 05:47:42 +00:00
use Webkul\Admin\Http\Controllers\Controller;
2019-07-01 11:33:36 +00:00
use Webkul\Customer\Repositories\CustomerGroupRepository;
2018-10-22 05:47:42 +00:00
class CustomerGroupController extends Controller
{
/**
2022-01-10 15:21:59 +00:00
* Contains route related configuration.
2018-10-22 05:47:42 +00:00
*
* @var array
2022-01-10 15:21:59 +00:00
*/
2018-10-22 05:47:42 +00:00
protected $_config;
2022-01-10 15:21:59 +00:00
/**
2018-10-22 05:47:42 +00:00
* Create a new controller instance.
*
2020-03-05 13:37:08 +00:00
* @param \Webkul\Customer\Repositories\CustomerGroupRepository $customerGroupRepository;
2018-10-22 05:47:42 +00:00
* @return void
*/
public function __construct(protected CustomerGroupRepository $customerGroupRepository)
2018-10-22 05:47:42 +00:00
{
$this->_config = request('_config');
}
/**
* Display a listing of the resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2022-01-10 15:21:59 +00:00
*/
2018-10-22 05:47:42 +00:00
public function index()
{
if (request()->ajax()) {
return app(CustomerGroupDataGrid::class)->toJson();
}
2018-10-22 05:47:42 +00:00
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-10-22 05:47:42 +00:00
*/
public function create()
{
return view($this->_config['view']);
}
2022-01-10 15:21:59 +00:00
/**
2018-10-22 05:47:42 +00:00
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
{
$this->validate(request(), [
'code' => ['required', 'unique:customer_groups,code', new \Webkul\Core\Contracts\Validations\Code],
'name' => 'required',
2018-10-22 05:47:42 +00:00
]);
2022-08-06 15:11:23 +00:00
$this->customerGroupRepository->create(array_merge(request()->all() , [
'is_user_defined' => 1,
]));
2018-10-22 05:47:42 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Customer Group']));
2018-10-22 05:47:42 +00:00
return redirect()->route($this->_config['redirect']);
}
2022-01-10 15:21:59 +00:00
/**
2018-10-22 05:47:42 +00:00
* Show the form for editing the specified resource.
*
* @param int $id
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-10-22 05:47:42 +00:00
*/
public function edit($id)
{
2019-07-01 11:33:36 +00:00
$group = $this->customerGroupRepository->findOrFail($id);
2018-10-22 05:47:42 +00:00
2019-02-13 12:12:07 +00:00
return view($this->_config['view'], compact('group'));
2018-10-22 05:47:42 +00:00
}
2022-01-10 15:21:59 +00:00
/**
2018-10-22 05:47:42 +00:00
* Update the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
2019-07-01 11:33:36 +00:00
public function update($id)
2018-10-22 05:47:42 +00:00
{
$this->validate(request(), [
'code' => ['required', 'unique:customer_groups,code,' . $id, new \Webkul\Core\Contracts\Validations\Code],
'name' => 'required',
2018-10-22 05:47:42 +00:00
]);
2022-01-10 15:21:59 +00:00
$this->customerGroupRepository->update(request()->all(), $id);
2018-10-22 05:47:42 +00:00
2019-01-16 08:38:39 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Customer Group']));
2018-10-22 05:47:42 +00:00
return redirect()->route($this->_config['redirect']);
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2019-07-01 11:33:36 +00:00
$customerGroup = $this->customerGroupRepository->findOrFail($id);
2018-12-03 13:20:20 +00:00
2022-08-06 15:11:23 +00:00
if (! $customerGroup->is_user_defined) {
2022-02-11 12:34:38 +00:00
return response()->json([
'message' => trans('admin::app.customers.customers.group-default'),
], 400);
2018-10-22 05:47:42 +00:00
}
2022-08-06 15:11:23 +00:00
if ($customerGroup->customers->count()) {
2022-02-11 12:34:38 +00:00
return response()->json([
'message' => trans('admin::app.response.customer-associate', ['name' => 'Customer Group']),
], 400);
}
try {
$this->customerGroupRepository->delete($id);
return response()->json(['message' => trans('admin::app.response.delete-success', ['name' => 'Customer Group'])]);
} catch (\Exception $e) {}
return response()->json(['message' => trans('admin::app.response.delete-failed', ['name' => 'Customer Group'])], 500);
2018-10-22 05:47:42 +00:00
}
2022-01-10 15:21:59 +00:00
}