ReportController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Helpers\Price;
  4. use App\Models\ObjectType;
  5. use App\Models\Order;
  6. use App\Models\OrderStatus;
  7. use App\Models\Product;
  8. use App\Models\ProductSKU;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Str;
  11. class ReportController extends Controller
  12. {
  13. protected array $data = [
  14. 'active' => 'reports',
  15. 'title' => 'Отчёты',
  16. 'id' => 'reports',
  17. ];
  18. public function index()
  19. {
  20. $doneStatuses = [5, 7, 8, 9, 10];
  21. $objectTypes = ObjectType::query()->get()->pluck('name', 'id')->toArray();
  22. $this->data['objectTypes'] = $objectTypes;
  23. $this->data['totalOrders'] = Order::all()->count();
  24. $this->data['doneOrders'] = Order::query()->whereIn('order_status_id', [9, 10])->count();
  25. $this->data['totalMafs'] = ProductSKU::all()->count();
  26. $this->data['doneMafs'] = ProductSKU::query()->
  27. whereHas('order', function ($query) {
  28. $query->whereIn('order_status_id', [9, 10]);
  29. })->count();
  30. $this->data['totalSum'] = Price::format(
  31. ProductSKU::query()->
  32. whereHas('order', function ($query) use ($doneStatuses) {
  33. $query->whereIn('order_status_id', $doneStatuses);
  34. })
  35. ->withSum('product', 'total_price')
  36. ->get()
  37. ->sum('product_sum_total_price')
  38. );
  39. foreach ($objectTypes as $objectTypeId => $objectType) {
  40. // total by types
  41. $this->data['totalOrdersType'][$objectTypeId] = Order::where('object_type_id', '=', $objectTypeId)->count();
  42. $this->data['totalMafsType'][$objectTypeId] = ProductSKU::query()->
  43. whereHas('order', function ($query) use ($objectTypeId) {
  44. $query->where('object_type_id', '=', $objectTypeId);
  45. })->count();
  46. // со статусами: в монтаже, готова к сдаче, не сдана замечания, сдана замечания, сдана - зеленый цвет
  47. $this->data['doneOrdersType'][$objectTypeId] = Order::query()
  48. ->where('object_type_id', '=', $objectTypeId)
  49. ->whereIn('order_status_id', $doneStatuses)
  50. ->count();
  51. $this->data['doneMafsType'][$objectTypeId] = ProductSKU::query()->
  52. whereHas('order', function ($query) use ($objectTypeId, $doneStatuses) {
  53. $query->where('object_type_id', '=', $objectTypeId)
  54. ->whereIn('order_status_id', $doneStatuses);
  55. })->count();
  56. // остальные - не готовы
  57. $this->data['notDoneOrdersType'][$objectTypeId] = Order::query()
  58. ->where('object_type_id', '=', $objectTypeId)
  59. ->whereNotIn('order_status_id', $doneStatuses)
  60. ->count();
  61. $this->data['notDoneMafsType'][$objectTypeId] = ProductSKU::query()->
  62. whereHas('order', function ($query) use ($objectTypeId, $doneStatuses) {
  63. $query->where('object_type_id', '=', $objectTypeId)
  64. ->whereNotIn('order_status_id', $doneStatuses);
  65. })->count();
  66. }
  67. return view('reports.index', $this->data);
  68. }
  69. }