<commit_before># = Informations
#
# == License
#
# Ekylibre - Simple ERP
# Copyright (C) 2009-2013 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# == Table: sale_items
#
#  account_id           :integer
#  amount               :decimal(19, 4)   default(0.0), not null
#  annotation           :text
#  created_at           :datetime         not null
#  creator_id           :integer
#  entity_id            :integer
#  id                   :integer          not null, primary key
#  label                :text
#  lock_version         :integer          default(0), not null
#  origin_id            :integer
#  position             :integer
#  pretax_amount        :decimal(19, 4)   default(0.0), not null
#  price_amount         :decimal(19, 4)
#  price_id             :integer          not null
#  price_template_id    :integer
#  product_id           :integer          not null
#  quantity             :decimal(19, 4)   default(1.0), not null
#  reduction_origin_id  :integer
#  reduction_percentage :decimal(19, 4)   default(0.0), not null
#  sale_id              :integer          not null
#  tax_id               :integer
#  tracking_id          :integer
#  unit                 :string(255)
#  updated_at           :datetime         not null
#  updater_id           :integer
#  warehouse_id         :integer
#


class SaleItem < Ekylibre::Record::Base
  after_save :set_reduction
  attr_accessible :annotation, :price_amount, :price_id, :product_id, :quantity, :reduction_percentage, :sale_id, :tax_id, :unit
  attr_readonly :sale_id
  belongs_to :account
  belongs_to :entity
  belongs_to :sale
  belongs_to :origin, :class_name => "SaleItem"
  belongs_to :price, :class_name => "ProductPrice"
  belongs_to :product
  belongs_to :reduction_origin, :class_name => "SaleItem"
  belongs_to :tax
  belongs_to :tracking
  enumerize :unit, :in => Nomenclatures["units"].list
  has_many :delivery_items, :class_name => "OutgoingDeliveryItem", :foreign_key => :sale_item_id
  has_one :reduction, :class_name => "SaleItem", :foreign_key => :reduction_origin_id
  has_many :credits, :class_name => "SaleItem", :foreign_key => :origin_id
  has_many :reductions, :class_name => "SaleItem", :foreign_key => :reduction_origin_id, :dependent => :delete_all
  has_many :subscriptions, :dependent => :destroy

  accepts_nested_attributes_for :subscriptions
  delegate :sold?, :to => :sale
  delegate :name, :to => :tax, :prefix => true
  delegate :subscribing?, :deliverable?, :to => :product, :prefix => true


  acts_as_list :scope => :sale
  acts_as_stockable :mode => :virtual, :if => :sold?
  sums :sale, :items, :pretax_amount, :amount

  #[VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
  validates_numericality_of :amount, :pretax_amount, :price_amount, :quantity, :reduction_percentage, :allow_nil => true
  validates_length_of :unit, :allow_nil => true, :maximum => 255
  validates_presence_of :amount, :pretax_amount, :price, :product, :quantity, :reduction_percentage, :sale
  #]VALIDATORS]
  validates_presence_of :tax


  before_validation do
    # if not self.price and self.sale and self.product
    #   self.price = self.product.price(:listing => self.sale.client.sale_price_listing)
    # end
    if self.price_amount and self.tax # and not self.price
      # raise "Yes"
      self.price = self.product.price(:pretax_amount => self.price_amount, :tax => self.tax, :listing => self.sale.client.sale_price_listing)
    else
      raise self.inspect
    end<commit_msg>Don't raise error when price_amount and tax_id are not given<commit_after># = Informations
#
# == License
#
# Ekylibre - Simple ERP
# Copyright (C) 2009-2013 <NAME>, <NAME>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see http://www.gnu.org/licenses.
#
# == Table: sale_items
#
#  account_id           :integer
#  amount               :decimal(19, 4)   default(0.0), not null
#  annotation           :text
#  created_at           :datetime         not null
#  creator_id           :integer
#  entity_id            :integer
#  id                   :integer          not null, primary key
#  label                :text
#  lock_version         :integer          default(0), not null
#  origin_id            :integer
#  position             :integer
#  pretax_amount        :decimal(19, 4)   default(0.0), not null
#  price_amount         :decimal(19, 4)
#  price_id             :integer          not null
#  price_template_id    :integer
#  product_id           :integer          not null
#  quantity             :decimal(19, 4)   default(1.0), not null
#  reduction_origin_id  :integer
#  reduction_percentage :decimal(19, 4)   default(0.0), not null
#  sale_id              :integer          not null
#  tax_id               :integer
#  tracking_id          :integer
#  unit                 :string(255)
#  updated_at           :datetime         not null
#  updater_id           :integer
#  warehouse_id         :integer
#


class SaleItem < Ekylibre::Record::Base
  after_save :set_reduction
  attr_accessible :annotation, :price_amount, :price_id, :product_id, :quantity, :reduction_percentage, :sale_id, :tax_id, :unit
  attr_readonly :sale_id
  belongs_to :account
  belongs_to :entity
  belongs_to :sale
  belongs_to :origin, :class_name => "SaleItem"
  belongs_to :price, :class_name => "ProductPrice"
  belongs_to :product
  belongs_to :reduction_origin, :class_name => "SaleItem"
  belongs_to :tax
  belongs_to :tracking
  enumerize :unit, :in => Nomenclatures["units"].list
  has_many :delivery_items, :class_name => "OutgoingDeliveryItem", :foreign_key => :sale_item_id
  has_one :reduction, :class_name => "SaleItem", :foreign_key => :reduction_origin_id
  has_many :credits, :class_name => "SaleItem", :foreign_key => :origin_id
  has_many :reductions, :class_name => "SaleItem", :foreign_key => :reduction_origin_id, :dependent => :delete_all
  has_many :subscriptions, :dependent => :destroy

  accepts_nested_attributes_for :subscriptions
  delegate :sold?, :to => :sale
  delegate :name, :to => :tax, :prefix => true
  delegate :subscribing?, :deliverable?, :to => :product, :prefix => true


  acts_as_list :scope => :sale
  acts_as_stockable :mode => :virtual, :if => :sold?
  sums :sale, :items, :pretax_amount, :amount

  #[VALIDATORS[ Do not edit these lines directly. Use `rake clean:validations`.
  validates_numericality_of :amount, :pretax_amount, :price_amount, :quantity, :reduction_percentage, :allow_nil => true
  validates_length_of :unit, :allow_nil => true, :maximum => 255
  validates_presence_of :amount, :pretax_amount, :price, :product, :quantity, :reduction_percentage, :sale
  #]VALIDATORS]
  validates_presence_of :tax


  before_validation do
    # if not self.price and self.sale and self.product
    #   self.price = self.product.price(:listing => self.sale.client.sale_price_listing)
    # end
    if self.price_amount and self.tax # and not self.price
      # raise "Yes"
      self.price = self.product.price(:pretax_amount => self.price_amount, :tax => self.tax, :listing => self.sale.client.sale_price_listing)
    else
      # raise "No price_amount/tax given..."self.inspect
      self.price = self.product.price(:listing => self.sale.client.sale_price_listing)
    end
{'fraction_non_alphanumeric': 0.10210930009587728, 'fraction_numerical': 0.009348034515819751, 'mean_word_length': 3.210393541876892, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6923613', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2574, 'n_words': 753}