update price_line after create p_product + destroy callbacks

This commit is contained in:
Barnabé 2021-10-11 19:13:34 +02:00
parent dbc8d3bfd6
commit e56c6bb37d
5 changed files with 27 additions and 3 deletions

View File

@ -74,6 +74,9 @@ class Admin::PProductsController < ApplicationController
@p_product = PProduct.new #(:p_customer_cat_ids => [3])
# @p_product.p_product_refs << PProductRef.new
fournisseur_label = params[:fournisseur_label] if params[:fournisseur_label]
if params[:price_line_id]
@price_line = PriceLine.find(params[:price_line_id])
end
@p_product_refs = @p_product.p_product_refs.build(ct_sub_name: fournisseur_label)
@p_product_ref_specs = @p_product_refs.p_product_ref_specs.build
@p_spec_type = @p_product_ref_specs.build_p_spec_type

View File

@ -103,15 +103,21 @@ class PProduct < ApplicationRecord
after_create do
end
after_save do
self.p_product_refs.each do |ppr|
ppr.save
end
self.class.destroy_temp_p_product
end
def self.destroy_temp_p_product
temp_p_product = PProduct.find_by(name: "--> Selectioner un produit <--", code: "--> Indiquer un code produit <--")
if temp_p_product.present?
temp_p_product.destroy
end
end
def tva_rate(accounting_zone_id)
if r = self.tva_rates.where(:accounting_zone_id => accounting_zone_id).first

View File

@ -53,6 +53,19 @@ class PProductRef < ApplicationRecord
acts_as_caching :fields => [:sorecop, :deee]
attr_accessor :price_line_id
after_create do
if self.price_line_id
pl = PriceLine.find(self.price_line_id)
pl.update(p_product_ref: self)
end
end
after_save do
PProduct.destroy_temp_p_product
end
def not_imported?
if !self.p_product or !self.p_product.imported
true

View File

@ -13,7 +13,7 @@
%input.p_product_autocomplete_input.form-control{:type => "text", :style => "", tabindex: 1, autofocus: true, :class => "p_product_name"}
=form.hidden_field :p_product_id, :class => "p_product_id"
=link_to ic(:plus)+ " Ajouter produit", new_admin_p_product_path(fournisseur_label: @p_product_ref.ct_sub_name)
=link_to ic(:plus)+ " Ajouter produit", new_admin_p_product_path(fournisseur_label: @p_product_ref.ct_sub_name, price_line_id: @price_line.id)
%tr
%td{:colspan => 3}

View File

@ -11,6 +11,8 @@
%td
= form.input :ref, :label => "Réf int. :"
=#form.hidden_field :ref_id
-if @price_line
=form.hidden_field :price_line_id, value: @price_line.id
%td
= form.input :ct_sub_name, :label => "Désignation :"