sarga/packages/Webkul/CartRule/src/Http/Controllers/CartRuleController.php

221 lines
6.7 KiB
PHP
Raw Normal View History

2019-12-21 05:25:32 +00:00
<?php
namespace Webkul\CartRule\Http\Controllers;
use Exception;
2019-12-21 05:25:32 +00:00
use Illuminate\Http\Request;
2020-10-21 15:41:28 +00:00
use Illuminate\Validation\ValidationException;
2022-01-11 05:36:21 +00:00
use Illuminate\View\View;
use Webkul\Admin\DataGrids\CartRuleDataGrid;
2022-01-11 05:36:21 +00:00
use Webkul\CartRule\Repositories\CartRuleRepository;
2019-12-21 05:25:32 +00:00
class CartRuleController extends Controller
{
/**
2022-01-11 05:36:21 +00:00
* Initialize _config, a default request parameter with route.
*
2019-12-21 05:25:32 +00:00
* @param array
*/
protected $_config;
/**
2022-01-11 05:36:21 +00:00
* To hold cart repository instance.
*
2020-03-05 05:34:57 +00:00
* @var \Webkul\CartRule\Repositories\CartRuleRepository
2019-12-21 05:25:32 +00:00
*/
protected $cartRuleRepository;
/**
* Create a new controller instance.
*
* @param \Webkul\CartRule\Repositories\CartRuleRepository $cartRuleRepository
2019-12-21 05:25:32 +00:00
* @return void
*/
public function __construct(
CartRuleRepository $cartRuleRepository
2022-01-11 05:36:21 +00:00
) {
2019-12-21 05:25:32 +00:00
$this->_config = request('_config');
$this->cartRuleRepository = $cartRuleRepository;
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
if (request()->ajax()) {
return app(CartRuleDataGrid::class)->toJson();
}
2019-12-21 05:25:32 +00:00
return view($this->_config['view']);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view($this->_config['view']);
}
/**
* Copy a given Cart Rule id. Always make the copy is inactive so the
* user is able to configure it before setting it live.
2022-01-11 05:36:21 +00:00
*
* @param int $cartRuleId
* @return \Illuminate\View\View
*/
public function copy(int $cartRuleId): View
{
$originalCartRule = $this->cartRuleRepository
->findOrFail($cartRuleId)
->load('channels')
->load('customer_groups');
$copiedCartRule = $originalCartRule
->replicate()
->fill([
'status' => 0,
'name' => __('admin::app.copy-of') . $originalCartRule->name,
]);
$copiedCartRule->save();
2020-06-05 05:54:47 +00:00
foreach ($copiedCartRule->channels as $channel) {
$copiedCartRule->channels()->save($channel);
}
2020-06-05 05:54:47 +00:00
foreach ($copiedCartRule->customer_groups as $group) {
$copiedCartRule->customer_groups()->save($group);
}
return view($this->_config['view'], [
'cartRule' => $copiedCartRule,
]);
}
2019-12-21 05:25:32 +00:00
/**
* Store a newly created resource in storage.
*
* @return \Illuminate\Http\Response
*/
public function store()
{
2020-10-21 15:41:28 +00:00
try {
$this->validate(request(), [
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'coupon_type' => 'required',
'use_auto_generation' => 'required_if:coupon_type,==,1',
'coupon_code' => 'required_if:use_auto_generation,==,0|unique:cart_rule_coupons,code',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric',
]);
$data = request()->all();
2019-12-21 05:25:32 +00:00
2022-01-11 05:36:21 +00:00
$this->cartRuleRepository->create($data);
2019-12-21 05:25:32 +00:00
2020-10-21 15:41:28 +00:00
session()->flash('success', trans('admin::app.response.create-success', ['name' => 'Cart Rule']));
2019-12-21 05:25:32 +00:00
2020-10-21 15:41:28 +00:00
return redirect()->route($this->_config['redirect']);
} catch (ValidationException $e) {
if ($firstError = collect($e->errors())->first()) {
session()->flash('error', $firstError[0]);
}
}
return redirect()->back();
2019-12-21 05:25:32 +00:00
}
/**
* Show the form for editing the specified resource.
*
2022-01-11 05:36:21 +00:00
* @param int $id
2019-12-21 05:25:32 +00:00
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$cartRule = $this->cartRuleRepository->findOrFail($id);
return view($this->_config['view'], compact('cartRule'));
}
/**
* Update the specified resource in storage.
*
2022-01-11 05:36:21 +00:00
* @param \Illuminate\Http\Request $request
* @param int $id
2019-12-21 05:25:32 +00:00
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
2020-10-21 15:41:28 +00:00
try {
$this->validate(request(), [
'name' => 'required',
'channels' => 'required|array|min:1',
'customer_groups' => 'required|array|min:1',
'coupon_type' => 'required',
'use_auto_generation' => 'required_if:coupon_type,==,1',
'starts_from' => 'nullable|date',
'ends_till' => 'nullable|date|after_or_equal:starts_from',
'action_type' => 'required',
'discount_amount' => 'required|numeric',
]);
2019-12-21 05:25:32 +00:00
2020-10-21 15:41:28 +00:00
$cartRule = $this->cartRuleRepository->findOrFail($id);
2019-12-21 05:25:32 +00:00
2021-01-13 11:12:47 +00:00
if ($cartRule->coupon_type) {
2021-01-15 07:57:46 +00:00
if ($cartRule->cart_rule_coupon) {
$this->validate(request(), [
'coupon_code' => 'required_if:use_auto_generation,==,0|unique:cart_rule_coupons,code,' . $cartRule->cart_rule_coupon->id,
]);
} else {
$this->validate(request(), [
'coupon_code' => 'required_if:use_auto_generation,==,0|unique:cart_rule_coupons,code',
]);
}
2021-01-13 11:12:47 +00:00
}
2020-10-21 15:41:28 +00:00
$cartRule = $this->cartRuleRepository->update(request()->all(), $id);
2019-12-21 05:25:32 +00:00
2020-10-21 15:41:28 +00:00
session()->flash('success', trans('admin::app.response.update-success', ['name' => 'Cart Rule']));
return redirect()->route($this->_config['redirect']);
} catch (ValidationException $e) {
if ($firstError = collect($e->errors())->first()) {
session()->flash('error', $firstError[0]);
}
}
2019-12-21 05:25:32 +00:00
2020-10-21 15:41:28 +00:00
return redirect()->back();
2019-12-21 05:25:32 +00:00
}
/**
* Remove the specified resource from storage.
*
2022-01-11 05:36:21 +00:00
* @param int $id
2019-12-21 05:25:32 +00:00
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2022-01-11 05:36:21 +00:00
$this->cartRuleRepository->findOrFail($id);
2019-12-21 05:25:32 +00:00
try {
$this->cartRuleRepository->delete($id);
2022-02-11 12:34:38 +00:00
return response()->json(['message' => trans('admin::app.response.delete-success', ['name' => 'Cart Rule'])]);
} catch (Exception $e) {}
2019-12-21 05:25:32 +00:00
2022-02-11 12:34:38 +00:00
return response()->json(['message' => trans('admin::app.response.delete-failed', ['name' => 'Cart Rule'])], 400);
2019-12-21 05:25:32 +00:00
}
2022-01-11 05:36:21 +00:00
}