File: /mnt/data/ghayatcom/ghayatcom-api/app/Exports/AdminDependantExport.php
<?php
namespace App\Exports;
use Auth;
use App\User;
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 AdminDependantExport 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;
$pageNumber = $this->request->page ? $this->request->page : 1;
$search_key = $this->request->search ? $this->request->search : '';
$from_date = $this->request->from_date ? $this->request->from_date : '';
$to_date = $this->request->to_date ? $this->request->to_date : '';
$status = trim($this->request->status,',');
$statusArr = explode(',', $status);
$sort_by_col = (isset($this->request->sort_by)) ? $this->request->sort_by : 'id';
$data_order_by = $this->request->order_by;
/** @var string $data_order_by */
$orderBy = $data_order_by ? strtoupper($data_order_by) : strtoupper('asc');
$list = User::withCount(['dependantAccountHolder'])->where('type','dependant');
if(!empty($from_date)) {
$from = Carbon::parse($from_date)->format('Y-m-d');
$list = $list->whereDate('created_at','>=',$from);
}
if(!empty($to_date)) {
$to = Carbon::parse($to_date)->format('Y-m-d');
$list = $list->whereDate('created_at','>=',$to);
}
if(!empty($statusArr)) {
$list->whereIn('status', $statusArr);
}
if(!empty($search_key)) {
$list = $list->where(function($q) use($search_key) {
$q->where('first_name', 'LIKE', "%{$search_key}%");
$q->orWhere('last_name', 'LIKE', "%{$search_key}%");
$q->orWhere('passport_number', 'LIKE', "%{$search_key}%");
});
}
if($authRole->hasRole(['clinic_admin'])) {
$clinic_id = $authRole->clinicDetail->id;
$list = $list->whereHas('myPatientAppointmentList.doctorDetails.userDetail.UserClinic', function($q) use($clinic_id) {
$q->where('clinic_id', $clinic_id);
});
}
if($sort_by_col) {
/**
* @var string $sort_by_col
* @var string $orderBy
*/
$list = $list->orderBy($sort_by_col, $orderBy);
}
return $list->get();
}
function map($dependant): array
{
/**
* @var object $dependant
*/
$data = $dependant->toArray();
print_r($data);die;
if($data['status'] == '0')
{
$status = 'onboard';
}
else if($data['status'] == '1')
{
$status = 'waiting for approval';
}
else if($data['status'] == '2')
{
$status = 'enabled';
}
else if($data['status'] == '-1')
{
$status = 'disabled';
}
else
{
$status = 'rejected';
}
return [
$data['first_name'].' '.$data['last_name'],
$data['passport_number'],
$data['dependant_account_holder_count'],
$status,
];
}
function headings(): array
{
return [
'Name',
'ID No',
'No of linked accounts',
'Account Status',
];
}
function columnWidths(): array
{
return [
'A' => 14,
'B' => 18,
'C' => 24,
'D' => 14,
];
}
function styles(Worksheet $sheet)
{
return [];
}
}