class SaleItemsPdf < Prawn::Document
  include ActionView::Helpers::NumberHelper
  attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width,:text_width

  def initialize(printer_settings, shop_details, period, type, account, from_date, to_date, shift, sale_items, total_other_charges)
    self.page_width = printer_settings.page_width  #PrintSetting.where("name = ?","Close Cashier").first.page_width
    self.page_height = printer_settings.page_height
    self.header_font_size = printer_settings.header_font_size.to_i
    self.item_font_size = printer_settings.item_font_size.to_i
    self.margin = 5
    self.price_width = 60
    self.qty_width = 20
    self.total_width = 40
    self.item_width = self.page_width - ((self.price_width + self.qty_width + self.total_width))
    self.item_height = 15
    self.item_description_width = (self.page_width-20) / 2
    @label_name_width = 70
    @label_item_width = self.page_width-@label_name_width
    self.label_width = 100

    self.text_width = (self.page_width - 80) - self.price_width / 3
    # @item_width = self.page_width.to_i / 2
    # @qty_width = @item_width.to_i / 3
    # @double = @qty_width * 1.3
    # @half_qty = @qty_width / 2
    #setting page margin and width
    super(:margin => [printer_settings.heading_space, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height])

    # db font setup
    if printer_settings.font != ""
        font_families.update("#{printer_settings.font}" => {
            :normal => "public/fonts/#{printer_settings.font}.ttf",
            :italic => "public/fonts/#{printer_settings.font}.ttf",
            :bold => "public/fonts/#{printer_settings.font}.ttf",
            :bold_italic => "public/fonts/#{printer_settings.font}.ttf"
        })

        font "#{printer_settings.font}"
        fallback_fonts ["Courier", "Helvetica", "Times-Roman"]
    end
    # font "public/fonts/Zawgyi-One.ttf"
    # font "public/fonts/padauk.ttf"

    #precision checked
    if printer_settings.precision.to_i > 2
        printer_settings.precision = 2
    end
    #check delimiter
    if printer_settings.delimiter
        delimiter = ","
    else
        delimiter = ""
    end

    header( shop_details)

    stroke_horizontal_rule

    sale_details(period, type, account, from_date, to_date, shift)

    sale_items_detail(sale_items, total_other_charges)
    move_down 10
  end

  def header (shop_details)
    move_down 7
    text "#{shop_details.name}", :left_margin => -10, :size => self.header_font_size,:align => :center
    move_down 5
    text "#{shop_details.address}", :size => self.item_font_size,:align => :center
    # move_down self.item_height
    move_down 5
    text "#{shop_details.phone_no}", :size => self.item_font_size,:align => :center
    move_down 5

    stroke_horizontal_rule
  end

  def sale_details(period, type, account, from_date, to_date, shift)
  	move_down 7
    y_position = cursor
    bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do
        text "Period : ", :size => self.item_font_size,:align => :left
    end
    bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do
        text "#{period}" , :size => self.item_font_size,:align => :left
    end

    y_position = cursor
    bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do
        text "Type : ", :size => self.item_font_size,:align => :left
    end
    bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do
        text "#{type}" , :size => self.item_font_size,:align => :left
    end

    y_position = cursor
    bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do
        text "Account : ", :size => self.item_font_size,:align => :left
    end
    bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do
        text "#{account}" , :size => self.item_font_size,:align => :left
    end

    y_position = cursor
    bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do
        text "From Date : ", :size => self.item_font_size,:align => :left
    end
    bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do
        text "#{ from_date.utc.getlocal.strftime("%d-%m-%Y")}" , :size => self.item_font_size,:align => :left
    end

    y_position = cursor
    bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do
        text "To Date : ", :size => self.item_font_size,:align => :left
    end
    bounding_box([@label_name_width,y_position], :width => @label_item_width, :height => self.item_height) do
        text "#{ to_date.utc.getlocal.strftime("%d-%m-%Y")}" , :size => self.item_font_size,:align => :left
    end

    y_position = cursor
    bounding_box([0,y_position], :width =>@label_name_width, :height => self.item_height) do
        text "Shift : ", :size => self.item_font_size,:align => :left
    end
    bounding_box([@label_name_width,y_position], :width => @label_item_width) do
        text "#{shift}" , :size => self.item_font_size,:align => :left
    end

    move_down 10
  end

  def sale_items_detail(sale_items, total_other_charges)
    y_position = cursor
    bounding_box([0,y_position], :width =>self.page_width - 10, :height => 20) do
     text "Sale Items Summary", :size => self.header_font_size, :align => :center
    end

    total_items = 0
    total_amount = 0
    total_qty = 0
   	sub_total = 0
    flag = false

    arr = Array.new

    unless sale_items.nil?
      sale_items.each do |item|

      	if !arr.include?(item['menu_category_id'])

      	  if flag == true
            move_down 5
            stroke_horizontal_rule
      	  	total_details('Sub Total', total_qty, sub_total)
      	  end

          flag = true

      	  total_qty = 0
      	  sub_total = 0

      	  move_down 10

      	  y_position = cursor
          bounding_box([0,y_position], :width =>self.page_width - 10, :height => 20) do
          	text "#{item['menu_category_name']}", :size => self.header_font_size, :align => :left
          end

          add_item_line('Items', 'Price', 'Qty', 'Total')
          move_down 2
          stroke_horizontal_rule
      	end


        move_down 3
        add_item_line(item['product_name'], item['unit_price'].to_i, item['total_item'].to_i, item['grand_total'].to_i)

        if item['total_item'].to_i > 0 or item['status_type'] == 'foc' or item['status_type'] == 'void'
          total_qty += item['total_item'].to_i
          total_items += item['total_item'].to_i
        end

        sub_total += item['grand_total'].to_i
        total_amount += item['grand_total'].to_i
        arr.push(item['menu_category_id'])
      end

      move_down 5
      stroke_horizontal_rule
      total_details('Sub Total', total_qty, sub_total)
      move_down 5
      total_other_amount, total_other_item = other_charges_detail(total_other_charges)

      total_items += total_other_item
      total_amount += total_other_amount
      move_down 5
      stroke_horizontal_rule
      total_details('Total Amount', total_items, total_amount)
    end
  end

  def total_details(col_name, total_qty, sub_total)
  	self.item_width = 73
    self.price_width = 60
    item_label_qty_front_width = (self.item_width+self.price_width) + 2
    item_label_qty_end_width = 32
    item_label_total_front_width = (self.item_width+self.price_width) + 2
    item_label_total_end_width = 64

    y_position = cursor
    pad_top(15) {
      text_box "#{col_name}", :at =>[0,y_position], :width => self.item_width, :height =>self.item_height, :size => self.item_font_size, :overflow => :shrink_to_fix
      text_box "#{total_qty.to_i}", :at =>[item_label_qty_front_width,y_position], :width => item_label_qty_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
      text_box "#{sub_total.to_i}", :at =>[item_label_total_front_width,y_position], :width => item_label_total_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
    }
  end

  def other_charges_detail(total_other_charges)
    total_charges = 0
    total_charges_items = 0

    unless total_other_charges.nil?
      y_position = cursor
      bounding_box([0,y_position], :width =>self.page_width - 10, :height => 20) do
        text "Other Charges", :size => self.header_font_size, :align => :left
      end

      move_down 5
      add_item_line('Items', 'Price', 'Qty', 'Total')
      move_down 2
      stroke_horizontal_rule

      total_other_charges.each do |charges|
        move_down 3
        add_item_line(charges['product_name'], charges['unit_price'].to_i, charges['total_item'].to_i, charges['grand_total'].to_i)

        total_charges_items += charges['total_item'].to_i
        total_charges += charges['grand_total'].to_i
      end
    end

    move_down 5
    stroke_horizontal_rule
    total_details('Total Charges Amount', total_charges_items, total_charges)

    return total_charges, total_charges_items
  end

  def add_item_line(product_name, unit_price, total_item, grand_total)
    self.item_width = 73
    self.price_width = 60
    item_label_qty_front_width = (self.item_width+self.price_width) + 2
    item_label_qty_end_width = 32
    item_label_total_front_width = (self.item_width+self.price_width) + 2
    item_label_total_end_width = 64

    y_position = cursor
    pad_top(15) {
      bounding_box([0,y_position], :width =>self.item_width) do
        text "#{product_name}", :size => self.item_font_size, :align => :left#, :overflow => :shrink_to_fix
      end
      text_box "#{unit_price}", :at =>[(self.item_width),y_position], :width => self.price_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
      text_box "#{total_item}", :at =>[item_label_qty_front_width,y_position], :width => item_label_qty_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
      text_box "#{grand_total}", :at =>[item_label_total_front_width,y_position], :width => item_label_total_end_width, :height =>self.item_height, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
    }
  end
end
