phpvms/app/Repositories/ExpenseRepository.php

76 lines
1.8 KiB
PHP
Raw Normal View History

<?php
namespace App\Repositories;
use App\Contracts\Repository;
use App\Models\Expense;
use Illuminate\Support\Collection;
use Prettus\Repository\Contracts\CacheableInterface;
use Prettus\Repository\Traits\CacheableRepository;
/**
* Class ExpenseRepository
*/
class ExpenseRepository extends Repository implements CacheableInterface
{
use CacheableRepository;
public function model()
{
return Expense::class;
}
/**
* Get all of the expenses for a given type, and also
* include expenses for a given airline ID
2018-08-27 00:40:04 +08:00
*
* @param $type
* @param int $airline_id
* @param string $ref_model
* @param mixed $ref_model_id
2018-08-27 00:40:04 +08:00
*
* @return Collection
*/
public function getAllForType($type, $airline_id = null, $ref_model = null, $ref_model_id = null)
{
$where = [
'type' => $type,
2018-08-27 00:40:04 +08:00
['airline_id', '=', null],
];
2018-04-02 03:32:01 +08:00
if ($ref_model) {
if (\is_object($ref_model)) {
$ref_model_type = \get_class($ref_model);
} else {
$ref_model_type = $ref_model;
}
if ($ref_model) {
$where['ref_model'] = $ref_model_type;
}
if ($ref_model_id) {
$where['ref_model_id'] = $ref_model_id;
}
}
$expenses = $this->findWhere($where);
if ($airline_id) {
$where = [
'type' => $type,
2018-08-27 00:40:04 +08:00
'airline_id' => $airline_id,
];
2018-04-02 03:32:01 +08:00
if ($ref_model) {
$where['ref_model'] = $ref_model_type;
}
$airline_expenses = $this->findWhere($where);
$expenses = $expenses->concat($airline_expenses);
}
return $expenses;
}
}