torg_api 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaad596c23f264b0814b3cc5bd78fd6ba13d242a
4
- data.tar.gz: fc99aa7738f8c4fc386613e0c1aa276fb8069ffd
3
+ metadata.gz: efa73776303a79730db0063486c6f5d2ca462d45
4
+ data.tar.gz: f383f424f346dd28818e69f757292e8a7bd567c7
5
5
  SHA512:
6
- metadata.gz: ebd4a454b2db3765429411320170d1e79c0e40b038789574d6c8d370b199221edb664ccc8a054a8297bcf4c91a492f148c7589a4b60ece2fc5e97483453befab
7
- data.tar.gz: 40d880eb6e0034de26c2a3f86e6663da7f297c4da2737bb0a6a7bacb671050a9b51056078ed2c0f46bd882e33b44e835330053533450ba4283d41fc69c34899c
6
+ metadata.gz: 3c6e294afdfac65d1c61d092b1cd72ab1b19bd3a2372632a1dfe30b0010ed3fde12f806b95f4618090685460713c0ad72c47324c9ec744e385e404dd2a21a751
7
+ data.tar.gz: 0e4c2f2e0d47c3677785f6bfa0afc8fbd4a7d8c1d2200bbbe633f8b0580f69ac161087e41b7dab82ddec7b5f1253d1bb485bcfe4539336a28d78723ec07caaba
data/README.md CHANGED
@@ -18,7 +18,63 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ ```ruby
22
+ require 'torg_api'
23
+ require 'awesome_print'
24
+ require 'logger'
25
+
26
+ # Параметры подключения к БД
27
+ TorgApi.establish_connection(
28
+ adapter: 'oracle_enhanced',
29
+ database: 'ksazd_backup',
30
+ username: 'mardok',
31
+ password: 'mardok'
32
+ )
33
+
34
+ # Логгер если нужен
35
+ TorgApi.logger = Logger.new('queries.log')
36
+
37
+ # Пример поиска по ИНН контрагентов
38
+ ids = TorgApi::Api::Contractor.find_by_inn('7707083893')
39
+ ap ids
40
+
41
+ # Пример создания контрагента
42
+
43
+ # хэш ответа веб-сервиса B2B
44
+ firm_info = {
45
+ firm_id: '15777',
46
+ date_lastedit: '2006-04-17 16:26:56 +1100',
47
+ org_name: 'Обучение 326',
48
+ org_name_short: 'Обучение 326',
49
+ code_okpo: '47526985',
50
+ bank_name: 'Сбербанк РФ',
51
+ bank_inn: '5836614002',
52
+ ogrn: nil,
53
+ ogrn_given: nil,
54
+ ogrn_date_given: nil,
55
+ bank_kpp: '583601001',
56
+ bank_bik: '045655724',
57
+ bank_r_account: '40702810006000000384',
58
+ bank_c_account: '30101810500000000724',
59
+ bank_comments: 'без комментариев',
60
+ jury_address: 'Нижегородская область, г. Бор',
61
+ post_address: 'Нижегородская область, г. Бор',
62
+ fact_address: 'Нижегородская область, г. Бор',
63
+ site_url: 'www.bsz.ru',
64
+ certification: 'Сертификат мира № 1 от 01.01.01, выдан мировым сообществом',
65
+ org_details: nil,
66
+ bank_details: nil,
67
+ country: 643,
68
+ is_smb: false
69
+ }
70
+
71
+ contractor_id = TorgApi::Api::Contractor.create_from_b2b(firm_info, 555)
72
+ ap contractor_id
73
+ ```
74
+
75
+ ## Documentation
76
+
77
+ http://www.rubydoc.info/gems/torg_api
22
78
 
23
79
  ## Contributing
24
80
 
