class Wco::Invoice

#
# 2023-08-18 vp Continue
# 2017-10-31 vp Start
# Invoice - for wasya.co
#

def create_stripe

def create_stripe
  if is_stripe
    stripe_invoice = Stripe::Invoice.create({
      customer:          leadset.customer_id,
      collection_method: 'send_invoice',
      days_until_due:    0,
      # collection_method: 'charge_automatically',
      pending_invoice_items_behavior: 'exclude',
    })
    items.each do |item|
      stripe_price = Wco::Price.find( item[:price_id] ).price_id
      invoice_item = Stripe::InvoiceItem.create({
        customer: leadset.customer_id,
        price:    stripe_price,
        invoice:  stripe_invoice.id,
        quantity: item[:quantity],
      })
    end
    Stripe::Invoice.finalize_invoice( stripe_invoice[:id] )
    self.invoice_id = stripe_invoice[:id]
  end
end

def filename

def filename
  "invoice-#{number}.pdf"
end

def generate_monthly_invoice

#
# tree image: 964 x 1248
# tree image: 791 x 1024
# tree image: 896 x 1159
#
# Canvas width: 612, height: 792
# Prawn/pdf unit of measure is 1/72"
def generate_monthly_invoice
  tree_img_url      = "#{Rails.root.join('public')}/tree-#{number}.jpg"
  wasya_co_logo_url = "#{Rails.root.join('public')}/259x66-WasyaCo-logo.png"
  ## canvas width: 612, height: 792
  pdf = Prawn::Document.new
  pdf.canvas do
    pdf.image tree_img_url, at: [ 0, 792 ], width: 612
    pdf.fill_color 'ffffff'
    pdf.transparent( 0.75 ) do
      pdf.fill_rectangle [0, 792], 612, 792
    end
    pdf.fill_color '000000'
    pdf.image wasya_co_logo_url, at: [252, 720], width: 108 ## 1.5"
    pdf.bounding_box( [0.75*72, 9.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "From:"
      pdf.text "Wasya Co"
      pdf.text "victor@wasya.co"
    end
    pdf.bounding_box( [4.5*72, 9.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "Stats:"
      pdf.text "Date: #{ Time.now.to_date.to_s }"
      pdf.text "Invoice #: #{ number }"
    end
    pdf.bounding_box( [0.75*72, 8.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "To:"
      pdf.text leadset.name
      pdf.text leadset.email
    end
    pdf.bounding_box( [4.5*72, 8.25*72], width: 3.25*72, height: 0.75*72 ) do
      pdf.text "Notes:"
      pdf.text "Support & various tasks, for the month of #{ month_on.strftime('%B') }."
    end
    lineitems_with_header = [
      [ 'Description', 'Per Unit', '# of Units', 'Subtotal' ]
    ]
    total = 0
    leadset.subscriptions.each do |i|
      subtotal = (i.price.amount_cents * i.quantity).to_f/100
      lineitems_with_header.push([ i.price.product.name, i.price.name_simple, i.quantity, subtotal ])
      total += subtotal
    end
    pdf.move_down 20
    pdf.table(lineitems_with_header, {
      position: :center,
      width: 7.5*72,
      cell_style: {
        inline_format: true,
        borders: [ :bottom ]
      },
    } )
    pdf.table([
      [ 'Total' ],
      [ total ],
    ], {
      position: 7*72,
      width: 1*72,
      cell_style: {
        inline_format: true,
        borders: [ :bottom ]
      },
    } )
    pdf.text_box "Thank you!", at: [ 3.25*72, 1.25*72 ], width: 2*72, height: 1*72, align: :center
  end
  return pdf
end