<commit_before># encoding: utf-8
# author: <NAME>
# email: <EMAIL>
class Transaction < ActiveRecord::Base
  acts_as_org

  STATES   = ["draft"  , "approved" , "paid" , "due", "inventory", "nulled"]
  TYPES    = ['Income' , 'Expense'  , 'Buy']
  DECIMALS = 2
  # Determines if the oprations is made on transaction or pay_plan or payment
  ###############################
  include Models::Transaction::Calculations
  include Models::Transaction::Trans
  include Models::Transaction::Approve
  include Models::Transaction::PayPlan
  include Models::Transaction::Payment

  ###############################
 
  # callbacks
  before_validation :set_defaults, :if => :new_record?
  before_create     :set_creator
  before_destroy    :null_transaction
  #before_save       :set_state

  # relationships
  #belongs_to :account
  belongs_to :contact
  belongs_to :currency
  belongs_to :project
  belongs_to :creator , :class_name => "User"
  belongs_to :approver, :class_name => "User"
  belongs_to :creditor, :class_name => "User"
  belongs_to :nuller,   :class_name => "User"

  #has_one  :account_ledger, :conditions => "operation = 'transaction'"

  has_many :pay_plans          , :dependent => :destroy , :order => "payment_date ASC", :autosave => true
  has_many :transaction_details, :dependent => :destroy
  has_many :account_ledgers    , :dependent => :destroy, :conditions => "operation != 'transaction'", :autosave => false

  has_and_belongs_to_many :taxes, :class_name => 'Tax'
  # nested attributes
  accepts_nested_attributes_for :transaction_details, :allow_destroy => true

  # validations
  #validates :account_id, :contact_account => true
  validates :contact_id, :contact  => true

  #default_scope where(:active => true)

  # scopes
  scope :draft    , where(:state => 'draft')
  scope :approved , where(:state => 'approved')
  scope :paid     , where(:state => 'paid')
  scope :due      , where("transactions.state = ? AND transactions.payment_date < ?" , 'approved' , Date.today)
  scope :inventory, where("transactions.deliver = ? AND transactions.balance_inventory > 0", true)
  scope :credit   , where(:cash => false)
  # Especial used to update
  scope :for_deliver, paid.where("transactions.deliver = ? AND transactions.balance_inventory > 0", false)
  scope :nulled, unscoped.org.where(:state => 'nulled')

  delegate :name, :symbol, :plural, :code, :to => :currency, :prefix => true


  # Define boolean methods for states
  STATES.each do |state|
    class_eval <<-CODE, __FILE__, __LINE__ + 1
      def #{state}?
        "#{state}" == state ? true : false
      end
    CODE
  end

  def self.all_states
    STATES + ["awaiting_payment"]
  end

  # Finds using the state
  def self.find_with_state(state)
    ret   = self.org.includes(:contact, :currency).order("created_at DESC")
    ret = ret.send(scoped_state(state)) if scoped_state(state)
    ret
  end

  # Fins with state
  def self.scoped_state(state)
    state = 'all' unless all_states.include?(state)

    case state
    when 'all' then false
    when 'awaiting_payment' then 'approved'
    else state
    end
  end

  # method used for searching
  def self.search(options)
    ret = self.org.includes(:contact, :currency)
    ret = ret.send(scoped_state(options[:option])) if scoped_state(options[:option])
    ret.where("transactions.ref_number LIKE :code OR accounts.name LIKE :code", :code => "%#{options[:search]}%")<commit_msg>Working search for transactions<commit_after># encoding: utf-8
# author: <NAME>
# email: <EMAIL>
class Transaction < ActiveRecord::Base
  acts_as_org

  STATES   = ["draft"  , "approved" , "paid" , "due", "inventory", "nulled"]
  TYPES    = ['Income' , 'Expense'  , 'Buy']
  DECIMALS = 2
  # Determines if the oprations is made on transaction or pay_plan or payment
  ###############################
  include Models::Transaction::Calculations
  include Models::Transaction::Trans
  include Models::Transaction::Approve
  include Models::Transaction::PayPlan
  include Models::Transaction::Payment

  ###############################
 
  # callbacks
  before_validation :set_defaults, :if => :new_record?
  before_create     :set_creator
  before_destroy    :null_transaction
  #before_save       :set_state

  # relationships
  #belongs_to :account
  belongs_to :contact
  belongs_to :currency
  belongs_to :project
  belongs_to :creator , :class_name => "User"
  belongs_to :approver, :class_name => "User"
  belongs_to :creditor, :class_name => "User"
  belongs_to :nuller,   :class_name => "User"

  #has_one  :account_ledger, :conditions => "operation = 'transaction'"

  has_many :pay_plans          , :dependent => :destroy , :order => "payment_date ASC", :autosave => true
  has_many :transaction_details, :dependent => :destroy
  has_many :account_ledgers    , :dependent => :destroy, :conditions => "operation != 'transaction'", :autosave => false

  has_and_belongs_to_many :taxes, :class_name => 'Tax'
  # nested attributes
  accepts_nested_attributes_for :transaction_details, :allow_destroy => true

  # validations
  #validates :account_id, :contact_account => true
  validates :contact_id, :contact  => true

  #default_scope where(:active => true)

  # scopes
  scope :draft    , where(:state => 'draft')
  scope :approved , where(:state => 'approved')
  scope :paid     , where(:state => 'paid')
  scope :due      , where("transactions.state = ? AND transactions.payment_date < ?" , 'approved' , Date.today)
  scope :inventory, where("transactions.deliver = ? AND transactions.balance_inventory > 0", true)
  scope :credit   , where(:cash => false)
  # Especial used to update
  scope :for_deliver, paid.where("transactions.deliver = ? AND transactions.balance_inventory > 0", false)
  scope :nulled, unscoped.org.where(:state => 'nulled')

  delegate :name, :symbol, :plural, :code, :to => :currency, :prefix => true


  # Define boolean methods for states
  STATES.each do |state|
    class_eval <<-CODE, __FILE__, __LINE__ + 1
      def #{state}?
        "#{state}" == state ? true : false
      end
    CODE
  end

  def self.all_states
    STATES + ["awaiting_payment"]
  end

  # Finds using the state
  def self.find_with_state(state)
    ret   = self.org.includes(:contact, :currency).order("created_at DESC")
    ret = ret.send(scoped_state(state)) if scoped_state(state)
    ret
  end

  # Fins with state
  def self.scoped_state(state)
    state = 'all' unless all_states.include?(state)

    case state
    when 'all' then false
    when 'awaiting_payment' then 'approved'
    else state
    end
  end

  # method used for searching
  def self.search(options)
    ret = self.org.includes(:contact, :currency)
    ret = ret.send(scoped_state(options[:option])) if scoped_state(options[:option])
    ret.where("transactions.ref_number LIKE :code OR contacts.matchcode LIKE :code", :code => "%#{options[:search]}%")
{'fraction_non_alphanumeric': 0.14200320746464498, 'fraction_numerical': 0.0014579384749963552, 'mean_word_length': 4.204855842185129, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3068268', 'n_tokens_mistral': 2218, 'n_tokens_neox': 2154, 'n_words': 550}