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

264 lines
7.6 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;
2019-02-08 11:23:48 +00:00
use Webkul\Admin\Imports\DataGridImport;
use Illuminate\Support\Facades\Validator;
use Excel;
/**
* 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
2019-01-15 11:54:41 +00:00
if (isset($data['is_zip'])) {
2018-10-11 14:25:59 +00:00
$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
}
2019-02-08 11:23:48 +00:00
/**
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)
{
2019-01-15 11:54:41 +00:00
// if ($this->taxRate->count() == 1) {
2018-12-21 08:48:59 +00:00
// 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();
}
2019-02-08 11:23:48 +00:00
/**
* import function for the upload
*
* @return \Illuminate\Http\Response
*/
public function import() {
$excelData = (new DataGridImport)->toArray(request()->file('tax_rate'));
foreach ($excelData as $data) {
foreach ($data as $column => $uploadData) {
$validator = Validator::make($uploadData, [
'identifier' => 'required|string',
'state' => 'required|string',
'country' => 'required|string',
'tax_rate' => 'required|numeric'
]);
if ($validator->fails()) {
$failedRules[$column+1] = $validator->errors();
}
$identiFier[$column+1] = $uploadData['identifier'];
}
$identiFierCount = array_count_values($identiFier);
$filtered = array_filter($identiFier, function ($value) use ($identiFierCount) {
return $identiFierCount[$value] > 1;
});
}
if ($filtered) {
foreach ($filtered as $position => $identifier) {
$message[] = 'Identifier must be unique, duplicate identifier' . ' ' . $identifier. ' at row' . ' ' . $position. '.';
}
$finalMsg = implode(" ", $message);
session()->flash('error', $finalMsg);
} else {
$errorMsg = [];
if (isset($failedRules)) {
foreach ($failedRules as $coulmn => $fail) {
if ($fail->first('identifier')){
$errorMsg[$coulmn] = $fail->first('identifier');
} else if ($fail->first('tax_rate')) {
$errorMsg[$coulmn] = $fail->first('tax_rate');
} else if ($fail->first('country')) {
$errorMsg[$coulmn] = $fail->first('country');
} else if ($fail->first('state')) {
$errorMsg[$coulmn] = $fail->first('state');
}
}
foreach ($errorMsg as $key => $msg) {
$msg = str_replace(".", "", $msg);
$message[] = $msg. ' at Row ' .$key . '.';
}
$finalMsg = implode(" ", $message);
session()->flash('error', $finalMsg);
} else {
$taxRate = $this->taxRate->get()->toArray();
foreach ($taxRate as $rate) {
$rateIdentifier[$rate['id']] = $rate['identifier'];
}
foreach ($excelData as $data) {
foreach ($data as $column => $uploadData) {
if (isset($rateIdentifier)) {
$id = array_search($uploadData['identifier'], $rateIdentifier);
if ($id) {
$this->taxRate->update($uploadData, $id);
} else {
$this->taxRate->create($uploadData);
}
} else {
$this->taxRate->create($uploadData);
}
}
}
session()->flash('success', trans('admin::app.response.upload-success', ['name' => 'Tax Rate']));
}
}
return redirect()->route($this->_config['redirect']);
}
}