class CloseCashierPdf < 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, shift_sale, sale_items, total_other_charges_info, acc_cate_count, menu_cate_count, total_by_acc, shop_details,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,total_dinein,total_takeway,total_other_charges,total_waste,total_spoile,total_credit_payments)
    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
    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

    shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,printer_settings.precision,delimiter,total_waste,total_spoile,total_other_charges,total_credit_payments) 

    if !sale_items.nil? or !sale_items.blank?
      sale_items_detail(sale_items, acc_cate_count, menu_cate_count, total_by_acc, total_other_charges_info)      
    end
  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 shift_detail(shift_sale,sale_taxes,other_payment,total_amount_by_account,total_discount_by_account,total_member_discount,precision,delimiter,total_waste,total_spoile,total_other_charges,total_credit_payments)
        move_down 7
         y_position = cursor
        bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Cashier : ", :size => self.item_font_size,:align => :left
        end
        bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do
            text "#{ shift_sale.employee.name}" , :size => self.item_font_size,:align => :left
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Cashier Station : ", :size => self.item_font_size,:align => :left
        end
        bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do
            text "#{ shift_sale.cashier_terminal.name}" , :size => self.item_font_size,:align => :left
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Opening Date : ", :size => self.item_font_size,:align => :left
        end
        bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do
            text "#{ shift_sale.shift_started_at.utc.getlocal.strftime('%d-%m-%Y %I:%M %p') }" , :size => self.item_font_size,:align => :left
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Closing Date : ", :size => self.item_font_size,:align => :left
        end
        bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do
            text "#{ shift_sale.shift_closed_at.utc.getlocal.strftime('%d-%m-%Y %I:%M %p') }" , :size => self.item_font_size,:align => :left
        end
        
        y_position = cursor
        bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Opening Float : ", :size => self.item_font_size,:align => :left
        end
        bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do
            text "#{ number_with_precision(shift_sale.opening_balance, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :left
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Closing Float : ", :size => self.item_font_size,:align => :left
        end
        bounding_box([self.label_width,y_position], :width => self.label_width, :height => self.item_height) do
            text "#{ number_with_precision(shift_sale.closing_balance, :precision => precision.to_i, :delimiter => delimiter)}" , :size => self.item_font_size,:align => :left
            # text_box  "#{number_with_precision(total_price, :precision => precision.to_i, :delimiter => delimiter)}"
        end


        move_down 10

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

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Received Amount :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{number_with_precision(shift_sale.closing_balance, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right

        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Cash In:", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{number_with_precision(shift_sale.cash_in, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Cash Out:", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{number_with_precision(shift_sale.cash_out, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
        end

        move_down -5
        stroke_horizontal_rule
        move_down 7

        @total_foc = 0
        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Cash Payment :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{ number_with_precision(shift_sale.cash_sales, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Credit Payment :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{number_with_precision(shift_sale.credit_sales, :precision => precision.to_i, :delimiter => delimiter) }", :size => self.item_font_size, :align => :right
        end

        #start other payment details 
        if shift_sale.other_sales > 0
            other_payment.each do |other|
              @total_foc = other.foc_amount.round(2)
              if other.mpu_amount && other.mpu_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "MPU Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.mpu_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.visa_amount && other.visa_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "VISA Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{number_with_precision(other.visa_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.master_amount && other.master_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "Master Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.master_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.jcb_amount && other.jcb_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "JCB Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.jcb_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.unionpay_amount && other.unionpay_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "UNIONPAY Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.unionpay_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.alipay_amount && other.alipay_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "Alipay Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.alipay_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.kbzpay_amount && other.kbzpay_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "KBZ Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.kbzpay_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.junctionpay_amount && other.junctionpay_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "JunctionPay Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.junctionpay_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.dinga_amount && other.dinga_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "Dinga Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.dinga_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.paypar_amount && other.paypar_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "Redeem Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.paypar_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.paymal_amount && other.paymal_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "Paymal Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.paymal_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end

              if other.giftvoucher_amount && other.giftvoucher_amount.to_f > 0
                y_position = cursor
                bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
                 text "GiftVoucher Payment :", :size => self.item_font_size, :align => :right
                end
                bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
                 text "#{ number_with_precision(other.giftvoucher_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
                end
              end
            end
        else
            y_position = cursor
            bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
             text "Other Payment :", :size => self.item_font_size, :align => :right
            end
            bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
             text "#{ number_with_precision(shift_sale.other_sales, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
            end
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Rounding Adjustments :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{ number_with_precision(shift_sale.total_rounding, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total :", :style => :bold, :size => self.header_font_size - 1, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{ number_with_precision(shift_sale.grand_total, :precision => precision.to_i, :delimiter => delimiter)}", :style => :bold, :size => self.header_font_size - 1, :align => :right
        end

        # end other payment details 
        move_down -5
        stroke_horizontal_rule
        move_down 7

        #start service charges and commercial tax
        sale_taxes.each do |tax|
          y_position = cursor
          bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
           text "#{tax.tax_name} :", :size => self.item_font_size, :align => :right
          end
          bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
           text "#{ number_with_precision(tax.st_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
          end
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Taxes :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{ number_with_precision(shift_sale.total_taxes, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Net Sales :", :style => :bold, :size => self.header_font_size - 1, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{number_with_precision(shift_sale.nett_sales, :precision => precision.to_i, :delimiter => delimiter) }", :style => :bold , :size => self.header_font_size - 1, :align => :right
        end

        if total_credit_payments && total_credit_payments.to_f > 0
          y_position = cursor
          bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
           text "Total Credit Payment :", :size => self.item_font_size, :align => :right
          end
          bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
           text "#{ number_with_precision(total_credit_payments, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
          end
        end
        #end for service charges and commercial tax
#COMMENTED FOR NO NEED AND NOT CORRECT WHEN OTHER CHARGES
        # y_position = cursor
        # bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
        #  text "Total Sale :", :size => self.item_font_size, :align => :right
        # end
        # bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
        #  text "#{shift_sale.total_revenue}", :size => self.item_font_size, :align => :right
        # end

        move_down -5
        stroke_horizontal_rule
        move_down 7
        #start total amount by Account Like Food / Beverage /..
        total_discount_by_account.each do |amount|
        
          y_position = cursor
          bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
           text "Total #{amount.account_name} Discount:", :size => self.item_font_size, :align => :right
          end
          bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
           text "#{ number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :size => self.item_font_size, :align => :right
          end
        end
        #end total amount by Account 

        if total_member_discount[0].member_discount.present?
          @member_discount = total_member_discount[0].member_discount rescue 0.0 
          @overall = shift_sale.total_discounts - @member_discount
                
            y_position = cursor
            bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
             text "Total Member Discount :", :size => self.item_font_size, :align => :right
            end
            bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
             text "#{ number_with_precision(@member_discount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
            end
        else 
          @overall = shift_sale.total_discounts                
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Discount :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{ number_with_precision(@overall, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size, :align => :right
        end 

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total FOC :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "(#{ number_with_precision(@total_foc, :precision => precision.to_i, :delimiter => delimiter)})", :size => self.item_font_size, :align => :right
        end     

        y_position = cursor 
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Void :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "(#{shift_sale.total_void})", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Waste :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "(#{ number_with_precision(total_waste, :precision => precision.to_i, :delimiter => delimiter)})", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Spoile :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "(#{ number_with_precision(total_spoile, :precision => precision.to_i, :delimiter => delimiter)})", :size => self.item_font_size, :align => :right
        end

        # y_position = cursor
        # bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
        #  text "Grand Total :", :size => self.item_font_size, :align => :right
        # end
        # bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
        #  text "#{shift_sale.grand_total}", :size => self.item_font_size, :align => :right
        # end
        
        move_down -5
        stroke_horizontal_rule
        move_down 7

        #start total amount by Account Like Food / Beverage /..
        total_amount_by_account.each do |amount|        
          y_position = cursor
          bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
           text "Total #{amount.account_name} Amount :", :size => self.item_font_size, :align => :right
          end
          bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
           text "#{number_with_precision(amount.total_price, :precision => precision.to_i, :delimiter => delimiter)} ", :size => self.item_font_size, :align => :right
          end
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Other Amount :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{total_other_charges}", :size => self.item_font_size, :align => :right
        end
        #end total amount by Account 

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Dine-in Count :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{shift_sale.dining_count}", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Takeaway Count :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{shift_sale.takeaway_count}", :size => self.item_font_size, :align => :right
        end

        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_description_width, :height => 20) do
         text "Total Receipts :", :size => self.item_font_size, :align => :right
        end
        bounding_box([self.item_description_width,y_position], :width =>self.price_width, :height => 20) do
         text "#{shift_sale.total_receipt}", :size => self.item_font_size, :align => :right
        end

        move_down 5
        stroke_horizontal_rule
        move_down 5

  end

  def sale_items_detail(sale_items, acc_cate_count, menu_cate_count, total_by_acc, 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

          total_qty = 0
          sub_total = 0
          flag = true

          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

        # y_position = cursor
        # pad_top(15) {
        #   bounding_box([0,y_position], :width =>self.item_width) do
        #     text "#{item['product_name']}", :size => self.item_font_size, :align => :left#, :overflow => :shrink_to_fix
        #   end
        #   text_box "#{item['unit_price'].to_i}", :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 "#{item['total_item'].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 "#{item['grand_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
        # }
        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 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

  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
end