class Printer::ReceiptPrinter < Printer::PrinterWorker

  def print_receipt(receipt_no)
    #Use CUPS service
    #Generate PDF
    #Print
    printer = PrintSetting.all
    pdf = OrderItemPdf.new

    if !printer.empty?
      printer.each do |printer_setting|
        if printer_setting.unique_code == 'OrderItemPdf' 
          pdf = OrderItemPdf.new
        elsif printer_setting.unique_code == 'OrderItemSlimPdf'
          pdf = OrderItemSlimPdf.new
        elsif printer_setting.unique_code == 'OrderSetItemPdf'
          pdf = OrderSetItemPdf.new
        elsif printer_setting.unique_code == 'OrderItemCustomisePdf'
          pdf = OrderItemCustomisePdf.new
        elsif printer_setting.unique_code == 'OrderSetItemCustomisePdf'
          pdf = OrderSetItemCustomisePdf.new 
        elsif printer_setting.unique_code == 'OrderItemSlimCustomisePdf'
          pdf = OrderItemSlimCustomisePdf.new
        end  
      end
    end    
    pdf.render_file "tmp/order_item_queue_#{order_id}_#{order_item_id}" + ".pdf"
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print("tmp/receipt.pdf")
    end
  end

  def print_receipt_payment_by_card(booking_id)
    #Use CUPS service
    #Generate PDF
    #Print
    filename = "tmp/order_summary_#{booking_id}" + ".pdf"
    printer = PrintSetting.all
    pdf = OrderSummaryPdf.new

    if !printer.empty?
      printer.each do |printer_setting|
        if printer_setting.unique_code == 'OrderSummaryPdf' 
          pdf = OrderSummaryPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
          pdf = OrderSummarySlimPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetPdf'
          pdf = OrderSummarySetPdf.new
        elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
          pdf = OrderSummaryCustomisePdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
          pdf = OrderSummarySetCustomisePdf.new  
        elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
          pdf = OrderSummarySlimCustomisePdf.new
        end  
      end
    end    
    pdf.render_file filename
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print(filename)
    end
  end

  def print_receipt_payment_by_account(sale_id)
    #Use CUPS service
    #Generate PDF
    #Print
    printer = PrintSetting.all
    filename = "tmp/order_summary_#{booking_id}" + ".pdf"
    pdf = OrderSummaryPdf.new
    if !printer.empty?
      printer.each do |printer_setting|
        if printer_setting.unique_code == 'OrderSummaryPdf' 
          pdf = OrderSummaryPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
          pdf = OrderSummarySlimPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetPdf'
          pdf = OrderSummarySetPdf.new
        elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
          pdf = OrderSummaryCustomisePdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
          pdf = OrderSummarySetCustomisePdf.new 
        elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
          pdf = OrderSummarySlimCustomisePdf.new
        end  
      end
    end        
    pdf.render_file filename
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print(filename)
    end
  end

  def print_receipt_payment_by_vochure(sale_id)
    #Use CUPS service
    #Generate PDF
    #Print
    printer = PrintSetting.all
    filename = "tmp/order_summary_#{booking_id}" + ".pdf"
    pdf = OrderSummaryPdf.new
    if !printer.empty?
      printer.each do |printer_setting|
        if printer_setting.unique_code == 'OrderSummaryPdf' 
          pdf = OrderSummaryPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
          pdf = OrderSummarySlimPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetPdf'
          pdf = OrderSummarySetPdf.new
        elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
          pdf = OrderSummaryCustomisePdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
          pdf = OrderSummarySetCustomisePdf.new  
        elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
          pdf = OrderSummarySlimCustomisePdf.new
        end  
      end
    end  
    pdf.render_file filename
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
     self.print(filename)
    end
  end

  def print_receipt_payment_by_giftcard(sale_id)
    #Use CUPS service
    #Generate PDF
    #Print
    printer = PrintSetting.all
    filename = "tmp/order_summary_#{booking_id}" + ".pdf"
    pdf = OrderSummaryPdf.new
    if !printer.empty?
      printer.each do |printer_setting|
        if printer_setting.unique_code == 'OrderSummaryPdf' 
          pdf = OrderSummaryPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
          pdf = OrderSummarySlimPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetPdf'
          pdf = OrderSummarySetPdf.new
        elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
          pdf = OrderSummaryCustomisePdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
          pdf = OrderSummarySetCustomisePdf.new 
        elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
          pdf = OrderSummarySlimCustomisePdf.new
        end  
      end
    end  
    pdf.render_file filename
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print(filename)
    end
  end

  def print_receipt_payment_by_foc(sale_id)
    #Use CUPS service
    #Generate PDF
    #Print
    printer = PrintSetting.all
    filename = "tmp/order_summary_#{booking_id}" + ".pdf"
    pdf = OrderSummaryPdf.new
    if !printer.empty?
      printer.each do |printer_setting|
        if printer_setting.unique_code == 'OrderSummaryPdf' 
          pdf = OrderSummaryPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySlimPdf'
          pdf = OrderSummarySlimPdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetPdf'
          pdf = OrderSummarySetPdf.new
        elsif printer_setting.unique_code == 'OrderSummaryCustomisePdf'
          pdf = OrderSummaryCustomisePdf.new
        elsif printer_setting.unique_code == 'OrderSummarySetCustomisePdf'
          pdf = OrderSummarySetCustomisePdf.new 
        elsif printer_setting.unique_code == 'OrderSummarySlimCustomisePdf'
          pdf = OrderSummarySlimCustomisePdf.new
        end  
      end
    end  
    pdf.render_file filename
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print(filename)
    end
  end

  #Bill Receipt Print
  def print_receipt_bill(printer_settings,cashier_terminal,sale_items,sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info = nil,rebate_amount=nil,shop_details, printed_status,balance,card_data)
    #Use CUPS service
    #Generate PDF
    #Print   
    pdf = ReceiptBillPdf.new(printer_settings, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info,rebate_amount,shop_details,printed_status,balance,card_data)
    receipt_bill_a5_pdf = Lookup.collection_of("print_settings") #print_settings with name:ReceiptBillA5Pdf
    if !receipt_bill_a5_pdf.empty?
      receipt_bill_a5_pdf.each do |receipt_bilA5|
        if receipt_bilA5[0] == 'ReceiptBillA5Pdf' 
          if receipt_bilA5[1] == '1'
            pdf = ReceiptBillA5Pdf.new(printer_settings, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info,rebate_amount,shop_details,printed_status,balance,card_data)
          else
            pdf = ReceiptBillPdf.new(printer_settings, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info,rebate_amount,shop_details,printed_status,balance,card_data)
          end
        end
      end
    end
    # print as print copies in printer setting
    count = printer_settings.print_copies

    # override print copies for print worker loop
    print_settings.print_copies = 1
    print_settings.save!

    directory_name = 'public/receipts'
    Dir.mkdir(directory_name) unless File.exists?(directory_name)

    begin
      if count == 1
        filename = directory_name + "/receipt_bill_#{sale_data.receipt_no}.pdf"
        pdf.render_file filename
        if printed_status != 'Paid'
          #no print in cloud server
          if ENV["SERVER_MODE"] != "cloud"
            self.print(directory_name + "/receipt_bill_#{sale_data.receipt_no}.pdf", cashier_terminal.printer_name)
          end
        end
      else
        filename = directory_name + "/receipt_bill_#{sale_data.receipt_no}_#{count}.pdf"
        pdf.render_file filename
        if printed_status != 'Paid'
          #no print in cloud server
          if ENV["SERVER_MODE"] != "cloud"
            self.print(directory_name + "/receipt_bill_#{sale_data.receipt_no}_#{count}.pdf", cashier_terminal.printer_name)
          end
        end
      end

      count -= 1
    end until count == 0

    return filename, sale_data.receipt_no, cashier_terminal.printer_name
  end

   # stock check
   def print_stock_check_result(print_settings,stockcheck, stockcheck_items,checker_name, shop_details)
     pdf = StockResultPdf.new(print_settings,stockcheck, stockcheck_items,checker_name, shop_details)
     pdf.render_file "tmp/print_stock_check_result.pdf"
     #no print in cloud server
     if ENV["SERVER_MODE"] != "cloud"
       self.print("tmp/print_stock_check_result.pdf")
      end
   end

  #Queue No Print
  def print_queue_no(printer_settings,queue)
    #Use CUPS service
    #Generate PDF
    #Print
    pdf = QueueNoPdf.new(printer_settings,queue)
    pdf.render_file "tmp/print_queue_no.pdf"
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print("tmp/print_queue_no.pdf")
    end
  end

  #Move Table Print
  def print_move_table(printer_settings,to,from,shop_detail,date,type,moved_by,order_items)
    #Use CUPS service
    #Generate PDF
    #Print
    pdf = MoveTablePdf.new(printer_settings,to,from,shop_detail,date,type,moved_by,order_items)
    pdf.render_file "tmp/print_move_table.pdf"
    self.print("tmp/print_move_table.pdf")
  end

  #Bill Receipt Print
  def print_crm_order(booking,order_items,setting)
    #Use CUPS service
    #Generate PDF
    #Print
    pdf = CrmOrderPdf.new(booking,order_items,setting)
    pdf.render_file "tmp/print_crm_order.pdf"
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print("tmp/print_crm_order.pdf")
    end
  end

  #Queue No Print
  def print_call_waiter(printer_settings,table,time,shop_detail)
    #Use CUPS service
    #Generate PDF
    #Print
    pdf = CallWaiterPdf.new(printer_settings,table,time,shop_detail)
    pdf.render_file "tmp/print_call_waiter.pdf"
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print("tmp/print_call_waiter.pdf")
    end
  end

  #print receipt pdf from js
  def print_receipt_pdf(filename,receipt_no,print_copies,printer_name)
    count = print_copies.to_i
    # if count == 0
    #   self.print(filename, printer_name)
    # else
      begin
        if count == 1
          #no print in cloud server
          if ENV["SERVER_MODE"] != "cloud"
            self.print(filename, printer_name)
          end
        else
          filename = "public/receipts/receipt_bill_#{receipt_no}_#{count}.pdf"
          #no print in cloud server
          if ENV["SERVER_MODE"] != "cloud"
            self.print(filename, printer_name)
          end
        end

        count -= 1
      end until count == 0
    # end
  end

end
