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-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: purchases

accounted_at        :datetime
affair_id           :integer
amount              :decimal(19, 4)   default(0.0), not null
confirmed_at        :datetime
created_at          :datetime         not null
creator_id          :integer
currency            :string           not null
custom_fields       :jsonb
delivery_address_id :integer
description         :text
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
planned_at          :datetime
pretax_amount       :decimal(19, 4)   default(0.0), not null
reference_number    :string
responsible_id      :integer
state               :string
supplier_id         :integer          not null
updated_at          :datetime         not null
updater_id          :integer

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

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

#can_generate_parcel?Boolean

Returns:

  • (Boolean)

258
259
260
# File 'app/models/purchase.rb', line 258

def can_generate_parcel?
  items.any? && delivery_address && (order? || invoice?)
end

#client_addressObject


250
251
252
# File 'app/models/purchase.rb', line 250

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


212
213
214
215
216
217
218
# File 'app/models/purchase.rb', line 212

def confirm(confirmed_at = nil)
  return false unless can_confirm?
  reload
  self.confirmed_at ||= confirmed_at || Time.zone.now
  save!
  super
end

#deal_taxes(mode = :debit) ⇒ Object

Globalizes taxes into an array of hash


165
166
167
168
169
170
171
172
173
174
# File 'app/models/purchase.rb', line 165

def deal_taxes(mode = :debit)
  return [] if deal_mode_amount(mode).zero?
  taxes = {}
  coeff = 1.to_d # (self.send("deal_#{mode}?") ? 1 : -1)
  for item in items
    taxes[item.tax_id] ||= { amount: 0.0.to_d, tax: item.tax }
    taxes[item.tax_id][:amount] += coeff * item.amount
  end
  taxes.values
end

#dealt_atObject


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

def dealt_at
  (invoice? ? invoiced_at : created_at? ? self.created_at : Time.zone.now)
end

#deliverable?Boolean

Returns:

  • (Boolean)

205
206
207
208
209
# File 'app/models/purchase.rb', line 205

def deliverable?
  # TODO: How to compute if it remains deliverable products
  true
  # (self.quantity - self.undelivered(:population)) > 0 and not self.invoice?
end

#has_content?Boolean

Returns:

  • (Boolean)

180
181
182
# File 'app/models/purchase.rb', line 180

def has_content?
  items.any?
end

#has_content_not_deliverable?Boolean

Returns:

  • (Boolean)

188
189
190
191
192
193
194
195
# File 'app/models/purchase.rb', line 188

def has_content_not_deliverable?
  return false unless has_content?
  deliverable = false
  for item in items
    deliverable = true if item.variant.deliverable?
  end
  !deliverable
end

#invoice(invoiced_at = nil) ⇒ Object

Save the last date when the invoice of purchase was received


221
222
223
224
225
226
227
# File 'app/models/purchase.rb', line 221

def invoice(invoiced_at = nil)
  return false unless can_invoice?
  reload
  self.invoiced_at ||= invoiced_at || Time.zone.now
  save!
  super
end

#invoiced_onObject


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

def invoiced_on
  dealt_at.to_date
end

#labelObject


229
230
231
# File 'app/models/purchase.rb', line 229

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

#purchased?Boolean

Returns:

  • (Boolean)

184
185
186
# File 'app/models/purchase.rb', line 184

def purchased?
  (order? || invoice?)
end

#refreshObject


176
177
178
# File 'app/models/purchase.rb', line 176

def refresh
  save
end

#state_labelObject

Prints human name of current state


234
235
236
# File 'app/models/purchase.rb', line 234

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

#statusObject


238
239
240
241
# File 'app/models/purchase.rb', line 238

def status
  return affair.status if invoice?
  :stop
end

#supplier_addressObject


243
244
245
246
247
248
# File 'app/models/purchase.rb', line 243

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

#taxes_amountObject


254
255
256
# File 'app/models/purchase.rb', line 254

def taxes_amount
  amount - pretax_amount
end

#undelivered(column) ⇒ Object

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

  • column can be :amount or :pretax_amount


199
200
201
202
203
# File 'app/models/purchase.rb', line 199

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