Skip to content

Commit

Permalink
fix(global): Fix code quality style
Browse files Browse the repository at this point in the history
  • Loading branch information
gnovaro committed Feb 9, 2024
1 parent 1b08cec commit 5723044
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 55 deletions.
9 changes: 8 additions & 1 deletion app/Http/Controllers/Campaign/CampaignIndexController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
114 changes: 61 additions & 53 deletions app/Http/Controllers/Customer/CustomerImportSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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;
}
}
1 change: 1 addition & 0 deletions resources/views/supplier/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<div class="card-body">
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
<caption></caption>
<thead>
<tr>
<th>{{ __('Name') }}</th>
Expand Down
2 changes: 1 addition & 1 deletion version.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

const APP_VERSION = '3.5.3';
const APP_VERSION = '3.5.4';

0 comments on commit 5723044

Please sign in to comment.