Files
2022-07-04 10:57:30 +08:00

69 lines
2.1 KiB
PHP

<?php
namespace App\Classes\Modules\Exports\Services;
use App\Classes\ValueObjects\Constants\BusinessType;
use App\Classes\ValueObjects\Constants\CompanyType;
use App\Models\Company;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromQuery;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class ExportsCustomers implements FromQuery, WithHeadings, WithHeadingRow, WithMapping, ShouldAutoSize
{
use Exportable;
public function headings(): array
{
return [
'Marking',
'Company Name',
'Email',
'Account Name',
'Phone Number',
'Business type',
'Number of bookings',
'Last booking Date',
'Bookings total',
'Registration Date',
'System Registration'
];
}
/**
* @return \Illuminate\Support\Collection|mixed
*/
public function query()
{
return Company::where('business_type', '=', 2);
}
/**
* @param Company $company
*
* @return array
*/
public function map($company): array
{
$employee = $company->employees()->first();
$lastBooking = $company->bookings()->orderByDesc('id')->first();
return [
$company->reference,
$company->name,
$employee ? $employee->email : '',
$employee ? $employee->name : '',
$company->contacts()->first() ? $company->contacts()->first()->phone : '',
$company->type === CompanyType::COMPANY_BUSINESS ? 'Company' : 'individual',
count($company->bookings),
$lastBooking ? $lastBooking->created_at : '',
$company->bookings()->sum('fix_amount'),
\PhpOffice\PhpSpreadsheet\Shared\Date::dateTimeToExcel($company->created_at),
count($company->segments()->where('segment_id', '=', 5)->get()) ? 'Exchange 1.0' : 'Exchange 2.0'
];
}
}