Restaurants.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace App\Models;
  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\Models\Photos','resto_id','id')->where('photo_type','logo');
  10. }
  11. public function pwa_photos(){
  12. return $this->hasOne('App\Models\Photos','resto_id','id')->where('photo_type','logo-pwa');
  13. }
  14. public function home_images(){
  15. return $this->hasOne('App\Models\Photos','resto_id','id')->where('photo_type','home_image');
  16. }
  17. public function users(){
  18. return $this->belongsTo('App\Models\User','user_id','id');
  19. }
  20. public function places(){
  21. return $this->belongsTo('App\Models\DMCities','place','id');
  22. }
  23. public function recipes(){
  24. return $this->hasMany('App\Models\Recipes','resto_id','id')->whereNull('deleted_at')->orderBy('display_order','DESC');
  25. }
  26. public function waiters(){
  27. return $this->hasMany('App\Models\Waiters','resto_id','id')->whereNull('deleted_at');
  28. }
  29. public function countries(){
  30. return $this->hasOne('App\Models\Countries','id','country_id');
  31. }
  32. public function cities(){
  33. return $this->hasOne('App\Models\Cities','id','city');
  34. }
  35. public function orders(){
  36. return $this->hasMany('App\Models\Orders','resto_id','id')->orderBy('created_at','DESC')->whereNull('deleted_at');
  37. }
  38. public function orders_count(){
  39. return $this->hasMany('App\Models\Orders','resto_id','id')->orderBy('created_at','DESC')->withCount('orders')->whereNull('deleted_at');
  40. }
  41. public function placed_orders(){
  42. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Placed')->orderBy('created_at','DESC')->whereNull('deleted_at');;
  43. }
  44. public function today_placed_orders(){
  45. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Placed')->orderBy('created_at','DESC')->whereDate('created_at',date('Y-m-d'))->whereNull('deleted_at');;
  46. }
  47. public function send_to_kitchen_orders(){
  48. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Send_to_Kitchen')->orderBy('created_at','DESC')->whereNull('deleted_at');;
  49. }
  50. public function served_orders(){
  51. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Served')->orderBy('created_at','DESC')->whereNull('deleted_at');;
  52. }
  53. public function rejected_orders(){
  54. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Rejected')->orderBy('created_at','DESC')->whereNull('deleted_at');;
  55. }
  56. public function delivered_orders(){
  57. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Has_Delivered')->orderBy('created_at','DESC')->whereNull('deleted_at');;
  58. }
  59. public function cancelled_by_customer_orders(){
  60. return $this->hasMany('App\Models\Orders','resto_id','id')->whereIn('status',['Cancelled_by_Customer','Rejected'])->orderBy('created_at','DESC')->whereNull('deleted_at');;
  61. }
  62. public function preparing_order_orders(){
  63. return $this->hasMany('App\Models\Orders','resto_id','id')->where('status','Preparing_Order')->orderBy('created_at','DESC')->whereNull('deleted_at');;
  64. }
  65. public function resto_metas(){
  66. return $this->hasMany('App\Models\RestoMetas','bussiness_id','id');
  67. }
  68. public function galleries(){
  69. return $this->hasMany('App\Models\Photos','resto_id','id')->where('photo_type','gallery');
  70. }
  71. public function tables(){
  72. return $this->hasMany('App\Models\RestoTables','resto_id','id')->whereNull('deleted_at');
  73. }
  74. public function special_offers(){
  75. return $this->hasMany('App\Models\SpecialOffers','resto_id','id')->whereNull('deleted_at');
  76. }
  77. public function delivery_fee(){
  78. return $this->hasMany('App\Models\PlaceDeliveryPrices','resto_id','id');
  79. }
  80. public function special_offers_active(){
  81. return $this->hasOne('App\Models\SpecialOffers','resto_id','id')->where('is_active',1)->whereNull('deleted_at');
  82. }
  83. public function order_notifications_unread(){
  84. return $this->hasMany('App\Models\OrderNotifications','resto_id','id')->orderBy('created_at','DESC')->where('status','unread');
  85. }
  86. public function order_notifications_all(){
  87. return $this->hasMany('App\Models\OrderNotifications','resto_id','id')->orderBy('created_at','DESC');
  88. }
  89. public static function totalRevenue($resto_id){
  90. $total_revenue = DB::table('tb_dm_orders')
  91. ->select(DB::raw('(sum(total_price)) as total_price '))
  92. ->where('resto_id' , $resto_id)
  93. ->where('status','Has_Delivered')
  94. ->whereNull('deleted_at')
  95. ->get()->toArray();
  96. //return $this->withCount('App\Contacts','contactgrp_def_id','def_id');
  97. return $total_revenue;
  98. }
  99. public static function totalOnlineOrders($resto_id){
  100. $total_revenue = DB::table('tb_dm_orders')
  101. ->select(DB::raw('count(*) as total_orders '))
  102. ->where('resto_id' , $resto_id)
  103. ->whereNull('deleted_at')
  104. ->get()->toArray();
  105. //return $this->withCount('App\Contacts','contactgrp_def_id','def_id');
  106. return $total_revenue;
  107. }
  108. public static function totalCountRevenue($resto_id){
  109. $date = \Carbon\Carbon::today()->subDays(7);
  110. $total_revenue = DB::table('tb_dm_orders')
  111. ->select(DB::raw('(sum(total_price)+sum(delivery_fee)) as total_price '))
  112. ->where('resto_id' , $resto_id)
  113. ->where('created_at','>=',$date)
  114. ->where('status','Has_Delivered')
  115. ->whereNull('deleted_at')
  116. ->get()->toArray();
  117. //return $this->withCount('App\Contacts','contactgrp_def_id','def_id');
  118. return $total_revenue;
  119. }
  120. public static function totalRevenueLast7Days($resto_id){
  121. $date = \Carbon\Carbon::today()->subDays(7);
  122. $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')
  123. ->where('created_at','>=',$date)
  124. ->whereNull('deleted_at')
  125. ->get()->toJson();
  126. return $orders;
  127. }
  128. public static function totalCountOnlineOrdersLast7Days($resto_id){
  129. $date = \Carbon\Carbon::today()->subDays(7);
  130. $orders = Orders::select(DB::raw('count(*) as total_orders'))
  131. ->where('created_at','>=',$date)
  132. ->whereNull('deleted_at')
  133. ->get()->toArray();
  134. return $orders;
  135. }
  136. public static function totalOnlineOrdersLast7Days($resto_id){
  137. $date = \Carbon\Carbon::today()->subDays(7);
  138. $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')
  139. ->where('created_at','>=',$date)
  140. ->whereNull('deleted_at')
  141. ->get()->toJson();
  142. return $orders;
  143. }
  144. public static function totalOrdersBySource($resto_id){
  145. $orders = Orders::select(DB::raw('(count(id)) as order_source'),DB::raw('campaign_type as campaign_type'))
  146. ->where('status','Has_Delivered')->groupby('campaign_type')
  147. ->where('resto_id',$resto_id)
  148. ->whereNull('deleted_at')
  149. ->get()->toArray();
  150. return $orders;
  151. }
  152. }