Last active
October 4, 2019 15:26
-
-
Save c80609a/cb663e804162301ae51115bb1233385b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Entities | |
module Payments | |
module Payers | |
class Juridical < AbstractPayer | |
# include Entity | |
attribute :name, type: String | |
attribute :juridical_address, type: String | |
attribute :actual_address, type: String | |
attribute :inn, type: String | |
attribute :kpp, type: String | |
attribute :ogrn, type: String | |
def type_id | |
Dictionaries::PaymentsPayersType::JURIDICAL | |
end | |
end | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Repositories | |
module ActiveRecord | |
module Payments | |
module Payers | |
class Juridical < AbstractPayer | |
def self.entity | |
Entities::Payments::Payers::Juridical | |
end | |
# модель "Плательщики" (data access object) | |
def self.dao | |
::Payments::Payers::Juridical | |
end | |
# модель "Информация о Юр. лице" (data access object) | |
def self.dao_details | |
::Payments::Payers::Related::JuridicalPayer | |
end | |
# @param [::Payments::Payers::Juridical] record | |
# @return [Entities::Payments::Payers::Juridical] | |
def self.wrap(record) | |
detail = dao_details.find_by(payer_id: record.id) | |
entity.instantiate({ | |
id: record.id, | |
agreement_number: record.agreement_number, | |
created_at: record.created_at, | |
name: detail.name, | |
juridical_address: detail.juridical_address, | |
actual_address: detail.actual_address, | |
inn: detail.inn, | |
kpp: detail.kpp, | |
ogrn: detail.ogrn | |
}) | |
end | |
private | |
# @param [Entities::Payments::Payers::Juridical] juridical Сущность "Юридическое Лицо" | |
def self.create_or_update!(juridical, is_new_record) | |
return true if juridical.changes.blank? | |
# сохранимся в таблице "Плательщики" | |
record = is_new_record ? dao.new : dao.find_by(id: juridical.id) | |
record.agreement_number = juridical.agreement_number | |
# сохранимся в таблице "Информация о Юр. лице" | |
record_detail = is_new_record ? dao_details.new : record.juridical_payer | |
record_detail.name = juridical.name | |
record_detail.juridical_address = juridical.juridical_address | |
record_detail.actual_address = juridical.actual_address | |
record_detail.inn = juridical.inn | |
record_detail.kpp = juridical.kpp | |
record_detail.ogrn = juridical.ogrn | |
# если Юр.Лицо новое - свяжем записи | |
record_detail.juridical = record if is_new_record | |
# запишем в базу | |
record_detail.save! | |
record.save! | |
# После сохранения записи в базе, берём её id и сразу же отдаём сущности (актуально для НОВЫХ записей) | |
juridical.id = record.id | |
juridical.clear_changes! | |
true | |
end | |
end # class | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment