vat-calculator 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +31 -0
- data/Rakefile +1 -0
- data/lib/vat-calculator.rb +67 -0
- data/lib/vat-calculator/version.rb +5 -0
- data/vat-calculator.gemspec +21 -0
- metadata +71 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Alex Klyanchin
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# Vat::Calculator
|
2
|
+
|
3
|
+
__Рассчет различных сумм с участием НДС__
|
4
|
+
|
5
|
+
## Использование
|
6
|
+
|
7
|
+
Для использования в модели вызываем метод `has_vat_calculator` и пользуемся
|
8
|
+
```ruby
|
9
|
+
class InvoicePosition < ActiveRecord::Base
|
10
|
+
has_vat_calculator
|
11
|
+
end
|
12
|
+
```
|
13
|
+
```ruby
|
14
|
+
ip = InvoicePositions.last
|
15
|
+
ip.sum_of_vat_for :supplier_price
|
16
|
+
ip.sum_of_vat_for :selling_price
|
17
|
+
# так же доступны методы sum_with_vat_for и sum_without_vat_for
|
18
|
+
```
|
19
|
+
Также если поля модели не соответствуют стандартным supplier_price, selling_price, vat и amount можно передать параметром какие поля использовать
|
20
|
+
```ruby
|
21
|
+
has_vat_calculator :selling_price => :price, :supplier_price => :manufacturer_price, :vat => :nds, :amount => :quantity
|
22
|
+
# параметры необязательны и можно указать только те, которые необходимы
|
23
|
+
```
|
24
|
+
По умолчанию все рассчеты выполняются с учетом количества, если необходимо обратное, то указываем это при подключении
|
25
|
+
```ruby
|
26
|
+
has_vat_calculator :with_amount => false
|
27
|
+
```
|
28
|
+
Также можно использовать параметр при вызове метода на экземпляре класса
|
29
|
+
```ruby
|
30
|
+
ip.sum_of_vat_for :supplier_price, :with_amount => false
|
31
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,67 @@
|
|
1
|
+
require "vat-calculator/version"
|
2
|
+
|
3
|
+
module Vat
|
4
|
+
module Calculator
|
5
|
+
def sum_of_vat_for *args
|
6
|
+
calculate_sum(args) do |price, vat|
|
7
|
+
(vat * price) / (100.0 + vat)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def sum_without_vat_for *args
|
12
|
+
calculate_sum(args) do |price, vat|
|
13
|
+
price / (1 + vat / 100.0)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def sum_with_vat_for *args
|
18
|
+
calculate_sum(args) do |price, _|
|
19
|
+
price
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def calculate_sum args
|
24
|
+
options = args.extract_options!
|
25
|
+
with_amount = options[:with_amount].nil? ? self.class.fields_for_calculations[:with_amount] : options[:with_amount]
|
26
|
+
price_type = args.first
|
27
|
+
|
28
|
+
price, vat = fields_values price_type, :vat
|
29
|
+
sum = yield price, vat
|
30
|
+
|
31
|
+
amount = field_value :amount
|
32
|
+
with_amount ? sum * amount : sum
|
33
|
+
end
|
34
|
+
|
35
|
+
def fields_values *fields
|
36
|
+
fields.map{ |field_name| field_value field_name}
|
37
|
+
end
|
38
|
+
|
39
|
+
def field_value field
|
40
|
+
field_name = self.class.fields_for_calculations[field.to_sym]
|
41
|
+
send field_name
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.included(base)
|
45
|
+
base.extend ClassMethods
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
def vat_calculations fields={}
|
50
|
+
default_params = {:vat => :vat, :amount => :amount, :selling_price => :selling_price, :supplier_price => :supplier_price, :with_amount => true}
|
51
|
+
@fields_for_calculations = default_params.merge fields
|
52
|
+
end
|
53
|
+
|
54
|
+
def fields_for_calculations
|
55
|
+
@fields_for_calculations
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
class ActiveRecord::Base
|
63
|
+
def self.has_vat_calculator params={}
|
64
|
+
include Vat::Calculator
|
65
|
+
vat_calculations params
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'vat-calculator/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "vat-calculator"
|
8
|
+
gem.version = Vat::Calculator::VERSION
|
9
|
+
gem.authors = ["7pikes"]
|
10
|
+
gem.email = ["info@7pikes.com"]
|
11
|
+
gem.description = %q{Gem for calculating product sum with vat, product sum without vat and product sum of vat.}
|
12
|
+
gem.summary = %q{Calculate product sum with vat, product sum without vat and product sum of vat.}
|
13
|
+
gem.homepage = "https://github.com/7Pikes/vat-calculator"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_dependency("activerecord", ">= 3.0.0")
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: vat-calculator
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- 7pikes
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-03-05 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 3.0.0
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 3.0.0
|
30
|
+
description: Gem for calculating product sum with vat, product sum without vat and
|
31
|
+
product sum of vat.
|
32
|
+
email:
|
33
|
+
- info@7pikes.com
|
34
|
+
executables: []
|
35
|
+
extensions: []
|
36
|
+
extra_rdoc_files: []
|
37
|
+
files:
|
38
|
+
- .gitignore
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- lib/vat-calculator.rb
|
44
|
+
- lib/vat-calculator/version.rb
|
45
|
+
- vat-calculator.gemspec
|
46
|
+
homepage: https://github.com/7Pikes/vat-calculator
|
47
|
+
licenses: []
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
require_paths:
|
51
|
+
- lib
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
none: false
|
60
|
+
requirements:
|
61
|
+
- - ! '>='
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
requirements: []
|
65
|
+
rubyforge_project:
|
66
|
+
rubygems_version: 1.8.24
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Calculate product sum with vat, product sum without vat and product sum of
|
70
|
+
vat.
|
71
|
+
test_files: []
|