Created
August 31, 2016 19:39
-
-
Save oleglukashev/fb561b7986b2d7e33b79aebb1843ed40 to your computer and use it in GitHub Desktop.
fin operations
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
class FinOperation < ActiveRecord::Base | |
acts_as_tenant | |
include FinOperableConcern | |
MAIN_TYPES = ['CreditFinOperation', 'DebitFinOperation', 'MoveFinOperation', 'DeliveryPayFinOperation'] | |
default_scope { order 'operation_date DESC, id DESC' } | |
scope :reverse_order, -> { reorder 'operation_date ASC, id ASC' } | |
scope :accepted, -> { where(accepted: true) } | |
scope :unaccepted, -> { where(accepted: false) } | |
has_attached_file :doc | |
has_many :attachments, | |
class_name: 'Attachment::FinOperationAttachment', | |
as: :attachable, | |
dependent: :destroy | |
accepts_nested_attributes_for :attachments, | |
reject_if: :all_blank, | |
allow_destroy: true | |
belongs_to :fin_account | |
belongs_to :person_account, class_name: 'FinAccount', foreign_key: :fin_account_id | |
belongs_to :restaurant_account, class_name: 'FinAccount', foreign_key: :fin_account_id | |
belongs_to :to_fin_account, class_name: 'FinAccount' | |
belongs_to :additional_fin_account, class_name: 'FinAccount' | |
belongs_to :fin_clause | |
belongs_to :person | |
belongs_to :staffing_element | |
belongs_to :partner | |
validates_presence_of :fin_account_id, :amount, :fin_clause_id | |
validates_presence_of :operation_date, if: lambda { accepted.present? && accepted_was.present? } | |
validates_presence_of :to_fin_account_id, if: lambda { move? && operation_id.nil? } | |
validate :accounts_must_be_different, if: lambda { move? } | |
include PgSearch | |
pg_search_scope :search_by_comment_and_amount, | |
against: [:comment, :amount], | |
using: { | |
tsearch: { prefix: true } | |
} | |
before_save :process_before_save | |
def self.model_class_by_name(name) | |
case name | |
when 'CreditFinOperation' | |
CreditFinOperation | |
when 'DebitFinOperation' | |
DebitFinOperation | |
when 'MoveFinOperation' | |
MoveFinOperation | |
when 'DeliveryPayFinOperation' | |
DeliveryPayFinOperation | |
when 'DeliveryAdvanceFinOperation' | |
DeliveryAdvanceFinOperation | |
when 'AppendPayFinOperation' | |
AppendPayFinOperation | |
when 'PurchaseFinOperation' | |
PurchaseFinOperation | |
else | |
FinOperation | |
end | |
end | |
def is_main_move_operation? | |
move? && operation_id.nil? | |
end | |
def is_double_move_operation? | |
move? && operation_id.present? | |
end | |
def is_personal_delivery_pay_operation? | |
delivery_pay? && operation_id.present? | |
end | |
def delivery_pay? | |
type == 'DeliveryPayFinOperation' | |
end | |
def delivery_advance? | |
type == 'DeliveryAdvanceFinOperation' | |
end | |
def append_pay? | |
type == 'AppendPayFinOperation' | |
end | |
def purchase? | |
type == 'PurchaseFinOperation' | |
end | |
def move? | |
type == 'MoveFinOperation' | |
end | |
def credit? | |
type == 'CreditFinOperation' | |
end | |
def debit? | |
type == 'DebitFinOperation' | |
end | |
private | |
def clear_to_fin_account_id | |
self.to_fin_account_id = nil | |
end | |
def clear_additional_fin_account_id | |
self.additional_fin_account_id = nil | |
end | |
def accounts_must_be_different | |
if to_fin_account_id.present? && to_fin_account_id == fin_account_id | |
errors.add(:fin_account_id, I18n::t('.errors.messages.accounts_must_be_different')) | |
errors.add(:to_fin_account_id, I18n::t('.errors.messages.accounts_must_be_different')) | |
end | |
end | |
def process_before_save | |
recalculate_amount_before_save | |
set_default_commission_before_save | |
recalculate_operation_date_before_save | |
recalculate_operation_balance_before_save | |
set_restaurant_id | |
remove_partner_id | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment