lib/xeroizer/models/invoice.rb in xeroizer-0.3.5 vs lib/xeroizer/models/invoice.rb in xeroizer-0.4.0

- old
+ new

@@ -38,17 +38,10 @@ 'SUBMITTED' => 'Invoices entered by an employee awaiting approval', 'VOIDED' => 'Approved invoices that are voided' } unless defined?(INVOICE_STATUS) INVOICE_STATUSES = INVOICE_STATUS.keys.sort - LINE_AMOUNT_TYPE = { - "Inclusive" => 'CreditNote lines are inclusive tax', - "Exclusive" => 'CreditNote lines are exclusive of tax (default)', - "NoTax" => 'CreditNotes lines have no tax' - } unless defined?(LINE_AMOUNT_TYPE) - LINE_AMOUNT_TYPES = LINE_AMOUNT_TYPE.keys.sort - set_primary_key :invoice_id set_possible_primary_keys :invoice_id, :invoice_number list_contains_summary_only true guid :invoice_id @@ -75,18 +68,30 @@ belongs_to :contact has_many :line_items has_many :payments has_many :credit_notes - validates_presence_of :date, :due_date, :unless => proc { |invoice| invoice.new_record? } + validates_presence_of :date, :due_date, :unless => :new_record? validates_inclusion_of :type, :in => INVOICE_TYPES - validates_inclusion_of :status, :in => INVOICE_STATUSES, :unless => proc { |invoice| invoice.new_record? } - validates_inclusion_of :line_amount_types, :in => LINE_AMOUNT_TYPES, :unless => proc { |invoice| invoice.new_record? } + validates_inclusion_of :status, :in => INVOICE_STATUSES, :unless => :new_record? + validates_inclusion_of :line_amount_types, :in => LINE_AMOUNT_TYPES, :unless => :new_record? validates_associated :contact - validates_associated :line_items, :allow_blanks => true, :unless => proc { |invoice| invoice.approved? } - validates_associated :line_items, :if => proc { |invoice| invoice.approved? } + validates_associated :line_items, :allow_blanks => true, :unless => :approved? + validates_associated :line_items, :if => :approved? public + + # Access the contact name without forcing a download of + # an incomplete, summary invoice. + def contact_name + attributes[:contact] && attributes[:contact][:name] + end + + # Access the contact ID without forcing a download of an + # incomplete, summary invoice. + def contact_id + attributes[:contact] && attributes[:contact][:contact_id] + end # Helper method to check if the invoice has been approved. def approved? [ 'AUTHORISED', 'PAID', 'VOIDED' ].include? status end