lib/xeroizer/models/invoice.rb in xeroizer-0.3.4 vs lib/xeroizer/models/invoice.rb in xeroizer-0.3.5
- old
+ new
@@ -75,18 +75,24 @@
belongs_to :contact
has_many :line_items
has_many :payments
has_many :credit_notes
- validates_presence_of :date, :due_date
+ validates_presence_of :date, :due_date, :unless => proc { |invoice| invoice.new_record? }
validates_inclusion_of :type, :in => INVOICE_TYPES
- validates_inclusion_of :status, :in => INVOICE_STATUSES
- validates_inclusion_of :line_amount_types, :in => LINE_AMOUNT_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_associated :contact
- validates_associated :line_items
+ validates_associated :line_items, :allow_blanks => true, :unless => proc { |invoice| invoice.approved? }
+ validates_associated :line_items, :if => proc { |invoice| invoice.approved? }
public
-
+
+ # Helper method to check if the invoice has been approved.
+ def approved?
+ [ 'AUTHORISED', 'PAID', 'VOIDED' ].include? status
+ end
+
# Helper method to check if the invoice is accounts payable.
def accounts_payable?
type == 'ACCPAY'
end