Class: Backend::ProductNatureVariantsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/product_nature_variants_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Class Method Details

.variants_conditionsObject

params:

:q Text search
:working_set
:nature_id
:category_id

32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/backend/product_nature_variants_controller.rb', line 32

def self.variants_conditions
  code = search_conditions(product_nature_variants: [:name, :number]) + " ||= []\n"
  code << "unless params[:working_set].blank?\n"
  code << "  item = Nomen::WorkingSet.find(params[:working_set])\n"
  code << "  c[0] << \" AND product_nature_variants.nature_id IN (SELECT id FROM product_natures WHERE \#{WorkingSet.to_sql(item.expression)})\"\n"
  code << "end\n"
  code << "if params[:nature_id].to_i > 0\n"
  code << "  c[0] << \" AND product_nature_variants.nature_id = ?\"\n"
  code << "  c << params[:nature_id].to_i\n"
  code << "end\n"
  code << "if params[:category_id].to_i > 0\n"
  code << "  c[0] << \" AND product_nature_variants.category_id = ?\"\n"
  code << "  c << params[:category_id].to_i\n"
  code << "end\n"
  code << "c\n"
  code.c
end

Instance Method Details

#detailObject


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'app/controllers/backend/product_nature_variants_controller.rb', line 103

def detail
  return unless @product_nature_variant = find_and_check
  product_nature = @product_nature_variant.nature
  infos = {
    name: @product_nature_variant.name,
    number: @product_nature_variant.number,
    depreciable: @product_nature_variant.depreciable?,
    unit: {
      name: @product_nature_variant.unit_name
    }
  }
  if product_nature.subscribing?
    entity = nil
    address = nil
    if params[:sale_address_id] || params[:purchase_address_id]
      address = EntityAddress.mails.find_by(id: params[:sale_address_id] || params[:purchase_address_id])
    end
    if params[:sale_client_id] || params[:purchase_supplier_id]
      entity = Entity.find_by(id: params[:sale_client_id] || params[:purchase_supplier_id])
    end
    entity ||= address.entity if address
    started_on = Time.zone.today
    subscription_nature = product_nature.subscription_nature
    if entity
      last_subscription = entity.last_subscription(subscription_nature)
      started_on = last_subscription.stopped_on + 1 if last_subscription
    end
    address ||= entity.default_mail_address if entity
    stopped_on = product_nature.subscription_stopped_on(started_on)
    infos[:subscription] = {
      nature_name: subscription_nature.name,
      started_on: started_on,
      stopped_on: stopped_on
    }
    infos[:subscription][:address_id] = address.id if address
  end
  if @product_nature_variant.picture.file?
    infos[:picture] = @product_nature_variant.picture.url(:thumb)
  end
  if pictogram = @product_nature_variant.category.pictogram
    infos[:pictogram] = pictogram
  end
  catalog = nil
  if params[:catalog_id]
    catalog = Catalog.find(params[:catalog_id])
  elsif params[:sale_nature_id]
    catalog = SaleNature.find(params[:sale_nature_id]).catalog
  end
  if catalog && item = catalog.items.find_by(variant_id: @product_nature_variant.id)
    infos[:all_taxes_included] = item.all_taxes_included
    unless infos[:tax_id] = (item.reference_tax ? item.reference_tax.id : nil)
      infos[:tax_id] = if (items = SaleItem.where(variant_id: @product_nature_variant.id)) && items.any?
                         items.order(id: :desc).first.tax_id
                       else
                         Tax.first.id
                       end
    end
    if tax = Tax.find_by(id: infos[:tax_id])
      if item.all_taxes_included
        infos[:unit][:pretax_amount] = tax.pretax_amount_of(item.amount)
        infos[:unit][:amount] = item.amount
      else
        infos[:unit][:pretax_amount] = item.amount
        infos[:unit][:amount] = tax.amount_of(item.amount)
      end
    end
  elsif params[:mode] == 'last_purchase_item'
    if (items = PurchaseItem.where(variant_id: @product_nature_variant.id)) && items.any?
      item = items.order(id: :desc).first
      infos[:tax_id] = item.tax_id
      infos[:unit][:pretax_amount] = item.unit_pretax_amount
      infos[:unit][:amount] = item.unit_amount
    end
  elsif params[:mode] == 'last_sale_item'
    if (items = SaleItem.where(variant_id: @product_nature_variant.id)) && items.any?
      item = items.order(id: :desc).first
      infos[:tax_id] = item.tax_id
      infos[:unit][:pretax_amount] = item.unit_pretax_amount
      infos[:unit][:amount] = item.unit_amount
    end
  end
  render json: infos
end

#quantifiersObject

Returns quantifiers for a given variant


99
100
101
# File 'app/controllers/backend/product_nature_variants_controller.rb', line 99

def quantifiers
  return unless @product_nature_variant = find_and_check
end