Created
August 31, 2016 19:42
-
-
Save oleglukashev/50bcec0a00beb1c6d544a2cf059386ac to your computer and use it in GitHub Desktop.
fin operation concern
This file contains hidden or 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 DeliveryFinOperableConcern | |
extend ActiveSupport::Concern | |
def self.included(base) | |
base.class_eval do | |
after_create :process_after_create | |
before_update :clear_to_fin_account_id | |
after_update :process_after_update | |
after_destroy :process_after_destroy | |
def change_pair_operation | |
if delivery_pay? || delivery_advance? | |
pair_fin_operation = get_pair_fin_operation | |
if pair_fin_operation.nil? | |
create_pair_operation | |
else | |
unless has_same_fields?(pair_fin_operation) | |
update_pair_operations(pair_fin_operation) | |
end | |
end | |
else | |
remove_pair_operation | |
end | |
end | |
def update_pair_operations(pair_fin_operation) | |
pair_fin_operation.update_attributes!({ | |
amount: amount, | |
commission: commission, | |
operation_date: operation_date, | |
additional_fin_account_id: fin_account_id, | |
fin_account_id: additional_fin_account_id, | |
accepted: accepted, | |
staffing_element_id: staffing_element_id, | |
advance_request_id: advance_request_id | |
}) | |
end | |
def create_pair_operation | |
pair_fin_operation = get_pair_fin_operation | |
if pair_fin_operation.nil? | |
additional_fin_account = FinAccount.find(is_main_operation? ? additional_fin_account_id : fin_account_id) | |
pair_additional_fin_account_id = is_main_operation? ? fin_account_id : additional_fin_account_id | |
main_operation_id = id if is_main_operation? | |
pair_fin_operation = additional_fin_account.fin_operations.create!({ | |
amount: amount.abs, | |
commission: commission, | |
type: type, | |
operation_date: operation_date, | |
fin_clause_id: fin_clause_id, | |
additional_fin_account_id: pair_additional_fin_account_id, | |
person_id: person_id, | |
balance: additional_fin_account.balance.to_f, | |
accepted: accepted, | |
operation_id: main_operation_id, | |
staffing_element_id: staffing_element_id, | |
advance_request_id: advance_request_id | |
}) | |
if is_personal_operation? | |
self.update_column(:operation_id, pair_fin_operation.id) | |
end | |
end | |
end | |
def is_main_operation? | |
operation_id.nil? | |
end | |
def is_personal_operation? | |
operation_id.present? | |
end | |
private | |
def has_same_fields?(pair_fin_operation) | |
operation_date_timestamp = operation_date.present? ? operation_date.to_time.to_i : nil | |
pair_operation_date_timestamp = pair_fin_operation.operation_date.present? ? | |
pair_fin_operation.operation_date.to_time.to_i : | |
nil | |
amount.abs == pair_fin_operation.amount.abs && | |
type == pair_fin_operation.type && | |
operation_date_timestamp == pair_operation_date_timestamp && | |
accepted == pair_fin_operation.accepted && | |
additional_fin_account_id == pair_fin_operation.fin_account_id && | |
fin_account_id == pair_fin_operation.additional_fin_account_id | |
end | |
def process_after_create | |
if accepted.present? | |
fin_account.recalculate_operations_and_balance(operation_date, id) | |
end | |
create_pair_operation | |
end | |
def process_after_update | |
if changes_for_recalculate? | |
remove_opposit_operation | |
change_pair_operation | |
recalculate_operations_and_balances_after_update | |
end | |
end | |
def process_after_destroy | |
if accepted.present? | |
fin_account.recalculate_operations_and_balance(operation_date, id) | |
end | |
remove_pair_operation | |
additional_fin_account.recalculate_balance | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment