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

#to_h

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

#ownershipString

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

Returns:

  • (String)

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



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

def ownership
  @ownership
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, user) ⇒ Integer

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

Parameters:

  • hash (Hash)

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

  • user (Integer)

    id автора

Returns:

  • (Integer)

    id созданного объекта



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/torg_api/api/contractor.rb', line 66

def create_from_b2b(hash, user)
  c = Contractor.new
  c.name = hash[:org_name_short]
  c.fullname = hash[:org_name]
  c.ownership = extract_ownership(hash[:org_name_short])
  c.inn = hash[:bank_inn]
  c.kpp = hash[:bank_kpp]
  c.ogrn = hash[:ogrn]
  c.okpo = hash[:code_okpo]
  c.status = 0 # enum status: { orig: 0, active: 1, old: 2, inactive: 3 }
  c.form = extract_form(hash[:bank_inn])
  c.legal_addr = hash[:jury_address]
  c.user_id = user
  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

  TorgApi::Models::Contractor.create!(c.to_h).id
end

.extract_form(bank_inn) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/torg_api/api/contractor.rb', line 92

def extract_form(bank_inn)
  # enum form: { businessman: 0, company: 1, foreign: 2, person: 3 }
  case bank_inn.size
  when 12 then 0
  when 10 then 1
  else 2
  end
end

.extract_ownership(org_name_short) ⇒ Object



88
89
90
# File 'lib/torg_api/api/contractor.rb', line 88

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

.extract_resident(country) ⇒ Object



101
102
103
# File 'lib/torg_api/api/contractor.rb', line 101

def extract_resident(country)
  country == 643
end

.find_by_inn(inn) ⇒ Integer[]

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

Parameters:

  • inn (String)

    ИНН

Returns:

  • (Integer[])

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



54
55
56
57
58
59
60
# File 'lib/torg_api/api/contractor.rb', line 54

def find_by_inn(inn)
  TorgApi::Models::Contractor
    .where(inn: inn)
    .where(next_id: nil)
    .order("#{DECODE_STATUS_ORDER}, updated_at desc")
    .pluck('id')
end