@@ -0,0 +1,33 @@
1
+ module TorgApi
2
+ module Api
3
+ # Участник закупки
4
+ class Bidder < TorgApi::Base
5
+ class << self
6
+ # Создаёт участника в системе и возвращает его id
7
+ # @param contractor_id [Integer] id контрагента
8
+ # @param tender_id [Integer] id закупки
9
+ # @param date_offer [Time] Дата и время регистрации конверта участника
10
+ # @return [Integer]
11
+ def create(contractor_id, tender_id, date_offer = nil)
12
+ b = nil
13
+ TorgApi::Models::Bidder.transaction do
14
+ b = TorgApi::Models::Bidder.create!(
15
+ tender_id: tender_id,
16
+ contractor_id: contractor_id
17
+ )
18
+
19
+ TorgApi::Models::Cover.create!(
20
+ bidder_id: b.id,
21
+ register_time: date_offer,
22
+ type_id: CoverLabels::REQUEST,
23
+ delegate: 'B2B-Center',
24
+ provision: 'Электронный конверт'
25
+ )
26
+ end
27
+
28
+ b.id
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,107 @@
1
+ module TorgApi
2
+ module Api
3
+ # Контрагент
4
+ class Contractor < TorgApi::Base
5
+ # Порядок сортировки по значению поля status
6
+ # enum status: { orig: 0, active: 1, old: 2, inactive: 3 }
7
+ STATUS_ORDER = [1, 0, 3, 2]
8
+
9
+ STATUS_ORDER_NUMS = [].tap do |mas|
10
+ STATUS_ORDER.each_with_index { |o, i| mas << [o, i] }
11
+ end.flatten.join(', ')
12
+
13
+ DECODE_STATUS_ORDER = "decode(status, #{STATUS_ORDER_NUMS})"
14
+
15
+ # @return [Integer] id контрагента
16
+ attr_accessor :id
17
+ # @return [String] Наименование
18
+ attr_accessor :name
19
+ # @return [String] Полное наименование
20
+ attr_accessor :fullname
21
+ # @return [String] Форма собственности
22
+ attr_accessor :ownership
23
+ # @return [String] ИНН
24
+ attr_accessor :inn
25
+ # @return [String] КПП
26
+ attr_accessor :kpp
27
+ # @return [String] ОГРН
28
+ attr_accessor :ogrn
29
+ # @return [String] ОКПО
30
+ attr_accessor :okpo
31
+ # @return [Integer] Статус записи(0-новая, 1-активная, 2-старая, 3-неактивная)
32
+ attr_accessor :status
33
+ # @return [Integer] Вид контрагента (0-ИП, 1-юр. лицо, 2-иностран., 3-физ. лицо)
34
+ attr_accessor :form
35
+ # @return [String] Юридический адрес
36
+ attr_accessor :legal_addr
37
+ # @return [Integer] ИД автора
38
+ attr_accessor :user_id
39
+ # @return [Boolean] Резидент?
40
+ attr_accessor :is_resident
41
+ # @return [Boolean] ДЗО?
42
+ attr_accessor :is_dzo
43
+ # @return [Boolean] Субъект малого и среднего предпринимательства?
44
+ attr_accessor :is_sme
45
+ # @return [Integer] Форма акционерного общества
46
+ attr_accessor :jsc_form_id
47
+ # @return [Integer] Малое или среднее предпринимательство
48
+ attr_accessor :sme_type_id
49
+
50
+ class << self
51
+ # Поиск id контрагентов по ИНН
52
+ # @param inn [String] ИНН
53
+ # @return [Integer[]] Массив id найденных контрагентов отсортированных по статусу и дате изменения
54
+ def find_by_inn(inn)
55
+ TorgApi::Models::Contractor
56
+ .where(inn: inn)
57
+ .where(next_id: nil)
58
+ .order("#{DECODE_STATUS_ORDER}, updated_at desc")
59
+ .pluck('id')
60
+ end
61
+
62
+ # Создаёт контрагента
63
+ # @param hash [Hash] хэш ответа веб-сервиса B2B
64
+ # @param user [Integer] id автора
65
+ # @return [Integer] id созданного объекта
66
+ def create_from_b2b(hash, user)
67
+ c = Contractor.new
68
+ c.name = hash[:org_name_short]
69
+ c.fullname = hash[:org_name]
70
+ c.ownership = extract_ownership(hash[:org_name_short])
71
+ c.inn = hash[:bank_inn]
72
+ c.kpp = hash[:bank_kpp]
73
+ c.ogrn = hash[:ogrn]
74
+ c.okpo = hash[:code_okpo]
75
+ c.status = 0 # enum status: { orig: 0, active: 1, old: 2, inactive: 3 }
76
+ c.form = extract_form(hash[:bank_inn])
77
+ c.legal_addr = hash[:jury_address]
78
+ c.user_id = user
79
+ c.is_resident = extract_resident(hash[:country])
80
+ c.is_dzo = nil
81
+ c.is_sme = hash[:is_smb]
82
+ c.jsc_form_id = nil
83
+ c.sme_type_id = nil
84
+
85
+ TorgApi::Models::Contractor.create!(c.to_h).id
86
+ end
87
+
88
+ def extract_ownership(org_name_short)
89
+ org_name_short[/^([\w\-]+)/]
90
+ end
91
+
92
+ def extract_form(bank_inn)
93
+ # enum form: { businessman: 0, company: 1, foreign: 2, person: 3 }
94
+ case bank_inn.size
95
+ when 12 then 0
96
+ when 10 then 1
97
+ else 2
98
+ end
99
+ end
100
+
101
+ def extract_resident(country)
102
+ country == 643
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,44 @@
1
+ module TorgApi
2
+ module Api
3
+ # Оферта участника
4
+ class Offer < TorgApi::Base
5
+ class << self
6
+ # Создаёт оферту
7
+ # @param lot_id [Integer] id лота
8
+ # @param bidder_id [Integer] id участника
9
+ # @param hash [Integer] хэш ответа веб-сервиса B2B
10
+ # @return [Integer] id созданного объекта
11
+ def create(lot_id, bidder_id, hash)
12
+ o = nil
13
+ TorgApi::Models::Offer.transaction do
14
+ o = TorgApi::Models::Offer.create!(
15
+ lot_id: lot_id,
16
+ bidder_id: bidder_id,
17
+ num: hash[:offer_num],
18
+ version: 0,
19
+ type_id: CoverLabels::REQUEST,
20
+ status_id: extract_status(hash[:winner])
21
+ )
22
+
23
+ TorgApi::Models::Specification.where(lot_id: lot_id).each do |spec|
24
+ TorgApi::Models::OfferSpecification.create!(
25
+ offer_id: o.id,
26
+ specification_id: spec.id,
27
+ cost: hash[:final_price_notax],
28
+ cost_nds: hash[:final_price],
29
+ final_cost: hash[:final_price_notax],
30
+ final_cost_nds: hash[:final_price]
31
+ )
32
+ end
33
+ end
34
+
35
+ o.id
36
+ end
37
+
38
+ def extract_status(winner)
39
+ winner ? OfferStatuses::WIN : OfferStatuses::NEW
40
+ end
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + '/api/*.rb'].each { |file| require file }
@@ -0,0 +1,17 @@
1
+ require 'torg_api/models'
2
+
3
+ module TorgApi
4
+ class Base
5
+ include TorgApi::Constants
6
+ REMOVE_HASH_ATTRS = [:@id]
7
+
8
+ def to_h
9
+ hash = {}
10
+ vars = instance_variables
11
+ REMOVE_HASH_ATTRS.each { |a| vars.delete(a) }
12
+
13
+ vars.each { |var| hash[var.to_s.delete('@').to_sym] = instance_variable_get(var) }
14
+ hash
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,14 @@
1
+ module TorgApi
2
+ module Constants
3
+ module CoverLabels
4
+ REQUEST = 22_001 # Заявка/Предложение
5
+ end
6
+
7
+ module OfferStatuses
8
+ NEW = 26_001 # Первичная
9
+ RECEIVE = 26_002 # Принята
10
+ REJECT = 26_003 # Отклонена
11
+ WIN = 26_004 # Победила
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,6 @@
1
+ module TorgApi
2
+ module Models
3
+ class Bidder < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module TorgApi
2
+ module Models
3
+ class Contractor < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module TorgApi
2
+ module Models
3
+ class Cover < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module TorgApi
2
+ module Models
3
+ class Offer < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module TorgApi
2
+ module Models
3
+ class OfferSpecification < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ module TorgApi
2
+ module Models
3
+ class Specification < ActiveRecord::Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1 @@
1
+ Dir[File.dirname(__FILE__) + '/models/*.rb'].each { |file| require file }
@@ -1,3 +1,3 @@
1
1
  module TorgApi
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
data/lib/torg_api.rb CHANGED
@@ -1,5 +1,15 @@
1
+ require 'active_record'
1
2
  require 'torg_api/version'
