File: /mnt/data/ghayatcom/ghayatcom-api/app/Exports/AdminAppointmentExport.php
<?php
namespace App\Exports;
use Auth;
use App\Appointment;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithStyles;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Maatwebsite\Excel\Concerns\WithColumnWidths;
class AdminAppointmentExport implements FromCollection,WithMapping,WithHeadings,WithStyles,WithColumnWidths
{
protected $request;
function __construct($request) {
$this->request = $request;
}
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
/** @var object $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
$paginate = $this->request->count_per_page ? $this->request->count_per_page : 10;
$orderBy = $this->request->order_by ? $this->request->order_by : 'desc';
$pageNumber = $this->request->page ? $this->request->page : 1;
$offset = ($pageNumber * $paginate) - $paginate; // for pagination
$sort_by = $this->request->sort_by ? $this->request->sort_by : 'id';
$consultation_type = [1,2,3];
if(!empty($this->request->consultation_type))
{
$consultation_type = explode(',',$this->request->consultation_type);
}
$list = Appointment::with(['paymentDetails','appointmentSymptoms','doctorDetails','patientDetails','appointmentReason'])->where(function($q) use($consultation_type){
$q->whereIn('consultation_type',$consultation_type);
});
if(!empty($request->type))
{
if($request->type == 'this_week' || $request->type == 'this_month')
{
$checkDate = Carbon::now()->subMonth()->format('Y-m-d');
if($request->type == 'this_week') {
$checkDate = Carbon::now()->subDays(7)->format('Y-m-d');
}
$list = $list->whereDate('created_at','>=',$checkDate);
}
if($request->type == 'last_week' || $request->type == 'last_month')
{
/** @var string|null $startDate */
$startDate = Carbon::now()->subMonths(2)->format('Y-m-d');
$endDate = Carbon::now()->subMonth()->format('Y-m-d');
if($request->type == 'last_week') {
$startDate = Carbon::now()->subDays(14)->format('Y-m-d');
$endDate = Carbon::now()->subDays(7)->format('Y-m-d');
}
$list = $list->where(function($q) use($startDate,$endDate){
$q->whereDate('created_at','>=',$startDate);
$q->whereDate('created_at','<=',$endDate);
});
}
if($request->type == 'this_year' || $request->type == 'last_year')
{
/** @var string|null $year */
$year = Carbon::now()->format('Y');
if($request->type == 'last_year') {
$year = Carbon::now()->subYear()->format('Y');
}
$list = $list->whereYear('created_at',$year);
}
}
if(!empty($request->start_date) && !empty($request->end_date))
{
$startDate = Carbon::parse($request->start_date)->format('Y-m-d');
$endDate = Carbon::parse($request->end_date)->format('Y-m-d');
$list = $list->whereHas(function($q) use($startDate,$endDate){
$q->whereDate('created_at','>=',$startDate);
$q->whereDate('created_at','<=',$endDate);
});
}
if($authRole->hasRole(['clinic_admin'])) {
$clinic_id = $authRole->clinicDetail->id;
$list = $list->whereHas('doctorDetails.userDetail.UserClinic', function($q) use($clinic_id) {
$q->where('clinic_id', $clinic_id);
});
}
$list = $list->orderBy($sort_by,$orderBy);
return $list->get();
}
function map($appointment): array
{
/**
* @var object $appointment
*/
$data = $appointment->toArray();
$total = 0;
$aggregate = 0;
if(!empty($data['payment_details']['total_amount']))
$total = $data['payment_details']['total_amount'];
if(!empty($data['payment_details']['total_amount']))
$aggregate = $data['payment_details']['platform_fee'] + $data['payment_details']['transaction_fee'] + $data['payment_details']['tax_amount'];
return [
Carbon::parse($data['appointment_start_dt'])->format('d/m/Y').' '.Carbon::parse($data['appointment_start_dt'])->format('H:i A').'-'.Carbon::parse($data['appointment_end_dt'])->format('H:i A'),
$data['doctor_details']['first_name'].$data['doctor_details']['last_name'],
$data['doctor_details']['passport_number'],
$data['price_detail']['currency_code'].' '.$total,
$data['price_detail']['currency_code'].' '.$aggregate,
];
}
function headings(): array
{
return [
'Consultation Date/Time',
'Doctor Full Name',
'ID No',
'Net Consultation Fee',
config('app.name').' Aggregate Fees',
];
}
function columnWidths(): array
{
return [
'A' => 28,
'B' => 16,
'C' => 14,
'D' => 18,
'E' => 18,
];
}
function styles(Worksheet $sheet)
{
return [];
}
}