Class: TorgApi::Api::Contractor

Inherits:
Base
  • Object
show all
Defined in:
lib/torg_api/api/contractor.rb

Overview

Контрагент

Constant Summary collapse

STATUS_ORDER =

Порядок сортировки по значению поля status enum status: { orig: 0, active: 1, old: 2, inactive: 3 }

[1, 0, 3, 2]
STATUS_ORDER_NUMS =
[].tap do |mas|
  STATUS_ORDER.each_with_index { |o, i| mas << [o, i] }
end.flatten.join(', ')
DECODE_STATUS_ORDER =
"decode(status, #{STATUS_ORDER_NUMS})"

Constants inherited from Base

Base::REMOVE_HASH_ATTRS

Instance Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Base

attr_accessor, attributes, #initialize, #to_h, torg_resource

Constructor Details

This class inherits a constructor from TorgApi::Base

Instance Attribute Details

#formInteger

Returns Вид контрагента (0-ИП, 1-юр. лицо, 2-иностран., 3-физ. лицо).

Returns:

  • (Integer)

    Вид контрагента (0-ИП, 1-юр. лицо, 2-иностран., 3-физ. лицо)



34
35
36
# File 'lib/torg_api/api/contractor.rb', line 34

def form
  @form
end

#fullnameString

Returns Полное наименование.

Returns:

  • (String)

    Полное наименование



20
21
22
# File 'lib/torg_api/api/contractor.rb', line 20

def fullname
  @fullname
end

#idInteger

Returns id контрагента.

Returns:

  • (Integer)

    id контрагента



16
17
18
# File 'lib/torg_api/api/contractor.rb', line 16

def id
  @id
end

#innString

Returns ИНН.

Returns:

  • (String)

    ИНН



24
25
26
# File 'lib/torg_api/api/contractor.rb', line 24

def inn
  @inn
end

#is_dzoBoolean

Returns ДЗО?.

Returns:

  • (Boolean)

    ДЗО?



42
43
44
# File 'lib/torg_api/api/contractor.rb', line 42

def is_dzo
  @is_dzo
end

#is_residentBoolean

Returns Резидент?.

Returns:

  • (Boolean)

    Резидент?



40
41
42
# File 'lib/torg_api/api/contractor.rb', line 40

def is_resident
  @is_resident
end

#is_smeBoolean

Returns Субъект малого и среднего предпринимательства?.

Returns:

  • (Boolean)

    Субъект малого и среднего предпринимательства?



44
45
46
# File 'lib/torg_api/api/contractor.rb', line 44

def is_sme
  @is_sme
end

#jsc_form_idInteger

Returns Форма акционерного общества.

Returns:

  • (Integer)

    Форма акционерного общества



46
47
48
# File 'lib/torg_api/api/contractor.rb', line 46

def jsc_form_id
  @jsc_form_id
end

#kppString

Returns КПП.

Returns:

  • (String)

    КПП



26
27
28
# File 'lib/torg_api/api/contractor.rb', line 26

def kpp
  @kpp
end

Returns Юридический адрес.

Returns:

  • (String)

    Юридический адрес



36
37
38
# File 'lib/torg_api/api/contractor.rb', line 36

def legal_addr
  @legal_addr
end

#nameString

Returns Наименование.

Returns:

  • (String)

    Наименование



18
19
20
# File 'lib/torg_api/api/contractor.rb', line 18

def name
  @name
end

#ogrnString

Returns ОГРН.

Returns:

  • (String)

    ОГРН



28
29
30
# File 'lib/torg_api/api/contractor.rb', line 28

def ogrn
  @ogrn
end

#okpoString

Returns ОКПО.

Returns:

  • (String)

    ОКПО



30
31
32
# File 'lib/torg_api/api/contractor.rb', line 30

def okpo
  @okpo
end

#oktmoString

Returns ОКТМО.

Returns:

  • (String)

    ОКТМО



50
51
52
# File 'lib/torg_api/api/contractor.rb', line 50

