mirror of
https://gitlab.com/CIEFWorldwideSdnBhd/exchange-2.0.git
synced 2026-08-21 05:23:58 +00:00
72 lines
2.2 KiB
PHP
72 lines
2.2 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 Carbon\Carbon;
|
|
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)
|
|
->whereBetween('created_at', [Carbon::now()->subMonths(12), Carbon::now()]); //Limit, 'expensive' query;
|
|
}
|
|
|
|
/**
|
|
* @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'
|
|
];
|
|
}
|
|
}
|