PageRenderTime 1288ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/doctor/dashboards_controller.rb

https://bitbucket.org/Ankit4991/dr.-crm
Ruby | 372 lines | 331 code | 35 blank | 6 comment | 27 complexity | d76f345b4af3e2b9ff57ea4e00a0860d MD5 | raw file
  1. ##
  2. # DashboardsController is associated to all functionality that showing on doctor Dhashboard.
  3. # All tabs that showing on Doctor Dashboard will be load with ajax.
  4. class Doctor::DashboardsController < Doctor::BaseController
  5. include ActionView::Helpers::NumberHelper
  6. before_action :set_seo_attributes, except: [:customer_profile]
  7. before_action :set_title_profile, only: [:customer_profile]
  8. include PatientAccountSection
  9. def index
  10. params[:q] ||= {}
  11. params[:search_form] ||= ""
  12. @doctor=current_doctor
  13. if read_org_appointment(@doctor)
  14. dashboard_data current_organisation
  15. elsif read_store_appointment(@doctor)
  16. dashboard_data_store @doctor.store
  17. else
  18. dashboard_data @doctor
  19. end
  20. if current_doctor.store.present?
  21. @contact_lens_distribution_orders = current_doctor.store.contact_lens_distribution_orders.where(soft_delete: false)
  22. end
  23. respond_to do |format|
  24. format.html
  25. format.js
  26. end
  27. end
  28. def update
  29. begin
  30. current_doctor.update!(doctor_params)
  31. flash[:status]="Success"
  32. flash[:notice]="Profile successfully updated"
  33. rescue Exception => e
  34. flash[:status] = "Error"
  35. if (e.message == "param is missing or the value is empty: doctor")
  36. flash[:notice] = "Please choose avatar"
  37. else
  38. flash[:notice] = "Something went wrong due to #{e} !! Try it again"
  39. end
  40. end
  41. respond_to do |format|
  42. format.json { render :json => {status: flash[:status], msg: flash[:notice]}.to_json }
  43. format.html {redirect_to root_url}
  44. end
  45. end
  46. def new_patient
  47. end
  48. def patient_log
  49. params[:q] ||= {}
  50. if params[:q][:patient_email_cont].present? && params[:q][:patient_email_cont].include?('n/a')
  51. @patients = current_organisation.patients.where(email_not_supplied: true).order(updated_at: :desc).map(&:joint_resource)
  52. @patients = @patients.paginate(:page => params[:page], :per_page => 30)
  53. else
  54. @patients_search = current_organisation.joint_resources.ransack(params[:q])
  55. @patients = @patients_search.result.includes(:patient,:examinations,:favourites,:referrals,:orders).order("patients.updated_at DESC").paginate(:page => params[:page], :per_page => 30)
  56. end
  57. @resources = current_organisation.stores.find_each
  58. @resource = @resources.first
  59. respond_to do |format|
  60. format.html
  61. format.js
  62. end
  63. end
  64. # This action use to show patient profile for doctor
  65. def customer_profile
  66. params[:q] ||= {}
  67. @patient = Patient.find_by(id: params[:patient_id])
  68. if @patient.present?
  69. @exams = current_organisation.examinations.where(joint_resource_id: @patient.try(:joint_resource).try(:id)).order('updated_at DESC')
  70. @search = current_organisation.orders.where(joint_resource_id: @patient.try(:joint_resource).try(:id)).order('order_date DESC').ransack(params[:q])
  71. @orders = @search.result.includes(:doctor).paginate(:page => params[:customer_orders_page], :per_page => 10)
  72. @referrals = @patient.referrals
  73. @appointments = current_organisation.bookings.with_deleted.where(joint_resource_id: @patient.joint_resource.id).order('start_time DESC')
  74. @follow_ups_appointments = @appointments.joins(:exam_procedures).where("exam_procedures.name =?", "Follow Up")
  75. respond_to do |format|
  76. format.html { render 'customer_profile' and return }
  77. format.js
  78. end
  79. else
  80. redirect_to :root
  81. end
  82. end
  83. def patient_info
  84. joint_resource = current_organisation.joint_resources.find(params[:jr_id])
  85. @patient = joint_resource.member
  86. @address = Address.find_by(id: joint_resource.current_address_id)
  87. jr=Hash["Patient id"=>joint_resource.id]
  88. @all_info = { :Personal => jr.merge(@patient.as_json),:Address => @address.as_json }
  89. respond_to do |format|
  90. format.json{ render :json => @all_info.present? ? @all_info.as_json.map{|key,value| [key,value.present? ? value.map{|k,v| [k.titleize,v.to_s]} : [] ] } : {} }
  91. end
  92. end
  93. def exam_audits
  94. @examination = current_organisation.examinations.find(params[:id])
  95. respond_to do |format|
  96. format.js{ render :status => 200 }
  97. end
  98. end
  99. def order_audits
  100. @order = current_organisation.orders.find(params[:id])
  101. respond_to do |format|
  102. format.js{ render :status => 200 }
  103. end
  104. end
  105. def card_verification
  106. begin
  107. @joint_resource = current_organisation.joint_resources.find(params[:jr_id])
  108. @validation_response = @joint_resource.patient.profile.validate_profile(params['creditcard']['verification_value'])
  109. if @validation_response.success?
  110. @joint_resource.patient.profile.update_attributes!(:validated => true)
  111. flash[:success] = 'success'
  112. flash[:notice] = "Profile Payment Method Successfully updated !!"
  113. else
  114. flash[:success] = 'error'
  115. flash[:notice] = "Invalid payment/credit card details"
  116. end
  117. respond_to do |format|
  118. format.json { render :json => {status: flash[:success], msg: flash[:notice]}.to_json }
  119. end
  120. rescue Exception => e
  121. render :json => {status: 'Failure', msg: "! Error due to #{e.message}"}.to_json
  122. end
  123. end
  124. def background_update
  125. current_doctor.update(bgurl: Background.find(params[:bg_id]).avatar.url)
  126. redirect_to :back
  127. end
  128. def password_update
  129. @doctor = current_doctor
  130. if @doctor.update_with_password(params.require(:edit_doctor).permit(:password, :password_confirmation, :current_password))
  131. # Sign in the user by passing validation in case their password changed
  132. sign_in @doctor, :bypass => true
  133. render :json => {status: 'Success', msg: "Password Successfully updated"}.to_json
  134. else
  135. render :json => {status: 'Success', msg: "Error Updating Password due to #{@doctor.errors.full_messages.join(', ')}"}.to_json
  136. end
  137. end
  138. def email_prescription
  139. if params[:glass].present?
  140. @glass_prescription = GlassesPrescription.find(params[:id])
  141. @exam = @glass_prescription.try(:examination)
  142. else
  143. @lens_prescription = current_organisation.try(:lens_prescriptions).find(params[:id])
  144. @exam = @lens_prescription.try(:examination)
  145. end
  146. if (@glass_prescription.present? || @lens_prescription.present?)
  147. AppMailer.send_prescription_to_patient(@exam.try(:joint_resource).try(:member),@exam).deliver_later unless @exam.try(:joint_resource).try(:patient).try(:email_not_supplied)
  148. render :json => {status: 'Success', msg: "Email has been sent successfully"}
  149. else
  150. render :json => {status: 'Error', msg: "Email not sent !"} unless params[:attachment].present?
  151. end
  152. end
  153. def confirm_account
  154. patient = current_organisation.patients.find(params[:id])
  155. patient.delay.resend_confirmation_instructions
  156. render :json => {status: 'Success', msg: "Email has been sent successfully"}
  157. end
  158. def reconfirm_notify
  159. if params[:patient_class] == "FamilyMember"
  160. patient = current_organisation.family_members.find(params[:id])
  161. else
  162. patient = current_organisation.patients.find(params[:id])
  163. end
  164. booking = Booking.find(params[:booking])
  165. AppMailer.send_booking_confirmation(booking).deliver_now
  166. one_click_url=Rails.application.routes.url_helpers.root_url(host: CrmDatabase::Application.config.action_mailer.default_url_options[:host], sudomain: patient.try(:subdomain), patient_email: patient.try(:email), authentication_token: patient.try(:authentication_token), subdomain: patient.try(:subdomain))
  167. one_click_url.gsub!(patient.try(:subdomain), patient.try(:subdomain) + ".dev") if Rails.env.include?('staging') && patient.try(:subdomain) != 'dev'
  168. one_click_url = Googl.shorten( one_click_url , (Net::HTTP.get URI "https://api.ipify.org"), ENV["GOOGLE_API_KEY"]).short_url
  169. notification_message = AppConfig.sms_notification["appointment_create"] % {patient_name: patient.try(:name), doctor_name: booking.doctor.name, appointment_time: booking.start_time.strftime('%m/%d/%Y %I:%M%P'), one_click_login: one_click_url, store_name: booking.store.description , store_address: "#{booking.store.try(:office_address).try(:address1)}, #{booking.store.try(:office_address).try(:address2)}, postal code #{booking.store.try(:office_address).try(:postal_code)}", store_phone_number: "#{number_to_phone(booking.store.phone_number.to_s[2..-1].to_i, area_code: true)}", store_office_homepage: "#{booking.store.url}"}
  170. TwilioSmsWorker.perform_async({'src'=> booking.organisation.twilio_number, 'dst' => patient.phone, 'text' => notification_message, 'trackable_id'=> booking.id, 'trackable_type'=> 'Booking', 'notification_method'=> booking.joint_resource.patient.notification_method,'org_sid'=> booking.organisation.twilio_acc_sid,'org_auth_token'=> booking.organisation.twilio_auth_token})
  171. flash[:status]="Success"
  172. flash[:notice] = "Reconfirmation successfully sent"
  173. respond_to do |format|
  174. format.html {redirect_to :back}
  175. format.json { render :json => {status: flash[:status], msg: flash[:notice]}.to_json }
  176. end
  177. end
  178. #This action is used just to relaod the today's appointments listing table over dashboard under overview tab
  179. def todays_appointments
  180. role = current_doctor.role - [""]
  181. @doctor=current_doctor
  182. @today_appointment_non_unique=current_organisation.bookings.with_deleted.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).order(:start_time => :asc)
  183. @today_appointment_unique=[]
  184. a = @today_appointment_non_unique.map(&:joint_resource_id).uniq
  185. a.each{|aa| @today_appointment_unique << @today_appointment_non_unique.where(joint_resource_id: aa).order(:start_time => :asc).last}
  186. @today_appointment = @today_appointment_unique.sort_by{|x| x.start_time}
  187. respond_to do |format|
  188. format.js { render layout: false, content_type: 'text/javascript' }
  189. end
  190. end
  191. def exam_section
  192. @doctor=current_doctor
  193. if read_org_appointment(@doctor)
  194. @recent_exams = current_organisation.examinations.where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  195. elsif read_store_appointment(@doctor)
  196. store = @doctor.store
  197. @recent_exams = Examination.joins(:doctor).where(doctors: {store_id: store.id}).where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  198. else
  199. @recent_exams = @doctor.examinations.where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  200. end
  201. render partial: 'doctor/dashboards/dashboard_tabs/exam_tab',locals: { recent_exams: @recent_exams}
  202. end
  203. def order_section
  204. @doctor=current_doctor
  205. if read_org_appointment(@doctor)
  206. @recent_orders = current_organisation.orders.where(:status => [0,1,2,3,4]).where("date(updated_at) > ?", 15.days.ago).order(:updated_at => :desc)
  207. @recent_renewed_orders = current_organisation.favourite_orders.where(:status => [0,1,2]).order(:updated_at => :desc)
  208. elsif read_store_appointment(@doctor)
  209. store = @doctor.store
  210. @recent_orders = Order.joins(:doctor).where(doctors: {store_id: store.id}).where(:status => [0,1,2,3,4]).where("date(orders.updated_at) > ?", 15.days.ago).order(:updated_at => :desc)
  211. @recent_renewed_orders = FavouriteOrder.joins(:doctor).where(doctors: {store_id: store.id}).where(:status => [0,1,2]).order(:updated_at => :desc)
  212. else
  213. @recent_orders = @doctor.orders.where(:status => [0,1,2,3,4]).where("date(updated_at) > ?", 15.days.ago).order(:updated_at => :desc)
  214. @recent_renewed_orders = @doctor.favourite_orders.where(:status => [0,1,2]).order(:updated_at => :desc)
  215. end
  216. @recent_orders = @recent_orders.map{|x| (x.contact_lens_orders.present? || x.eye_glass_orders.present?) ? x : nil}.compact
  217. render partial: 'doctor/dashboards/dashboard_tabs/order_tab',locals: { recent_orders: @recent_orders, recent_renewed_orders: @recent_renewed_orders}
  218. end
  219. def follow_section
  220. @doctor=current_doctor
  221. if read_org_appointment(@doctor)
  222. @follow_ups = current_organisation.orders.follow.order(:updated_at => :desc)
  223. @follow_list = FollowUp.where("due_date >= ? AND doctor_id IN (?)", DateTime.now,current_organisation.doctors.map(&:id))
  224. @renewed_follow_ups = current_organisation.favourite_orders.follow.order(:updated_at => :desc)
  225. elsif read_store_appointment(@doctor)
  226. store = @doctor.store
  227. @follow_ups = Order.joins(:doctor).where(doctors: {store_id: store.id}).follow.order(:updated_at => :desc)
  228. @follow_list = FollowUp.where("due_date >= ? AND doctor_id IN (?)", DateTime.now,current_organisation.doctors.map(&:id))
  229. @renewed_follow_ups = FavouriteOrder.joins(:doctor).where(doctors: {store_id: store.id}).follow.order(:updated_at => :desc)
  230. else
  231. @follow_ups = @doctor.orders.follow.order(:updated_at => :desc)
  232. @follow_list = FollowUp.where("due_date >= ? AND doctor_id IN (?)", DateTime.now,current_organisation.doctors.map(&:id))
  233. @renewed_follow_ups = @doctor.favourite_orders.follow.order(:updated_at => :desc)
  234. end
  235. render partial: 'doctor/dashboards/dashboard_tabs/follow_tab',locals: { follow_ups: @follow_ups, follow_list: @follow_list, renewed_follow_ups: @renewed_follow_ups}
  236. end
  237. def dispense_section
  238. @doctor=current_doctor
  239. if read_org_appointment(@doctor)
  240. @to_be_dispensed = current_organisation.orders.ready_for_pick_up.where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  241. @re_newed_dispensed = current_organisation.favourite_orders.ready_for_pick_up.order(:updated_at => :desc)
  242. elsif read_store_appointment(@doctor)
  243. store = @doctor.store
  244. @to_be_dispensed = Order.joins(:doctor).where(doctors: {store_id: store.id}).ready_for_pick_up.order(:updated_at => :desc)
  245. @re_newed_dispensed = FavouriteOrder.joins(:doctor).where(doctors: {store_id: store.id}).ready_for_pick_up.order(:updated_at => :desc)
  246. else
  247. @to_be_dispensed = @doctor.orders.ready_for_pick_up.where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  248. @re_newed_dispensed = @doctor.favourite_orders.ready_for_pick_up.order(:updated_at => :desc)
  249. end
  250. @to_be_ordered = current_organisation.orders.ordered.order(:updated_at => :desc)
  251. @re_newed_ordered = current_organisation.favourite_orders.ordered.order(:updated_at => :desc)
  252. render partial: 'doctor/dashboards/dashboard_tabs/dispense_tab',locals: { to_be_dispensed: @to_be_dispensed, re_newed_dispensed: @re_newed_dispensed }
  253. end
  254. private
  255. def doctor_params
  256. params.require(:doctor).permit :first_name, :last_name, :type, :mobile_number,:email,:password,:password_confirmation,:designation,:about,:portfolio,:avatar,
  257. :theme_color,:sign_image,:DOB,home_address_attributes: [:address1,:address2,:city,:postal_code,:addr_type,:state_id,:country_id,:phone],
  258. office_address_attributes: [:address1,:address2,:city,:postal_code,:addr_type,:state_id,:country_id,:phone]
  259. end
  260. def dashboard_data current_object
  261. role = current_doctor.role - [""]
  262. @recent_orders = current_object.orders.where(:status => [0,1,2,3,4]).where("date(updated_at) > ?", 15.days.ago).order(:updated_at => :desc)
  263. @recent_orders = @recent_orders.map{|x| (x.contact_lens_orders.present? || x.eye_glass_orders.present?) ? x : nil}.compact
  264. @recent_renewed_orders = current_object.favourite_orders.where(:status => [0,1,2]).order(:updated_at => :desc)
  265. @to_be_dispensed = current_object.orders.ready_for_pick_up.where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  266. @follow_ups = current_object.orders.follow.order(:updated_at => :desc)
  267. @follow_list = FollowUp.where("due_date >= ? AND doctor_id IN (?)", DateTime.now,current_organisation.doctors.map(&:id))
  268. @renewed_follow_ups = current_object.favourite_orders.follow.order(:updated_at => :desc)
  269. @recent_exams = current_object.examinations.where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  270. @today_exam=current_object.bookings.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).count
  271. @weekly_exam=current_object.bookings.where(start_time: (Time.now.to_date-8).beginning_of_day..(Time.now.to_date-1).end_of_day).count
  272. @coming_exam=current_object.bookings.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).count
  273. @total_exam=current_object.bookings.count
  274. @comming_search = current_object.bookings.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).ransack(params[:q])
  275. @comming_appointment=@comming_search.result.includes(:joint_resource,:exam_procedures,:doctor).paginate(:page => params[:upcoming_appointments_page], :per_page => 10).order(:start_time => :asc)
  276. @todays_search =current_object.bookings.with_deleted.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).ransack(params[:q])
  277. @today_appointment_non_unique=@todays_search.result.includes(:joint_resource,:exam_procedures,:doctor).paginate(:page => params[:todays_appointments_page], :per_page => 20).order(:start_time => :asc)
  278. @today_appointment_unique=[]
  279. a = @today_appointment_non_unique.map(&:joint_resource_id).uniq
  280. a.each{|aa| @today_appointment_unique << @today_appointment_non_unique.where(joint_resource_id: aa).order(:start_time => :asc).last}
  281. @today_appointment = @today_appointment_unique.sort_by{|x| x.start_time}
  282. @total_orders=current_object.orders
  283. @today_orders=current_object.orders.where("updated_at::date=?", Date.today)
  284. @total_eye_glass_orders=current_object.eye_glass_orders
  285. @today_eye_glass_orders=current_object.eye_glass_orders.where("eye_glass_orders.updated_at::date=?", Date.today)
  286. @total_contact_lens_orders=current_object.contact_lens_orders
  287. @today_contact_lens_orders=current_object.contact_lens_orders.where("contact_lens_orders.updated_at::date=?", Date.today)
  288. end
  289. def dashboard_data_store store
  290. role = current_doctor.role - [""]
  291. @recent_orders = Order.joins(:doctor).where(doctors: {store_id: store.id}).where(:status => [0,1,2,3,4]).where("date(orders.updated_at) > ?", 15.days.ago).order(:updated_at => :desc)
  292. @recent_orders = @recent_orders.map{|x| (x.contact_lens_orders.present? || x.eye_glass_orders.present?) ? x : nil}.compact
  293. @recent_renewed_orders = FavouriteOrder.joins(:doctor).where(doctors: {store_id: store.id}).where(:status => [0,1,2]).order(:updated_at => :desc)
  294. @to_be_dispensed = Order.joins(:doctor).where(doctors: {store_id: store.id}).ready_for_pick_up.order(:updated_at => :desc)
  295. @follow_ups = Order.joins(:doctor).where(doctors: {store_id: store.id}).follow.order(:updated_at => :desc)
  296. @follow_list = FollowUp.where("due_date >= ? AND doctor_id IN (?)", DateTime.now,current_organisation.doctors.map(&:id))
  297. @renewed_follow_ups = FavouriteOrder.joins(:doctor).where(doctors: {store_id: store.id}).follow.order(:updated_at => :desc)
  298. @recent_exams = Examination.joins(:doctor).where(doctors: {store_id: store.id}).where(:status => [0,1,2,3,4]).order(:updated_at => :desc)
  299. @today_exam=store.bookings.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).count
  300. @weekly_exam=store.bookings.where(start_time: (Time.now.to_date-8).beginning_of_day..(Time.now.to_date-1).end_of_day).count
  301. @coming_exam=store.bookings.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).count
  302. @total_exam=store.bookings.count
  303. @comming_search = store.bookings.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).ransack(params[:q])
  304. @comming_appointment=@comming_search.result.includes(:joint_resource,:exam_procedures,:doctor).paginate(:page => params[:upcoming_appointments_page], :per_page => 10).order(:start_time => :asc)
  305. @todays_search = store.bookings.with_deleted.where(start_time: Time.now.beginning_of_day..Time.now.end_of_day).where.not(joint_resource_id: nil).ransack(params[:q])
  306. @today_appointment_non_unique=@todays_search.result.includes(:joint_resource,:exam_procedures,:doctor).paginate(:page => params[:todays_appointments_page], :per_page => 20).order(:start_time => :asc)
  307. @today_appointment_unique=[]
  308. a = @today_appointment_non_unique.map(&:joint_resource_id).uniq
  309. a.each{|aa| @today_appointment_unique << @today_appointment_non_unique.where(joint_resource_id: aa).order(:start_time => :asc).last}
  310. @today_appointment = @today_appointment_unique.sort_by{|x| x.start_time}
  311. @total_orders=Order.joins(:doctor).where(doctors: {store_id: store.id})
  312. @today_orders=Order.joins(:doctor).where(doctors: {store_id: store.id}).where("orders.updated_at::date=?", Date.today)
  313. @total_eye_glass_orders=EyeGlassOrder.joins(:doctor).where(doctors: {store_id: store.id})
  314. @today_eye_glass_orders=EyeGlassOrder.joins(:doctor).where(doctors: {store_id: store.id}).where("eye_glass_orders.updated_at::date=?", Date.today)
  315. @total_contact_lens_orders=ContactLensOrder.joins(:doctor).where(doctors: {store_id: store.id})
  316. @today_contact_lens_orders=ContactLensOrder.joins(:doctor).where(doctors: {store_id: store.id}).where("contact_lens_orders.updated_at::date=?", Date.today)
  317. end
  318. def set_seo_attributes
  319. @seo = Seo.find_by view_name: 'doctor_dashboard'
  320. if @seo.present?
  321. set_meta_tags title: @seo.title,
  322. description: @seo.description,
  323. keywords: @seo.keywords
  324. end
  325. end
  326. def set_title_profile
  327. @patient = Patient.find_by(id: params[:patient_id]) unless @patient.present?
  328. if @patient.try(:birthday).present?
  329. title = @patient.try(:name) + ' ' + @patient.try(:birthday).to_s
  330. elsif current_doctor.store.description.present? && @patient.present?
  331. title = @patient.try(:name) + ' ' + current_doctor.try(:store).try(:description)
  332. else
  333. title = @patient.try(:name)
  334. end
  335. set_meta_tags title: title
  336. end
  337. end