sacarmonar15
9/22/2017 - 5:18 PM

Invoice Report per Attendee

#Reporte de invoices por attendee. 
report = []
report << [ 'name', 'company', 'paid', 'invoice number', 'invoice date', 'total charges', 'charges without vat', 'payments amount', 'payment type', 'payment_date' ]
event = Event.find(11885)
 
payments_index = Payment::TYPE.invert 
event.attendees.each do |attendee|
    next if attendee.total_charges == 0.0
    payments = Payment.get_by_entity(Attendee.name, attendee.id)
 
    charges_amout = attendee.total_charges
    charges_wv    = attendee.subtotal
    deposits_amount = attendee.total_deposits
    payment_types = []
    payment_date = nil
    invoice = nil
 
    Payment.get_by_entity(Attendee.name, attendee.id).each do |p|
        invoice =  p.event_invoice if invoice.blank? or invoice.number < p.event_invoice.number
 
        if p.operation == 1
        else
            payment_types << p.payment_type if !payment_types.include?(p.payment_type)
            payment_date = p.created_at if payment_date.blank? or payment_date < p.created_at
        end
    end
 
    invoice_number = invoice.present? ? invoice.number : ""
    invoice_date  = invoice.present? ? invoice.created_at.strftime("%d/%m/%Y - %R") : ""
    types = payment_types.map{|a| payments_index[a].to_s}
    str_types = types.join(",")
    str_payment_date =  payment_date.present? ?  payment_date.strftime("%d/%m/%Y - %R") : ""
    taxes =  charges_wv.to_f - charges_amout.to_f
 
    report << [attendee.full_name, attendee.company, attendee.paid?, invoice_number, invoice_date.to_s, charges_amout.to_f, charges_wv.to_f, taxes, deposits_amount.to_f, str_types, str_payment_date]
 
end