def oktmo
  @oktmo
end

#ownershipString

Returns Форма собственности.

Returns:

  • (String)

    Форма собственности



22
23
24
# File 'lib/torg_api/api/contractor.rb', line 22

def ownership
  @ownership
end

#reg_dateString

Returns Дата регистрации юр. лица.

Returns:

  • (String)

    Дата регистрации юр. лица



52
53
54
# File 'lib/torg_api/api/contractor.rb', line 52

def reg_date
  @reg_date
end

#sme_type_idInteger

Returns Малое или среднее предпринимательство.

Returns:

  • (Integer)

    Малое или среднее предпринимательство



48
49
50
# File 'lib/torg_api/api/contractor.rb', line 48

def sme_type_id
  @sme_type_id
end

#statusInteger

Returns Статус записи(0-новая, 1-активная, 2-старая, 3-неактивная).

Returns:

  • (Integer)

    Статус записи(0-новая, 1-активная, 2-старая, 3-неактивная)



32
33
34
# File 'lib/torg_api/api/contractor.rb', line 32

def status
  @status
end

#user_idInteger

Returns ИД автора.

Returns:

  • (Integer)

    ИД автора



38
39
40
# File 'lib/torg_api/api/contractor.rb', line 38

def user_id
  @user_id
end

Class Method Details

.create_from_b2b(hash) ⇒ Object

Создаёт контрагента

Parameters:

  • hash (Hash)

    хэш ответа веб-сервиса B2B

  • user (Integer)

    id автора

Returns:

  • Contractor



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/torg_api/api/contractor.rb', line 77

def create_from_b2b(hash)
  c = Contractor.new
  c.name = hash[:org_name_short].gsub('"','')
  c.fullname = hash[:org_name]
  c.inn = hash[:bank_inn]
  c.kpp = hash[:bank_kpp]
  c.ogrn = hash[:ogrn]
  c.okpo = hash[:code_okpo]
  c.ownership = extract_ownership(hash[:org_name_short])
  c.status = 0
  c.form = extract_form(hash[:bank_inn])
  c.legal_addr = hash[:jury_address]
  c.user_id = Settings.service_user[:id]
  c.is_resident = extract_resident(hash[:country])
  c.is_dzo = nil
  c.is_sme = hash[:is_smb]
  c.jsc_form_id = nil
  c.sme_type_id = nil
  c.oktmo = 0

  responce = JSON.parse(
    torg_resource["contractors"].post(
      contractor: c.to_h,
      accept: :json,
      content_type: :json,
      format: :json
    ),
    symbolize_names: true
  )
  c.id = responce[:id]
  c
end

.extract_form(bank_inn) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/torg_api/api/contractor.rb', line 114

def extract_form(bank_inn)
  case bank_inn.size
  when 12 then "businessman"
  when 10 then "company"
  else "foreign"
  end
end

.extract_ownership(org_name_short) ⇒ Object



110
111
112
# File 'lib/torg_api/api/contractor.rb', line 110

def extract_ownership(org_name_short)
  org_name_short[/^([\w\-]+)/]
end

.extract_resident(country) ⇒ Object



122
123
124
# File 'lib/torg_api/api/contractor.rb', line 122

def extract_resident(country)
  country == 643
end

.find_by_inn(inn, kpp = nil) ⇒ Integer[]

Поиск id контрагентов по ИНН

Parameters:

  • inn (String)

    ИНН

Returns:

  • (Integer[])

    Массив id найденных контрагентов отсортированных по статусу и дате изменения



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/torg_api/api/contractor.rb', line 58

def find_by_inn(inn, kpp = nil)
  responce = JSON.parse(
    torg_resource["contractors/find_by_inn"].get(
      params: { inn: inn, kpp: kpp },
      accept: :json,
      content_type: :json,
      format: :json
    ),
    symbolize_names: true,
    quirks_mode: true
  )

  responce && new(responce)
end