sarga/packages/Webkul/Tax/src/Http/Controllers/TaxRateController.php

170 lines
4.1 KiB
PHP
Raw Normal View History

<?php
2018-10-11 14:25:59 +00:00
namespace Webkul\Tax\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
2018-12-21 12:48:34 +00:00
use Illuminate\Support\Facades\Event;
2018-10-15 10:39:09 +00:00
use Webkul\Tax\Repositories\TaxRateRepository as TaxRate;
/**
* Tax controller
*
* @author Prashant Singh <prashant.singh852@webkul.com>
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
*/
class TaxRateController extends Controller
{
/**
* Contains route related configuration
*
* @var array
*/
protected $_config;
/**
* Tax Rate Repository object
*
* @var array
*/
protected $taxRate;
/**
* Create a new controller instance.
*
2018-10-15 10:39:09 +00:00
* @param Webkul\Tax\Repositories\TaxRateRepository $taxRate
* @return void
*/
public function __construct(TaxRate $taxRate)
{
$this->taxRate = $taxRate;
$this->_config = request('_config');
}
/**
* Display a listing
* resource for the
* available tax rates.
*
* @return mixed
*/
public function index() {
return view($this->_config['view']);
}
/**
* Display a create
* form for tax rate
*
* @return view
*/
public function show()
{
return view($this->_config['view']);
}
/**
* Create the tax rate
*
* @return mixed
*/
public function create() {
$this->validate(request(), [
'identifier' => 'required|string|unique:tax_rates,identifier',
2018-10-11 14:25:59 +00:00
'is_zip' => 'sometimes',
2018-10-09 12:04:25 +00:00
'zip_code' => 'sometimes|required_without:is_zip',
'zip_from' => 'nullable|required_with:is_zip',
'zip_to' => 'nullable|required_with:is_zip,zip_from',
'state' => 'required|string',
'country' => 'required|string',
'tax_rate' => 'required|numeric'
]);
2018-10-11 14:25:59 +00:00
$data = request()->all();
2018-11-23 10:52:19 +00:00
2018-10-11 14:25:59 +00:00
if(isset($data['is_zip'])) {
$data['is_zip'] = 1;
unset($data['zip_code']);
}
2018-12-21 12:48:34 +00:00
Event::fire('tax.tax_rate.create.before');
2018-12-21 12:48:34 +00:00
$taxRate = $this->taxRate->create($data);
2018-12-21 12:48:34 +00:00
Event::fire('tax.tax_rate.create.after', $taxRate);
2018-12-21 12:48:34 +00:00
session()->flash('success', trans('admin::app.settings.tax-rates.create-success'));
return redirect()->route($this->_config['redirect']);
}
/**
* Show the edit form
* for the previously
* created tax rates.
*
* @return mixed
*/
public function edit($id)
{
$taxRate = $this->taxRate->find($id);
return view($this->_config['view'])->with('taxRate', $taxRate);
}
/**
* Edit the previous
* tax rate
*
* @return mixed
*/
public function update($id)
{
$this->validate(request(), [
'identifier' => 'required|string|unique:tax_rates,identifier,'.$id,
2018-10-11 14:25:59 +00:00
'is_zip' => 'sometimes',
'zip_from' => 'nullable|required_with:is_zip',
'zip_to' => 'nullable|required_with:is_zip,zip_from',
'state' => 'required|string',
'country' => 'required|string',
'tax_rate' => 'required|numeric'
]);
2018-12-21 12:48:34 +00:00
Event::fire('tax.tax_rate.update.before', $id);
2018-12-21 12:48:34 +00:00
$taxRate = $this->taxRate->update(request()->input(), $id);
2018-12-21 12:48:34 +00:00
Event::fire('tax.tax_rate.update.after', $taxRate);
2018-12-21 12:48:34 +00:00
session()->flash('success', trans('admin::app.settings.tax-rates.update-success'));
return redirect()->route($this->_config['redirect']);
2018-10-18 05:03:00 +00:00
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
2018-12-21 08:48:59 +00:00
// if($this->taxRate->count() == 1) {
// session()->flash('error', trans('admin::app.settings.tax-rates.atleast-one'));
// } else {
2018-10-18 05:03:00 +00:00
2018-12-21 08:48:59 +00:00
// session()->flash('success', trans('admin::app.settings.tax-rates.delete'));
// }
2018-12-21 13:05:23 +00:00
Event::fire('tax.tax_rate.delete.before', $id);
2018-12-21 08:48:59 +00:00
2018-12-21 13:05:23 +00:00
$this->taxRate->delete($id);
Event::fire('tax.tax_rate.delete.after', $id);
2018-10-18 05:03:00 +00:00
return redirect()->back();
}
}