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

account_lettering_states_crit, accounts_range_crit, amount_range_crit, crit_params, journal_entries_states_crit, journal_letter_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name, #session_controller?

Class Method Details

.variants_conditionsObject

params:

:q Text search
:working_set
:nature_id
:category_id

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

def self.variants_conditions
  code = search_conditions(product_nature_variants: %i[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


177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'app/controllers/backend/product_nature_variants_controller.rb', line 177

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
    },
    stock: @product_nature_variant.current_stock
  }

  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
                       elsif @product_nature_variant.category.sale_taxes.any?
                         @product_nature_variant.category.sale_taxes.first.id
                       else
                         Tax.current.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'
    # get last item with tax, pretax amount and amount
    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
    # or get tax and amount from catalog
    elsif (items = @product_nature_variant.catalog_items.of_usage(:purchase)) && items.any?
      item = items.order(id: :desc).first
      if item.all_taxes_included
        infos[:unit][:pretax_amount] = item.reference_tax.pretax_amount_of(item.amount)
        infos[:unit][:amount] = item.amount
      else
        infos[:unit][:pretax_amount] = item.amount
        infos[:unit][:amount] = item.reference_tax&.amount_of(item.amount)
      end
    # or get tax from category
    elsif @product_nature_variant.category.sale_taxes.any?
      infos[:tax_id] = @product_nature_variant.category.sale_taxes.first.id
    end
  elsif params[:mode] == 'last_sale_item'
    # get last item with tax, pretax amount and amount
    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
    # or get tax and amount from catalog
    elsif (items = @product_nature_variant.catalog_items.of_usage(:sale)) && items.any?
      item = items.order(id: :desc).first
      if item.all_taxes_included
        infos[:unit][:pretax_amount] = item.reference_tax.pretax_amount_of(item.amount)
        infos[:unit][:amount] = item.amount
      else
        infos[:unit][:pretax_amount] = item.amount
        infos[:unit][:amount] = item.reference_tax.amount_of(item.amount)
      end
    # or get tax from category
    elsif @product_nature_variant.category.purchase_taxes.any?
      infos[:tax_id] = @product_nature_variant.category.purchase_taxes.first.id
    end
  end
  render json: infos
end

#quantifiersObject

Returns quantifiers for a given variant


173
174
175
# File 'app/controllers/backend/product_nature_variants_controller.rb', line 173

def quantifiers
  return unless @product_nature_variant = find_and_check
end

#storage_detailObject


293
294
295
296
297
298
299
300
301
# File 'app/controllers/backend/product_nature_variants_controller.rb', line 293

def storage_detail
  quantity = ParcelItemStoring.where(storage_id: params[:storage_id])
                              .joins(:parcel_item)
                              .where(parcel_items: { variant_id: params[:id] })
                              .joins(parcel_item: :parcel)
                              .where(parcels: { state: 'given' })
                              .sum(:quantity)
  render json: { quantity: quantity, unit: ProductNatureVariant.find(params[:id])&.unit_name }
end