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-2019 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
activity_budget_id   :integer
amount               :decimal(19, 4)   default(0.0), not null
annotation           :text
codes                :jsonb
compute_from         :string           not null
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
team_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?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Instance Method Details

#already_credited_quantityObject


227
228
229
# File 'app/models/sale_item.rb', line 227

def already_credited_quantity
  credits.sum(:quantity)
end

#creditable_quantityObject


231
232
233
# File 'app/models/sale_item.rb', line 231

def creditable_quantity
  quantity + already_credited_quantity
end

#designationObject


195
196
197
198
199
200
# File 'app/models/sale_item.rb', line 195

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

#new_subscription(attributes = {}) ⇒ Object


202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'app/models/sale_item.rb', line 202

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


236
237
238
239
240
241
242
# File 'app/models/sale_item.rb', line 236

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

#reduction_coefficientObject


187
188
189
# File 'app/models/sale_item.rb', line 187

def reduction_coefficient
  (100.0 - (reduction_percentage || 0.0)) / 100.0
end

#taxes_amountObject


223
224
225
# File 'app/models/sale_item.rb', line 223

def taxes_amount
  amount - pretax_amount
end

#undelivered_quantityObject


191
192
193
# File 'app/models/sale_item.rb', line 191

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