From 44ca6f86d6e3799dc9bd3ef2f96d0539e6ae8a56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Mon, 25 Oct 2021 17:37:31 +0200 Subject: [PATCH] Ticket 375 : Fix duplicate Export CRM for newsletter --- app/Http/Controllers/CustomerController.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/Http/Controllers/CustomerController.php b/app/Http/Controllers/CustomerController.php index ef34a83..7ae92c5 100644 --- a/app/Http/Controllers/CustomerController.php +++ b/app/Http/Controllers/CustomerController.php @@ -9,6 +9,7 @@ use App\Models\Customer; use App\Models\CustomerInfo; use App\Models\CustomerLogin; use App\Models\SingleOptin; +use Exception; use Illuminate\Database\Query\JoinClause; use Illuminate\Http\Request; use Illuminate\Support\Carbon; @@ -179,11 +180,11 @@ class CustomerController extends Controller $continue = true; // continue case CustomerExportType::customer(): - $i = 0; + $page = 1; /** @var \Illuminate\Database\Eloquent\Builder $req */ $req = CustomerInfo::where('brand_id', $request->brand); $total = $req->count(); - $pages = $total / $pagination; + $pages = \ceil($total / $pagination); do { /** @var \Illuminate\Pagination\LengthAwarePaginator $items */ $items = $req->with('customer', function ($q) { @@ -192,7 +193,7 @@ class CustomerController extends Controller $pagination, ['lastname', 'firstname', 'hashcode', 'customer_id'], 'page', - $i + $page ); $items->map(function (CustomerInfo $customerInfo) use ($w) { $w->insertOne([ @@ -202,7 +203,7 @@ class CustomerController extends Controller 'hashcode' => $customerInfo->hashcode ]); }); - } while ($i++ <= $pages); + } while ($page++ <= $pages); if (!isset($continue)) { break; } @@ -218,6 +219,8 @@ class CustomerController extends Controller ]); }); break; + default: + throw new Exception(sprintf('Unhandled CustomerExportType %s', $exportType->label)); } //we create a callable to output the CSV in chunk