Created
December 7, 2016 23:50
-
-
Save Jks15063/8602cb8bfaf413c8afe7d895b24f2ab5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LabelsController < ApplicationController | |
respond_to :html, :js, :json | |
before_filter :authenticate_user! | |
before_filter :get_menu_item_or_redirect, only: [:show] | |
def index | |
@menu = Menu.get(params[:menu_id].to_i) | |
@menu_items = dispensary.active_menu_items.sort_by{|e| [e.category.name.to_s, e.name.to_s.downcase]} | |
respond_with @menu | |
end | |
def show | |
@hide_inner_sidebar = true | |
dispensary | |
params[:count] = 1 if params[:count].blank? | |
params[:label_format] ||= (dispensary.dispensary_detail.label_layout.blank? ? '3-10' : dispensary.dispensary_detail.label_layout) | |
@visit = Visit.get(params[:visit_id].to_i) if params[:visit_id] | |
if [email protected]? | |
@visit = nil unless dispensary.id == @visit.dispensary_id | |
end | |
respond_with @menu_item | |
end | |
def generate_label_pdf | |
require 'prawn/layout' | |
dispensary | |
time_now = SecureRandom.hex | |
@menu_item = MenuItem.get_with_deleted(params[:id].to_i) || MenuItem.get_with_deleted(params[:menu_item_ids].try(:first)) | |
label_type = visit_label? ? :receipt : :menu_item | |
pdf = PdfMaker.new(params[:label_format], params[:label_count], label_type) | |
filename = pdf.generate_all_labels(menu_item_ids: params[:menu_item_ids], positions: params[:positions], visit_id: params[:visit_id], show_prices: params[:show_prices]) | |
if filename | |
s3u = S3Upload.new(filename, 'labels') | |
if (url = s3u.upload) | |
return redirect_to(url) | |
else | |
flash.now[:error] = "There was an error generating the PDF. Please try again in just a minute." | |
render :show | |
end | |
elsif params[:menu_item_ids].present? && @menu_item.present? | |
flash.now[:error] = "There was an error generating the PDF. Please try again in just a minute." | |
render :show | |
else | |
redirect_to dispensary_menu_labels_path(dispensary, dispensary.menu), notice: "You did not select any menu items to create labels for." | |
end | |
end | |
def update_labels | |
ids = [] | |
params[:menu_item_ids].each do |id| | |
ids << id.to_i | |
end | |
@menu_items = MenuItem.all(:id => ids) | |
respond_with @menu_items | |
end | |
private | |
def dispensary | |
@dispensary ||= Dispensary.get(params[:dispensary_id].to_i) | |
end | |
def get_menu_item_or_redirect | |
if @menu_item = MenuItem.get(params[:id].to_i) | |
@menu_item | |
else | |
return(redirect_to dispensary_menu_labels_path(dispensary, dispensary.menu), notice: "This menu item has been deleted") | |
end | |
end | |
def visit_label? | |
params[:visit_id].present? # THIS DETERMINES A RECEIPT LABEL FROM A MENU ITEM LABEL | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment