Advertisement
Guest User

Untitled

a guest
Jan 5th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 87.14 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Http\Controllers;
  4.  
  5. use Illuminate\Contracts\Validation\Validator;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Hash;
  8. use Illuminate\Support\Facades\Input;
  9. use Illuminate\Support\Facades\Redirect;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Route;
  12. use \Carbon\Carbon;
  13.  
  14. use Auth;
  15. use App;
  16. use App\User;
  17. use App\UserLilu;
  18. use App\Transaction;
  19. use App\Product;
  20. use App\ProductClass;
  21. use App\ProductCategory;
  22. use App\ServiceCategory;
  23.  
  24. use App\Booking;
  25. use App\Client;
  26. use App\Ticket;
  27. use App\Sale;
  28. use App\Business;
  29. use App\Staff;
  30.  
  31.  
  32. class HomeController extends Controller
  33. {
  34. /**
  35. * Create a new controller instance.
  36. *
  37. * @return void
  38. */
  39. public function __construct()
  40. {
  41. $this->middleware('auth');
  42. }
  43.  
  44. /**
  45. * Show the application dashboard.
  46. *
  47. * @return \Illuminate\Http\Response
  48. */
  49. public function index(Request $request) {
  50. date_default_timezone_set("America/Mexico_City");
  51.  
  52.  
  53. $today = \Carbon\Carbon::now()->toDateString();
  54.  
  55. $dayago = \Carbon\Carbon::now()->subDay(1)->toDateString();
  56. $twodayago = \Carbon\Carbon::now()->subDay(2)->toDateString();
  57. $threedayago = \Carbon\Carbon::now()->subDay(3)->toDateString();
  58. $fourdayago = \Carbon\Carbon::now()->subDay(4)->toDateString();
  59. $fivedayago = \Carbon\Carbon::now()->subDay(5)->toDateString();
  60. $sixdayago = \Carbon\Carbon::now()->subDay(6)->toDateString();
  61.  
  62. $monthago = \Carbon\Carbon::now()->subMonth()->toDateString(); # YA NO ES SEMANAL ES MENSUAL
  63. $twomonthago = \Carbon\Carbon::now()->subMonth(2)->toDateString();
  64.  
  65.  
  66. ///HACER UN LOOP CON PARA SACAR TODOS LOS USER Y HACER GROUP BY USER_ID
  67. //return $users_ids;
  68. $all_users_ids=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->get();
  69. if ($request->isMethod('post')) {
  70. //
  71. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->whereIn('user_id',Input::get('user_ids'))->select('user_id')->get();
  72. $selected_users=Input::get('user_ids');
  73.  
  74. }else{
  75. if(UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->count()>0){
  76. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->get();
  77. foreach($users as $user){
  78. $selected_users[]=$user->user_id;
  79. }
  80. }else{
  81. $selected_users[]='';
  82. }
  83. //$selected_users=$all_users_ids->pluck('user_id');
  84. }
  85. $all_users_ids=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->get();
  86. //$user= UserLilu::where('franchise_id',Auth::user()->franchise_id)->first();
  87.  
  88.  
  89.  
  90. # CITAS REALIZADAS DEL ÚLTIMO MES
  91. $bookings_week = 0;
  92. $bookings_twomonthago = 0;
  93. $clients_week=0;
  94. $clients_twomonthago=0;
  95. $sales_week=0;
  96. $sales_twomonthago=0;
  97. $income_month = 0;
  98. $income_twomonthago = 0;
  99. # FOREACH RECORRIENDO TODOS LOS NEGOCIOS DE LA FRANQUICIA
  100. if(UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->count()>0) {
  101. foreach($users as $user){
  102. for($x=0;$x<=30;$x++)
  103. $bookings_week += $this->getBookingsCount($x,$user);
  104. # CITAS REALIZADAS DEL MES PASADO
  105. for($x=30;$x<=60;$x++)
  106. $bookings_twomonthago += $this->getBookingsCount($x,$user);
  107.  
  108. # CLIENTES NUEVOS LA ÚLTIMA SEMANA
  109. $clients_week += Client::where('user_id',$user->user_id)->whereBetween( DB::raw('date(created_at)'), [$monthago, $today] )->whereNull('deleted_at')->get()->count();
  110. # CLIENTES NUEVOS LA SEMANA PASADA
  111. $clients_twomonthago += Client::where('user_id',$user->user_id)->whereBetween( DB::raw('date(created_at)'), [$twomonthago, $monthago] )->whereNull('deleted_at')->get()->count();
  112. # VENTAS NUEVOS LA ÚLTIMA SEMANA
  113. $sales_week += Ticket::where('user_id',$user->user_id)->whereBetween( DB::raw('date(created_at)'), [$monthago, $today] )->whereNull('deleted_at')->get()->count();
  114. # VENTAS NUEVOS LA SEMANA PASADA
  115. $sales_twomonthago += Ticket::where('user_id',$user->user_id)->whereBetween( DB::raw('date(created_at)'), [$twomonthago, $monthago] )->whereNull('deleted_at')->get()->count();
  116. # INGRESOS NUEVOS DEL ÚLTIMO MES
  117. for($x=0;$x<=30;$x++)
  118. $income_month += $this->getIncomeSubday($x,$user) + $this->getIncomeProductsSubday($x,$user);
  119. # INGRESOS NUEVOS DEL MES PASADO
  120. for($x=30;$x<=60;$x++)
  121. $income_twomonthago += $this->getIncomeSubday($x,$user) + $this->getIncomeProductsSubday($x,$user);
  122.  
  123. }
  124. }
  125.  
  126.  
  127.  
  128.  
  129. # CITAS COMPLETADAS LA ÚLTIMA SEMANA
  130. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  131. $todayy = \Carbon\Carbon::now()->format('m/d/Y');
  132. $todayy = (substr( $todayy, 0, 1 ) === "0") ? substr($todayy,1) : $todayy;
  133. $todayy = (substr( explode("/",$todayy)[1], 0, 1 ) === "0") ? explode("/",$todayy)[0]."/".substr(explode("/",$todayy)[1],1)."/".explode("/",$todayy)[2] : $todayy;
  134.  
  135. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  136. $dayagoo = \Carbon\Carbon::now()->subDay(1)->format('m/d/Y');
  137. $dayagoo = (substr( $dayagoo, 0, 1 ) === "0") ? substr($dayagoo,1) : $dayagoo;
  138. $dayagoo = (substr( explode("/",$dayagoo)[1], 0, 1 ) === "0") ? explode("/",$dayagoo)[0]."/".substr(explode("/",$dayagoo)[1],1)."/".explode("/",$dayagoo)[2] : $dayagoo;
  139.  
  140. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  141. $twodayagoo = \Carbon\Carbon::now()->subDay(2)->format('m/d/Y');
  142. $twodayagoo = (substr( $twodayagoo, 0, 1 ) === "0") ? substr($twodayagoo,1) : $twodayagoo;
  143. $twodayagoo = (substr( explode("/",$twodayagoo)[1], 0, 1 ) === "0") ? explode("/",$twodayagoo)[0]."/".substr(explode("/",$twodayagoo)[1],1)."/".explode("/",$twodayagoo)[2] : $twodayagoo;
  144.  
  145. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  146. $threedayagoo = \Carbon\Carbon::now()->subDay(3)->format('m/d/Y');
  147. $threedayagoo = (substr( $threedayagoo, 0, 1 ) === "0") ? substr($threedayagoo,1) : $threedayagoo;
  148. $threedayagoo = (substr( explode("/",$threedayagoo)[1], 0, 1 ) === "0") ? explode("/",$threedayagoo)[0]."/".substr(explode("/",$threedayagoo)[1],1)."/".explode("/",$threedayagoo)[2] : $threedayagoo;
  149.  
  150. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  151. $fourdayagoo = \Carbon\Carbon::now()->subDay(4)->format('m/d/Y');
  152. $fourdayagoo = (substr( $fourdayagoo, 0, 1 ) === "0") ? substr($fourdayagoo,1) : $fourdayagoo;
  153. $fourdayagoo = (substr( explode("/",$fourdayagoo)[1], 0, 1 ) === "0") ? explode("/",$fourdayagoo)[0]."/".substr(explode("/",$fourdayagoo)[1],1)."/".explode("/",$fourdayagoo)[2] : $fourdayagoo;
  154.  
  155. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  156. $fivedayagoo = \Carbon\Carbon::now()->subDay(5)->format('m/d/Y');
  157. $fivedayagoo = (substr( $fivedayagoo, 0, 1 ) === "0") ? substr($fivedayagoo,1) : $fivedayagoo;
  158. $fivedayagoo = (substr( explode("/",$fivedayagoo)[1], 0, 1 ) === "0") ? explode("/",$fivedayagoo)[0]."/".substr(explode("/",$fivedayagoo)[1],1)."/".explode("/",$fivedayagoo)[2] : $fivedayagoo;
  159.  
  160. # 1 tomamos la fecha que necesitamos y la igualamos con la que almacenamos en la bd, 2 vemos si el mes comienza con 0 si es asi lo eliminamos, despues vemos si el día comienza con 0 si es asi lo eliminamos
  161. $sixdayagoo = \Carbon\Carbon::now()->subDay(6)->format('m/d/Y');
  162. $sixdayagoo = (substr( $sixdayagoo, 0, 1 ) === "0") ? substr($sixdayagoo,1) : $sixdayagoo;
  163. $sixdayagoo = (substr( explode("/",$sixdayagoo)[1], 0, 1 ) === "0") ? explode("/",$sixdayagoo)[0]."/".substr(explode("/",$sixdayagoo)[1],1)."/".explode("/",$sixdayagoo)[2] : $sixdayagoo;
  164.  
  165. # FOREACH RECORRIENDO TODOS LOS NEGOCIOS DE LA FRANQUICIA
  166. $bookings_completed_today=0;
  167. $bookings_completed_1ago=0;
  168. $bookings_completed_2ago=0;
  169. $bookings_completed_3ago=0;
  170. $bookings_completed_4ago=0;
  171. $bookings_completed_5ago=0;
  172. $bookings_completed_6ago=0;
  173. $bookings_cancelled_today=0;
  174. $bookings_cancelled_1ago=0;
  175. $bookings_cancelled_2ago=0;
  176. $bookings_cancelled_3ago=0;
  177. $bookings_cancelled_4ago=0;
  178. $bookings_cancelled_5ago=0;
  179. $bookings_cancelled_6ago=0;
  180. $bookings_pending_today=0;
  181. $bookings_pending_1ago=0;
  182. $bookings_pending_2ago=0;
  183. $bookings_pending_3ago=0;
  184. $bookings_pending_4ago=0;
  185. $bookings_pending_5ago=0;
  186. $bookings_pending_6ago=0;
  187. if(UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->count()>0) {
  188. foreach ($users as $user) {
  189. $bookings_completed_today += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $todayy . '%')->whereNull('deleted_at')->get()->count();
  190. $bookings_completed_1ago += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $dayagoo . '%')->whereNull('deleted_at')->get()->count();
  191. $bookings_completed_2ago += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $twodayagoo . '%')->whereNull('deleted_at')->get()->count();
  192. $bookings_completed_3ago += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $threedayagoo . '%')->whereNull('deleted_at')->get()->count();
  193. $bookings_completed_4ago += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $fourdayagoo . '%')->whereNull('deleted_at')->get()->count();
  194. $bookings_completed_5ago += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $fivedayagoo . '%')->whereNull('deleted_at')->get()->count();
  195. $bookings_completed_6ago += Booking::where('user_id', $user->user_id)->where('status', 2)->where('end_date', 'like', '%' . $sixdayagoo . '%')->whereNull('deleted_at')->get()->count();
  196. # CITAS COMPLETADAS LA ÚLTIMA SEMANA
  197. $bookings_cancelled_today += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $todayy . '%')->whereNull('deleted_at')->get()->count();
  198. $bookings_cancelled_1ago += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $dayagoo . '%')->whereNull('deleted_at')->get()->count();
  199. $bookings_cancelled_2ago += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $twodayagoo . '%')->whereNull('deleted_at')->get()->count();
  200. $bookings_cancelled_3ago += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $threedayagoo . '%')->whereNull('deleted_at')->get()->count();
  201. $bookings_cancelled_4ago += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $fourdayagoo . '%')->whereNull('deleted_at')->get()->count();
  202. $bookings_cancelled_5ago += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $fivedayagoo . '%')->whereNull('deleted_at')->get()->count();
  203. $bookings_cancelled_6ago += Booking::where('user_id', $user->user_id)->where('status', 3)->where('end_date', 'like', '%' . $sixdayagoo . '%')->whereNull('deleted_at')->get()->count();
  204. # CITAS PENDIENTES LA ÚLTIMA SEMANA
  205. $bookings_pending_today += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $todayy . '%')->whereNull('deleted_at')->get()->count();
  206. $bookings_pending_1ago += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $dayagoo . '%')->whereNull('deleted_at')->get()->count();
  207. $bookings_pending_2ago += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $twodayagoo . '%')->whereNull('deleted_at')->get()->count();
  208. $bookings_pending_3ago += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $threedayagoo . '%')->whereNull('deleted_at')->get()->count();
  209. $bookings_pending_4ago += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $fourdayagoo . '%')->whereNull('deleted_at')->get()->count();
  210. $bookings_pending_5ago += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $fivedayagoo . '%')->whereNull('deleted_at')->get()->count();
  211. $bookings_pending_6ago += Booking::where('user_id', $user->user_id)->where('status', 1)->where('end_date', 'like', '%' . $sixdayagoo . '%')->whereNull('deleted_at')->get()->count();
  212. }
  213. }
  214. # CITAS PENDIENTES LA ÚLTIMA SEMANA
  215. #$booking = Booking::where('user_id',$user->user_id)->where('status',1)->whereDate('created_at',$today)->first();
  216. #$monday = Booking::where('user_id',$user->user_id)->where('status',1)->whereDate('created_at',$today)->first()->mon;
  217. //$user->franchise_id;
  218.  
  219.  
  220. # FOREACH RECORRIENDO TODOS LOS NEGOCIOS DE LA FRANQUICIA
  221. $staff_count=0;
  222. $test = null;
  223. $bookings_available_today=0;
  224. $bookings_available_1ago=0;
  225. $bookings_available_2ago=0;
  226. $bookings_available_3ago=0;
  227. $bookings_available_4ago=0;
  228. $bookings_available_5ago=0;
  229. $bookings_available_6ago=0;
  230. if(UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->count()>0) {
  231. foreach($users as $user) {
  232. $business = Business::where('user_id', $user->user_id)->first(); # CHECK, IT WAS 'ID' CHANGED TO 'USER_ID'
  233. $staff_count += Staff::where('user_id', $user->user_id)->whereNull('deleted_at')->get()->count();
  234.  
  235. if (\Carbon\Carbon::now()->format('l') == 'Monday') {
  236. if ($business->mon) {
  237. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  238. } else {
  239. $bookings_available_today = 0;
  240. }
  241. }
  242. if (\Carbon\Carbon::now()->format('l') == 'Tuesday') {
  243. if ($business->tue) {
  244. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  245. } else {
  246. $bookings_available_today = 0;
  247. }
  248. }
  249. if (\Carbon\Carbon::now()->format('l') == 'Wednesday') {
  250. if ($business->wed) {
  251. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  252. } else {
  253. $bookings_available_today = 0;
  254. }
  255. }
  256. if (\Carbon\Carbon::now()->format('l') == 'Thursday') {
  257. if ($business->thu) {
  258. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  259. } else {
  260. $bookings_available_today = 0;
  261. }
  262. }
  263. if (\Carbon\Carbon::now()->format('l') == 'Friday') {
  264. if ($business->fri) {
  265. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  266. } else {
  267. $bookings_available_today = 0;
  268. }
  269. }
  270. if (\Carbon\Carbon::now()->format('l') == 'Sathurday') {
  271. if ($business->sat) {
  272. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  273. } else {
  274. $bookings_available_today = 0;
  275. }
  276. }
  277. if (\Carbon\Carbon::now()->format('l') == 'Sunday') {
  278. if ($business->sun) {
  279. $bookings_available_today = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_today - $bookings_pending_today;
  280. } else {
  281. $bookings_available_today = 0;
  282. }
  283. }
  284.  
  285. try {
  286. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Monday') {
  287. if ($business->mon) {
  288. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  289. } else {
  290. $bookings_available_1ago = 0;
  291. }
  292. }
  293. } catch (Exception $e) {
  294. $bookings_available_1ago = 0;
  295. }
  296. try {
  297. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Tuesday') {
  298. if ($business->tue) {
  299. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  300. } else {
  301. $bookings_available_1ago = 0;
  302. }
  303. }
  304. } catch (Exception $e) {
  305. $bookings_available_1ago = 0;
  306. }
  307. try {
  308. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Wednesday') {
  309. if ($business->wed) {
  310. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  311. } else {
  312. $bookings_available_1ago = 0;
  313. }
  314. }
  315. } catch (Exception $e) {
  316. $bookings_available_1ago = 0;
  317. }
  318. try {
  319. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Thursday') {
  320. if ($business->thu) {
  321. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  322. } else {
  323. $bookings_available_1ago = 0;
  324. }
  325. }
  326. } catch (Exception $e) {
  327. $bookings_available_1ago = 0;
  328. }
  329. try {
  330. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Friday') {
  331. if ($business->fri) {
  332. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  333. } else {
  334. $bookings_available_1ago = 0;
  335. }
  336. }
  337. } catch (Exception $e) {
  338. $bookings_available_1ago = 0;
  339. }
  340. try {
  341. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Sathurday') {
  342. if ($business->sat) {
  343. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  344. } else {
  345. $bookings_available_1ago = 0;
  346. }
  347. }
  348. } catch (Exception $e) {
  349. $bookings_available_1ago = 0;
  350. }
  351. try {
  352. if (\Carbon\Carbon::now()->subDay(1)->format('l') == 'Sunday') {
  353. if ($business->sun) {
  354. $bookings_available_1ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_1ago - $bookings_pending_1ago;
  355. } else {
  356. $bookings_available_1ago = 0;
  357. }
  358. }
  359. } catch (Exception $e) {
  360. $bookings_available_1ago = 0;
  361. }
  362.  
  363. try {
  364. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Monday') {
  365. if ($business->mon) {
  366. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  367. } else {
  368. $bookings_available_2ago = 0;
  369. }
  370. }
  371. } catch (Exception $e) {
  372. $bookings_available_2ago = 0;
  373. }
  374. try {
  375. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Tuesday') {
  376. if ($business->tue) {
  377. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  378. } else {
  379. $bookings_available_2ago = 0;
  380. }
  381. }
  382. } catch (Exception $e) {
  383. $bookings_available_2ago = 0;
  384. }
  385. try {
  386. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Wednesday') {
  387. if ($business->wed) {
  388. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  389. } else {
  390. $bookings_available_2ago = 0;
  391. }
  392. }
  393. } catch (Exception $e) {
  394. $bookings_available_2ago = 0;
  395. }
  396. try {
  397. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Thursday') {
  398. if ($business->thu) {
  399. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  400. } else {
  401. $bookings_available_2ago = 0;
  402. }
  403. }
  404. } catch (Exception $e) {
  405. $bookings_available_2ago = 0;
  406. }
  407. try {
  408. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Friday') {
  409. if ($business->fri) {
  410. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  411. } else {
  412. $bookings_available_2ago = 0;
  413. }
  414. }
  415. } catch (Exception $e) {
  416. $bookings_available_2ago = 0;
  417. }
  418. try {
  419. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Sathurday') {
  420. if ($business->sat) {
  421. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  422. } else {
  423. $bookings_available_2ago = 0;
  424. }
  425. }
  426. } catch (Exception $e) {
  427. $bookings_available_2ago = 0;
  428. }
  429. try {
  430. if (\Carbon\Carbon::now()->subDay(2)->format('l') == 'Sunday') {
  431. if ($business->sun) {
  432. $bookings_available_2ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_2ago - $bookings_pending_2ago;
  433. } else {
  434. $bookings_available_2ago = 0;
  435. }
  436. }
  437. } catch (Exception $e) {
  438. $bookings_available_2ago = 0;
  439. }
  440.  
  441. try {
  442. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Monday') {
  443. if ($business->mon) {
  444. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  445. } else {
  446. $bookings_available_3ago = 0;
  447. }
  448. }
  449. } catch (Exception $e) {
  450. $bookings_available_3ago = 0;
  451. }
  452. try {
  453. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Tuesday') {
  454. if ($business->tue) {
  455. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  456. } else {
  457. $bookings_available_3ago = 0;
  458. }
  459. }
  460. } catch (Exception $e) {
  461. $bookings_available_3ago = 0;
  462. }
  463. try {
  464. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Wednesday') {
  465. if ($business->wed) {
  466. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  467. } else {
  468. $bookings_available_3ago = 0;
  469. }
  470. }
  471. } catch (Exception $e) {
  472. $bookings_available_3ago = 0;
  473. }
  474. try {
  475. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Thursday') {
  476. if ($business->thu) {
  477. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  478. } else {
  479. $bookings_available_3ago = 0;
  480. }
  481. }
  482. } catch (Exception $e) {
  483. $bookings_available_3ago = 0;
  484. }
  485. try {
  486. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Friday') {
  487. if ($business->fri) {
  488. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  489. } else {
  490. $bookings_available_3ago = 0;
  491. }
  492. }
  493. } catch (Exception $e) {
  494. $bookings_available_3ago = 0;
  495. }
  496. try {
  497. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Sathurday') {
  498. if ($business->sat) {
  499. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  500. } else {
  501. $bookings_available_3ago = 0;
  502. }
  503. }
  504. } catch (Exception $e) {
  505. $bookings_available_3ago = 0;
  506. }
  507. try {
  508. if (\Carbon\Carbon::now()->subDay(3)->format('l') == 'Sunday') {
  509. if ($business->sun) {
  510. $bookings_available_3ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_3ago - $bookings_pending_3ago;
  511. } else {
  512. $bookings_available_3ago = 0;
  513. }
  514. }
  515. } catch (Exception $e) {
  516. $bookings_available_3ago = 0;
  517. }
  518.  
  519. try {
  520. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Monday') {
  521. if ($business->mon) {
  522. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  523. } else {
  524. $bookings_available_4ago = 0;
  525. }
  526. }
  527. } catch (Exception $e) {
  528. $bookings_available_4ago = 0;
  529. }
  530. try {
  531. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Tuesday') {
  532. if ($business->tue) {
  533. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  534. } else {
  535. $bookings_available_4ago = 0;
  536. }
  537. }
  538. } catch (Exception $e) {
  539. $bookings_available_4ago = 0;
  540. }
  541. try {
  542. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Wednesday') {
  543. if ($business->wed) {
  544. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  545. } else {
  546. $bookings_available_4ago = 0;
  547. }
  548. }
  549. } catch (Exception $e) {
  550. $bookings_available_4ago = 0;
  551. }
  552. try {
  553. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Thursday') {
  554. if ($business->thu) {
  555. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  556. } else {
  557. $bookings_available_4ago = 0;
  558. }
  559. }
  560. } catch (Exception $e) {
  561. $bookings_available_4ago = 0;
  562. }
  563. try {
  564. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Friday') {
  565. if ($business->fri) {
  566. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  567. } else {
  568. $bookings_available_4ago = 0;
  569. }
  570. }
  571. } catch (Exception $e) {
  572. $bookings_available_4ago = 0;
  573. }
  574. try {
  575. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Sathurday') {
  576. if ($business->sat) {
  577. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  578. } else {
  579. $bookings_available_4ago = 0;
  580. }
  581. }
  582. } catch (Exception $e) {
  583. $bookings_available_4ago = 0;
  584. }
  585. try {
  586. if (\Carbon\Carbon::now()->subDay(4)->format('l') == 'Sunday') {
  587. if ($business->sun) {
  588. $bookings_available_4ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_4ago - $bookings_pending_4ago;
  589. } else {
  590. $bookings_available_4ago = 0;
  591. }
  592. }
  593. } catch (Exception $e) {
  594. $bookings_available_4ago = 0;
  595. }
  596.  
  597. try {
  598. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Monday') {
  599. if ($business->mon) {
  600. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  601. } else {
  602. $bookings_available_5ago = 0;
  603. }
  604. }
  605. } catch (Exception $e) {
  606. $bookings_available_5ago = 0;
  607. }
  608. try {
  609. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Tuesday') {
  610. if ($business->tue) {
  611. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  612. } else {
  613. $bookings_available_5ago = 0;
  614. }
  615. }
  616. } catch (Exception $e) {
  617. $bookings_available_5ago = 0;
  618. }
  619. try {
  620. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Wednesday') {
  621. if ($business->wed) {
  622. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  623. } else {
  624. $bookings_available_5ago = 0;
  625. }
  626. }
  627. } catch (Exception $e) {
  628. $bookings_available_5ago = 0;
  629. }
  630. try {
  631. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Thursday') {
  632. if ($business->thu) {
  633. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  634. } else {
  635. $bookings_available_5ago = 0;
  636. }
  637. }
  638. } catch (Exception $e) {
  639. $bookings_available_5ago = 0;
  640. }
  641. try {
  642. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Friday') {
  643. if ($business->fri) {
  644. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  645. } else {
  646. $bookings_available_5ago = 0;
  647. }
  648. }
  649. } catch (Exception $e) {
  650. $bookings_available_5ago = 0;
  651. }
  652. try {
  653. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Sathurday') {
  654. if ($business->sat) {
  655. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  656. } else {
  657. $bookings_available_5ago = 0;
  658. }
  659. }
  660. } catch (Exception $e) {
  661. $bookings_available_5ago = 0;
  662. }
  663. try {
  664. if (\Carbon\Carbon::now()->subDay(5)->format('l') == 'Sunday') {
  665. if ($business->sun) {
  666. $bookings_available_5ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_5ago - $bookings_pending_5ago;
  667. } else {
  668. $bookings_available_5ago = 0;
  669. }
  670. }
  671. } catch (Exception $e) {
  672. $bookings_available_5ago = 0;
  673. }
  674.  
  675. try {
  676. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Monday') {
  677. if ($business->mon) {
  678. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->mon))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  679. } else {
  680. $bookings_available_6ago = 0;
  681. }
  682. }
  683. } catch (Exception $e) {
  684. $bookings_available_6ago = 0;
  685. }
  686. try {
  687. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Tuesday') {
  688. if ($business->tue) {
  689. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->tue))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  690. } else {
  691. $bookings_available_6ago = 0;
  692. }
  693. }
  694. } catch (Exception $e) {
  695. $bookings_available_6ago = 0;
  696. }
  697. try {
  698. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Wednesday') {
  699. if ($business->wed) {
  700. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->wed))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  701. } else {
  702. $bookings_available_6ago = 0;
  703. }
  704. }
  705. } catch (Exception $e) {
  706. $bookings_available_6ago = 0;
  707. }
  708. try {
  709. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Thursday') {
  710. if ($business->thu) {
  711. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->thu))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  712. } else {
  713. $bookings_available_6ago = 0;
  714. }
  715. }
  716. } catch (Exception $e) {
  717. $bookings_available_6ago = 0;
  718. }
  719. try {
  720. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Friday') {
  721. if ($business->fri) {
  722. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->fri))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  723. } else {
  724. $bookings_available_6ago = 0;
  725. }
  726. }
  727. } catch (Exception $e) {
  728. $bookings_available_6ago = 0;
  729. }
  730. try {
  731. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Sathurday') {
  732. if ($business->sat) {
  733. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sat))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  734. } else {
  735. $bookings_available_6ago = 0;
  736. }
  737. }
  738. } catch (Exception $e) {
  739. $bookings_available_6ago = 0;
  740. }
  741. try {
  742. if (\Carbon\Carbon::now()->subDay(6)->format('l') == 'Sunday') {
  743. if ($business->sun) {
  744. $bookings_available_6ago = Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[0])->diffInHours(Carbon::parse(explode('-', str_replace(' ', '', $business->sun))[1])) * $staff_count - $bookings_completed_6ago - $bookings_pending_6ago;
  745. } else {
  746. $bookings_available_6ago = 0;
  747. }
  748. }
  749. } catch (Exception $e) {
  750. $bookings_available_6ago = 0;
  751. }
  752.  
  753.  
  754. }
  755.  
  756. }
  757. //$business = Business::where('user_id',$user->user_id)->first(); # CHECK, IT WAS 'ID' CHANGED TO 'USER_ID'
  758. //$staff_count = Staff::where('user_id',$user->user_id)->whereNull('deleted_at')->get()->count();
  759.  
  760. /*
  761. * CICLO FOREACH USER
  762. */
  763. $income_products_today=0;
  764. $income_products_1ago=0;
  765. $income_products_2ago=0;
  766. $income_products_3ago=0;
  767. $income_products_4ago=0;
  768. $income_products_5ago=0;
  769. $income_products_6ago=0;
  770. $income_products_7ago=0;
  771.  
  772. $clients_today=0;
  773. $clients_1ago=0;
  774. $clients_2ago=0;
  775. $clients_3ago=0;
  776. $clients_4ago=0;
  777. $clients_5ago=0;
  778. $clients_6ago=0;
  779. $clients_7ago=0;
  780.  
  781. $ticket_today=0;
  782. $ticket_1ago=0;
  783. $ticket_2ago=0;
  784. $ticket_3ago=0;
  785. $ticket_4ago=0;
  786. $ticket_5ago=0;
  787. $ticket_6ago=0;
  788. $ticket_7ago=0;
  789.  
  790. $ticketcount_today=0;
  791. $ticketcount_1ago=0;
  792. $ticketcount_2ago=0;
  793. $ticketcount_3ago=0;
  794. $ticketcount_4ago=0;
  795. $ticketcount_5ago=0;
  796. $ticketcount_6ago=0;
  797. $ticketcount_7ago=0;
  798.  
  799. $income_services_today=0;
  800. $income_services_1ago=0;
  801. $income_services_2ago=0;
  802. $income_services_3ago=0;
  803. $income_services_4ago=0;
  804. $income_services_5ago=0;
  805. $income_services_6ago=0;
  806. $income_services_7ago=0;
  807.  
  808. if(UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->count()>0) {
  809. foreach($users as $user){$income_products_today+=$this->getIncomeProductsSubday(0,$user);};
  810. foreach($users as $user){$income_products_1ago+=$this->getIncomeProductsSubday(1,$user);};
  811. foreach($users as $user){$income_products_2ago+=$this->getIncomeProductsSubday(2,$user);};
  812. foreach($users as $user){$income_products_3ago+=$this->getIncomeProductsSubday(3,$user);};
  813. foreach($users as $user){$income_products_4ago+=$this->getIncomeProductsSubday(4,$user);};
  814. foreach($users as $user){$income_products_5ago+=$this->getIncomeProductsSubday(5,$user);};
  815. foreach($users as $user){$income_products_6ago+=$this->getIncomeProductsSubday(6,$user);};
  816. foreach($users as $user){$income_products_7ago+=$this->getIncomeProductsSubday(7,$user);};
  817.  
  818. foreach($users as $user){$clients_today+=$this->getIncomeClientCountSubday(0,$user);};
  819. foreach($users as $user){$clients_1ago+=$this->getIncomeClientCountSubday(1,$user);};
  820. foreach($users as $user){$clients_2ago+=$this->getIncomeClientCountSubday(2,$user);};
  821. foreach($users as $user){$clients_3ago+=$this->getIncomeClientCountSubday(3,$user);};
  822. foreach($users as $user){$clients_4ago+=$this->getIncomeClientCountSubday(4,$user);};
  823. foreach($users as $user){$clients_5ago+=$this->getIncomeClientCountSubday(5,$user);};
  824. foreach($users as $user){$clients_6ago+=$this->getIncomeClientCountSubday(6,$user);};
  825. foreach($users as $user){$clients_7ago+=$this->getIncomeClientCountSubday(7,$user);};
  826.  
  827. foreach($users as $user){$ticket_today+=$this->getTicketAvSubday(0,$user);};
  828. foreach($users as $user){$ticket_1ago+=$this->getTicketAvSubday(1,$user);};
  829. foreach($users as $user){$ticket_2ago+=$this->getTicketAvSubday(2,$user);};
  830. foreach($users as $user){$ticket_3ago+=$this->getTicketAvSubday(3,$user);};
  831. foreach($users as $user){$ticket_4ago+=$this->getTicketAvSubday(4,$user);};
  832. foreach($users as $user){$ticket_5ago+=$this->getTicketAvSubday(5,$user);};
  833. foreach($users as $user){$ticket_6ago+=$this->getTicketAvSubday(6,$user);};
  834. foreach($users as $user){$ticket_7ago+=$this->getTicketAvSubday(7,$user);};
  835.  
  836. foreach($users as $user){$ticketcount_today+=$this->getTicketCountSubday(0,$user);};
  837. foreach($users as $user){$ticketcount_1ago+=$this->getTicketCountSubday(1,$user);};
  838. foreach($users as $user){$ticketcount_2ago+=$this->getTicketCountSubday(2,$user);};
  839. foreach($users as $user){$ticketcount_3ago+=$this->getTicketCountSubday(3,$user);};
  840. foreach($users as $user){$ticketcount_4ago+=$this->getTicketCountSubday(4,$user);};
  841. foreach($users as $user){$ticketcount_5ago+=$this->getTicketCountSubday(5,$user);};
  842. foreach($users as $user){$ticketcount_6ago+=$this->getTicketCountSubday(6,$user);};
  843. foreach($users as $user){$ticketcount_7ago+=$this->getTicketCountSubday(7,$user);};
  844.  
  845. foreach($users as $user){$income_services_today+=$this->getIncomeSubday(0,$user);};
  846. foreach($users as $user){$income_services_1ago+=$this->getIncomeSubday(1,$user);};
  847. foreach($users as $user){$income_services_2ago+=$this->getIncomeSubday(2,$user);};
  848. foreach($users as $user){$income_services_3ago+=$this->getIncomeSubday(3,$user);};
  849. foreach($users as $user){$income_services_4ago+=$this->getIncomeSubday(4,$user);};
  850. foreach($users as $user){$income_services_5ago+=$this->getIncomeSubday(5,$user);};
  851. foreach($users as $user){$income_services_6ago+=$this->getIncomeSubday(6,$user);};
  852. foreach($users as $user){$income_services_7ago+=$this->getIncomeSubday(7,$user);};
  853.  
  854.  
  855.  
  856. }
  857.  
  858. /*
  859. * END CICLO FOREACH USER
  860. */
  861.  
  862. $data = [
  863.  
  864. 'test' => $test,
  865.  
  866. //'business' => DB::connection('mysql2')->table('business_details')->select('size')->where('user_id',$user->user_id)->first(),
  867.  
  868. 'bookings_week' => $bookings_week,
  869. 'bookings_gainedlost' => abs($bookings_week - $bookings_twomonthago),
  870. 'bookings_ispositive' => ($bookings_week - $bookings_twomonthago >= 1) ? true : false,
  871.  
  872. 'clients_week' => $clients_week,
  873. 'clients_gainedlost' => abs($clients_week - $clients_twomonthago),
  874. 'clients_ispositive' => ($clients_week - $clients_twomonthago >= 1) ? true : false,
  875.  
  876. 'sales_week' => $sales_week,
  877. 'sales_gainedlost' => abs($sales_week - $sales_twomonthago),
  878. 'sales_ispositive' => ($sales_week - $sales_twomonthago >= 1) ? true : false,
  879.  
  880. 'income_month' => $income_month,
  881. 'income_gainedlost' => abs($income_month - $income_twomonthago),
  882. 'income_ispositive' => ($income_month - $income_twomonthago >= 1) ? true : false,
  883. # CITAS COMPLETADAS LA ÚLTIMA SEMANA
  884. 'bookings_completed_today' => (isset($bookings_completed_today)) ? $bookings_completed_today : 0,
  885. 'bookings_completed_1ago' => (isset($bookings_completed_1ago)) ? $bookings_completed_1ago : 0,
  886. 'bookings_completed_2ago' => (isset($bookings_completed_2ago)) ? $bookings_completed_2ago : 0,
  887. 'bookings_completed_3ago' => (isset($bookings_completed_3ago)) ? $bookings_completed_3ago : 0,
  888. 'bookings_completed_4ago' => (isset($bookings_completed_4ago)) ? $bookings_completed_4ago : 0,
  889. 'bookings_completed_5ago' => (isset($bookings_completed_5ago)) ? $bookings_completed_5ago : 0,
  890. 'bookings_completed_6ago' => (isset($bookings_completed_6ago)) ? $bookings_completed_6ago : 0,
  891. # CITAS CANCELADAS LA ÚLTIMA SEMANA
  892. 'bookings_cancelled_today' => (isset($bookings_cancelled_today)) ? $bookings_cancelled_today : 0,
  893. 'bookings_cancelled_1ago' => (isset($bookings_cancelled_1ago)) ? $bookings_cancelled_1ago : 0,
  894. 'bookings_cancelled_2ago' => (isset($bookings_cancelled_2ago)) ? $bookings_cancelled_2ago : 0,
  895. 'bookings_cancelled_3ago' => (isset($bookings_cancelled_3ago)) ? $bookings_cancelled_3ago : 0,
  896. 'bookings_cancelled_4ago' => (isset($bookings_cancelled_4ago)) ? $bookings_cancelled_4ago : 0,
  897. 'bookings_cancelled_5ago' => (isset($bookings_cancelled_5ago)) ? $bookings_cancelled_5ago : 0,
  898. 'bookings_cancelled_6ago' => (isset($bookings_cancelled_6ago)) ? $bookings_cancelled_6ago : 0,
  899. # CITAS PENDIENTES LA ÚLTIMA SEMANA
  900. 'bookings_pending_today' => (isset($bookings_pending_today)) ? $bookings_pending_today : 0,
  901. 'bookings_pending_1ago' => (isset($bookings_pending_1ago)) ? $bookings_pending_1ago : 0,
  902. 'bookings_pending_2ago' => (isset($bookings_pending_2ago)) ? $bookings_pending_2ago : 0,
  903. 'bookings_pending_3ago' => (isset($bookings_pending_3ago)) ? $bookings_pending_3ago : 0,
  904. 'bookings_pending_4ago' => (isset($bookings_pending_4ago)) ? $bookings_pending_4ago : 0,
  905. 'bookings_pending_5ago' => (isset($bookings_pending_5ago)) ? $bookings_pending_5ago : 0,
  906. 'bookings_pending_6ago' => (isset($bookings_pending_6ago)) ? $bookings_pending_6ago : 0,
  907. # CITAS DISPONIBLES LA ÚLTIMA SEMANA
  908. 'bookings_available_today' => (isset($bookings_available_today)) ? $bookings_available_today : 0,
  909. 'bookings_available_1ago' => (isset($bookings_available_1ago)) ? $bookings_available_1ago : 0,
  910. 'bookings_available_2ago' => (isset($bookings_available_2ago)) ? $bookings_available_2ago : 0,
  911. 'bookings_available_3ago' => (isset($bookings_available_3ago)) ? $bookings_available_3ago : 0,
  912. 'bookings_available_4ago' => (isset($bookings_available_4ago)) ? $bookings_available_4ago : 0,
  913. 'bookings_available_5ago' => (isset($bookings_available_5ago)) ? $bookings_available_5ago : 0,
  914. 'bookings_available_6ago' => (isset($bookings_available_6ago)) ? $bookings_available_6ago : 0,
  915.  
  916. # INGRESOS PRODUCTOS LA ÚLTIMA SEMANA
  917.  
  918. 'income_products_today' => $income_products_today,
  919. 'income_products_1ago' => $income_products_1ago,
  920. 'income_products_2ago' => $income_products_2ago,
  921. 'income_products_3ago' => $income_products_3ago,
  922. 'income_products_4ago' => $income_products_4ago,
  923. 'income_products_5ago' => $income_products_5ago,
  924. 'income_products_6ago' => $income_products_6ago,
  925. 'income_products_7ago' => $income_products_7ago,
  926.  
  927.  
  928.  
  929.  
  930.  
  931. # CLIENTES LA ÚLTIMA SEMANA
  932. 'clients_today' => $clients_today,
  933. 'clients_1ago' => $clients_1ago,
  934. 'clients_2ago' => $clients_2ago,
  935. 'clients_3ago' => $clients_3ago,
  936. 'clients_4ago' => $clients_4ago,
  937. 'clients_5ago' => $clients_5ago,
  938. 'clients_6ago' => $clients_6ago,
  939. 'clients_7ago' => $clients_7ago,
  940.  
  941. # TICKETS LA ÚLTIMA SEMANA
  942. 'ticket_today' => $ticket_today,
  943. 'ticket_1ago' => $ticket_1ago,
  944. 'ticket_2ago' => $ticket_2ago,
  945. 'ticket_3ago' => $ticket_3ago,
  946. 'ticket_4ago' => $ticket_4ago,
  947. 'ticket_5ago' => $ticket_5ago,
  948. 'ticket_6ago' => $ticket_6ago,
  949. 'ticket_7ago' => $ticket_7ago,
  950.  
  951. # contador de TICKETS LA ÚLTIMA SEMANA
  952. 'ticketcount_today' => $ticketcount_today,
  953. 'ticketcount_1ago' => $ticketcount_1ago,
  954. 'ticketcount_2ago' => $ticketcount_2ago,
  955. 'ticketcount_3ago' => $ticketcount_3ago,
  956. 'ticketcount_4ago' => $ticketcount_4ago,
  957. 'ticketcount_5ago' => $ticketcount_5ago,
  958. 'ticketcount_6ago' => $ticketcount_6ago,
  959. 'ticketcount_7ago' => $ticketcount_7ago,
  960.  
  961.  
  962. # INGRESOS SERVICIOS LA ÚLTIMA SEMANA
  963. 'income_services_today' => $income_services_today,
  964. 'income_services_1ago' => $income_services_1ago,
  965. 'income_services_2ago' => $income_services_2ago,
  966. 'income_services_3ago' => $income_services_3ago,
  967. 'income_services_4ago' => $income_services_4ago,
  968. 'income_services_5ago' => $income_services_5ago,
  969. 'income_services_6ago' => $income_services_6ago,
  970. 'income_services_7ago' => $income_services_7ago,
  971.  
  972. #'test' => $this->getIncomeSubday(0),
  973. 'test' => \Carbon\Carbon::now()->subDay(0)->format('n/j/Y'),
  974.  
  975. 'users'=>$all_users_ids,
  976. 'selected_users'=>$selected_users,
  977.  
  978. ];
  979.  
  980.  
  981. #return dd($this->getTopClients());
  982.  
  983. # SAVE LAST TIME USER WAS ACTIVE, WE ARE USING "DASHBOARD" AS THE USER LANDS HERE EVERYTIME IT LOGS IN
  984.  
  985. //try { UserLilu::where('user_id',$user->user_id)->update([ 'last_login'=> \Carbon\Carbon::now() ]); } catch(Exception $e) { }
  986.  
  987. /*Cookie::queue('name', 'Jose', 60);
  988. return response('Set cookie');*/
  989.  
  990. /*$created = \Carbon\Carbon::parse($user->conekta_trial_endsat);
  991. $now = \Carbon\Carbon::now();
  992. $days_left = $now->diff($created)->format("%r%a");
  993. if($days_left >= 0 && $days_left <= 5) {
  994. if(!isset($_COOKIE['trial_message_lilu'])) {
  995. #setcookie('trial_message_lilu','0',3600*6);
  996. }
  997. }
  998.  
  999. \Cookie::make('trial_message_lilu','0',3600*6);
  1000.  
  1001. $ok = (\Cookie::get('trial_message_lilu')) ? 'Existe' : 'No existe';
  1002. dd($ok);*/
  1003.  
  1004.  
  1005. return view('home',$data);
  1006. }
  1007. public function getBookingsCount($days,$user) {
  1008.  
  1009. $date = \Carbon\Carbon::now()->subDay($days)->format('n/j/Y');
  1010.  
  1011. $count = Booking::where('user_id',$user->user_id)->where('start_date','like','%'.$date.'%')->where('status',2)->whereNull('deleted_at')->get()->count();
  1012.  
  1013. return $count;
  1014. }
  1015.  
  1016. public function getIncomeSubday($days,$user) {
  1017. date_default_timezone_set("America/Mexico_City");
  1018. $date = \Carbon\Carbon::now()->subDay($days)->format('n/j/Y');
  1019.  
  1020. $services = Booking::where('user_id',$user->user_id)->where('start_date','like','%'.$date.'%')->where('status',2)->whereNull('deleted_at')->get();
  1021. $total =0;
  1022. foreach($services as $res) {
  1023. if($res->discount!=0)
  1024. $total += $res->price - ($res->price * ($res->discount/100));
  1025. else
  1026. $total += $res->price;
  1027. }
  1028.  
  1029. return $total;
  1030.  
  1031.  
  1032. }
  1033. public function getIncomeProductsSubday($days,$user) {
  1034. $date = \Carbon\Carbon::now()->subDay($days)->toDateString();
  1035.  
  1036. $from_products = Sale::where('user_id',$user->user_id)
  1037. ->where('created_at','like','%'.$date.'%')
  1038. ->whereNull('deleted_at')
  1039. ->get()
  1040. ->sum('total');
  1041.  
  1042. return $from_products;
  1043. }
  1044.  
  1045.  
  1046. public function getIncomeClientCountSubday($days,$user) {
  1047. $date = \Carbon\Carbon::now()->subDay($days)->toDateString();
  1048.  
  1049. $from_clients = Client::where('user_id',$user->user_id)
  1050. ->where('created_at','like','%'.$date.'%')
  1051. ->whereNull('deleted_at')
  1052. ->count();
  1053.  
  1054. return $from_clients;
  1055. }
  1056.  
  1057. public function getTicketAvSubday($days,$user) {
  1058. $date = \Carbon\Carbon::now()->subDay($days)->toDateString();
  1059.  
  1060. $from_tickets = Ticket::where('user_id',$user->user_id)
  1061. ->where('finalized_at','like','%'.$date.'%')
  1062. ->whereNull('deleted_at')
  1063. ->sum('total1');
  1064.  
  1065. return $from_tickets;
  1066. }
  1067.  
  1068. public function getTicketCountSubday($days,$user) {
  1069. $date = \Carbon\Carbon::now()->subDay($days)->toDateString();
  1070.  
  1071. $from_tickets = Ticket::where('user_id',$user->user_id)
  1072. ->where('finalized_at','like','%'.$date.'%')
  1073. ->whereNull('deleted_at')
  1074. ->count();
  1075.  
  1076. return $from_tickets;
  1077. }
  1078.  
  1079.  
  1080. public function clients() {
  1081. $data = [
  1082. 'users' => DB::connection('mysql2')->table('users')->where('role',1)->whereNull('deleted_at')->orderBy('last_login','desc')->get(),
  1083. ];
  1084.  
  1085. return view('clients',$data);
  1086. }
  1087.  
  1088. public function demo() {
  1089. $data['demo'] = DB::connection('mysql2')->table('demo')->whereNull('deleted_at')->orderBy('created_at','asc')->get();
  1090.  
  1091. return view('demo',$data);
  1092. }
  1093.  
  1094. public function promociones() {
  1095. $data = [
  1096. 'promos' => DB::connection('mysql2')->table('promocodes')->whereNull('deleted_at')->get(),
  1097. ];
  1098.  
  1099. return view('promociones',$data);
  1100. }
  1101.  
  1102. public function promocionesPost() {
  1103. $name = Input::get('name');
  1104. $code = Input::get('code');
  1105. $price = Input::get('price');
  1106. $stock = Input::get('stock');
  1107. $expires = Input::get('expires');
  1108.  
  1109. date_default_timezone_set("America/Mexico_City");
  1110. $created_at = new \DateTime();
  1111.  
  1112. DB::connection('mysql2')->table('promocodes')->insert([
  1113. 'name' => $name,
  1114. 'code' => $code,
  1115. 'price' => $price,
  1116. 'stock' => $stock,
  1117. 'created_at' => $created_at,
  1118. ]);
  1119.  
  1120. return Redirect::route('promociones');
  1121. }
  1122.  
  1123.  
  1124. public function getDashboardTopClients() {
  1125. if(count(Input::get('user_ids'))>0){
  1126. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->whereIn('user_id',Input::get('user_ids'))->select('user_id')->get();
  1127. foreach($users as $user){
  1128. $selected_users[]=$user->user_id;
  1129. }
  1130. }else{
  1131. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->get();
  1132. foreach($users as $user){
  1133. $selected_users[]=$user->user_id;
  1134. }
  1135. }
  1136.  
  1137.  
  1138. $clients = Client::select('id','user_id','name','lastname')->whereIn('user_id',$selected_users)->whereNull('deleted_at')->get();
  1139. $i=0;
  1140. foreach($clients as $client) {
  1141.  
  1142. $bookings = collect(Booking::select('ticket_total','ticket_total2')->where('user_id',$client->user_id)->where('client_id',$client->id)->where('status',2)->whereNull('parent_booking')->whereNull('deleted_at')->get());
  1143. $transactions = collect(Transaction::select('ticket_total','ticket_total2')->where('user_id',$client->user_id)->where('client_id',$client->id)->whereNull('event_id')->whereNull('deleted_at')->get());
  1144.  
  1145. $tickets = $bookings->merge($transactions);
  1146. $x=0;
  1147. foreach($tickets as $res) {
  1148. $x += $res->ticket_total + $res->ticket_total2;
  1149. }
  1150. $ret[$i] = [
  1151. 'client_id' => $client->id,
  1152. 'client' => $client->lastname.' '.$client->name,
  1153. 'total' => $x,
  1154. ];
  1155. $i++;
  1156. }
  1157. $collection = collect($ret)->sortByDesc('total')->take(10);
  1158. $i=0;
  1159. foreach($collection as $res) {
  1160. $ok[$i] = [
  1161. 'client' => str_limit($res['client'],20),
  1162. 'total' => '$'.number_format((int)$res['total']),
  1163. ];
  1164. $i++;
  1165. }
  1166. return $ok;
  1167. }
  1168.  
  1169. public function getDashboardStock() {
  1170. /*$products = Product::select('id','name','product_category','product_class','stock')->where('user_id',Auth::user()->user_id)->whereNull('deleted_at')->orderBy('stock','asc')->get()->take(10);
  1171.  
  1172. $i=0;
  1173. foreach($products as $res) {
  1174. try {
  1175. $category = ProductCategory::where('id',$res->product_category)->first()->name;
  1176. } catch(Exception $e) { $category = '-';}
  1177. try {
  1178. $make = ProductClass::where('id',$res->product_class)->first()->name;
  1179. } catch(Exception $e) { $make = '-';}
  1180.  
  1181. $ok[$i] = [
  1182. 'nombre' => str_limit($res->name,15),
  1183. 'stock' => str_limit($res->stock,15),
  1184. 'marca' => str_limit($make,15),
  1185. 'categoria' => $category,
  1186. ];
  1187. $i++;
  1188. }
  1189. return $ok;*/
  1190. if(count(Input::get('user_ids'))>0){
  1191. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->whereIn('user_id',Input::get('user_ids'))->select('user_id')->get();
  1192. foreach($users as $user){
  1193. $selected_users[]=$user->user_id;
  1194. }
  1195. }else{
  1196. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->get();
  1197. foreach($users as $user){
  1198. $selected_users[]=$user->user_id;
  1199. }
  1200. }
  1201. $products = Product::select('id','name','product_category','product_class','stock')->whereIn('user_id',$selected_users)->whereNull('deleted_at')->orderBy('stock','asc')->get();
  1202. $i=0;
  1203. $return = null;
  1204.  
  1205. $ok=[];
  1206. foreach($products as $res) {
  1207.  
  1208. try {
  1209. $category = (ProductCategory::where('id',$res->product_category)->count()) ? ProductCategory::where('id',$res->product_category)->first()->name : '-';
  1210. } catch(Exception $e) { $category = '-';}
  1211. try {
  1212. $make = (ProductClass::where('id',$res->product_class)->count()) ? ProductClass::where('id',$res->product_class)->first()->name : '-';
  1213. } catch(Exception $e) { $make = '-';}
  1214. $sales = Sale::where('product_id',$res->id)->get();
  1215.  
  1216. $sum = 0;
  1217. if($sales->count()) {
  1218. foreach($sales as $sale) {
  1219. $sum += $sale->total;
  1220. }
  1221. }
  1222.  
  1223. $ok[] = [
  1224. 'nombre' => str_limit($res->name,15),
  1225. 'sum' => $sum,
  1226. 'marca' => str_limit($make,15),
  1227. 'categoria' => $category,
  1228. ];
  1229. $i++;
  1230. }
  1231. if(count($ok)){
  1232. $collection = collect($ok)->keyBy('sum')->sortByDesc('sum')->take(10);
  1233.  
  1234. $i=0;
  1235. foreach($collection as $res) {
  1236. $xd[] = [
  1237. 'nombre' => $res['nombre'],
  1238. 'sum' => '$'.number_format((int)$res['sum']),
  1239. 'marca' => $res['marca'],
  1240. 'categoria' => $res['categoria'],
  1241. ];
  1242. $i++;
  1243. }
  1244. return $xd;
  1245. }else{
  1246. $xd[0] = [
  1247. 'nombre' => '-',
  1248. 'sum' => '-',
  1249. 'marca' => '-',
  1250. 'categoria' => '-',
  1251. ];
  1252. return $xd;
  1253. }
  1254.  
  1255.  
  1256. }
  1257. public function getDashboardServicesSold(){
  1258.  
  1259. /*try{
  1260. $topServiceCategories=Booking::join('services', 'bookings.service_id', '=', 'services.id')->select('services.service_category', DB::raw('count(*) as total'),DB::raw('sum(bookings.price*(bookings.discount/100)) as ganancias'))->where('bookings.user_id',Auth::user()->user_id)->whereNull('bookings.deleted_at')->groupBy('services.service_category')->orderBy('ganancias','DESC')->pluck('services.service_category')->take(10);
  1261. }catch(Exception $e){
  1262. $topServiceCategories=Booking::join('services', 'bookings.service_id', '=', 'services.id')->select('services.service_category', DB::raw('count(*) as total'),DB::raw('sum(bookings.price) as ganancias'))->where('bookings.user_id',Auth::user()->user_id)->whereNull('bookings.deleted_at')->groupBy('services.service_category')->orderBy('ganancias','DESC')->pluck('services.service_category')->take(10);
  1263. }*/
  1264. if(count(Input::get('user_ids'))>0){
  1265. $users=Input::get('user_ids');
  1266. }else{
  1267. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->pluck('user_id');
  1268. }
  1269. //$users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->pluck('user_id');
  1270.  
  1271. $topServiceCategories=Booking::join('services', 'bookings.service_id', '=', 'services.id')->when('bookings.discount'>0, function ($query){
  1272. return $query->select('services.service_category', DB::raw('count(*) as total'),DB::raw('sum(bookings.price -(bookings.price*(bookings.discount/100))) as ganancias'));
  1273. }, function ($query) {
  1274. return $query->select('services.service_category', DB::raw('count(*) as total'),DB::raw('sum(bookings.price) as ganancias'));
  1275. })->whereIn('bookings.user_id',$users)->whereNull('bookings.deleted_at')->groupBy('services.service_category')->orderBy('ganancias','DESC')->pluck('services.service_category');#->take(10);
  1276.  
  1277.  
  1278.  
  1279. //$topServiceCategories=Booking::join('services', 'bookings.service_id', '=', 'services.id')->select('services.service_category', DB::raw('count(*) as total'),DB::raw('sum(bookings.price) as ganancias'))->where('bookings.user_id',Auth::user()->user_id)->whereNull('bookings.deleted_at')->groupBy('services.service_category')->orderBy('ganancias','DESC')->pluck('services.service_category')->take(10);
  1280. $topServices_names=[];
  1281. $a=0;
  1282. $booking_sales_service_categories=[];
  1283. foreach($topServiceCategories as $topServiceCategory){
  1284. $service_category=ServiceCategory::whereIn('user_id',$users)->where('id',$topServiceCategory)->get()->first();
  1285. $topServices_names[]=$service_category['name'];
  1286. $booking_total_7_days=[];
  1287. for($i=0;$i<=7;$i++){
  1288. $day=Carbon::now()->subDay($i)->toDateString();
  1289. $bookingsInDay=Booking::join('services', 'bookings.service_id', '=', 'services.id')->whereIn('bookings.user_id',$users)->whereNull('bookings.deleted_at')->Where('services.service_category',$topServiceCategory)->where('bookings.created_at','like','%'.$day.'%')->get();
  1290. $totalSalesBookingInDay=0;
  1291. foreach( $bookingsInDay as $bookingInDay){
  1292. if ($bookingInDay->discount!=0){
  1293. $totalSalesBookingInDay+=$bookingInDay->price -($bookingInDay->price * ($bookingInDay->discount/100));
  1294. }else{
  1295. $totalSalesBookingInDay+=$bookingInDay->price;
  1296. }
  1297. }
  1298. $booking_total_7_days[]=$totalSalesBookingInDay;
  1299. }
  1300. $booking_sales_service_categories[$a]=$booking_total_7_days;
  1301. $a++;
  1302. }
  1303. $otherServiceCategories=ServiceCategory::whereIn('user_id',$users)->whereNotIn('id',$topServiceCategories)->get();
  1304. $other=0;
  1305. $other_booking_sales_service_categories=[];
  1306. foreach($otherServiceCategories as $otherServiceCategory){
  1307. for($i=0;$i<=7;$i++) {
  1308. $day = Carbon::now()->subDay($i)->toDateString();
  1309. $other_bookingsInDay = Booking::join('services', 'bookings.service_id', '=', 'services.id')->whereIn('bookings.user_id', $users)->whereNull('bookings.deleted_at')->Where('services.service_category',$otherServiceCategory['id'])->where('bookings.created_at', 'like', '%' . $day . '%')->get();
  1310. $other_totalSalesBookingInDay = 0;
  1311. foreach ($other_bookingsInDay as $other_bookingInDay) {
  1312. if ($other_bookingInDay->discount != 0) {
  1313. $other_totalSalesBookingInDay += $other_bookingInDay->price -($other_bookingInDay->price * ($other_bookingInDay->discount / 100));
  1314. } else {
  1315. $other_totalSalesBookingInDay += $other_bookingInDay->price;
  1316. }
  1317. }
  1318. $other_category_booking_total_7_days[] = $other_totalSalesBookingInDay;
  1319. }
  1320. $other_booking_sales_service_categories[$other]=$other_category_booking_total_7_days;
  1321. $other++;
  1322. }
  1323. //return $other_booking_sales_service_categories;
  1324. /*for($i=0;$i<7;$i++){
  1325. $value = array_sum(array_column($other_booking_sales_service_categories,$i));
  1326. return $value;
  1327. }*/
  1328. $one=0;
  1329. $two=0;
  1330. $tree=0;
  1331. $four=0;
  1332. $five=0;
  1333. $six=0;
  1334. $seven=0;
  1335. $eight=0;
  1336. foreach ($other_booking_sales_service_categories as $subArray) {
  1337. $one += $subArray[0];
  1338. $two += $subArray[1];
  1339. $tree+= $subArray[2];
  1340. $four+= $subArray[3];
  1341. $five+= $subArray[4];
  1342. $six += $subArray[5];
  1343. $seven+= $subArray[6];
  1344. $eight+= $subArray[7];
  1345.  
  1346. /*foreach ($subArray as $id=>$value) {
  1347. $sumArray[$id]+=$value;
  1348. }*/
  1349. }
  1350. if($one+$two+$tree+$four+$five+$six+$seven+$eight > 0)
  1351. $resultss=[$one,$two,$tree,$four,$five,$six,$seven,$eight];
  1352. else
  1353. $resultss=false;
  1354.  
  1355. $dataBookings=['topServices_names'=>$topServices_names,'data'=>$booking_sales_service_categories,'others_data'=>$resultss];
  1356. return $dataBookings;
  1357. }
  1358.  
  1359.  
  1360. public function getDashboardProductsSold() {
  1361. /*$topProductBrands=Sale::where('sales.user_id',Auth::user()->user_id)
  1362. ->join('products','sales.product_id','=','products.id')
  1363. ->join('product_category','products.product_category','=','product_category.id')
  1364. ->select('products.product_category', DB::raw('count(*) as total'),DB::raw('sum(sales.total) as ganancias'))
  1365. ->whereNull('sales.deleted_at')
  1366. ->groupBy('products.product_category')
  1367. ->orderBy('ganancias','DESC')
  1368. ->get()
  1369. ->take(10)
  1370. ;*/
  1371. if(count(Input::get('user_ids'))>0){
  1372. $users=Input::get('user_ids');
  1373. }else{
  1374. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->pluck('user_id');
  1375. }
  1376. //$users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->pluck('user_id');
  1377. //$user=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->get()->first();
  1378. $products = Product::select('id','name','product_category','product_class','stock')->whereIn('user_id',$users)->whereNull('deleted_at')->orderBy('stock','asc')->get();
  1379.  
  1380.  
  1381. $i=0;
  1382. $return = null;
  1383. foreach($products as $res) {
  1384.  
  1385. try {
  1386. $category = (ProductCategory::where('id',$res->product_category)->count()) ? ProductCategory::where('id',$res->product_category)->first()->name : '-';
  1387. } catch(Exception $e) { $category = '-';}
  1388. try {
  1389. $make = (ProductClass::where('id',$res->product_class)->count()) ? ProductClass::where('id',$res->product_class)->first()->name : '-';
  1390. } catch(Exception $e) { $make = '-';}
  1391.  
  1392.  
  1393. $sales = Sale::where('product_id',$res->id)->get();
  1394. $sum = 0;
  1395. if($sales->count()) {
  1396. foreach($sales as $sale) {
  1397. $sum += $sale->total;
  1398. }
  1399. }
  1400.  
  1401. $ok[] = [
  1402. 'id'=>$res->id,
  1403. 'nombre' => str_limit($res->name,15),
  1404. 'sum' => $sum,
  1405. 'marca' => str_limit($make,15),
  1406. 'categoria' => $category,
  1407. ];
  1408. $i++;
  1409. }
  1410.  
  1411. $collection = collect($ok)->keyBy('sum')->sortByDesc('sum');#->take(10);
  1412. $i=0;
  1413. $product_names=[];
  1414. foreach($collection as $res) {
  1415. $product_names[]=$res['nombre'];
  1416. $xd[] = [
  1417. 'id'=>$res['id'],
  1418. 'nombre' => $res['nombre'],
  1419. 'sum' => '$'.number_format((int)$res['sum']),
  1420. 'marca' => $res['marca'],
  1421. 'categoria' => $res['categoria'],
  1422. ];
  1423. $xd_ids[]=$res['id'];
  1424. $i++;
  1425. }
  1426. $a=0;
  1427. foreach($xd as $product){
  1428. //$salesInDay=0;
  1429. $product_day =[];
  1430. for($i=0;$i<=7;$i++){
  1431. $day=Carbon::now()->subDay($i)->toDateString();
  1432. $salesInDay=Sale::whereIn('user_id',$users)->where('product_id',$product['id'])->where('created_at','like','%'.$day.'%')->whereNull('deleted_at')->get();
  1433. $totalSalesProductInDay=0;
  1434. foreach($salesInDay as $saleInDay){
  1435. if($saleInDay->discount!=0){
  1436. $totalSalesProductInDay+=$saleInDay->total * ($saleInDay->discount/100);
  1437. }else{
  1438. $totalSalesProductInDay+=$saleInDay->total;
  1439. }
  1440. }
  1441. $product_day[]=$totalSalesProductInDay;
  1442. }
  1443.  
  1444. $products_sales[$a]=$product_day;
  1445. $a++;
  1446. }
  1447.  
  1448. $b=0;
  1449. $otherProducts = Product::select('id','name','product_category','product_class','stock')->whereIn('user_id',$users)->whereNotIn('id',$xd_ids)->whereNull('deleted_at')->orderBy('stock','asc')->get();
  1450. foreach($otherProducts as $product){
  1451.  
  1452. $other_product_day =[];
  1453. for($i=0;$i<=7;$i++){
  1454. $day=Carbon::now()->subDay($i)->toDateString();
  1455. $other_salesInDay=Sale::whereIn('user_id',$users)->where('product_id',$product['id'])->where('created_at','like','%'.$day.'%')->whereNull('deleted_at')->get();
  1456. $other_totalSalesProductInDay=0;
  1457. foreach($other_salesInDay as $other_saleInDay){
  1458. if($other_saleInDay->discount!=0){
  1459. $other_totalSalesProductInDay+=$other_saleInDay->total * ($other_saleInDay->discount/100);
  1460. }else{
  1461. $other_totalSalesProductInDay+=$other_saleInDay->total;
  1462. }
  1463. }
  1464. $other_product_day[]=$other_totalSalesProductInDay;
  1465. }
  1466. $other_products_sales[$b]=$other_product_day;
  1467. $b++;
  1468. }
  1469. if(empty($other_products_sales)){
  1470. $other_products_sales[]=0;
  1471. };
  1472. $sumArray = [];
  1473. $one=0;
  1474. $two=0;
  1475. $tree=0;
  1476. $four=0;
  1477. $five=0;
  1478. $six=0;
  1479. $seven=0;
  1480. $eight=0;
  1481. foreach ($other_products_sales as $subArray) {
  1482. $one += $subArray[0];
  1483. $two += $subArray[1];
  1484. $tree+= $subArray[2];
  1485. $four+= $subArray[3];
  1486. $five+= $subArray[4];
  1487. $six += $subArray[5];
  1488. $seven+= $subArray[6];
  1489. $eight+= $subArray[7];
  1490. }
  1491. if($one+$two+$tree+$four+$five+$six+$seven+$eight > 0)
  1492. $other_products_sales=[$one,$two,$tree,$four,$five,$six,$seven,$eight];
  1493. else
  1494. $other_products_sales=false;
  1495. $dataProducts=['product_names'=>$product_names,'data'=>$products_sales,'others_data'=>$other_products_sales];
  1496. return $dataProducts;
  1497.  
  1498. }
  1499.  
  1500.  
  1501. public function getDashboardGraphSchedule(){
  1502. $morning=0;
  1503. $midday=0;
  1504. $noon=0;
  1505. $afternoon=0;
  1506. $night=0;
  1507. if(count(Input::get('user_ids'))>0){
  1508. $users=Input::get('user_ids');
  1509. }else{
  1510. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->pluck('user_id');
  1511. }
  1512. $tickets=Ticket::whereIn('user_id',$users)->whereNull('deleted_at')->get();
  1513. foreach($tickets as $ticket){
  1514. switch(true){
  1515. case ($ticket->created_at->format('H:i:s')>='09:00:01')&& ($ticket->created_at->format('H:i:s')<='11:00:00'):
  1516. $morning+=1;
  1517. break;
  1518. case ($ticket->created_at->format('H:i:s')>='11:00:01')&& ($ticket->created_at->format('H:i:s')<='14:00:00'):
  1519. $midday+=1;
  1520. break;
  1521. case ($ticket->created_at->format('H:i:s')>='14:00:01')&& ($ticket->created_at->format('H:i:s')<='17:00:00'):
  1522. $noon+=1;
  1523. break;
  1524. case ($ticket->created_at->format('H:i:s')>='17:00:01')&& ($ticket->created_at->format('H:i:s')<='20:00:00'):
  1525. $afternoon+=1;
  1526. break;
  1527. case ($ticket->created_at->format('H:i:s')>='20:00:01')&& ($ticket->created_at->format('H:i:s')<='11:59:59'):
  1528. $night+=1;
  1529. break;
  1530. }
  1531. }
  1532.  
  1533. $schdule_morning=[ 'name'=>'9am - 11am','y'=>$morning ];
  1534. $schdule_midday=[ 'name'=>'11am - 2pm','y'=>$midday ];
  1535. $schdule_noon=[ 'name'=>'2pm - 5pm','y'=>$noon ];
  1536. $schdule_afternoon=[ 'name'=>'5pm - 8pm','y'=>$afternoon ];
  1537. $schdule_night=[ 'name'=>'8pm+','y'=>$night ];
  1538.  
  1539. $dataSchedule=[$schdule_morning,$schdule_midday,$schdule_noon,$schdule_afternoon,$schdule_night];
  1540. return $dataSchedule;
  1541. }
  1542. public function getDashboardTicketDays(){
  1543. if(count(Input::get('user_ids'))>0){
  1544. $users=Input::get('user_ids');
  1545. }else{
  1546. $users=UserLilu::where('franchise_id',Auth::user()->franchise_id)->groupBy('user_id')->select('user_id')->pluck('user_id');
  1547. }
  1548.  
  1549. $business = Business::whereIn('user_id',$users)->first(); # CHECK, IT WAS 'ID' CHANGED TO 'USER_ID'
  1550. $today = \Carbon\Carbon::now()->format('m/d/Y');
  1551.  
  1552. $monthAgo=\Carbon\Carbon::now()->subMonth(1)->format('m/d/Y');
  1553. $bookingsAgo=Booking::whereIn('user_id',$users)->where('status',2)->whereNull('deleted_at')->get();
  1554. # SALES
  1555. $sales=0;
  1556. $sales_mon=0;
  1557. $sales_tues=0;
  1558. $sales_wed=0;
  1559. $sales_thu=0;
  1560. $sales_fri=0;
  1561. $sales_satu=0;
  1562. $sales_sun=0;
  1563. $salesAgo=Sale::whereIn('user_id',$users)->whereNull('deleted_at')->get();
  1564. foreach($salesAgo as $sale){
  1565. $carbon_date= Carbon::parse($sale->created_at) ;
  1566. switch($carbon_date->format('l')){
  1567. case 'Monday': $sales_mon+=1; break;
  1568. case 'Tuesday': $sales_tues+=1; break;
  1569. case 'Wednesday': $sales_wed+=1; break;
  1570. case 'Thursday': $sales_thu+=1; break;
  1571. case 'Friday': $sales_fri+=1; break;
  1572. case 'Saturday': $sales_satu+=1; break;
  1573. case 'Sunday': $sales_sun+=1; break;
  1574. }
  1575. }
  1576. # END SALES
  1577. $mon=0;
  1578. $tues=0;
  1579. $wed=0;
  1580. $thu=0;
  1581. $fri=0;
  1582. $satu=0;
  1583. $sun=0;
  1584. foreach($bookingsAgo as $booking){
  1585. $carbon_start_date= Carbon::parse($booking->start_date) ;
  1586. switch($carbon_start_date->format('l')){
  1587. case 'Monday': $mon+=1; break;
  1588. case 'Tuesday': $tues+=1; break;
  1589. case 'Wednesday': $wed+=1; break;
  1590. case 'Thursday': case 'Friday': $fri+=1; break;
  1591. case 'Saturday': $satu+=1; break;
  1592. case 'Sunday': $sun+=1; break;
  1593. }
  1594. }
  1595. $services=[$mon,$tues,$wed,$thu,$fri,$satu,$sun];
  1596. $products=[$sales_mon,$sales_tues,$sales_wed,$sales_thu,$sales_fri,$sales_satu,$sales_sun];
  1597. $dataBooking=['services'=>$services,'products'=>$products];
  1598.  
  1599. return $dataBooking;
  1600. }
  1601.  
  1602.  
  1603.  
  1604. public function user_settings(){
  1605. return view('settings.index');
  1606. }
  1607. public function change_password(){
  1608. $data = Input::only(
  1609. 'old_password',
  1610. 'new_password'
  1611. );
  1612. $rules = array(
  1613. 'old_password' => 'required|min:4',
  1614. 'new_password' => 'required|min:4',
  1615. );
  1616. $validator = \Illuminate\Support\Facades\Validator::make($data, $rules);
  1617. if ($validator->passes()) {
  1618. if (Hash::check(Input::get('old_password'), Auth::user()->password)) {
  1619. Auth::user()->password=Hash::make(Input::get('new_password'));
  1620. Auth::user()->save();
  1621. return redirect()->back()->with('global-success', 'Contraseña guardada correctamente');
  1622. }else{
  1623. return redirect()->back()->with('global-error', 'La contraseña es incorrecta');
  1624. }
  1625. }else{
  1626. return redirect()->back()->with('global-error', 'La contraseña debe ser de más de 3 caracteres');
  1627.  
  1628. }
  1629. }
  1630. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement