HomeController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\DMCities;
  4. use App\Models\OrderActivities;
  5. use App\Models\Orders;
  6. use App\Models\Outlets;
  7. use App\Restaurants;
  8. use App\User;
  9. use App\RestoUsers;
  10. use App\SendEmail;
  11. use Carbon\Carbon;
  12. use Illuminate\Http\Request;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Hash;
  15. use Illuminate\Support\Str;
  16. use Session;
  17. use Auth;
  18. use File;
  19. use Image;
  20. use Brian2694\Toastr\Facades\Toastr;
  21. use Illuminate\Support\Facades\Redis;
  22. use App\Helpers\CommonMethods;
  23. class HomeController extends Controller
  24. {
  25. /**
  26. * Create a new controller instance.
  27. *
  28. * @return void
  29. */
  30. public function __construct()
  31. {
  32. // $this->middleware('auth');
  33. }
  34. public function marketings(){
  35. return view('marketing.marketings');
  36. }
  37. public function change_lang($lang){
  38. session(['app_lang' => $lang]);
  39. }
  40. public function reset_customer(){
  41. return view('customers.customers');
  42. }
  43. public function getOrderActivities($id){
  44. $status_array = ["Placed","Accepted","On_Road","Has_Delivered"];
  45. $orderActivities = OrderActivities::where('order_id',$id)->get();
  46. $new_array = [];
  47. foreach($orderActivities as $status){
  48. $new_array[$status->status] = array('id'=>$status->id,'description'=>$status->status.' found');
  49. }
  50. $final_array = [];
  51. foreach($status_array as $status){
  52. if(isset($new_array[$status])){
  53. $final_array[$status] = $new_array[$status];
  54. }else{
  55. $final_array[$status] = array('id'=>0,'description'=>$status. ' not found in activities');
  56. }
  57. }
  58. dump($final_array);
  59. }
  60. public function getDashboardAnalytics(Request $request){
  61. $s = $request->start_date;
  62. $e = $request->end_date;
  63. // $s = "2023-08-16";
  64. // $e = "2023-09-14";
  65. $start_date = Carbon::parse($s)->format('Y-m-d');
  66. if($s==$e)
  67. $start_date = Carbon::parse($s)->subDay()->format('Y-m-d');
  68. $end_date = Carbon::parse($e)->format('Y-m-d');
  69. $resto_id = CommonMethods::getRestuarantID();
  70. $orders = Orders::whereNull('deleted_at')->where('resto_id',$resto_id)->where('status','Has_Delivered')->whereBetween('created_at',[$start_date,$end_date])->count();
  71. // dd($orders);
  72. $total_revenue = DB::table('tb_dm_orders')
  73. ->select(DB::raw('(sum(total_price)) as total_price '))
  74. ->where('resto_id' , $resto_id)
  75. ->where('status','Has_Delivered')
  76. ->whereBetween('created_at',[$start_date,$end_date])
  77. ->whereNull('deleted_at')->first();
  78. $total_price = isset($total_revenue)?$total_revenue->total_price:0;
  79. $old_customer = \App\Models\Orders::select(\DB::raw('count(count(customer_id)) OVER() as customer_order'))->where('resto_id',$resto_id)->where('status','Has_Delivered')
  80. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('customer_id')->having(\DB::raw('count(customer_id)'),'>',1)->first();
  81. $old_customer = isset($old_customer)?($old_customer->customer_order):0;
  82. $new_customer = \App\Models\Orders::select(\DB::raw('count(customer_id) as customer_order'))->where('resto_id',$resto_id)->where('status','Has_Delivered')
  83. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('customer_id')->having(\DB::raw('count(customer_id)'),'=',1)->get()->toArray();
  84. $new_customer = array_sum(array_column($new_customer, 'customer_order'));
  85. $old_orders = \App\Models\Orders::select(\DB::raw('count(id) as customer_order'))->where('resto_id',$resto_id)->where('status','Has_Delivered')
  86. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('customer_id')->having(\DB::raw('count(id)'),'>',1)->get()->toArray();
  87. $old_orders = array_sum(array_column($old_orders, 'customer_order'));
  88. $new_order = \App\Models\Orders::select(\DB::raw('count(id) as customer_order'))->where('resto_id',$resto_id)->where('status','Has_Delivered')
  89. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('customer_id')->having(\DB::raw('count(id)'),'=',1)->get()->toArray();
  90. //dd($new_order);
  91. $new_order = array_sum(array_column($new_order, 'customer_order'));
  92. // $order_status_customers = \App\Models\Orders::select(\DB::raw('count(customer_id) as customer_order'))
  93. // ->whereBetween('created_at',[$start_date,$end_date])
  94. // ->where('customer_order' ,'>',1)
  95. // ->where('resto_id',$resto_id)->where('status','Has_Delivered')->groupBy('customer_id')->get()->take(2);
  96. $average_new_order = \App\Models\Orders::select(\DB::raw('sum(sum(total_price)) OVER() as total_price'))->where('resto_id',$resto_id)->where('status','Has_Delivered')
  97. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('customer_id')->having(\DB::raw('count(id)'),'=',1)->first();
  98. // dd($average_new_order);
  99. $average_new_order = isset($average_new_order)?$average_new_order->total_price:0;
  100. $average_old_order = \App\Models\Orders::select(\DB::raw('sum(sum(total_price)) OVER() as total_price'))->where('resto_id',$resto_id)->where('status','Has_Delivered')
  101. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('customer_id')->having(\DB::raw('count(id)'),'>',1)->first();
  102. $average_old_order = isset($average_old_order)?$average_old_order->total_price:0;
  103. $campaign_data = \App\Models\Orders::select(\DB::raw('count(campaign_type) as total_campaign'),'campaign_type')->where('resto_id',$resto_id)->where('status','Has_Delivered')
  104. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('campaign_type')->having('total_campaign','>',0)->get();
  105. $outlet_data = \App\Models\Orders::with('outlet')->select(\DB::raw('count(outlet_id) as total_outlet_orders'),'outlet_id')->where('resto_id',$resto_id)->where('status','Has_Delivered')
  106. ->whereBetween('created_at',[$start_date,$end_date])->groupBy('outlet_id')->having('total_outlet_orders','>',0)->get();
  107. $outlet_data_array = [];
  108. if($outlet_data){
  109. foreach($outlet_data as $d){
  110. $outlet_data_array[$d->outlet_id] = array('outlet_name'=>$d->outlet->name,'orders'=>$d->total_outlet_orders);
  111. }
  112. }
  113. $outlets = Outlets::where('resto_id',$resto_id)->whereNull('deleted_at')->get();
  114. $outlets_array = [];
  115. if($outlets){
  116. foreach($outlets as $outlet){
  117. if(isset($outlet_data_array[$outlet->id]))
  118. $outlets_array[] = array('name'=>$outlet->name,'orders'=>$outlet_data_array[$outlet->id]['orders']);
  119. else
  120. $outlets_array[] = array('name'=>$outlet->name,'orders'=>0);
  121. }
  122. }
  123. // dd($average_old_order,$average_new_order);
  124. $total_customers = $old_customer + $new_customer;
  125. $total_orders = $old_orders + $new_order;
  126. $dasboard_data = array(
  127. 'total_orders'=>number_format($orders),
  128. 'total_sale'=>number_format($total_price),
  129. 'order_rating'=>'N/A',
  130. 'average_basket_value'=>$orders > 0 && $total_price > 0?number_format( round($total_price / $orders,2)):"N/A",
  131. 'customers'=>array(
  132. 'new_customer'=>array('value'=>$new_customer,'percentage'=>(round($new_customer / ($total_customers?$total_customers:1),2)*100).'%'),
  133. 'old_customer'=>array('value'=>$old_customer,'percentage'=>(round($old_customer / ($total_customers?$total_customers:1),2)*100).'%'),
  134. ),
  135. 'orders'=>array(
  136. 'new_orders'=>array('value'=>$new_order,'percentage'=>number_format(round($new_order / ($total_orders > 0?$total_orders:1),2)*100).'%'),
  137. 'old_orders'=>array('value'=>$old_orders,'percentage'=>number_format(round($old_orders / ($total_orders > 0?$total_orders:1),2)*100).'%'),
  138. ),
  139. 'average_basket'=>array(
  140. 'new'=>$new_order > 0?number_format(round($average_new_order/$new_order,2)):0,
  141. 'returning'=>$old_orders > 0?number_format(round($average_old_order/$old_orders,2)):0,
  142. 'total_averge_basket'=>round(($new_order > 0?($average_new_order/$new_order):0) + ($old_orders > 0?$average_old_order/$old_orders:0),2),
  143. ),
  144. 'total_basket_sale'=>array(
  145. 'new'=>$new_order > 0?number_format(round($average_new_order,2)):0,
  146. 'returning'=>$old_orders > 0?number_format(round($average_old_order,2)):0,
  147. 'total_sale'=> round(($new_order > 0?$average_new_order:0)+($old_orders > 0?$average_old_order:0),2)
  148. ),
  149. 'order_by_source'=>$campaign_data,
  150. 'order_by_outlet'=>$outlets_array
  151. );
  152. return response()->json($dasboard_data,200);
  153. }
  154. public function reset_phone_customer(Request $request){
  155. $user = User::where('email',$request->mobile_number)->first();
  156. if(isset($user)){
  157. $existing_email = $user->email;
  158. $existing_email = $existing_email.'-reset-at-'.date('y-m-d H:i:s');
  159. $user->email = $existing_email;
  160. $user->save();
  161. echo json_encode(array('type'=>'success','message'=>$request->mobile_number. ' is reset.'));
  162. }else{
  163. echo json_encode(array('type'=>'error','message'=>$request->mobile_number. ' not found in db, use correct number with country code'));
  164. }
  165. }
  166. public function reset_update_password(Request $request){
  167. $user_id = $request->user_id;
  168. $user = User::find($user_id);
  169. $username = $user->username;
  170. $password = $request->password;
  171. $user->password = Hash::make($password);
  172. $user->is_reset_password_sent = "Changed";
  173. $result = $user->save();
  174. if($result){
  175. Auth::loginUsingId($user_id);
  176. return response()->json(array('type'=>'success','message'=>'Password reset successfully.'));
  177. }
  178. }
  179. public function reset_password(Request $request){
  180. $user_id = $request->get('u');
  181. $user = User::where('id',$user_id)->where('is_reset_password_sent','Yes')->first();;
  182. return view('reset-password',['user'=>$user]);
  183. }
  184. public function send_reset_link(Request $request){
  185. $email = $request->email;
  186. $user_id = "";
  187. $user = User::where('username',$email)->first();
  188. if(isset($user))
  189. $user_id = $user->id;
  190. if(!isset($user)){
  191. $user = RestoUsers::whereNull('deleted_at')->where('email',$email)->first();
  192. $user_id = isset($user->user_id)?$user->user_id:"";
  193. }
  194. if(!isset($user) && empty($user_id))
  195. return response()->json(['type'=>'error','message'=>'No user found against this <strong>'.$request->email.'</strong>']);
  196. $u = User::where('id',$user_id)->update(['is_reset_password_sent'=>'Yes']);
  197. $param = array(
  198. 'email'=>$request->email,
  199. 'name' => $user->first_name,
  200. 'link'=> env('APP_URL').'reset/my/password?q='.md5(time()).'&u='.$user->user_id.'&action=reset'
  201. );
  202. SendEmail::SendRestPasswordLink($param);
  203. return response()->json(['type'=>'success','message'=>'Reset link sent to <strong>'.$request->email.'</strong>']);
  204. }
  205. public function send_mail(){
  206. $logo = 'https://meemappaws.imgix.net/meemcdn/31-titanium-store/titanium-store-logo-1669151471?fm=webp&h=500&w=500&trim=color&q=100&fit=center&crop=center';
  207. $imageData = base64_encode(file_get_contents($logo));
  208. $logo = 'data:image/png;base64,'.$imageData;
  209. $param = array(
  210. 'email'=>$_GET['email'],
  211. 'shop_name' => 'Titanium Store',
  212. 'order_message' => 'You have New order placed OrderID: 217 , kindly login to https://admin.meemorder.io to serve customer.',
  213. 'logo' => $logo
  214. );
  215. SendEmail::sendOrderNotification($param);
  216. exit;
  217. $param = array(
  218. 'email'=>$_GET['email'],
  219. 'name' => 'Mujtaba Ahmad',
  220. 'shop_name' => 'NB Flowers',
  221. 'role' => 'Manager',
  222. 'access_level' => 'selected-outlets',
  223. 'selected_outlets' => 'Outlet1, Outlet2',
  224. //'link' => 'https://dashboard.chatfood.io/invite/36eaa3c8403efe29d775db4423a62bb0?utm_medium=email&utm_source=mailgun&utm_campaign=user_invitation',
  225. 'link' => '#'
  226. );
  227. SendEmail::SendInvitationLink($param);
  228. }
  229. public function test_redis(){
  230. Redis::set("user:mujtaba","It is testing code");
  231. dd(Redis::get('user:mujtaba'));
  232. }
  233. public function create_link(Request $request){
  234. $campaign_name = $request->campaign_name;
  235. $campaign_date = $request->campaign_date;
  236. $campaign_type = $request->campaign_type;
  237. $site_url = $request->site_url;
  238. $link = '?a='.$campaign_type.'&c='.Str::slug($campaign_name).'&cd='.$campaign_date;
  239. return $site_url.($link);
  240. }
  241. public function make_slug(){
  242. $resto = Restaurants::all();
  243. foreach($resto as $r){
  244. $rr = Restaurants::find($r->id);
  245. $rr->resto_unique_name = Str::slug($r->name);
  246. $rr->save();
  247. }
  248. }
  249. /**
  250. * Show the application dashboard.
  251. *
  252. * @return \Illuminate\Contracts\Support\Renderable
  253. */
  254. public function index()
  255. {
  256. return view('auth.login');
  257. }
  258. public function dashboard(){
  259. if(Auth::user()->role=="administrator")
  260. return view('dashboards.admin_dashboard');
  261. return view('dashboards.resto_dashboard');
  262. }
  263. public function getLogout()
  264. {
  265. Session::flush();
  266. Auth::logout();
  267. return redirect('/');
  268. }
  269. public function change_password(){
  270. return view('password');
  271. }
  272. public function update_password(Request $request){
  273. $old_password = $request->old_password;
  274. $new_password = $request->password;
  275. $confirm_password = $request->confirm_password;
  276. $user = Auth::user();
  277. if ($user && Hash::check($old_password, $user->password)) {
  278. if($new_password==$confirm_password){
  279. $u = User::find($user->id);
  280. $u->password = Hash::make($new_password);
  281. $u->save();
  282. echo json_encode(array('type'=>'success','message'=>'Password changed successfully.'));
  283. }else{
  284. echo json_encode(array('type'=>'error','message'=>'new password and confirm password are not matched.'));
  285. exit;
  286. }
  287. }else{
  288. echo json_encode(array('type'=>'error','message'=>'Old password is incorrect, enter correct password.'));
  289. exit;
  290. }
  291. }
  292. public function download_image(Request $request){
  293. $data = $request->data;
  294. $resto = $request->resto;
  295. $data = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $data));
  296. file_put_contents(public_path('/uploads/qrcode/qrcode.png'), $data);
  297. $this->image_recreate(public_path('/uploads/qrcode/qrcode.png'),public_path('/uploads/qrcode/'.$resto.'-qrcode.png'));
  298. echo env('APP_PUBLIC_URL').'uploads/qrcode/'.$resto.'-qrcode.png';
  299. }
  300. public function image_recreate($sourceFile,$destinationFile){
  301. $orig_filename = $sourceFile;
  302. $new_filename = $orig_filename;
  303. list($orig_w, $orig_h) = getimagesize($orig_filename);
  304. $orig_img = imagecreatefromstring(file_get_contents($orig_filename));
  305. $output_w = 2200;
  306. $output_h = 2200;
  307. // determine scale based on the longest edge
  308. if ($orig_h > $orig_w) {
  309. $scale = $output_h/$orig_h;
  310. } else {
  311. $scale = $output_w/$orig_w;
  312. }
  313. $scale = $scale-0.1;
  314. // calc new image dimensions
  315. $new_w = ($orig_w * $scale);
  316. $new_h = ($orig_h * $scale);
  317. // determine offset coords so that new image is centered
  318. $offest_x = (($output_w - $new_w) / 2);
  319. $offest_y = (($output_h - $new_h) / 2);
  320. // create new image and fill with background colour
  321. $new_img = imagecreatetruecolor($output_w, $output_h);
  322. $bgcolor = imagecolorallocate($new_img, 255, 255, 255); // red
  323. imagefill($new_img, 0, 0, $bgcolor); // fill background colour
  324. // copy and resize original image into center of new image
  325. imagecopyresampled($new_img, $orig_img, $offest_x, $offest_y, 0, 0, $new_w, $new_h, $orig_w, $orig_h);
  326. //save it
  327. imagejpeg($new_img, $destinationFile, 80);
  328. }
  329. public function resizeMainRecipeImages(){
  330. $path = public_path('uploads/main_image');
  331. ini_set('max_execution_time', '300');
  332. $files = File::allfiles($path);
  333. foreach($files as $file){
  334. $pth = ($file->getRealPath());
  335. $file_name = $file->getFileName();
  336. echo "Main Image: ".$file_name."<br />";
  337. $destinationPath = public_path('/uploads/main_image/');
  338. if($file->getExtension()!="jfif") {
  339. $img = Image::make($destinationPath . '/' . $file_name)->resize(85, null, function ($constraint) {
  340. $constraint->aspectRatio();
  341. });
  342. $img->save($destinationPath . '/thumbnails/' . $file_name);
  343. }
  344. }
  345. }
  346. public function resizeLogo(){
  347. $path = public_path('uploads/logo');
  348. ini_set('max_execution_time', '300');
  349. $files = File::allfiles($path);
  350. foreach($files as $file){
  351. $pth = ($file->getRealPath());
  352. $file_name = $file->getFileName();
  353. echo "Main Image: ".$file_name."<br />";
  354. $destinationPath = public_path('/uploads/logo/');
  355. if($file->getExtension()!="jfif") {
  356. $img = Image::make($destinationPath . '/' . $file_name)->resize(50, null, function ($constraint) {
  357. $constraint->aspectRatio();
  358. });
  359. $img->save($destinationPath . '/thumbnails/' . $file_name);
  360. }
  361. }
  362. }
  363. public function resizeGalleryRecipeImages(){
  364. $path = public_path('uploads/resto-gallery');
  365. $files = File::allfiles($path);
  366. foreach($files as $file){
  367. $pth = ($file->getRealPath());
  368. $file_name = $file->getFileName();
  369. echo "Gallery: ".$file_name."<br />";
  370. $destinationPath = public_path('/uploads/resto-gallery/');
  371. if($file->getExtension()!="jfif"){
  372. $img = Image::make($destinationPath . '/' . $file_name)->resize(800, null, function ($constraint) {
  373. $constraint->aspectRatio();
  374. });
  375. $img->save($destinationPath . '/thumbnails/' . $file_name);
  376. }
  377. }
  378. }
  379. public function resizeGalleryRecipeImagesToGallery(){
  380. $path = public_path('uploads/resto-gallery');
  381. $files = File::allfiles($path);
  382. foreach($files as $file){
  383. $pth = ($file->getRealPath());
  384. $file_name = $file->getFileName();
  385. echo "Gallery: ".$file_name."<br />";
  386. $destinationPath = public_path('/uploads/resto-gallery/');
  387. if($file->getExtension()!="jfif"){
  388. $img = Image::make($destinationPath . '/' . $file_name)->resize(800, null, function ($constraint) {
  389. $constraint->aspectRatio();
  390. });
  391. $img->save($destinationPath . '/gallery-resized/' . $file_name);
  392. }
  393. }
  394. }
  395. public function load_json(){
  396. $jsonString = file_get_contents("https://api.chatfood.io/api/v1/businesses/ceeba7a3-5dd4-48a7-9a07-96111efab2e4/areas");
  397. $data = json_decode($jsonString, true);
  398. // dd($data);
  399. foreach($data as $city){
  400. foreach($city as $cc){
  401. // dump($cc);
  402. $c = new DMCities();
  403. $c->city_name = $cc['name'];
  404. $c->city_unique_id = $cc['id'];
  405. $c->save();
  406. }
  407. /* */
  408. }
  409. }
  410. }