From 2da9f80bea1815f6de9635688adeb8138cc6f0cb Mon Sep 17 00:00:00 2001 From: rahul shukla Date: Thu, 21 Feb 2019 18:40:43 +0530 Subject: [PATCH] issues #586 --- .../Admin/src/Resources/lang/en/app.php | 3 +- .../Http/Controllers/TaxRateController.php | 129 +++++++++--------- 2 files changed, 70 insertions(+), 62 deletions(-) diff --git a/packages/Webkul/Admin/src/Resources/lang/en/app.php b/packages/Webkul/Admin/src/Resources/lang/en/app.php index f73d76faf..7d212e34d 100755 --- a/packages/Webkul/Admin/src/Resources/lang/en/app.php +++ b/packages/Webkul/Admin/src/Resources/lang/en/app.php @@ -771,7 +771,8 @@ return [ 'xls' => 'XLS', 'file' => 'File', 'upload-error' => 'The file must be a file of type: xls, xlsx, csv.', - 'duplicate-error' => 'Identifier must be unique, duplicate identifier :identifier at row :position.' + 'duplicate-error' => 'Identifier must be unique, duplicate identifier :identifier at row :position.', + 'enough-row-error' => 'file has not enough rows' ], 'response' => [ diff --git a/packages/Webkul/Tax/src/Http/Controllers/TaxRateController.php b/packages/Webkul/Tax/src/Http/Controllers/TaxRateController.php index 85d1271d7..6b661771d 100755 --- a/packages/Webkul/Tax/src/Http/Controllers/TaxRateController.php +++ b/packages/Webkul/Tax/src/Http/Controllers/TaxRateController.php @@ -9,6 +9,7 @@ use Webkul\Tax\Repositories\TaxRateRepository as TaxRate; use Webkul\Admin\Imports\DataGridImport; use Illuminate\Support\Facades\Validator; use Excel; +use Maatwebsite\Excel\Validators\Failure; /** * Tax controller @@ -183,86 +184,92 @@ class TaxRateController extends Controller if (!in_array(request()->file('file')->getClientOriginalExtension(), $valid_extension)) { session()->flash('error', trans('admin::app.export.upload-error')); } else { - $excelData = (new DataGridImport)->toArray(request()->file('file')); + try { + $excelData = (new DataGridImport)->toArray(request()->file('file')); - 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' - ]); + 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[] = trans('admin::app.export.duplicate-error', ['identifier' => $identifier, 'position' => $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'); + if ($validator->fails()) { + $failedRules[$column+1] = $validator->errors(); } + + $identiFier[$column+1] = $uploadData['identifier']; } - foreach ($errorMsg as $key => $msg) { - $msg = str_replace(".", "", $msg); - $message[] = $msg. ' at Row ' .$key . '.'; - } + $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[] = trans('admin::app.export.duplicate-error', ['identifier' => $identifier, 'position' => $position]); + } $finalMsg = implode(" ", $message); session()->flash('error', $finalMsg); } else { - $taxRate = $this->taxRate->get()->toArray(); + $errorMsg = []; - foreach ($taxRate as $rate) { - $rateIdentifier[$rate['id']] = $rate['identifier']; - } + 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 ($excelData as $data) { - foreach ($data as $column => $uploadData) { - if (isset($rateIdentifier)) { - $id = array_search($uploadData['identifier'], $rateIdentifier); - if ($id) { - $this->taxRate->update($uploadData, $id); + 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); } - } else { - $this->taxRate->create($uploadData); } } - } - session()->flash('success', trans('admin::app.response.upload-success', ['name' => 'Tax Rate'])); + session()->flash('success', trans('admin::app.response.upload-success', ['name' => 'Tax Rate'])); + } } + } catch (\Exception $e) { + $failure = new Failure(1, 'rows', [0 => trans('admin::app.export.enough-row-error')]); + + session()->flash('error', $failure->errors()[0]); } }