Class: SaleItem

Inherits:
Ekylibre::Record::Base show all
Includes:
PeriodicCalculable
Defined in:
app/models/sale_item.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2016 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero 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 Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see 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
credited_item_id     :integer
credited_quantity    :decimal(19, 4)
currency             :string           not null
id                   :integer          not null, primary key
label                :text
lock_version         :integer          default(0), not null
position             :integer
pretax_amount        :decimal(19, 4)   default(0.0), not null
quantity             :decimal(19, 4)   default(1.0), not null
reduction_percentage :decimal(19, 4)   default(0.0), not null
sale_id              :integer          not null
tax_id               :integer
unit_amount          :decimal(19, 4)   default(0.0), not null
unit_pretax_amount   :decimal(19, 4)
updated_at           :datetime         not null
updater_id           :integer
variant_id           :integer          not null

Constant Summary

Constants included from PeriodicCalculable

PeriodicCalculable::PARAMETERS

Instance Method Summary collapse

Methods inherited from Ekylibre::Record::Base

#already_updated?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Instance Method Details

#already_credited_quantityObject


170
171
172
# File 'app/models/sale_item.rb', line 170

def already_credited_quantity
  credits.sum(:quantity)
end

#creditable_quantityObject


174
175
176
# File 'app/models/sale_item.rb', line 174

def creditable_quantity
  quantity + already_credited_quantity
end

#designationObject


138
139
140
141
142
143
# File 'app/models/sale_item.rb', line 138

def designation
  d = self.label
  d << "\n" + annotation.to_s unless annotation.blank?
  d << "\n" + tc(:tracking, serial: tracking.serial.to_s) if tracking
  d
end

#new_subscription(attributes = {}) ⇒ Object


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/models/sale_item.rb', line 145

def new_subscription(attributes = {})
  # raise StandardError.new attributes.inspect
  subscription = Subscription.new((attributes || {}).merge(sale_id: sale.id, product_id: product_id, nature_id: product.subscription_nature_id, sale_item_id: id))
  subscription.attributes = attributes
  product = subscription.product
  nature  = subscription.nature
  if nature
    if nature.period?
      subscription.started_at ||= Time.zone.today
      subscription.stopped_at ||= Delay.compute((product.subscription_duration || '1 year') + ', 1 day ago', subscription.started_at)
    else
      subscription.first_number ||= nature.actual_number.to_i
      subscription.last_number ||= subscription.first_number + (product.subscription_quantity || 1) - 1
    end
  end
  subscription.quantity ||= 1
  subscription.address_id ||= sale.delivery_address_id
  subscription.entity_id ||= subscription.address_entity_id if subscription.address
  subscription
end

#payment_ratioObject

know how many percentage of invoiced VAT to declare


179
180
181
182
183
184
185
# File 'app/models/sale_item.rb', line 179

def payment_ratio
  if sale.affair.balanced?
    return 1.00
  elsif sale.affair.credit != 0.0
    return (1 - (-sale.affair.balance / sale.affair.credit)).to_f
  end
end

#taxes_amountObject


166
167
168
# File 'app/models/sale_item.rb', line 166

def taxes_amount
  amount - pretax_amount
end

#undelivered_quantityObject


134
135
136
# File 'app/models/sale_item.rb', line 134

def undelivered_quantity
  quantity - parcel_items.sum(:quantity)
end