3
+ require 'torg_api/constants'
4
+ require 'torg_api/base'
5
+ require 'torg_api/api'
2
6
 
3
7
  module TorgApi
4
- # Your code goes here...
8
+ def self.establish_connection(spec = nil)
9
+ ActiveRecord::Base.establish_connection(spec)
10
+ end
11
+
12
+ def self.logger=(logger)
13
+ ActiveRecord::Base.logger = logger
14
+ end
5
15
  end
data/torg_api.gemspec CHANGED
@@ -20,4 +20,6 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'bundler', '~> 1.3'
22
22
  spec.add_development_dependency 'rake'
23
+
24
+ spec.add_dependency 'activerecord', '~> 4.2.1'
23
25
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: torg_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Archakov Aleksandr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-04-07 00:00:00.000000000 Z
11
+ date: 2015-04-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activerecord
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 4.2.1
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 4.2.1
41
55
  description: API for Torg system
42
56
  email:
43
57
  - archakov@gmail.com
@@ -52,6 +66,19 @@ files:
52
66
  - README.md
53
67
  - Rakefile
54
68
  - lib/torg_api.rb
69
+ - lib/torg_api/api.rb
70
+ - lib/torg_api/api/bidder.rb
71
+ - lib/torg_api/api/contractor.rb
72
+ - lib/torg_api/api/offer.rb
73
+ - lib/torg_api/base.rb
74
+ - lib/torg_api/constants.rb
75
+ - lib/torg_api/models.rb
76
+ - lib/torg_api/models/bidder.rb
77
+ - lib/torg_api/models/contractor.rb
78
+ - lib/torg_api/models/cover.rb
79
+ - lib/torg_api/models/offer.rb
80
+ - lib/torg_api/models/offer_specification.rb
81
+ - lib/torg_api/models/specification.rb
55
82
  - lib/torg_api/version.rb
56
83
  - torg_api.gemspec
57
84
  homepage: https://github.com/kodram/torg_api