HEX
Server: nginx/1.24.0
System: Linux DGT-WORDPRESS-VM-SERVER 6.14.0-1017-azure #17~24.04.1-Ubuntu SMP Mon Dec 1 20:10:50 UTC 2025 x86_64
User: ubuntu (1000)
PHP: 8.4.12
Disabled: NONE
Upload Files
File: /mnt/data/ghayatcom/ghayatcom-api/app/Exports/DoctorBillingExport.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 DoctorBillingExport implements FromCollection,WithMapping,WithHeadings,WithStyles,WithColumnWidths
{
    protected $request;

    function __construct($request) {
            $this->request = $request;
    }

    /**
    * @return \Illuminate\Support\Collection
    */
    public function collection()
    {
        $authRole = Auth::user();
        $auth_id = auth()->user()->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')->where('doctor_id',$auth_id)->where(function($q) use($consultation_type){
            $q->whereIn('consultation_type',$consultation_type);
        });

        if(!empty($this->request->type))
        {
            if($this->request->type == 'this_week' || $this->request->type == 'this_month')
            {
                $checkDate = Carbon::now()->subMonth()->format('Y-m-d');
                if($this->request->type == 'this_week') {
                    $checkDate = Carbon::now()->subDays(7)->format('Y-m-d');
                }
                $list = $list->whereHas('paymentDetails',function($q) use($checkDate){
                    $q->whereDate('created_at','>=',$checkDate);
                });
            }
            if($this->request->type == 'last_week' || $this->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($this->request->type == 'last_week') {
                    $startDate = Carbon::now()->subDays(14)->format('Y-m-d');
                    $endDate = Carbon::now()->subDays(7)->format('Y-m-d');
                }
                $list = $list->whereHas('paymentDetails',function($q) use($startDate,$endDate){
                    // $q->whereBetween('Date(created_at)',[$startDate,$endDate]);
                    $q->whereDate('created_at','>=',$startDate);
                    $q->whereDate('created_at','<=',$endDate);
                });
            }
            if($this->request->type == 'this_year' || $this->request->type == 'last_year')
            {
                /** @var string|null $year */
                $year = Carbon::now()->format('Y');
                if($this->request->type == 'last_year') {
                    $year = Carbon::now()->subYear()->format('Y');
                }
                $list = $list->whereHas('paymentDetails',function($q) use($year){
                    $q->whereYear('created_at',$year);
                });
            }
        }

        if(!empty($this->request->start_date) && !empty($this->request->end_date))
        {
            $startDate = Carbon::parse($this->request->start_date)->format('Y-m-d');
            $endDate = Carbon::parse($this->request->end_date)->format('Y-m-d');
            $list = $list->whereHas('paymentDetails',function($q) use($startDate,$endDate){
                $q->whereDate('created_at','>=',$startDate);
                $q->whereDate('created_at','<=',$endDate);
            });
        }
        $list = $list->orderBy($sort_by,$orderBy);
        return $list->get();
    }

    function map($appointment): array
    {
        /**
         * @var object $appointment
         */
        $data = $appointment->toArray();

        $consultation_fee = !empty($data['payment_details']['consultation_fee']) ? $data['payment_details']['consultation_fee'] : 0;
        $voucher = !empty($data['payment_details']['voucher_code']) ? $data['payment_details']['voucher_code'] : 0;
        $discount = !empty($data['payment_details']['discount']) ? $data['payment_details']['discount'].'%' : 0;
        $total_amount = !empty($data['payment_details']['total_amount']) ? $data['payment_details']['total_amount'] : 0;

        $total_amount = $total_amount - ($voucher + $discount);

        return [
            Carbon::parse($data['appointment_start_dt'])->format('d/m/Y'),
            '#'.$data['consultation_id'],
            $data['consultation_type_str'],
            $consultation_fee,
            $voucher,
            $discount,
            Carbon::parse($data['payment_details']['created_at'])->format('d/m/Y'),
            $data['price_detail']['currency_code'].' '.$total_amount,
        ];
    }

    function headings(): array
    {
        return [
            'Date',
            'Consultation ID',
            'Consultation type',
            'Consultation fee',
            'Voucher',
            'Discount',
            'Paid on',
            'Total Amount',
        ];
    }

    function columnWidths(): array
    {
        return [
            'A' => 14,
            'B' => 18,
            'C' => 24,
            'D' => 24,
            'E' => 12,
            'F' => 12,
            'G' => 14,
            'H' => 14,
        ];
    }

    function styles(Worksheet $sheet)
    {
        return [
            // Style the first row as bold text.
            1    => ['font' => ['bold' => true,'size' => 12]],
            // Styling an entire column.
            'B'  => ['font' => ['bold' => true]],
            'H'  => ['font' => ['bold' => true]],
        ];
    }
}