class Printer::OrderQueuePrinter < Printer::PrinterWorker

  def print_order_item(print_settings,oqs, order_id, order_item_id, print_status, before_updated_qty="", options="")
    # Must be one print
    if print_settings.print_copies == 0
       print_settings.print_copies = 1
       print_settings.save!
    end

    order_item =  print_query('order_item', order_item_id) #OrderItem.find_by_item_code(item_code)
    options = order_item[0].options
    # filename = "tmp/order_item_#{order_id}_#{order_item_id}" + ".pdf"

    # pdf = OrderItemPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
    print_setting = PrintSetting.all

    # check for item not to show
    pdf = ''
    # if order_item[0].price != 0
      if !print_setting.empty?
        print_setting.each do |print_settings|
          if print_settings.unique_code == 'OrderItemPdf'
            pdf = OrderItemPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          elsif print_settings.unique_code == 'OrderItemStarPdf'
            pdf = OrderItemStarPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          elsif print_settings.unique_code == 'OrderItemSlimPdf'
            pdf = OrderItemSlimPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          elsif print_settings.unique_code == 'OrderSetItemPdf'
            pdf = OrderSetItemPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          elsif print_settings.unique_code == 'OrderItemCustomisePdf'
            pdf = OrderItemCustomisePdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          elsif print_settings.unique_code == 'OrderSetItemCustomisePdf'
            pdf = OrderSetItemCustomisePdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          elsif print_settings.unique_code == 'OrderItemSlimCustomisePdf'
            pdf = OrderItemSlimCustomisePdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
          end
        end
      end
    # end

    shop = Shop.first
    directory_name = 'public/orders_'+shop.shop_code
    Dir.mkdir(directory_name) unless File.exists?(directory_name)

    filename = directory_name + "/order_item_#{order_id}_#{order_item_id}" + ".pdf"

    pdf.render_file filename

    if oqs.print_copy
      #no print in cloud server
      if ENV["SERVER_MODE"] != "cloud"
        self.print(filename, oqs.printer_name)
      end
      #For print copy
      # pdf.render_file filename.gsub(".","-copy.")
      # self.print(filename.gsub(".","-copy."), oqs.printer_name)
    else
      print_settings.print_copies = 1
      print_settings.save!
      #no print in cloud server
      if ENV["SERVER_MODE"] != "cloud"
        self.print(filename, oqs.printer_name)
      end
    end

    return filename, order_id, oqs.printer_name
  end

   # Query for per order
  def print_order_summary(print_settings,oqs, order_id, order_items, print_status, before_updated_qty="", options="")
    #Use CUPS service
    #Generate PDF
    #Print
    # Must be one print
    if print_settings.print_copies == 0
      print_settings.print_copies = 1
      print_settings.save!
    end

    print_setting = PrintSetting.all.order("id ASC")
    order=print_query('order_summary', order_id)

    shop = Shop.first
    directory_name = 'public/orders_'+shop.shop_code
    Dir.mkdir(directory_name) unless File.exists?(directory_name)

    # For Print Per Item
    if oqs.cut_per_item

      order_items.each do|odi|
        odi_item=print_query('order_item', odi.order_items_id)

        filename = directory_name + "/order_item_#{order_id}" + ".pdf"
        # filename = "tmp/order_item_#{order_id}" + ".pdf"
        # For Item Options
        options = odi.options == "[]"? "" : odi.options

        # check for item not to show
        #if odi.price != 0
        pdf = OrderItemPdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
          if !print_setting.empty?
            print_setting.each do |print_settings|
              if print_settings.unique_code == 'OrderItemPdf'
                pdf = OrderItemPdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemStarPdf'
                pdf = OrderItemStarPdf.new(print_settings,order_item[0], print_status, options, oqs.use_alternate_name, before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemSlimPdf'
                pdf = OrderItemSlimPdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderSetItemPdf'
                pdf = OrderSetItemPdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemCustomisePdf'
                pdf = OrderItemCustomisePdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderSetItemCustomisePdf'
                pdf = OrderSetItemCustomisePdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemSlimCustomisePdf'
                pdf = OrderItemSlimCustomisePdf.new(print_settings,odi_item[0], print_status, options, oqs.use_alternate_name,before_updated_qty)
              end
            end
          end

          # pdf.render_file "tmp/order_item.pdf"
          pdf.render_file filename
          if oqs.print_copy
             #no print in cloud server
            if ENV["SERVER_MODE"] != "cloud"
              self.print(filename, oqs.printer_name)
              # self.print(filename.gsub(".","-copy."), oqs.printer_name)
            end
          else
            print_settings.print_copies = 1
            print_settings.save!
             #no print in cloud server
            if ENV["SERVER_MODE"] != "cloud"
              self.print(filename, oqs.printer_name)
            end
          end
        #end
      end
    # For Print Order Summary
    else
      filename = directory_name + "/order_summary_#{order_id}" + ".pdf"
      # filename = "tmp/order_summary_#{order_id}" + ".pdf"
      pdf = OrderSummaryPdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
      if !print_setting.empty?
        print_setting.each do |print_settings|
          if print_settings.unique_code == 'OrderSummaryPdf'
            pdf = OrderSummaryPdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySlimPdf'
            pdf = OrderSummarySlimPdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySetPdf'
            pdf = OrderSummarySetPdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummaryCustomisePdf'
            pdf = OrderSummaryCustomisePdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySetCustomisePdf'
            pdf = OrderSummarySetCustomisePdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySlimCustomisePdf'
            pdf = OrderSummarySlimCustomisePdf.new(print_settings,order, print_status, order_items, oqs.use_alternate_name,before_updated_qty)
          end
        end
      end

      pdf.render_file filename
      if oqs.print_copy
        #no print in cloud server
        if ENV["SERVER_MODE"] != "cloud"
          self.print(filename, oqs.printer_name)
        end
        #For print copy
        # pdf.render_file filename.gsub(".","-copy.")
        # self.print(filename.gsub(".","-copy."), oqs.printer_name)
      else
        print_settings.print_copies = 1
        print_settings.save!
         #no print in cloud server
        if ENV["SERVER_MODE"] != "cloud"
          self.print(filename, oqs.printer_name)
        end
      end
    end
    return filename, order_id, oqs.printer_name
  end

  # Print for orders in booking
  def print_booking_summary(print_settings,oqs, booking_id, print_status,before_updated_qty="")
    # Must be one print
    if print_settings.print_copies == 0
      print_settings.print_copies = 1
      print_settings.save!
    end

    print_setting = PrintSetting.all.order("id ASC")

    order=print_query('booking_summary', booking_id)

    shop = Shop.first
    directory_name = 'public/orders_'+shop.shop_code
    Dir.mkdir(directory_name) unless File.exists?(directory_name)

    # For Print Per Item
    if oqs.cut_per_item
      order.each do|odi|
        # filename = "tmp/order_item_#{booking_id}" + ".pdf"
        filename = directory_name + "/order_item_#{booking_id}" + ".pdf"
        # For Item Options
        options = odi.options == "[]"? "" : odi.options

        # check for item not to show
        #if odi.price != 0
          pdf = OrderItemPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
          if !print_setting.empty?
            print_setting.each do |print_settings|
              if print_settings.unique_code == 'OrderItemPdf'
                pdf = OrderItemPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemStarPdf'
                pdf = OrderItemStarPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemSlimPdf'
                pdf = OrderItemSlimPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderSetItemPdf'
                pdf = OrderSetItemPdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemCustomisePdf'
                pdf = OrderItemCustomisePdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderSetItemCustomisePdf'
                pdf = OrderSetItemCustomisePdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              elsif print_settings.unique_code == 'OrderItemSlimCustomisePdf'
                pdf = OrderItemSlimCustomisePdf.new(print_settings,odi, print_status, options,oqs.use_alternate_name,before_updated_qty)
              end
            end
          end
          pdf.render_file filename

          if oqs.print_copy
            #no print in cloud server
            if ENV["SERVER_MODE"] != "cloud"
              self.print(filename, oqs.printer_name)
            end
            #For print copy
            # pdf.render_file filename.gsub(".","-copy.")
            # self.print(filename.gsub(".","-copy."), oqs.printer_name)
          else
            print_settings.print_copies = 1
            print_settings.save!
            #no print in cloud server
            if ENV["SERVER_MODE"] != "cloud"
              self.print(filename, oqs.printer_name)
            end
          end
        #end
      end
    # For Print Order Summary
    else
      # filename = "tmp/booking_summary_#{booking_id}" + ".pdf"
      filename = directory_name + "/booking_summary_#{booking_id}" + ".pdf"
      pdf = OrderSummaryPdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)

      if !print_setting.empty?
        print_setting.each do |print_settings|
          if print_settings.unique_code == 'OrderSummaryPdf'
            pdf = OrderSummaryPdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySlimPdf'
            pdf = OrderSummarySlimPdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySetPdf'
            pdf = OrderSummarySetPdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummaryCustomisePdf'
            pdf = OrderSummaryCustomisePdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySetCustomisePdf'
            pdf = OrderSummarySetCustomisePdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)
          elsif print_settings.unique_code == 'OrderSummarySlimCustomisePdf'
            pdf = OrderSummarySlimCustomisePdf.new(print_settings,order, print_status,oqs.use_alternate_name,before_updated_qty)
          end
        end
      end

      pdf.render_file filename
      if oqs.print_copy
        #no print in cloud server
        if ENV["SERVER_MODE"] != "cloud"
          self.print(filename, oqs.printer_name)
        end
        #For print copy
        # pdf.render_file filename.gsub(".","-copy.")
        # self.print(filename.gsub(".","-copy."), oqs.printer_name)
      else
        print_settings.print_copies = 1
        print_settings.save!
         #no print in cloud server
        if ENV["SERVER_MODE"] != "cloud"
          self.print(filename, oqs.printer_name)
        end
      end
    end
    return filename, booking_id, oqs.printer_name
  end

  # Query for OQS with status
  def print_query(type, id)
    if type == "order_item"
      OrderItem.select("order_items.order_id, order_items.item_code, order_items.item_name, order_items.qty, order_items.price, order_items.options, order_items.item_order_by as order_by, order_items.created_at as order_at, order_items.set_menu_items, cus.name as customer, df.type, df.name as dining,item.alt_name as alt_name")
        .joins("left join orders ON orders.order_id = order_items.order_id
                left join booking_orders AS bo ON bo.order_id=order_items.order_id
                left join bookings AS b ON b.booking_id = bo.booking_id
                left join dining_facilities AS df ON df.id = b.dining_facility_id
                left join customers as cus ON cus.customer_id = orders.customer_id
                left join menu_items as item ON item.item_code = order_items.item_code")
        .where("order_items.order_items_id = '#{ id }'")
        .group("order_items.item_code")
    elsif type == "order_summary"
      OrderItem.select("order_items.order_id, order_items.item_code, order_items.item_name, order_items.qty, order_items.price, order_items.options, order_items.item_order_by as order_by, order_items.created_at as order_at, order_items.set_menu_items, cus.name as customer, df.type, df.name as dining,item.alt_name as alt_name")
        .joins("left join orders ON orders.order_id = order_items.order_id
                left join booking_orders AS bo ON bo.order_id=order_items.order_id
                left join bookings AS b ON b.booking_id = bo.booking_id
                left join dining_facilities AS df ON df.id = b.dining_facility_id
                left join customers as cus ON cus.customer_id = orders.customer_id
                left join menu_items as item ON item.item_code = order_items.item_code")
        .where("orders.order_id = '#{ id }'")
        .group("order_items.order_items_id")
    else
      # order summary for booking
      OrderItem.select("order_items.order_id, order_items.item_code, order_items.item_name, order_items.qty, order_items.price, order_items.options, order_items.item_order_by as order_by, order_items.created_at as order_at, order_items.set_menu_items, cus.name as customer, df.type, df.name as dining,item.alt_name as alt_name")
        .joins("left join orders ON orders.order_id = order_items.order_id
                left join booking_orders AS bo ON bo.order_id=order_items.order_id
                left join bookings AS b ON b.booking_id = bo.booking_id
                left join dining_facilities AS df ON df.id = b.dining_facility_id
                left join customers as cus ON cus.customer_id = orders.customer_id
                left join menu_items as item ON item.item_code = order_items.item_code")
        .where("b.booking_id = '#{ id }'")
    end
  end

   # Check in-out time
  def print_check_in_out(print_settings, cashier_terminal, booking, table)
    #Use CUPS service
    #Generate PDF
    #Print
    sale_id = booking.sale_id
    filename = "tmp/check_in_out_#{sale_id}" + ".pdf"

    pdf = CheckInOutPdf.new(print_settings,booking, table)
    print_setting = PrintSetting.all

    # if order_item[0].price != 0
    if !print_setting.empty?
      print_setting.each do |print_settings|
        if print_settings.unique_code == 'CheckInOutPdf'
          pdf = CheckInOutPdf.new(print_settings,booking, table)
        end
      end
    end

    pdf.render_file filename

    # Must be one print
    if print_settings.print_copies == 0
       print_settings.print_copies = 1
       print_settings.save!
    end
    # print_settings.print_copies = 1
    # print_settings.save!

    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print(filename, print_settings.printer_name)
    end
  end
end
