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

143 lines
3.9 KiB
PHP
Raw Normal View History

2018-10-22 05:47:42 +00:00
<?php
namespace Webkul\Admin\Http\Controllers\Customer;
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
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* CustomerGroupRepository object
*
2020-03-05 13:37:08 +00:00
* @var \Webkul\Customer\Repositories\CustomerGroupRepository
2018-10-22 05:47:42 +00:00
*/
2019-07-01 11:33:36 +00:00
protected $customerGroupRepository;
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
*/
2019-07-01 11:33:36 +00:00
public function __construct(CustomerGroupRepository $customerGroupRepository)
2018-10-22 05:47:42 +00:00
{
$this->_config = request('_config');
2018-11-29 06:42:44 +00:00
$this->middleware('admin');
2019-07-01 11:33:36 +00:00
$this->customerGroupRepository = $customerGroupRepository;
2018-10-22 05:47:42 +00:00
}
/**
* Display a listing of the resource.
*
2019-08-19 09:30:24 +00:00
* @return \Illuminate\View\View
2018-10-22 05:47:42 +00:00
*/
public function index()
{
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']);
}
/**
* 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
]);
2018-12-03 13:28:57 +00:00
$data = request()->all();
$data['is_user_defined'] = 1;
2019-07-01 11:33:36 +00:00
$this->customerGroupRepository->create($data);
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']);
}
/**
* 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
}
/**
* 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
]);
2019-07-01 11:33:36 +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
2019-04-09 01:01:52 +00:00
if ($customerGroup->is_user_defined == 0) {
2019-01-02 14:42:49 +00:00
session()->flash('warning', trans('admin::app.customers.customers.group-default'));
2020-02-24 12:19:12 +00:00
} elseif (count($customerGroup->customer) > 0) {
2019-01-28 09:34:45 +00:00
session()->flash('warning', trans('admin::app.response.customer-associate', ['name' => 'Customer Group']));
2018-12-03 13:28:57 +00:00
} else {
2019-04-09 01:01:52 +00:00
try {
2019-07-01 11:33:36 +00:00
$this->customerGroupRepository->delete($id);
2018-10-22 05:47:42 +00:00
2019-04-09 01:01:52 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Customer Group']));
2018-10-22 05:47:42 +00:00
return response()->json(['message' => true], 200);
2019-04-09 01:01:52 +00:00
} catch(\Exception $e) {
session()->flash('error', trans('admin::app.response.delete-failed', ['name' => 'Customer Group']));
}
2018-10-22 05:47:42 +00:00
}
return response()->json(['message' => false], 400);
2018-10-22 05:47:42 +00:00
}
}