Sha256: ef2c034289bae8d391409deefe2b1205599fbcfe1851b8e63decd4f372169dd4

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 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} },
        :nominals     => banknotes
      }
  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

8 entries across 8 versions & 1 rubygems

Version Path
smartkiosk-client-0.2.1 app/models/collection.rb
smartkiosk-client-0.2 app/models/collection.rb
smartkiosk-client-0.1.19 app/models/collection.rb
smartkiosk-client-0.1.18 app/models/collection.rb
smartkiosk-client-0.1.17 app/models/collection.rb
smartkiosk-client-0.1.16 app/models/collection.rb
smartkiosk-client-0.1.15 app/models/collection.rb
smartkiosk-client-0.1.14 app/models/collection.rb