diff --git a/app/Classes/Modules/Exports/Services/ExportsAllCustomersInfoForLarkSystem.php b/app/Classes/Modules/Exports/Services/ExportsAllCustomersInfoForLarkSystem.php
index 40294a21..821ea70c 100644
--- a/app/Classes/Modules/Exports/Services/ExportsAllCustomersInfoForLarkSystem.php
+++ b/app/Classes/Modules/Exports/Services/ExportsAllCustomersInfoForLarkSystem.php
@@ -2,19 +2,27 @@
namespace App\Classes\Modules\Exports\Services;
-use App\Models\Company;
+use App\Models\CompanyConnection;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
-use App\Classes\General\Eloquent\ApplyFiltersToQuery;
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\Log;
class ExportsAllCustomersInfoForLarkSystem implements FromQuery, WithMapping, WithHeadings, ShouldAutoSize
{
use Exportable;
- public function __construct() {}
+ protected $deduplicateMarking;
+ protected $request;
+
+ public function __construct(Request $request, $deduplicateMarking = true)
+ {
+ $this->deduplicateMarking = $deduplicateMarking;
+ $this->request = $request;
+ }
public function headings(): array
{
@@ -33,48 +41,44 @@ class ExportsAllCustomersInfoForLarkSystem implements FromQuery, WithMapping, Wi
public function query()
{
- return (new ApplyFiltersToQuery())->execute(
- Company::query()->with([
- 'companyModules.employees',
- 'companyModules.connections.segments',
- 'contacts'
- ]),
- ['has_business_module_type' => 1]
- );
+ $baseQuery = CompanyConnection::has('invitee.company');
+
+ if ($this->deduplicateMarking) {
+ $baseQuery->whereIn('id', function ($sub) {
+ $sub->selectRaw('min(id)')
+ ->from('company_connections')
+ ->groupBy('invitee_reference');
+ });
+ }
+
+ return $baseQuery->with([
+ 'invitee.company.contacts',
+ 'invitee.company.companyModules.employees',
+ 'segments'
+ ]);
}
- public function map($company): array
+ public function map($connection): array
{
- $companyModule = $company->companyModules->first();
- $marking = $companyModule ? $companyModule->getMarking() : '';
- $creditTermCustomer = '';
- if ($companyModule && $companyModule->connections->first()) {
- $creditTermCustomer = $companyModule->connections->first()->is_credit_term ? 'YES' : 'NO';
- }
+ $company = optional(optional($connection)->invitee)->company;
+ $companyModule = optional($company)->companyModules->first();
+ $employee = optional($companyModule)->employees->first();
+ $contact = optional($company)->contacts->first();
- $employeeEmail = '';
- if ($companyModule && $companyModule->employees->first()) {
- $employeeEmail = $companyModule->employees->first()->email;
- }
-
- $contactPhone = '';
- if ($company->contacts->first()) {
- $contactPhone = $company->contacts->first()->phone;
- }
-
- $segments = '';
- if ($companyModule && $companyModule->connections->first()) {
- $segments = $companyModule->connections->first()->segments->pluck('name')->implode(', ');
- }
+ $marking = $connection->invitee_reference;
+ $creditTermCustomer = $connection->is_credit_term ? 'YES' : 'NO';
+ $employeeEmail = optional($employee)->email ?? '';
+ $contactPhone = optional($contact)->phone ?? '';
+ $segments = optional($connection->segments)->pluck('name')->implode(', ') ?: '';
return [
$marking,
- $company->name,
+ optional($company)->name ?? '',
$contactPhone,
$employeeEmail,
- $company->created_at ? $company->created_at->toDateString() : '',
- $company->updated_at ? $company->updated_at->toDateString() : '',
- $company->debtor,
+ optional($company)->created_at ? $company->created_at->toDateString() : '',
+ optional($company)->updated_at ? $company->updated_at->toDateString() : '',
+ optional($company)->debtor ?? '',
$creditTermCustomer,
$segments,
];
diff --git a/resources/views/partials/menu.blade.php b/resources/views/partials/menu.blade.php
index 1e502ae6..cd189817 100644
--- a/resources/views/partials/menu.blade.php
+++ b/resources/views/partials/menu.blade.php
@@ -163,6 +163,17 @@
Payment And Billing
+