Class: Xeroizer::Record::Invoice
- Defined in:
- lib/xeroizer/models/invoice.rb
Constant Summary collapse
- INVOICE_TYPE =
{ 'ACCREC' => 'Accounts Receivable', 'ACCPAY' => 'Accounts Payable' }
- INVOICE_TYPES =
INVOICE_TYPE.keys.sort
- INVOICE_STATUS =
{ 'AUTHORISED' => 'Approved invoices awaiting payment', 'DELETED' => 'Draft invoices that are deleted', 'DRAFT' => 'Invoices saved as draft or entered via API', 'PAID' => 'Invoices approved and fully paid', 'SUBMITTED' => 'Invoices entered by an employee awaiting approval', 'VOIDED' => 'Approved invoices that are voided' }
- INVOICE_STATUSES =
INVOICE_STATUS.keys.sort
Instance Attribute Summary
Attributes inherited from Base
#attributes, #complete_record_downloaded, #errors, #model, #parent
Instance Method Summary collapse
-
#accounts_payable? ⇒ Boolean
Helper method to check if the invoice is accounts payable.
-
#accounts_receivable? ⇒ Boolean
Helper method to check if the invoice is accounts receivable.
-
#approve! ⇒ Object
Approve a draft invoice.
-
#approved? ⇒ Boolean
Helper method to check if the invoice has been approved.
-
#contact_id ⇒ Object
Access the contact ID without forcing a download of an incomplete, summary invoice.
-
#contact_name ⇒ Object
Access the contact name without forcing a download of an incomplete, summary invoice.
- #currency_rate ⇒ Object
-
#delete! ⇒ Object
Delete an approved invoice with no payments.
- #loaded_record? ⇒ Boolean
- #not_summary_or_loaded_record(always_summary) ⇒ Object
-
#pdf(filename = nil) ⇒ Object
Retrieve the PDF version of this invoice.
-
#sub_total(always_summary = false) ⇒ Object
Calculate sub_total from line_items.
- #sub_total=(sub_total) ⇒ Object
-
#total(always_summary = false) ⇒ Object
Calculate the total from line_items.
- #total=(total) ⇒ Object
-
#total_tax(always_summary = false) ⇒ Object
Calculate total_tax from line_items.
- #total_tax=(total_tax) ⇒ Object
-
#void! ⇒ Object
Void an approved invoice with no payments.
Methods inherited from Base
#[], #[]=, #as_json, build, #complete_record_downloaded?, #download_complete_record!, #initialize, #inspect, #new_model_class, #new_record?, #non_calculated_attributes, #save, #saved!, #to_h, #to_json, #update_attributes
Methods included from XmlHelper
Methods included from ValidationHelper
Methods included from RecordAssociationHelper
Methods included from ModelDefinitionHelper
Methods included from ClassLevelInheritableAttributes
Constructor Details
This class inherits a constructor from Xeroizer::Record::Base
Instance Method Details
#accounts_payable? ⇒ Boolean
Helper method to check if the invoice is accounts payable.
105 106 107 |
# File 'lib/xeroizer/models/invoice.rb', line 105 def accounts_payable? type == 'ACCPAY' end |
#accounts_receivable? ⇒ Boolean
Helper method to check if the invoice is accounts receivable.
110 111 112 |
# File 'lib/xeroizer/models/invoice.rb', line 110 def accounts_receivable? type == 'ACCREC' end |
#approve! ⇒ Object
Approve a draft invoice
198 199 200 |
# File 'lib/xeroizer/models/invoice.rb', line 198 def approve! change_status!('AUTHORISED') end |
#approved? ⇒ Boolean
Helper method to check if the invoice has been approved.
100 101 102 |
# File 'lib/xeroizer/models/invoice.rb', line 100 def approved? [ 'AUTHORISED', 'PAID', 'VOIDED' ].include? status end |
#contact_id ⇒ Object
Access the contact ID without forcing a download of an incomplete, summary invoice.
95 96 97 |
# File 'lib/xeroizer/models/invoice.rb', line 95 def contact_id attributes[:contact] && attributes[:contact][:contact_id] end |
#contact_name ⇒ Object
Access the contact name without forcing a download of an incomplete, summary invoice.
89 90 91 |
# File 'lib/xeroizer/models/invoice.rb', line 89 def contact_name attributes[:contact] && attributes[:contact][:name] end |
#currency_rate ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/xeroizer/models/invoice.rb', line 114 def currency_rate if attributes[:currency_rate] if attributes[:currency_rate].is_a? BigDecimal attributes[:currency_rate] else BigDecimal.new(attributes[:currency_rate]) end else BigDecimal.new('1.0') end end |
#delete! ⇒ Object
Delete an approved invoice with no payments.
188 189 190 |
# File 'lib/xeroizer/models/invoice.rb', line 188 def delete! change_status!('DELETED') end |
#loaded_record? ⇒ Boolean
176 177 178 179 |
# File 'lib/xeroizer/models/invoice.rb', line 176 def loaded_record? new_record? || (!new_record? && line_items && line_items.size > 0) end |
#not_summary_or_loaded_record(always_summary) ⇒ Object
172 173 174 |
# File 'lib/xeroizer/models/invoice.rb', line 172 def not_summary_or_loaded_record(always_summary) !always_summary && loaded_record? end |
#pdf(filename = nil) ⇒ Object
Retrieve the PDF version of this invoice.
183 184 185 |
# File 'lib/xeroizer/models/invoice.rb', line 183 def pdf(filename = nil) parent.pdf(id, filename) end |
#sub_total(always_summary = false) ⇒ Object
Calculate sub_total from line_items.
142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/xeroizer/models/invoice.rb', line 142 def sub_total(always_summary = false) if !@sub_total_is_set && not_summary_or_loaded_record(always_summary) sum = (line_items || []).inject(BigDecimal.new('0')) { | sum, line_item | sum + line_item.line_amount } # If the default amount types are inclusive of 'tax' then remove the tax amount from this sub-total. sum -= total_tax if line_amount_types == 'Inclusive' sum else attributes[:sub_total] end end |
#sub_total=(sub_total) ⇒ Object
126 127 128 129 |
# File 'lib/xeroizer/models/invoice.rb', line 126 def sub_total=(sub_total) @sub_total_is_set = true attributes[:sub_total] = sub_total end |
#total(always_summary = false) ⇒ Object
Calculate the total from line_items.
164 165 166 167 168 169 170 |
# File 'lib/xeroizer/models/invoice.rb', line 164 def total(always_summary = false) if !@total_is_set && not_summary_or_loaded_record(always_summary) sub_total + total_tax else attributes[:total] end end |
#total=(total) ⇒ Object
136 137 138 139 |
# File 'lib/xeroizer/models/invoice.rb', line 136 def total=(total) @total_is_set = true attributes[:total] = total end |
#total_tax(always_summary = false) ⇒ Object
Calculate total_tax from line_items.
155 156 157 158 159 160 161 |
# File 'lib/xeroizer/models/invoice.rb', line 155 def total_tax(always_summary = false) if !@total_tax_is_set && not_summary_or_loaded_record(always_summary) (line_items || []).inject(BigDecimal.new('0')) { | sum, line_item | sum + line_item.tax_amount } else attributes[:total_tax] end end |
#total_tax=(total_tax) ⇒ Object
131 132 133 134 |
# File 'lib/xeroizer/models/invoice.rb', line 131 def total_tax=(total_tax) @total_tax_is_set = true attributes[:total_tax] = total_tax end |
#void! ⇒ Object
Void an approved invoice with no payments.
193 194 195 |
# File 'lib/xeroizer/models/invoice.rb', line 193 def void! change_status!('VOIDED') end |