class Printer::CashierStationPrinter < Printer::PrinterWorker

  def print_open_cashier(receipt_no)
    time = Time.current

    #Use CUPS service
    #Generate PDF
    #Print
    pdf = CashierStationOpening.new
    pdf.render_file "tmp/cashier_station_#{order_id}_closing_#{time}.pdf"
    #no print in cloud server
    if ENV["SERVER_MODE"] != "cloud"
      self.print("tmp/cashier_station_#{order_id}_closing_#{time}.pdf")
    end
  end

  # def print_close_cashier(receipt_no)
  #   #Use CUPS service
  #   #Generate PDF
  #   time = Time.current
  #   #Print
  #   pdf = CashierStationClosing.new
  #   pdf.render_file "tmp/cashier_station_#{order_id}_closing_#{time}.pdf"
  #   self.print("tmp/receipt.pdf")
  # end

  #Bill Receipt Print
  # def print_close_cashier(printer_settings,cashier_terminal,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges)
  #   #Use CUPS service
  #   #Generate PDF
  #   #Print
  #   cashier = shift_sale.employee.name
  #   shift_name = shift_sale.shift_started_at.strftime("%d-%m-%Y %I:%M %p") + "_" + shift_sale.shift_closed_at.strftime("%d-%m-%Y %I:%M %p")

  #   pdf = CloseCashierCustomisePdf.new(printer_settings,shift_sale,shop_details,sale_taxes,other_payment,amount,discount,member_discount,total_dinein,total_takeway,total_other_charges)
  #   filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf"
  #   pdf.render_file filename
  #   self.print(filename, cashier_terminal.printer_name)
  # end

  def print_close_cashier(printer_settings, cashier_terminal, shift_sale, sale_items, total_other_charges_info, shop_details, sale_taxes, other_payment, amount, discount, member_discount, total_dinein, total_takeway, total_other_charges, total_waste, total_spoile, total_credit_payments, payment_methods, foodcourt = nil)
    if Lookup.collection_of('print_settings').none? { |x| x == ["ActionCable", "1"] }
      if !sale_items.blank? or !sale_items.nil?
        @account_cate_count = Hash.new {|hash, key| hash[key] = 0}
        sale_items.each {|acc_cate| @account_cate_count[acc_cate.account_id] += 1}

        @menu_cate_count = Hash.new {|hash, key| hash[key] = 0}
        sale_items.each {|cate| @menu_cate_count[cate.menu_category_id] += 1}

        @totalByAccount = Hash.new {|hash, key| hash[key] = 0}
        sale_items.each {|acc| @totalByAccount[acc.account_id] += acc.grand_total}
      end

      #Use CUPS service
      #Generate PDF
      #Print
      cashier = shift_sale.employee.name
      shift_name = shift_sale.shift_started_at.strftime("%d-%m-%Y %I:%M %p") + "_" + shift_sale.shift_closed_at.strftime("%d-%m-%Y %I:%M %p")
      filename = "tmp/close_cashier_#{cashier}_#{shift_name}.pdf"
      pdf = CloseCashierPdf.new(printer_settings, shift_sale, sale_items, total_other_charges_info, @account_cate_count, @menu_cate_count, @totalByAccount, shop_details, sale_taxes, other_payment, amount, discount, member_discount, total_dinein, total_takeway, total_other_charges, total_waste, total_spoile, total_credit_payments, payment_methods, foodcourt)
      close_cashier_pdf = Lookup.collection_of("print_settings") #print_settings with name:CloseCashierPdf

      if !close_cashier_pdf.empty?
        close_cashier_pdf.each do |close_cashier|
          if close_cashier[0] == 'CloseCashierCustomisePdf'
            if close_cashier[1] == '1'
              pdf = CloseCashierCustomisePdf.new(printer_settings, shift_sale, shop_details, sale_taxes, other_payment, amount, discount, member_discount, total_dinein, total_takeway, total_other_charges, total_waste, total_spoile, total_credit_payments, foodcourt)
            else
              pdf = CloseCashierPdf.new(printer_settings, shift_sale, sale_items, total_other_charges_info, @account_cate_count, @menu_cate_count, @totalByAccount, shop_details, sale_taxes, other_payment, amount,discount, member_discount, total_dinein, total_takeway, total_other_charges, total_waste, total_spoile, total_credit_payments, payment_methods, foodcourt)
            end
          end
        end
      end

      pdf.render_file filename
      #no print in cloud server
      if ENV["SERVER_MODE"] != "cloud"
        self.print(filename, cashier_terminal.printer_name)
      end
    else
      ActionCable.server.broadcast("print_channel_#{Shop.current_shop.shop_code}",
        queue: cashier_terminal.printer_name,
        unique_code: print_settings.unique_code,
        print_copies: print_settings.print_copies,
        data: {
          shop_details: shop_details.as_json,
          shift_sale: shift_sale,
          cashier_terminal: cashier_terminal,
          shift_employee: shift_sale.employee,
          sale_items: sale_items,
          other_charges: total_other_charges_info,
          sale_taxes: sale_taxes,
          other_payment: other_payment,
          total_amount_by_account: amount,
          total_discount_by_account: discount,
          total_member_discount: member_discount,
          total_waste: total_waste,
          total_spoile: total_spoile,
          total_other_charges: total_other_charges,
          total_credit_payments: total_credit_payments
        }
      )
    end
  end

  def print_sale_items_report(print_settings, shop_details, period_name, type, account, from_date, to_date, shift_name, sale_items, total_other_charges)
    if Lookup.collection_of('print_settings').none? { |x| x == ["ActionCable", "1"] }
      filename = "tmp/reports_sale_items.pdf"

      if print_settings.unique_code == "SaleItemsPdf"
        pdf = SaleItemsPdf.new(print_settings, shop_details, period_name, type, account, from_date, to_date, shift_name, sale_items, total_other_charges)
      end

      if print_settings.unique_code == "SaleItemsStarPdf"
        pdf = SaleItemsStarPdf.new(print_settings, shop_details, period_name, type, account, from_date, to_date, shift_name, sale_items, total_other_charges)
      end

      pdf.render_file filename

      #no print in cloud server
      if ENV["SERVER_MODE"] != "cloud"
        self.print(filename, print_settings.printer_name)
      end
    else
      ActionCable.server.broadcast("print_channel_#{Shop.current_shop.shop_code}",
        queue: print_settings.printer_name,
        unique_code: print_settings.unique_code,
        print_copies: print_settings.print_copies,
        data: {
          sale_items: sale_items.as_json,
          total_other_charges: total_other_charges,
          shop_details:shop_details.as_json,
          sale_details: {
            period: period_name,
            type: type,
            account: account,
            from_date: from_date,
            to_date: to_date,
            shift: shift_name,
          },
        }
      )
    end
  end
  #sqa
end
