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

300 lines
10 KiB
PHP
Raw Normal View History

<?php
2018-10-11 14:25:59 +00:00
namespace Webkul\Tax\Http\Controllers;
2018-12-21 12:48:34 +00:00
use Illuminate\Support\Facades\Event;
2019-07-01 11:33:36 +00:00
use Webkul\Tax\Repositories\TaxRateRepository;
2019-02-08 11:23:48 +00:00
use Webkul\Admin\Imports\DataGridImport;
use Illuminate\Support\Facades\Validator;
use Excel;
2019-02-21 13:10:43 +00:00
use Maatwebsite\Excel\Validators\Failure;
/**
* 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;
/**
2019-07-01 11:33:36 +00:00
* TaxRateRepository object
*
2019-07-01 11:33:36 +00:00
* @var Object
*/
2019-07-01 11:33:36 +00:00
protected $taxRateRepository;
/**
* Create a new controller instance.
*
2019-07-01 11:33:36 +00:00
* @param \Webkul\Tax\Repositories\TaxRateRepository $taxRateRepository
* @return void
*/
2019-07-01 11:33:36 +00:00
public function __construct(TaxRateRepository $taxRateRepository)
{
2019-07-01 11:33:36 +00:00
$this->taxRateRepository = $taxRateRepository;
$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',
2019-03-31 17:26:30 +00:00
'tax_rate' => 'required|numeric|min:0.0001'
]);
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');
2019-07-01 11:33:36 +00:00
$taxRate = $this->taxRateRepository->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)
{
2019-07-01 11:33:36 +00:00
$taxRate = $this->taxRateRepository->findOrFail($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',
2019-03-31 17:26:30 +00:00
'tax_rate' => 'required|numeric|min:0.0001'
]);
2018-12-21 12:48:34 +00:00
Event::fire('tax.tax_rate.update.before', $id);
2019-07-01 11:33:36 +00:00
$taxRate = $this->taxRateRepository->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-07-01 11:33:36 +00:00
$taxRate = $this->taxRateRepository->findOrFail($id);
2018-12-21 08:48:59 +00:00
2019-04-09 01:01:52 +00:00
try {
Event::fire('tax.tax_rate.delete.before', $id);
2018-12-21 08:48:59 +00:00
2019-07-01 11:33:36 +00:00
$this->taxRateRepository->delete($id);
2018-10-18 05:03:00 +00:00
2019-04-09 01:01:52 +00:00
Event::fire('tax.tax_rate.delete.after', $id);
2018-12-21 08:48:59 +00:00
2019-04-09 01:01:52 +00:00
session()->flash('success', trans('admin::app.response.delete-success', ['name' => 'Tax Rate']));
2018-12-21 13:05:23 +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' => 'Tax Rate']));
}
2018-10-18 05:03:00 +00:00
return response()->json(['message' => false], 400);
}
2019-02-08 11:23:48 +00:00
/**
* import function for the upload
*
* @return \Illuminate\Http\Response
*/
public function import() {
2019-02-11 11:45:22 +00:00
$valid_extension = ['xlsx', 'csv', 'xls'];
2019-02-08 11:23:48 +00:00
2019-07-01 11:33:36 +00:00
if (! in_array(request()->file('file')->getClientOriginalExtension(), $valid_extension)) {
2019-02-11 11:45:22 +00:00
session()->flash('error', trans('admin::app.export.upload-error'));
2019-02-08 11:54:48 +00:00
} else {
2019-02-21 13:10:43 +00:00
try {
$excelData = (new DataGridImport)->toArray(request()->file('file'));
foreach ($excelData as $data) {
foreach ($data as $column => $uploadData) {
2019-03-28 07:20:00 +00:00
if (!is_null($uploadData['zip_from']) && !is_null($uploadData['zip_to'])) {
$uploadData['is_zip'] = 1;
}
2019-02-21 13:10:43 +00:00
$validator = Validator::make($uploadData, [
'identifier' => 'required|string',
'state' => 'required|string',
'country' => 'required|string',
2019-04-01 11:47:26 +00:00
'tax_rate' => 'required|numeric|min:0.0001',
2019-03-28 07:20:00 +00:00
'is_zip' => 'sometimes',
'zip_code' => 'sometimes|required_without:is_zip',
'zip_from' => 'nullable|required_with:is_zip',
'zip_to' => 'nullable|required_with:is_zip,zip_from',
2019-02-21 13:10:43 +00:00
]);
if ($validator->fails()) {
$failedRules[$column+1] = $validator->errors();
}
2019-02-08 11:23:48 +00:00
2019-02-21 13:10:43 +00:00
$identiFier[$column+1] = $uploadData['identifier'];
}
2019-02-08 11:23:48 +00:00
2019-02-21 13:10:43 +00:00
$identiFierCount = array_count_values($identiFier);
2019-02-08 11:23:48 +00:00
2019-02-21 13:10:43 +00:00
$filtered = array_filter($identiFier, function ($value) use ($identiFierCount) {
return $identiFierCount[$value] > 1;
});
2019-02-08 11:23:48 +00:00
}
2019-02-21 13:10:43 +00:00
if ($filtered) {
foreach ($filtered as $position => $identifier) {
$message[] = trans('admin::app.export.duplicate-error', ['identifier' => $identifier, 'position' => $position]);
2019-02-08 11:54:48 +00:00
}
2019-07-01 11:33:36 +00:00
2019-02-08 11:54:48 +00:00
$finalMsg = implode(" ", $message);
2019-02-08 11:23:48 +00:00
2019-02-08 11:54:48 +00:00
session()->flash('error', $finalMsg);
} else {
2019-02-21 13:10:43 +00:00
$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');
2019-03-28 07:20:00 +00:00
} else if ($fail->first('zip_code')) {
$errorMsg[$coulmn] = $fail->first('zip_code');
} else if ($fail->first('zip_from')) {
$errorMsg[$coulmn] = $fail->first('zip_from');
} else if ($fail->first('zip_to')) {
$errorMsg[$coulmn] = $fail->first('zip_to');
2019-02-21 13:10:43 +00:00
}
}
2019-02-08 11:54:48 +00:00
2019-02-21 13:10:43 +00:00
foreach ($errorMsg as $key => $msg) {
$msg = str_replace(".", "", $msg);
$message[] = $msg. ' at Row ' .$key . '.';
}
$finalMsg = implode(" ", $message);
session()->flash('error', $finalMsg);
} else {
2019-07-01 11:33:36 +00:00
$taxRate = $this->taxRateRepository->get()->toArray();
2019-02-08 11:54:48 +00:00
2019-02-21 13:10:43 +00:00
foreach ($taxRate as $rate) {
$rateIdentifier[$rate['id']] = $rate['identifier'];
}
foreach ($excelData as $data) {
foreach ($data as $column => $uploadData) {
2019-03-28 07:20:00 +00:00
if (!is_null($uploadData['zip_from']) && !is_null($uploadData['zip_to'])) {
$uploadData['is_zip'] = 1;
$uploadData['zip_code'] = NULL;
}
2019-02-21 13:10:43 +00:00
if (isset($rateIdentifier)) {
$id = array_search($uploadData['identifier'], $rateIdentifier);
if ($id) {
2019-07-01 11:33:36 +00:00
$this->taxRateRepository->update($uploadData, $id);
2019-02-21 13:10:43 +00:00
} else {
2019-07-01 11:33:36 +00:00
$this->taxRateRepository->create($uploadData);
2019-02-21 13:10:43 +00:00
}
2019-02-08 11:54:48 +00:00
} else {
2019-07-01 11:33:36 +00:00
$this->taxRateRepository->create($uploadData);
2019-02-08 11:54:48 +00:00
}
2019-02-08 11:23:48 +00:00
}
}
2019-02-21 13:10:43 +00:00
session()->flash('success', trans('admin::app.response.upload-success', ['name' => 'Tax Rate']));
}
2019-02-08 11:54:48 +00:00
}
2019-02-21 13:10:43 +00:00
} catch (\Exception $e) {
$failure = new Failure(1, 'rows', [0 => trans('admin::app.export.enough-row-error')]);
session()->flash('error', $failure->errors()[0]);
2019-02-08 11:23:48 +00:00
}
}
return redirect()->route($this->_config['redirect']);
}
2019-04-24 05:18:12 +00:00
}