class ProductqrcodePdf < Prawn::Document
    def initialize(reportdata, compdetail, uRl,productinfo,session)
      super(:left_margin=>5,:right_margin=>5,:top_margin=>5,:bottom_margin=>2,:page_size =>[220, 380])
      @reportdata = reportdata
      @compDetail = compdetail
      @uRl2        = uRl
      @uRl         = Rails.root.join "public"
      @logoSize    = 0.2   
      @productinfo = productinfo
      @selectfield = session[:reuested_selectprodlist]
      line_items
      
   end
  
   
    def count_mcell
    @count_cell ||= 0
    @count_cell = @count_cell+1
  end
  
  
  def line_items
    newcompdteail =  @compDetail.cmp_addressline1.to_s+((@compDetail.cmp_addressline1.to_s.length.to_i >1) ? ', ': '' )+@compDetail.cmp_addressline2.to_s+(@compDetail.cmp_addressline3.to_s.strip.length > 1 ? " -#{@compDetail.cmp_addressline3.strip}" : '').to_s
    if @compDetail.cmp_logos.to_s.length >1
      @filesExt = Rails.root.join "public", "images", "logo",@compDetail.cmp_logos.to_s
      if File.exist?(@filesExt)
        image_path = {:image=>open(@uRl.to_s+"/images/logo/"+@compDetail.cmp_logos.to_s) ,:position =>:center,:fit => [200,35]}           
      else
        image_path =''
      end
   
   end
  

  
      data1 = ([ 
    # [image_path,{:content => "<b>#{@compDetail.cmp_companyname.upcase}", :inline_format => true, :size => 12,:padding_bottom=>1}],
    [image_path]

     ])
  # Use the repeat method to add the header on every page
   
      table(data1, :width => 210) do
        style row(0..1).column(0..1), :border_width => 0
        style column(0), :width => 210, :align => :left, :inline_format => :true
        # style column(1), :width => 170,:text_color => 'DC143CS'
        cells.padding = 1
       end
       if @reportdata && @reportdata.pd_qrcode.present?
          qrimagepath = Rails.root.join("public", "images", "qrcode",@reportdata.pd_qrpath.to_s ,@reportdata.pd_qrcode.to_s)  
          if File.exist?(qrimagepath)           
            image_path_fl   = {:image=>open(@uRl.to_s+"/images/qrcode/#{@reportdata.pd_qrpath}/"+@reportdata.pd_qrcode.to_s) ,:position =>:center,:fit => [100,100]}   
          else
            image_path_fl = {:image=>open(@uRl.to_s+"/images/no-image.jpg") ,:position =>:center,:fit => [100,100]}
          end      
      else
        image_path_fl = {:image=>open(@uRl2.to_s+"/images/no-image.jpg") ,:position =>:center,:fit => [100,100]}
      end
       data1 = ([ 
            [image_path_fl]
    
         ])
          # Use the repeat method to add the header on every page       
          table(data1, :width => 210) do
            style row(0..1).column(0..1), :border_width => 0
            style column(0), :width => 210, :align => :left, :inline_format => :true
            # style column(1), :width => 170,:text_color => 'DC143CS'
            cells.padding = 1
           end

           if @productinfo&.pd_bsc_currency.present?
           if @productinfo.pd_bsc_currency == "Indian Rupees"
            currency   = "Rs."
         else
            currency   = @productinfo.pd_bsc_currency
         end
        end

           data2 = []
           if @selectfield.present?
           # Check if 'Product ID' is included in @selectfield
           if @selectfield.to_s.include?('Product ID')
             data2 << [{ content: "Product ID" }, { content: ":" }, { content: @reportdata.pd_id.to_s }]
           end
           
           # Check if 'Gender' is included in @selectfield
           if @selectfield.to_s.include?('Gender')
             data2 << [{ content: "Gender" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_gender.to_s : '').to_s }]
           end

           # Check if 'Material' is included in @selectfield
           if @selectfield.to_s.include?('Material')
            data2 << [{ content: "Material" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_material.to_s : '').to_s }]
           end

           # Check if 'Wash' is included in @selectfield
           if @selectfield.to_s.include?('Wash')
            data2 << [{ content: "Wash" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_wash.to_s : '').to_s }]
           end

            # Check if 'Buyer Name' is included in @selectfield
            if @selectfield.to_s.include?('Buyer Name')
              data2 << [{ content: "Buyer Name" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_buyer.to_s : '').to_s }]
            end

              # Check if 'Color' is included in @selectfield
           if @selectfield.to_s.include?('Color')
            data2 << [{ content: "Color" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_color.to_s : '').to_s }]
           end

            # Check if 'Size' is included in @selectfield
            if @selectfield.to_s.include?('Size')
            data2 << [{ content: "Size" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_size.to_s : '').to_s }]
            end

           # Check if 'Selling Price' is included in @selectfield
           if @selectfield.to_s.include?('Selling Price')
            data2 << [{ content: "Selling Price" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_sp.to_s : '').to_s }]
           end

          # Check if 'Sampling Time' is included in @selectfield
          if @selectfield.to_s.include?('Sampling Time')
            data2 << [{ content: "Sampling Time" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_sampling.to_s : '').to_s }]
            end

             # Check if 'Currency' is included in @selectfield
          if @selectfield.to_s.include?('Currency')
            data2 << [{ content: "Currency" }, { content: ":" }, { content: currency.to_s }]
            end

             # Check if 'Product Cost' is included in @selectfield
          if @selectfield.to_s.include?('Product Cost')
            data2 << [{ content: "Product Cost" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_prd_cost.to_s : '').to_s }]
            end


      else

           data2 = [
            [{ content: "Product ID" }, { content: ":" }, { content: @reportdata.pd_id.to_s }]
          ]
          
          if @productinfo&.pd_bsc_gender.present?
            data2 += [
              [{ content: "Gender" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_gender.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_material.present?
            data2 += [
              [{ content: "Material" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_material.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_wash.present?
            data2 += [
              [{ content: "Wash" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_wash.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_buyer.present?
            data2 += [
              [{ content: "Buyer Name" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_buyer.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_color.present?
            data2 += [
              [{ content: "Color" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_color.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_size.present?
            data2 += [
              [{ content: "Size" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_size.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_sp.present?
            data2 += [
              [{ content: "Selling Price" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_sp.to_s : '').to_s }]
            ]
          end
          
          if @productinfo&.pd_bsc_sampling.present?
            data2 += [
              [{ content: "Sampling Time" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_sampling.to_s : '').to_s }]
            ]
          end

          if @productinfo&.pd_bsc_currency.present?
            data2 += [
              [{ content: "Currency" }, { content: ":" }, { content: currency.to_s }]
            ]
          end

          if @productinfo&.pd_bsc_prd_cost.present?
            data2 += [
              [{ content: "Product Cost" }, { content: ":" }, { content: (@productinfo ? @productinfo.pd_bsc_prd_cost.to_s : '').to_s }]
            ]
          end

     end        
    
     table([] + data2,:width =>220)  do
     
     style column(0..2),:inline_format=>:true ,  :border_width => 0,:size=>8
     # style row(0),  :size => 9, :align=>:center
     style column(0), width: 80  
     style column(1), width: 15, :font_style=>:bold  # ":" column
     style column(2), width: 125, :font_style=>:bold
    cells.padding = 4
     end
     
    end
    
    
    
    private
    def format_oblig_date(dates)
         newdate = ''
         if dates!=nil && dates!=''
              dts    = Date.parse(dates.to_s)
              newdate = dts.strftime("%d/%m/%Y")
         end
         return newdate
    end
    
  private
  def number_currency_in_words
     to_words(@tnetamt.to_f)  
   end
  
  private
     def currency_formatted(amt)
          amts = ''
          if amt!=nil && amt!=''
            amts = "%.2f" % amt.to_f
          end
          return amts
     end
  
  private
   def formatted_date(dates)
        newdate = ''
        if dates!=nil && dates!=''
             dts    = Date.parse(dates.to_s)
             newdate = dts.strftime("%d-%b-%Y")
        end
        return newdate
   end
  
  def count
    @count ||= 0
    @count = @count+1
  end
  
  
  def to_words(num)
    numbers_to_name = {
        10000000 => "Crore",
        100000 => "Lakh",
        1000 => "Thousand",
        100 => "Hundred",
        90 => "Ninety",
        80 => "Eighty",
        70 => "Seventy",
        60 => "Sixty",
        50 => "Fifty",
        40 => "Forty",
        30 => "Thirty",
        20 => "Twenty",
        19=>"Nineteen",
        18=>"Eighteen",
        17=>"Seventeen",
        16=>"Sixteen",
        15=>"Fifteen",
        14=>"Fourteen",
        13=>"Thirteen",
        12=>"Twelve",
        11 => "Eleven",
        10 => "Ten",
        9 => "Nine",
        8 => "Eight",
        7 => "Seven",
        6 => "Six",
        5 => "Five",
        4 => "Four",
        3 => "Three",
        2 => "Two",
        1 => "One"
      }
  
    log_floors_to_ten_powers = {
      0 => 1,
      1 => 10,
      2 => 100,
      3 => 1000,
      4 => 1000,
      5 => 100000,
      6 => 100000,
      7 => 10000000
    }
  
    num = num.to_i
    return '' if num <= 0 or num >= 100000000
  
    log_floor = Math.log(num, 10).floor
    ten_power = log_floors_to_ten_powers[log_floor]
  
    if num <= 20
      numbers_to_name[num]
    elsif log_floor == 1
      rem = num % 10
      [ numbers_to_name[num - rem], to_words(rem) ].join(' ')
    else
      [ to_words(num / ten_power), numbers_to_name[ten_power], to_words(num % ten_power) ].join(' ')
    end
  end
  end