Class: Tax

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/tax.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: taxes

active                           :boolean          default(FALSE), not null
amount                           :decimal(19, 4)   default(0.0), not null
collect_account_id               :integer
country                          :string           not null
created_at                       :datetime         not null
creator_id                       :integer
deduction_account_id             :integer
description                      :text
fixed_asset_collect_account_id   :integer
fixed_asset_deduction_account_id :integer
id                               :integer          not null, primary key
lock_version                     :integer          default(0), not null
name                             :string           not null
nature                           :string           not null
reference_name                   :string
updated_at                       :datetime         not null
updater_id                       :integer

Class Method Summary collapse

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?

Class Method Details

.available_naturesObject

Returns TaxNature items which are used by recorded taxes


78
79
80
81
82
83
84
# File 'app/models/tax.rb', line 78

def available_natures
  Nomen::TaxNature.select do |item|
    references = Nomen::Tax.list.keep_if { |tax| tax.nature.to_s == item.name.to_s }
    taxes = Tax.where(reference_name: references.map(&:name))
    taxes.any?
  end
end

.clean!Object


86
87
88
89
90
# File 'app/models/tax.rb', line 86

def clean!
  Tax.find_each do |tax|
    tax.destroy if tax.destroyable?
  end
end

.import_all_from_nomenclature(options = {}) ⇒ Object

Load all tax from tax nomenclature by country


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'app/models/tax.rb', line 130

def import_all_from_nomenclature(options = {})
  country = options[:country] || Preference[:country]
  today = Time.zone.today
  Nomen::Tax.where(country: country.to_sym).find_each do |tax|
    if options[:active]
      if tax.started_on
        next unless today > tax.started_on
      end
      if tax.stopped_on
        next unless today < tax.stopped_on
      end
    end
    import_from_nomenclature(tax.name, true)
  end
end

.import_from_nomenclature(reference_name, active = nil) ⇒ Object

Load a tax from tax nomenclature


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/models/tax.rb', line 93

def import_from_nomenclature(reference_name, active = nil)
  unless item = Nomen::Tax.find(reference_name)
    raise ArgumentError, "The tax #{reference_name.inspect} is not known"
  end
  tax = Tax.find_by(amount: item.amount, nature: item.nature, country: item.country)
  tax ||= Tax.find_by(reference_name: reference_name)

  if tax
    tax.update_column(:active, active) unless active.nil?
    return tax
  end
  nature = Nomen::TaxNature.find(item.nature)
  if nature.computation_method != :percentage
    raise StandardError, 'Can import only percentage computed taxes'
  end
  attributes = {
    amount: item.amount,
    name: item.human_name,
    nature: item.nature,
    country: item.country,
    active: active,
    reference_name: item.name
  }
  [:deduction, :collect, :fixed_asset_deduction, :fixed_asset_collect].each do ||
    next unless name = nature.send("#{}_account")
    tax_radical = Account.find_or_import_from_nomenclature(name)
    # find if already account tax  by number was created
     = Account.find_or_create_by_number("#{tax_radical.number}#{nature.suffix}") do |a|
      a.name = "#{tax_radical.name} - #{item.human_name}"
      a.usages = tax_radical.usages
    end
    attributes["#{}_account_id"] = .id
  end
  Tax.create!(attributes)
end

.load_defaultsObject

Load default taxes of instance country


147
148
149
# File 'app/models/tax.rb', line 147

def load_defaults
  import_all_from_nomenclature(country: Preference[:country].to_sym)
end

.used_for_untaxed_dealsObject


73
74
75
# File 'app/models/tax.rb', line 73

def used_for_untaxed_deals
  where(amount: 0).reorder(:id).first
end

Instance Method Details

#amount_of(pretax_amount) ⇒ Object

Returns the amount of a pretax amount


173
174
175
# File 'app/models/tax.rb', line 173

def amount_of(pretax_amount)
  (pretax_amount.to_d * coefficient)
end

#coefficientObject

Returns the matching coefficient k of the percentage where pretax_amount * k = amount_with_tax


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

def coefficient
  (100 + amount) / 100
end

#compute(amount, all_taxes_included = false) ⇒ Object

Compute the tax amount If with_taxes is true, it's considered that the given amount is an amount with tax


159
160
161
162
163
164
165
# File 'app/models/tax.rb', line 159

def compute(amount, all_taxes_included = false)
  if all_taxes_included
    amount.to_d / (1 + 100 / self.amount.to_d)
  else
    amount.to_d * self.amount.to_d / 100
  end
end

#null_amount?Boolean

Returns true if amount is equal to 0


178
179
180
# File 'app/models/tax.rb', line 178

def null_amount?
  amount.zero?
end

#pretax_amount_of(amount) ⇒ Object

Returns the pretax amount of an amount


168
169
170
# File 'app/models/tax.rb', line 168

def pretax_amount_of(amount)
  (amount.to_d / coefficient)
end

#short_labelObject

Returns the short label of a tax


189
190
191
# File 'app/models/tax.rb', line 189

def short_label
  "#{amount.l(precision: 0)}% (#{country})"
end