class ReceiptBillOrderPdf < Prawn::Document
    include NumberFormattable

    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, :description_width, :price_num_width, :line_move

    def initialize(printer_settings, sale_items, sale_data, customer_name, item_price_by_accounts, discount_price_by_accounts, member_info = nil,rebate_amount = nil,shop_details, printed_status,current_balance,card_data,other_charges_amount,latest_order_no,card_balance_amount,order_reservation)
        self.page_width = printer_settings.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 = 0
        self.price_width = 60
        self.qty_width = 25
        self.total_width = 60
        self.item_width = self.page_width - ((self.qty_width + self.price_width + self.total_width))
        self.item_height = 15
        self.item_description_width = (self.page_width-5) / 2
        self.label_width = 100

        self.description_width = 150
        self.price_num_width = 50
        self.line_move = 2
        # @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"

        header(shop_details)
        if sale_data.orders[0].source =='app'
          move_down 2
          y_position = cursor
          text "Mobile Order", :size => self.header_font_size,:align => :center, :style=>:bold
          move_down 2
          y_position = cursor
          bounding_box([0,y_position], :width =>self.item_description_width) do
            text "#{sale_data.customer.contact_no}", :size => self.header_font_size,:align => :left
          end
          if !sale_data.bookings[0].dining_facility.nil?
            bounding_box([self.item_description_width,y_position], :width =>self.label_width) do
              text "#{ sale_data.bookings[0].dining_facility.type } -#{ sale_data.bookings[0].dining_facility.name }" , :size => self.header_font_size,:align => :right
            end
          end
          move_down 1
        end
        cashier_info(sale_data, customer_name, latest_order_no,order_reservation)
        line_items(sale_items,precision,delimiter,order_reservation)
        all_total(sale_data,precision,delimiter,order_reservation)

       footer(printed_status)
   end

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

        stroke_horizontal_rule
    end

    def cashier_info(sale_data, customer_name, latest_order_no,order_reservation)
        stroke_horizontal_rule
        move_down line_move

        # move_down 2

        y_position = cursor
        bounding_box([0, y_position], :width =>self.label_width , :height => self.item_height) do
            text "Order Date " , :size => self.item_font_size, :align => :left
        end
        bounding_box([self.label_width - 2,y_position], :width =>self.label_width, :height => self.item_height) do
          text "#{order_reservation.created_at.strftime('%d-%m-%Y %H:%M %p')}", :size => self.item_font_size,:align => :right
        end

        y_position = cursor
        bounding_box([0, y_position], :width =>self.label_width , :height => self.item_height) do
            text "Requested Date Time " , :size => self.item_font_size, :align => :left
        end
        bounding_box([self.label_width - 2,y_position], :width =>self.label_width, :height => self.item_height) do
            text "#{order_reservation.requested_time.strftime('%d-%m-%Y %H:%M %p')}", :size => self.item_font_size,:align => :right
        end

        y_position = cursor
        bounding_box([0, y_position], :width =>self.label_width , :height => self.item_height) do
            text "Payment Type" , :size => self.item_font_size, :align => :left
        end
        bounding_box([self.label_width - 2,y_position], :width =>self.label_width, :height => self.item_height) do
            text "#{order_reservation.payment_type}", :size => self.item_font_size,:align => :right
        end

        y_position = cursor
        bounding_box([0, y_position], :width =>self.label_width , :height => self.item_height) do
            text "#{customer_name} " , :size => self.item_font_size, :align => :left
        end
        bounding_box([self.label_width - 2,y_position], :width =>self.label_width, :height => self.item_height) do
            text "Online Order", :size => self.item_font_size,:align => :right
        end

        stroke_horizontal_rule
    end

    def line_items(sale_items,precision,delimiter,order_reservation)
        if precision.to_i > 0
            item_label_qty_front_width = (self.item_width+self.price_width) + 5
            item_label_qty_end_width = self.qty_width + 4
            item_label_total_front_width = (self.item_width+self.price_width) + 10
            item_label_total_end_width = self.total_width + 9
        else
            self.item_width = self.item_width.to_i + 8
            item_label_qty_front_width = (self.item_width+self.price_width) + 8
            item_label_qty_end_width = self.qty_width + 7
            item_label_total_front_width = (self.item_width+self.price_width) + 5
            item_label_total_end_width = self.total_width + 4
        end
        move_down line_move
        y_position = cursor
        move_down line_move
        pad_top(15) {
            # @item_width.to_i + @half_qty.to_i
            text_box "Items", :at =>[0,y_position], :width => self.item_width, :height =>self.item_height, :size => self.item_font_size, :overflow => :shrink_to_fix
            text_box "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 "Qty", :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 "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
      }
       # move_down line_move
       stroke_horizontal_rule
       add_line_item_row(sale_items,precision,delimiter,order_reservation)
   end

    #Change Sub_Total and others...........
    def add_line_item_row(sale_items,precision,delimiter,order_reservation)

        if precision.to_i > 0
            item_name_width = (self.item_width+self.price_width)
            item_qty_front_width = (self.item_width+self.price_width) + 5
            item_qty_end_width = self.qty_width + 4
            item_total_front_width = item_name_width + 10
            item_total_end_width = self.total_width + 9
        else
            item_name_width = (self.item_width+self.price_width)
            item_qty_front_width = item_name_width + 8
            item_qty_end_width = self.qty_width + 7
            item_total_front_width = item_name_width + 5
            item_total_end_width = self.total_width + 4
        end

        y_position = cursor
        move_down line_move
        sub_total = 0.0
        total_qty = 0.0
        show_price  = Lookup.find_by_lookup_type("show_price")
        sale_items.each do |item|
            # check for item not to show

            if item.status != 'Discount' && item.qty > 0
                if !show_price.nil? && show_price.value.to_i > 0 && item.price == 0
                    total_qty    += item.qty
                else
                    if item.price != 0
                        total_qty    += item.qty
                    end
                end
            end

            product_name  = item.product_name

            # if item.status = 'promotion' && (item.remark =='promotion nett price' || item.remark == 'promotion discount')
            #     sub_total    += -item.price  #(item.qty*item.unit_price) - comment for room charges
            #     qty           = -item.qty
            #     total_price   = -item.price #item.qty*item.unit_price - comment for room charges
            #     price         = -item.unit_price
            # else
                sub_total    += item.price  #(item.qty*item.unit_price) - comment for room charges
                qty           = item.qty
                total_price   = item.price #item.qty*item.unit_price - comment for room charges
                price         = item.unit_price

            # end
            if !show_price.nil? && show_price.value.to_i>0
                item_row(item,precision,delimiter,product_name,price,qty ,total_price)
            else
                if item.price != 0
                    item_row(item,precision,delimiter,product_name,price,qty ,total_price)
                end
            end
        end

        stroke_horizontal_rule

        move_down line_move
        y_position = cursor
        bounding_box([0,y_position], :width =>self.item_width + self.price_width, :height => self.item_height) do
         text "Sub Total", :size => self.item_font_size,:align => :left
        end
        text_box "#{number_format(total_qty, :precision => precision.to_i)}", :at =>[item_qty_front_width,y_position], :width => item_qty_end_width,  :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
        text_box  "#{number_format(order_reservation.total_amount, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[item_total_front_width,y_position], :width =>item_total_end_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix

        if !order_reservation.delivery_type.nil? && order_reservation.delivery_fee.to_f > 0
            y_position = cursor
            bounding_box([0, y_position], :width =>self.description_width - 13, :height => self.item_height) do
                text "Delivery Charges (#{order_reservation.delivery_type})" , :size => self.item_font_size, :align => :left
            end

            bounding_box([self.description_width - 48,y_position], :width =>self.label_width, :height => self.item_height) do

                text "#{number_format(order_reservation.delivery_fee, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right

            end
        end

        if order_reservation.total_tax > 0
            y_position = cursor
            bounding_box([0, y_position], :width =>self.item_description_width , :height => self.item_height) do
                text "Tax " , :size => self.item_font_size, :align => :left
            end

            bounding_box([self.item_description_width,y_position], :width =>self.label_width, :height => self.item_height) do

                text "#{number_format(order_reservation.total_tax, :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 => self.item_height) do
            text "Convenience Charges " , :size => self.item_font_size, :align => :left
        end
        bounding_box([self.item_description_width,y_position], :width =>self.label_width, :height => self.item_height) do
          text "#{number_format(order_reservation.convenience_charge, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right
        end

        if order_reservation.discount_amount > 0
            y_position = cursor
            bounding_box([0, y_position], :width =>self.item_description_width , :height => self.item_height) do
                text "Promotion " , :size => self.item_font_size, :align => :left
            end

            bounding_box([self.item_description_width,y_position], :width =>self.label_width, :height => self.item_height) do

                text "#{number_format(order_reservation.discount_amount, :precision => precision.to_i, :delimiter => delimiter)}", :size => self.item_font_size,:align => :right

            end
        end
    end

    #Change items rows
    def item_row(item,precision,delimiter,product_name,price,qty ,total_price)
        if precision.to_i > 0
            item_name_width = (self.item_width+self.price_width)
            item_qty_front_width = (self.item_width+self.price_width) + 5
            item_qty_end_width = self.qty_width + 4
            item_total_front_width = item_name_width + 10
            #Change item_total_end_width
            item_total_end_width = self.total_width + 3
        else
            item_name_width = (self.item_width+self.price_width)
            item_qty_front_width = item_name_width + 8
            item_qty_end_width = self.qty_width + 7
            item_total_front_width = item_name_width + 5
            #Change item_total_end_width
            item_total_end_width = self.total_width + 3
        end
        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
        end
            # text_box "#{product_name}", :at =>[0,y_position], :width => self.item_width, :size => self.item_font_size

            text_box "#{number_format(price, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[self.item_width,y_position], :width => self.price_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix
            text_box "#{number_format(qty, :precision => precision.to_i)}", :at =>[item_qty_front_width,y_position], :width => item_qty_end_width,  :size => self.item_font_size, :align => :center, :overflow => :shrink_to_fix
            text_box  "#{number_format(total_price, :precision => precision.to_i, :delimiter => delimiter)}", :at =>[item_total_front_width,y_position], :width =>item_total_end_width, :size => self.item_font_size, :align => :right, :overflow => :shrink_to_fix


            if show_alt_name
                if !(item.product_alt_name).empty?
                    move_down 2
                    # font("public/fonts/NotoSansCJKtc-Regular.ttf") do
                        text "(#{item.product_alt_name})", :size => self.item_font_size,:align => :left, :inline_format => true
                    # end
                end
            end
            move_down line_move
        }
    end

    #Change Grand Total
    def all_total(sale_data,precision,delimiter,order_reservation)
        move_down line_move
        item_name_width = self.item_width
        y_position = cursor

        move_down line_move
        stroke_horizontal_rule
        y_position = cursor
        move_down line_move
        bounding_box([0,y_position], :width =>self.item_description_width) do
           text "Grand Total",:style => :bold, :size => self.header_font_size,:align => :left
       end

       bounding_box([self.item_description_width,y_position], :width =>self.label_width) do

          text "#{number_format(order_reservation.grand_total, :precision => precision.to_i, :delimiter => delimiter)}" , :style => :bold, :size => self.header_font_size,:align => :right

      end
      move_down line_move

      # sale_payment(sale_data,precision,delimiter)
    end

    #Change Footer
    def footer(printed_status)
        move_down line_move
        stroke_horizontal_rule
        move_down line_move

        move_down line_move
        y_position = cursor
        bounding_box([0, y_position], :width =>self.label_width) do
            text "#{printed_status}",:style => :bold, :size => header_font_size - 1,:align => :left
        end
        bounding_box([self.item_description_width,y_position], :width =>self.item_description_width, :height => self.item_height) do
            text "Thank You! See you Again", :left_margin => -5, :size => self.item_font_size - 1,:align => :left
        end

        move_down line_move
    end

    #check ReceiptBillAltName included
    def show_alt_name
        bill_alt_name = Lookup.collection_of("print_settings") #print_settings with name:ReceiptBillA5Pdf
        status = false
        if !bill_alt_name.empty?
            bill_alt_name.each do |alt_name|
                if alt_name[0] == 'ReceiptBillAltName'
                    if alt_name[1] == '1'
                        status = true
                    else
                        status = false
                    end
                end
            end
        end
        return status
    end
end
