You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.7 KiB

  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Cost;
  4. use App\Models\Business;
  5. use Illuminate\Http\Request;
  6. use Spatie\QueryBuilder\QueryBuilder;
  7. use Spatie\QueryBuilder\AllowedFilter;
  8. class InvoiceController extends Controller
  9. {
  10. public function index(Request $request, int $business)
  11. {
  12. $business = Business::findOrFail($business);
  13. $builder = Cost::select('month')
  14. ->selectRaw("concat_ws('-',business_id,month) as factor_id")
  15. ->selectRaw("MIN(created_at) as begin")
  16. ->selectRaw("MAX(updated_at) as end")
  17. ->selectRaw("sum(cost) as cost")
  18. ->selectRaw("sum(tax) as tax")
  19. ->where('business_id','=',$business->id)
  20. ->groupBy('month','factor_id');
  21. $costs = QueryBuilder::for($builder)
  22. ->allowedSorts([
  23. 'factor_id',
  24. 'begin',
  25. 'end',
  26. 'cost',
  27. ])
  28. ->allowedFilters([
  29. AllowedFilter::exact('month'),
  30. AllowedFilter::exact('type'),
  31. ]);
  32. return $costs->paginate($request->per_page);
  33. }
  34. public function indexFiltering($business)
  35. {
  36. $query = File::where('business_id', $business);
  37. $fileQ = QueryBuilder::for($query)
  38. ->allowedFilters([
  39. AllowedFilter::exact('user_id'),
  40. AllowedFilter::exact('project_id'),
  41. AllowedFilter::exact('extension'),
  42. ]);
  43. if (\request('_business_info')['info']['users'][\auth()->id()]['level'] != enum('levels.owner.id')) {
  44. $requested_projects = isset(\request('filter')['project_id']) ?
  45. array_unique(explode(',', \request('filter')['project_id'] ?? null)) :
  46. null;
  47. $requested_projects = collect($requested_projects)->keyBy(null)->toArray();
  48. $project_ids = $this->myStateProjects($requested_projects);
  49. $fileQ->where(function ($q) use ($project_ids) {
  50. $q->whereIn('project_id', $project_ids['non_guest_ids'])
  51. ->orWhere(function ($q) use ($project_ids) {
  52. $q->whereIn('project_id', $project_ids['guest_ids'])
  53. ->where('user_id', auth()->id());
  54. });
  55. });
  56. }
  57. if (request()->filled('group')) {
  58. $fileQ->selectRaw("files.group, count(files.id) as file_count, sum(files.size) as file_size")->groupBy('group');
  59. }
  60. return $fileQ;
  61. }
  62. public function show(Request $request, int $business, string $date)
  63. {
  64. return Cost::where('business_id', '=', $business)
  65. ->where("month","=",$date)
  66. ->get();
  67. }
  68. }