Sha256: 00be9f73b6fbea0bb4c263d8aaf3db057045697b3d73d9c59e7d6fef75ec1375

Contents?: true

Size: 1.45 KB

Versions: 36

Compression:

Stored size: 1.45 KB

Contents

class Collection < ActiveRecord::Base
  has_one :receipt, :as => :document, :dependent => :destroy

  serialize :banknotes
  serialize :payment_ids

  after_save do
    period_start   = Collection.where("id != ?", self.id).order('created_at DESC').first.try(:created_at)
    period_start ||= Payment.order(:created_at).first.try(:created_at)
    period_start ||= Date.civil(0, 1, 1)

    r = Receipt.find_or_create_by_document_id_and_document_type(id, self.class.name)
    r.update_attributes :template => ReceiptTemplate.read('collection'), 
      :keyword => 'collection',
      :fields => {
        :period_start => I18n.l(period_start),
        :period_end   => I18n.l(created_at),
        :amount       => amount,
        :banknotes    => Hash[banknotes.sort].inject([]){|arr, (nom, cnt)| arr << {'nominal' => nom, 'count' => cnt, 'product' => nom.to_i*cnt.to_i} }
      }
  end

  def self.collect!
    payments = Payment.uncollected.complete.all

    return false if payments.blank?

    self.transaction do
      collection = self.create!(
        :banknotes => Payment.merge_banknotes(payments),
        :payment_ids => payments.map(&:id)
      )

      payments.each{|x| x.update_attribute :collection_id, collection.id}

      return collection
    end

    return false
  end

  def title
    amount
  end

  def report
    Payments::CollectWorker.perform_async(id)
  end

  def amount
    banknotes.inject(0){|sum, (nominal, count)| sum + nominal.to_i*count.to_i }
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
smartkiosk-client-0.1.13 app/models/collection.rb
smartkiosk-client-0.1.12 app/models/collection.rb
smartkiosk-client-0.1.11 app/models/collection.rb
smartkiosk-client-0.1.10 app/models/collection.rb
smartkiosk-client-0.1.9 app/models/collection.rb
smartkiosk-client-0.1.8 app/models/collection.rb
smartkiosk-client-0.1.7 app/models/collection.rb
smartkiosk-client-0.1.6 app/models/collection.rb
smartkiosk-client-0.1.5 app/models/collection.rb
smartkiosk-client-0.1.4 app/models/collection.rb
smartkiosk-client-0.1.3 app/models/collection.rb
smartkiosk-client-0.1.2 app/models/collection.rb
smartkiosk-client-0.1.1 app/models/collection.rb
smartkiosk-client-0.1.0 app/models/collection.rb
smartkiosk-client-0.0.28 app/models/collection.rb
smartkiosk-client-0.0.26 app/models/collection.rb
smartkiosk-client-0.0.25 app/models/collection.rb
smartkiosk-client-0.0.24 app/models/collection.rb
smartkiosk-client-0.0.23 app/models/collection.rb
smartkiosk-client-0.0.22 app/models/collection.rb