diff --git a/app/Http/Controllers/Campaign/CampaignIndexController.php b/app/Http/Controllers/Campaign/CampaignIndexController.php index 5282e882..6ab0298c 100755 --- a/app/Http/Controllers/Campaign/CampaignIndexController.php +++ b/app/Http/Controllers/Campaign/CampaignIndexController.php @@ -14,7 +14,14 @@ class CampaignIndexController extends MainController public function index(Request $request) { $campaign = new Campaign(); - $data['bootstrap_colors'] = ['text-bg-primary', 'text-bg-secondary', 'text-bg-success', 'text-bg-danger', 'text-bg-warning', 'text-bg-info']; + $data['bootstrap_colors'] = [ + 'text-bg-primary', + 'text-bg-secondary', + 'text-bg-success', + 'text-bg-danger', + 'text-bg-warning', + 'text-bg-info', + ]; $data['campaigns'] = $campaign->getAllByCompany(Auth::user()->company_id); return view('campaign.index', $data); diff --git a/app/Http/Controllers/Customer/CustomerImportSaveController.php b/app/Http/Controllers/Customer/CustomerImportSaveController.php index 0b1e10aa..4eaf4d71 100755 --- a/app/Http/Controllers/Customer/CustomerImportSaveController.php +++ b/app/Http/Controllers/Customer/CustomerImportSaveController.php @@ -18,29 +18,27 @@ class CustomerImportSaveController extends MainController */ public function save(Request $request) { - $errors = []; - if (! $request->hasFile('upload')) { - return redirect('/customer')->withErrors(__("Upload file can't be in blank")); - } + $validatedData = $request->validate([ + 'upload' => ['required', 'file', 'mimes:csv,txt'], + ], [ + 'upload.mimes' => __('The file uploaded must be a CSV.'), + ]); + $errors = []; $file = $request->file('upload'); - $extension = $file->getClientOriginalExtension(); - - //if($extension != 'csv') - // return redirect('/customer')->withErrors(__("File upload only accept .csv")); - $filePath = $file->getPath().DIRECTORY_SEPARATOR.$file->getFilename(); try { $handle = fopen($filePath, 'r'); } catch (\Throwable $t) { - return redirect('/customer')->withErrors(__("Can't read uploaded file")); + return redirect('/customer/import')->withErrors(__("Can't read uploaded file")); } // HEADER (25) //external_id;name;business_name;vat;dob;phone;phone2;mobile;email;email2;website;country_id;province;city;locality;street;zipcode;notes;facebook;instagram;linkedin;twitter;youtube;tiktok;tags $rowCount = 0; - $separator = (! empty($request->separator)) ? $request->separator : ';'; + $successfulImports = 0; + $separator = $request->input('separator', ';'); while (($data = fgetcsv($handle, 1000, $separator)) !== false) { //Skip header starting in 1 if ($data[0] == 'external_id') { @@ -52,63 +50,73 @@ public function save(Request $request) continue; } - $country = trim($data[11]); - $customer = new Customer(); + $customer = $this->mapCsvRowToCustomer($data); - $customer->company_id = Auth::user()->company_id; - - $customer->external_id = isset($data[0]) ? (int) $data[0] : null; - $customer->name = $data[1]; - $customer->business_name = $data[2]; - $customer->vat = $data[3]; - try { - $dob = isset($data[4]) ? Carbon::createFromFormat('d/m/Y', $data[4])->format('Y-m-d') : null; - } catch (\Throwable $t) { - $dob = null; - } - $customer->dob = $dob; - $customer->phone = str_replace([' ', '(', ')', '.', '-', '/', '|'], '', $data[5]); - $customer->phone2 = str_replace([' ', '(', ')', '.', '-', '/', '|'], '', $data[6]); - $customer->mobile = str_replace([' ', '(', ')', '.', '-', '/', '|'], '', $data[7]); - $customer->email = $data[8]; - $customer->email2 = $data[9]; - $customer->website = rtrim($data[10], '/'); - $customer->country_id = strlen($country) == 2 ? strtolower($country) : ''; - $customer->province = $data[12]; - $customer->city = $data[13]; - $customer->locality = $data[14]; - $customer->street = $data[15]; - $customer->zipcode = $data[16]; - $customer->notes = $data[17]; - $customer->facebook = (isset($data[18])) ? $data[18] : null; - $customer->instagram = (isset($data[19])) ? $data[19] : null; - $customer->linkedin = (isset($data[20])) ? $data[20] : null; - $customer->twitter = (isset($data[21])) ? $data[21] : null; - $customer->youtube = (isset($data[22])) ? $data[22] : null; - $customer->tiktok = (isset($data[23])) ? $data[23] : null; - //@todo improve tags import should have a json format beetween braces[]? - $customer->tags = (isset($data[24])) ? $data[24] : null; - - $customer->seller_id = Auth::user()->id; - $customer->created_at = now(); try { $customer->save(); - $rowCount++; + $successfulImports++; } catch (\Throwable $t) { Log::error($t->getMessage().' | row number:'.($rowCount + 1)); - $errors[] = $rowCount; + $errors[] = __('Error in row :row. Message: :message', ['row' => $rowCount + 1, 'message' => $t->getMessage()]); } + $rowCount++; } fclose($handle); - $status = ($rowCount > 0) ? 'success' : 'error'; + $status = $successfulImports > 0 ? 'success' : 'error'; + $message = $status === 'success' ? __('Customers imported successfully, with :count errors.', ['count' => count($errors)]) : __('An error occurred while importing customers.'); $response = [ 'status' => $status, - 'message' => ($status) ? 'Customers imported :count successfully' : 'An error occurred while importing customers '.$errors, + 'message' => $message, 'count' => $rowCount, ]; return redirect('/customer')->with($response); } + + private function mapCsvRowToCustomer(array $data): Customer + { + $country = trim($data[11]); + $customer = new Customer(); + + $customer->company_id = Auth::user()->company_id; + $customer->external_id = isset($data[0]) ? (int) $data[0] : null; + $customer->name = $data[1]; + $customer->business_name = $data[2]; + $customer->vat = $data[3]; + try { + $dob = isset($data[4]) ? Carbon::createFromFormat('d/m/Y', $data[4])->format('Y-m-d') : null; + } catch (\Throwable $t) { + $dob = null; + } + $customer->dob = $dob; + $customer->phone = str_replace([' ', '(', ')', '.', '-', '/', '|'], '', $data[5]); + $customer->phone2 = str_replace([' ', '(', ')', '.', '-', '/', '|'], '', $data[6]); + $customer->mobile = str_replace([' ', '(', ')', '.', '-', '/', '|'], '', $data[7]); + $customer->email = $data[8]; + $customer->email2 = $data[9]; + $customer->website = rtrim($data[10], '/'); + $customer->country_id = strlen($country) == 2 ? strtolower($country) : ''; + $customer->province = $data[12]; + $customer->city = $data[13]; + $customer->locality = $data[14]; + $customer->street = $data[15]; + $customer->zipcode = $data[16]; + $customer->notes = $data[17]; + $customer->facebook = (isset($data[18])) ? $data[18] : null; + $customer->instagram = (isset($data[19])) ? $data[19] : null; + $customer->linkedin = (isset($data[20])) ? $data[20] : null; + $customer->twitter = (isset($data[21])) ? $data[21] : null; + $customer->youtube = (isset($data[22])) ? $data[22] : null; + $customer->tiktok = (isset($data[23])) ? $data[23] : null; + if (isset($data[24]) && json_validate($data[24])) { + $customer->tags = $data[24]; + } + + $customer->seller_id = Auth::user()->id; + $customer->created_at = now(); + + return $customer; + } } diff --git a/resources/views/supplier/index.blade.php b/resources/views/supplier/index.blade.php index 8809c6c6..72aa1913 100755 --- a/resources/views/supplier/index.blade.php +++ b/resources/views/supplier/index.blade.php @@ -11,6 +11,7 @@
{{ __('Name') }} | diff --git a/version.php b/version.php index ece3d8c3..4ee97f40 100755 --- a/version.php +++ b/version.php @@ -1,3 +1,3 @@
---|