lib/skr/models/invoice.rb in stockor-0.3.0 vs lib/skr/models/invoice.rb in stockor-0.4.0

- old
+ new

@@ -28,11 +28,10 @@ has_visible_id has_random_hash_code has_gl_transaction is_order_like - has_additional_events :amount_paid_change belongs_to :sales_order, export: true belongs_to :customer_project, export: true belongs_to :customer, export: true belongs_to :location, export: true @@ -45,42 +44,50 @@ has_many :lines, -> { order(:position) }, class_name: 'Skr::InvLine', inverse_of: :invoice, extend: Concerns::INV::Lines, export: { writable: true } - before_save :maybe_mark_paid + has_many :payments, inverse_of: :invoice, + extend: Concerns::INV::Payments, + listen: { save: :apply_payment }, + export: { writable: true } before_validation :set_defaults, on: :create validates :customer, :location, set: true validate :ensure_unlocked, :ensure_location_matches_so + # used to update customer account balances when invoices created / paid scope :open_for_customer, lambda{ | customer | where(state: :open, customer_id: customer.is_a?(Customer) ? customer.id : customer) }, export: true + scope :with_sku_id, lambda { | sku_id | + joins("join (select skr_sku_inv_xref.invoice_id as invoice_id from skr_sku_inv_xref " + + "where sku_id=#{sku_id.to_i} group by skr_sku_inv_xref.invoice_id) as sku_inv " + + "on sku_inv.invoice_id = skr_invoices.id") + }, export: true + scope :with_details, lambda { |should_use=true | compose_query_using_detail_view( view: 'skr_inv_details', join_to: 'invoice_id' ) }, export: true enum state: { open: 1, paid: 5, - partial: 10 + partialy_paid: 10 } state_machine do state :open, initial: true state :paid - state :partial + state :partialy_paid event :mark_paid do - transitions from: [:open,:partial], to: :paid - before :apply_balances + transitions from: [:open,:partialy_paid], to: :paid end - event :mark_partial do - transitions from: [:open,:partial], to: :partial - before :apply_balances + event :mark_partialy_paid do + transitions from: :open, to: :partialy_paid end end def initialize(attributes = {}) super @@ -88,11 +95,11 @@ self.invoice_date ||= Date.today end # @return [BigDecimal] total - amount_paid def unpaid_amount - self.total - amount_paid + self.total - self.payments.total end # @return [Boolean] is the invoice paid in full def fully_paid? unpaid_amount <= 0 @@ -107,39 +114,21 @@ GlPeriod.is_date_locked?(self.invoice_date) end private - # attributes for GlTransaction - def attributes_for_gl_transaction - { location: location, source: self, - description: "INV #{self.visible_id}" } - end - # set the state if the amount_paid was changed - def maybe_mark_paid - return unless amount_paid_changed? + def apply_payment(pymnt) if self.fully_paid? && self.may_mark_paid? - self.mark_paid - elsif self.amount_paid > 0 && self.may_mark_partial? - self.mark_partial - end - end + self.mark_paid! + elsif self.payments.total > 0 && self.may_mark_partialy_paid? + Lanes.logger_debug('paying') - def apply_balances - return unless amount_paid_changed? - change = amount_paid - amount_paid_was - Lanes.logger.debug "Applying payment #{amount_paid} changed: #{change}" - return if change.zero? - GlTransaction.push_or_save( - owner: self, amount: change, - debit: customer.gl_receivables_account, - credit: GlAccount.default_for(:deposit_holding) - ) - fire_pubsub_event( :amount_paid_change ) - true - end + #self.mark_partialy_paid! + Lanes.logger_debug('paid') + end + end def set_defaults if pick_ticket self.location = pick_ticket.location