Class: ProductType

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
ErpTechSvcs::Utils::DefaultNestedSetMethods
Defined in:
app/models/product_type.rb

Overview

create_table :product_types do |t|

#these columns are required to support the behavior of the plugin 'better_nested_set'
#ALL products have the ability to act as packages in a nested set-type structure
#
#The package behavior is treated differently from other product_relationship behavior
#which is implemented using a standard relationship structure.
#
#This is to allow quick construction of highly nested product types.
t.column  	:parent_id,              :integer
t.column  	:lft,                    :integer
t.column  	:rgt,                    :integer

#custom columns go here
t.column  :description,              :string
t.column  :product_type_record_id,   :integer
t.column  :product_type_record_type, :string
t.column 	:external_identifier, 	   :string
t.column  :internal_identifier,      :string
t.column 	:external_id_source, 	     :string
t.column  :default_image_url,        :string
t.column  :list_view_image_id,       :integer
t.column  :length,                   :decimal
t.column  :width,                    :decimal
t.column  :height,                   :decimal
t.column  :weight,                   :decimal
t.column  :cylindrical,              :boolean
t.column  :taxable                   :boolean
t.column  :available_on_web          :boolean
t.references :unit_of_measurement
t.references :biz_txn_acct_root

t.timestamps

end

add_index :product_types, :biz_txn_acct_root_id

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.apply_filters(filters, statement = nil) ⇒ ActiveRecord::Relation

Filter records

Parameters:

  • filters (Hash)

    a hash of filters to be applied,

  • statement (ActiveRecord::Relation) (defaults to: nil)

    the query being built

Returns:

  • (ActiveRecord::Relation)

    the query being built



67
68
69
70
71
72
73
# File 'app/models/product_type.rb', line 67

def apply_filters(filters, statement=nil)
  unless statement
    statement = ProductType
  end

  statement
end

.count_by_status(product_type, prod_availability_status_type) ⇒ Object



140
141
142
# File 'app/models/product_type.rb', line 140

def self.count_by_status(product_type, prod_availability_status_type)
  ProductInstance.count("product_type_id = #{product_type.id} and prod_availability_status_type_id = #{prod_availability_status_type.id}")
end

.scope_by_dba_organization(dba_organization) ⇒ ActiveRecord::Relation

scope by dba organization

Parameters:

  • dba_organization (Party)

    dba organization to scope by

Returns:

  • (ActiveRecord::Relation)


84
85
86
# File 'app/models/product_type.rb', line 84

def scope_by_dba_organization(dba_organization)
  scope_by_party(dba_organization, {role_types: [RoleType.iid('dba_org')]})
end

.scope_by_party(party, options = {}) ⇒ ActiveRecord::Relation

scope by party

or an array of Party ids

Parameters:

  • party (Integer | Party | Array)

    either a id of Party record, a Party record, an array of Party records

  • options (Hash) (defaults to: {})

    options to apply to this scope

Options Hash (options):

  • :role_types (Array)

    role types to include in the scope

Returns:

  • (ActiveRecord::Relation)


96
97
98
99
100
101
102
103
104
# File 'app/models/product_type.rb', line 96

def scope_by_party(party, options={})
  statement = joins(:product_type_pty_roles).where("product_type_pty_roles.party_id" => party).uniq

  if options[:role_types]
    statement = statement.where("product_type_pty_roles.role_type_id" => RoleType.find_child_role_types(options[:role_types]))
  end

  statement
end

Instance Method Details

#add_party_with_role(party, role_type) ⇒ ProductTypePtyRole

add party with passed role to this ProductType

Parameters:

  • party (Party)

    party to add

  • role_type (RoleType)

    role type to use in the association

Returns:



112
113
114
115
116
117
118
# File 'app/models/product_type.rb', line 112

def add_party_with_role(party, role_type)
  ProductTypePtyRole.create(
      product_type: self,
      party: party,
      role_type: role_type
  )
end

#add_party_with_role_type(party, role_type) ⇒ Object



188
189
190
191
192
193
194
# File 'app/models/product_type.rb', line 188

def add_party_with_role_type(party, role_type)
  if role_type.is_a?(String)
    role_type = RoleType.iid(role_type)
  end

  ProductTypePtyRole.create(party: party, role_type: role_type, product_type: self)
end

#has_dimensions?Boolean

Returns:

  • (Boolean)


196
197
198
# File 'app/models/product_type.rb', line 196

def has_dimensions?
  (cylindrical && length && width && weight) or (length && width && height && weight)
end

#images_pathObject



144
145
146
147
# File 'app/models/product_type.rb', line 144

def images_path
  file_support = ErpTechSvcs::FileSupport::Base.new(:storage => Rails.application.config.erp_tech_svcs.file_storage)
  File.join(file_support.root, Rails.application.config.erp_tech_svcs.file_assets_location, 'products', 'images', "#{self.description.underscore}_#{self.id}")
end

#parent_dba_organizations(dba_orgs = []) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
# File 'app/models/product_type.rb', line 176

def parent_dba_organizations(dba_orgs=[])
  ProductTypePtyRole.
      where('product_type_id = ?', id).
      where('role_type_id' => RoleType.iid('dba_org').id).each do |prod_party_reln|

    dba_orgs.push(prod_party_reln.party)
    prod_party_reln.party.parent_dba_organizations(dba_orgs)
  end

  dba_orgs.uniq
end

#prod_type_relns_fromObject



128
129
130
# File 'app/models/product_type.rb', line 128

def prod_type_relns_from
  ProdTypeReln.where('prod_type_id_from = ?', id)
end

#prod_type_relns_toObject



124
125
126
# File 'app/models/product_type.rb', line 124

def prod_type_relns_to
  ProdTypeReln.where('prod_type_id_to = ?', id)
end

#taxable?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'app/models/product_type.rb', line 120

def taxable?
  self.taxable
end

#to_data_hashObject



149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/product_type.rb', line 149

def to_data_hash
  to_hash(only: [
              :id,
              :description,
              :internal_identifier,
              :sku,
              :comment,
              :created_at,
              :updated_at
          ],
          unit_of_measurement: try(:unit_of_measurement).try(:to_data_hash),
          price: try(:get_current_simple_plan).try(:money_amount),
          gl_account: try(:gl_account).try(:to_data_hash))
end

#to_display_hashObject



164
165
166
167
168
169
170
171
172
173
174
# File 'app/models/product_type.rb', line 164

def to_display_hash
  {
      id: id,
      description: description,
      offer_list_description: find_descriptions_by_view_type('list_description').first.try(:description),
      offer_short_description: find_descriptions_by_view_type('short_description').first.try(:description),
      offer_long_description: find_descriptions_by_view_type('long_description').first.try(:description),
      offer_base_price: get_current_simple_amount_with_currency,
      images: images.pluck(:id)
  }
end

#to_labelObject



132
133
134
# File 'app/models/product_type.rb', line 132

def to_label
  "#{description}"
end

#to_sObject



136
137
138
# File 'app/models/product_type.rb', line 136

def to_s
  "#{description}"
end