Restaurants.php_1 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace App;
  3. use Illuminate\Database\Eloquent\Model;
  4. use DB;
  5. class Restaurants extends Model
  6. {
  7. //
  8. public function photos(){
  9. return $this->hasOne('App\Photos','resto_id','id')->where('photo_type','logo');
  10. }
  11. public function home_images(){
  12. return $this->hasOne('App\Photos','resto_id','id')->where('photo_type','home_image');
  13. }
  14. public function users(){
  15. return $this->belongsTo('App\User','user_id','id');
  16. }
  17. public function recipes(){
  18. return $this->hasMany('App\Recipes','resto_id','id')->whereNull('deleted_at');
  19. }
  20. public function waiters(){
  21. return $this->hasMany('App\Waiters','resto_id','id')->whereNull('deleted_at');
  22. }
  23. public function orders(){
  24. return $this->hasMany('App\Orders','resto_id','id')->orderBy('created_at','DESC');
  25. }
  26. public function orders_count(){
  27. return $this->hasMany('App\Orders','resto_id','id')->orderBy('created_at','DESC')->withCount('orders');
  28. }
  29. public function placed_orders(){
  30. return $this->hasMany('App\Orders','resto_id','id')->where('status','Placed')->orderBy('created_at','DESC');;
  31. }
  32. public function send_to_kitchen_orders(){
  33. return $this->hasMany('App\Orders','resto_id','id')->where('status','Send_to_Kitchen')->orderBy('created_at','DESC');;
  34. }
  35. public function served_orders(){
  36. return $this->hasMany('App\Orders','resto_id','id')->where('status','Served')->orderBy('created_at','DESC');;
  37. }
  38. public function rejected_orders(){
  39. return $this->hasMany('App\Orders','resto_id','id')->where('status','Rejected')->orderBy('created_at','DESC');;
  40. }
  41. public function delivered_orders(){
  42. return $this->hasMany('App\Orders','resto_id','id')->where('status','Has_Delivered')->orderBy('created_at','DESC');;
  43. }
  44. public function cancelled_by_customer_orders(){
  45. return $this->hasMany('App\Orders','resto_id','id')->whereIn('status',['Cancelled_by_Customer','Rejected'])->orderBy('created_at','DESC');;
  46. }
  47. public function preparing_order_orders(){
  48. return $this->hasMany('App\Orders','resto_id','id')->where('status','Preparing_Order')->orderBy('created_at','DESC');;
  49. }
  50. public function galleries(){
  51. return $this->hasMany('App\Photos','resto_id','id')->where('photo_type','gallery');
  52. }
  53. public function tables(){
  54. return $this->hasMany('App\RestoTables','resto_id','id')->whereNull('deleted_at');
  55. }
  56. public function special_offers(){
  57. return $this->hasMany('App\SpecialOffers','resto_id','id')->whereNull('deleted_at');
  58. }
  59. public function delivery_fee(){
  60. return $this->hasMany('App\PlaceDeliveryPrices','resto_id','id');
  61. }
  62. public function special_offers_active(){
  63. return $this->hasOne('App\SpecialOffers','resto_id','id')->where('is_active',1)->whereNull('deleted_at');
  64. }
  65. public function order_notifications_unread(){
  66. return $this->hasMany('App\OrderNotifications','resto_id','id')->orderBy('created_at','DESC')->where('status','unread');
  67. }
  68. public function order_notifications_all(){
  69. return $this->hasMany('App\OrderNotifications','resto_id','id')->orderBy('created_at','DESC');
  70. }
  71. public static function totalRevenue($resto_id){
  72. $total_revenue = DB::table('tb_dm_orders')
  73. ->select(DB::raw('(sum(total_price)+sum(delivery_fee)) as total_price '))
  74. ->where('resto_id' , $resto_id)
  75. ->where('status','Has_Delivered')
  76. ->get()->toArray();
  77. //return $this->withCount('App\Contacts','contactgrp_def_id','def_id');
  78. return $total_revenue;
  79. }
  80. public static function totalOnlineOrders($resto_id){
  81. $total_revenue = DB::table('tb_dm_orders')
  82. ->select(DB::raw('count(*) as total_orders '))
  83. ->where('resto_id' , $resto_id)
  84. ->get()->toArray();
  85. //return $this->withCount('App\Contacts','contactgrp_def_id','def_id');
  86. return $total_revenue;
  87. }
  88. public static function totalCountRevenue($resto_id){
  89. $date = \Carbon\Carbon::today()->subDays(7);
  90. $total_revenue = DB::table('tb_dm_orders')
  91. ->select(DB::raw('(sum(total_price)+sum(delivery_fee)) as total_price '))
  92. ->where('resto_id' , $resto_id)
  93. ->where('created_at','>=',$date)
  94. ->where('status','Has_Delivered')
  95. ->get()->toArray();
  96. //return $this->withCount('App\Contacts','contactgrp_def_id','def_id');
  97. return $total_revenue;
  98. }
  99. public static function totalRevenueLast7Days($resto_id){
  100. $date = \Carbon\Carbon::today()->subDays(7);
  101. $orders = Orders::select(DB::raw('(sum(total_price)+sum(delivery_fee)) as total_price'),DB::raw('DATE(created_at) date'),DB::raw('DATE_FORMAT(created_at,"%a") day_name'))->where('status','Has_Delivered')->groupby('date')->orderBy('date','ASC')
  102. ->where('created_at','>=',$date)
  103. ->get()->toJson();
  104. return $orders;
  105. }
  106. public static function totalCountOnlineOrdersLast7Days($resto_id){
  107. $date = \Carbon\Carbon::today()->subDays(7);
  108. $orders = Orders::select(DB::raw('count(*) as total_orders'))
  109. ->where('created_at','>=',$date)
  110. ->get()->toArray();
  111. return $orders;
  112. }
  113. public static function totalOnlineOrdersLast7Days($resto_id){
  114. $date = \Carbon\Carbon::today()->subDays(7);
  115. $orders = Orders::select(DB::raw('count(*) as total_orders'),DB::raw('DATE(created_at) date'),DB::raw('DATE_FORMAT(created_at,"%a") day_name'))->groupby('date')->orderBy('date','ASC')
  116. ->where('created_at','>=',$date)
  117. ->get()->toJson();
  118. return $orders;
  119. }
  120. }