|
|
<?php
namespace App\Http\Controllers;
use App\Models\Cost; use App\Models\Business; use Illuminate\Http\Request; use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\AllowedFilter;
class InvoiceController extends Controller {
public function index(Request $request, int $business) { $business = Business::findOrFail($business);
$builder = Cost::select('month') ->selectRaw("concat_ws('-',business_id,month) as factor_id") ->selectRaw("MIN(created_at) as begin") ->selectRaw("MAX(updated_at) as end") ->selectRaw("sum(cost) as cost") ->selectRaw("sum(tax) as tax") ->where('business_id','=',$business->id) ->groupBy('month','factor_id');
$costs = QueryBuilder::for($builder) ->allowedSorts([ 'factor_id', 'begin', 'end', 'cost', ]) ->allowedFilters([ AllowedFilter::exact('month'), AllowedFilter::exact('type'), ]);
return $costs->paginate($request->per_page); }
public function indexFiltering($business) { $query = File::where('business_id', $business); $fileQ = QueryBuilder::for($query) ->allowedFilters([ AllowedFilter::exact('user_id'), AllowedFilter::exact('project_id'), AllowedFilter::exact('extension'), ]); if (\request('_business_info')['info']['users'][\auth()->id()]['level'] != enum('levels.owner.id')) { $requested_projects = isset(\request('filter')['project_id']) ? array_unique(explode(',', \request('filter')['project_id'] ?? null)) : null; $requested_projects = collect($requested_projects)->keyBy(null)->toArray(); $project_ids = $this->myStateProjects($requested_projects); $fileQ->where(function ($q) use ($project_ids) { $q->whereIn('project_id', $project_ids['non_guest_ids']) ->orWhere(function ($q) use ($project_ids) { $q->whereIn('project_id', $project_ids['guest_ids']) ->where('user_id', auth()->id()); }); }); }
if (request()->filled('group')) { $fileQ->selectRaw("files.group, count(files.id) as file_count, sum(files.size) as file_size")->groupBy('group'); }
return $fileQ; }
public function show(Request $request, int $business, string $date) { return Cost::where('business_id', '=', $business) ->where("month","=",$date) ->get(); } }
|