Class: Purchase

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/purchase.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: purchases

accounted_at                             :datetime
affair_id                                :integer
amount                                   :decimal(19, 4)   default(0.0), not null
command_mode                             :string
confirmed_at                             :datetime
contract_id                              :integer
created_at                               :datetime         not null
creator_id                               :integer
currency                                 :string           not null
custom_fields                            :jsonb
delivery_address_id                      :integer
description                              :text
estimate_reception_date                  :datetime
id                                       :integer          not null, primary key
invoiced_at                              :datetime
journal_entry_id                         :integer
lock_version                             :integer          default(0), not null
nature_id                                :integer
number                                   :string           not null
ordered_at                               :datetime
payment_at                               :datetime
payment_delay                            :string
planned_at                               :datetime
pretax_amount                            :decimal(19, 4)   default(0.0), not null
quantity_gap_on_invoice_journal_entry_id :integer
reconciliation_state                     :string
reference_number                         :string
responsible_id                           :integer
state                                    :string           not null
supplier_id                              :integer          not null
tax_payability                           :string           not null
type                                     :string
undelivered_invoice_journal_entry_id     :integer
updated_at                               :datetime         not null
updater_id                               :integer

Direct Known Subclasses

PurchaseInvoice, PurchaseOrder

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

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

Class Method Details

.affair_classObject


149
150
151
# File 'app/models/purchase.rb', line 149

def self.affair_class
  "#{name}Affair".constantize
end

.third_attributeObject


145
146
147
# File 'app/models/purchase.rb', line 145

def self.third_attribute
  :supplier
end

Instance Method Details

#client_addressObject


203
204
205
# File 'app/models/purchase.rb', line 203

def client_address
  Entity.of_company.default_mail_address.mail_coordinate
end

#confirm(confirmed_at = nil) ⇒ Object

Save the last date when the purchase was confirmed


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

def confirm(confirmed_at = nil)
  # no more usage of state machine
  # return false unless can_confirm?
  reload
  self.confirmed_at ||= confirmed_at || Time.zone.now
  save!
  super
end

#default_currencyObject


153
154
155
# File 'app/models/purchase.rb', line 153

def default_currency
  currency || nature.currency
end

#labelObject


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

def label
  number # tc('label', :supplier => self.supplier.full_name.to_s, :address => self.delivery_address.mail_coordinate.to_s)
end

#precisionObject


157
158
159
# File 'app/models/purchase.rb', line 157

def precision
  Nomen::Currency.find(currency).precision
end

#refreshObject


161
162
163
# File 'app/models/purchase.rb', line 161

def refresh
  save
end

#sepable?Boolean

Returns:

  • (Boolean)

207
208
209
210
211
212
213
214
# File 'app/models/purchase.rb', line 207

def sepable?
  cash_mode = OutgoingPaymentMode.mode_sepa.first

  cash_mode &&
    currency == cash_mode.cash.currency &&
    payee.iban.present? &&
    payee..present?
end

#state_labelObject

Prints human name of current state


192
193
194
# File 'app/models/purchase.rb', line 192

def state_label
  self.class.state_machine.state(state.to_sym).human_name
end

#supplier_addressObject


196
197
198
199
200
201
# File 'app/models/purchase.rb', line 196

def supplier_address
  if supplier.default_mail_address
    return supplier.default_mail_address.mail_coordinate
  end
  nil
end

#thirdObject


165
166
167
# File 'app/models/purchase.rb', line 165

def third
  send(third_attribute)
end

#undelivered(column) ⇒ Object

Computes an amount (with or without taxes) of the undelivered products

  • column can be :amount or :pretax_amount


171
172
173
174
175
# File 'app/models/purchase.rb', line 171

def undelivered(column)
  sum  = send(column)
  sum -= parcels.sum(column)
  sum.round(2)
end