Table name: invoices
id :integer not null, primary key fixxpert_id :integer user_id :integer invoiceable_id :integer created_at :datetime updated_at :datetime paid :boolean default(FALSE) email_notifications :integer default(0) last_notification :datetime manually_billed :boolean default(FALSE) payable_amount :integer description :string(255) company_id :integer invoiceable_type :string(255) charged :boolean default(FALSE)
Attribute Accessors
Attribute Accessors
Attribute Accessors
Attribute Accessors
Attribute Accessors
Attribute Accessors
Attribute Accessors
Attribute Accessors
Attribute Accessors
It sets the payable amount.
# File app/models/invoice.rb, line 119 def assign_payable_amount self.payable_amount = calculate_payable_amount end
Calculate amount for expert session on hours length.
# File app/models/invoice.rb, line 124 def calculate_payable_amount if invoiceable.is_a? ExpertSession self.total_amount.cents - (500*session_length_hours) else self.total_amount.cents if self.fixxpert.present? end end
It returns the chargeable amount.
# File app/models/invoice.rb, line 199 def chargeable_amount total_amount end
To create or update Invoice for expert session.
# File app/models/invoice.rb, line 67 def create_or_update_expert_session if self.manually_billed? ts = (self.invoiceable || self.build_invoiceable) ts.attributes = { subtrade_id: subtrade_id, trade_id: trade_id, date: date, start: start_time, end: end_time, category_id: category_id, rate_in_cents: rate_in_cents, fixxpert_id: User.current.id, user_id: user_id, location: location} ts.save! end
Returns user as customer
# File app/models/invoice.rb, line 160 def customer user end
Validation to ensure date must be in past
# File app/models/invoice.rb, line 147 def date_must_be_past if end_time.present? and date.present? if Time.zone.parse("#{date} #{end_time}") > Time.zone.now if date.to_date == Date.today errors.add(:end_time, 'Must be less than current time') else errors.add(:date, 'Must be in past') end end end end
Update paid to true
# File app/models/invoice.rb, line 204 def pay! self.update_attribute(:paid, true) end
Pay fixxpert
# File app/models/invoice.rb, line 165 def pay_fixxpert unless paid? if AccountBalance.credit(self.fixxpert, self, true, self.payable_amount) self.paid = true self.save(:validate => false) self.invoiceable.update_attribute(:occured, true) if invoiceable.is_a? ExpertSession InvoiceMailer.delay(queue: :mailer).paid_email(self) InvoiceMailer.delay(queue: :mailer).paid_email_fixxpert(self) self.create_activity key: 'payment.payfixxpert', owner: user, recipient: fixxpert, type: 'Notification' return true end end end
Rate as currency object
# File app/models/invoice.rb, line 180 def rate Money.new(expert_session.try(:rate_in_cents), Money.default_currency) end
send notification email for Invoice
# File app/models/invoice.rb, line 99 def send_invoice_email InvoiceMailer.delay(queue: :mailer).create_email(self) if self.fixxpert.present? self.create_activity key: 'invoice.create', owner: fixxpert, recipient: user, type: 'Notification' end
To get the session hours length.
# File app/models/invoice.rb, line 185 def session_length_hours (invoiceable.length / 60.0).round 2 end
if invoice is manually build than assign current user id as fixxpert id
# File app/models/invoice.rb, line 105 def set_fixxpert_rate_and_session_length if self.manually_billed? self.fixxpert_id = User.current.id end end
validation to ensure that start time must be less than end time
# File app/models/invoice.rb, line 138 def start_time_must_be_less_than_end_time if start_time.present? and end_time.present? and date.present? if Time.zone.parse("#{date} #{start_time}") >= Time.zone.parse("#{date} #{end_time}") errors.add(:end_time, 'must be greater than Start Time') end end end
Returns the status of Invoice.
# File app/models/invoice.rb, line 87 def status if paid? 'Paid' elsif invoiceable.is_a? ExpertSession (invoiceable.occured.nil? or invoiceable.occured?) ? 'Pending' : 'Rejected' else 'Pending' end end
It returns the total amount.
# File app/models/invoice.rb, line 190 def total_amount if manually_billed? amount else self.invoiceable.total_amount end end
Update invoiced minutes for this fixxpert
# File app/models/invoice.rb, line 112 def update_fixxpert_billed_length if invoiceable.is_a? ExpertSession new_length = self.fixxpert.expert_sessions.confirmed.joins(:invoice).sum('expert_sessions.end_time - expert_sessions.start_time') end end
It updates payable amount.
# File app/models/invoice.rb, line 133 def update_payable_amount update_columns(payable_amount: calculate_payable_amount) end