<?php


namespace App\Repository;


use App\Models\Purchase;

class PurchaseRepository
{
    /**
     * @var Purchase
     */
    private $model;

    /**
     * PurchaseRepository constructor.
     * @param Purchase $model
     */
    public function __construct(Purchase $model)
    {

        $this->model = $model;
    }

    /**
     * @param string $date
     * @return mixed
     */
    public function findByDay(string $date)
    {
        $branchId = auth()->user()->branch_id;
        $isSuperAdmin = auth()->user()->hasRole('superadmin');
        
        $query = $this->model->where('purchase_date',$date);
        
        if (!$isSuperAdmin && $branchId) {
            $query->where('branch_id', $branchId);
        }
        
        return $query->get();
    }

    public function findByMonth(string $fromDate, string $toDate)
    {
        $branchId = auth()->user()->branch_id;
        $isSuperAdmin = auth()->user()->hasRole('superadmin');
        
        $query = $this->model->whereBetween('purchase_date',[$fromDate, $toDate]);
        
        if (!$isSuperAdmin && $branchId) {
            $query->where('branch_id', $branchId);
        }
        
        return $query->get();
    }
}
