From e56c6bb37dae798bdc6d0a5e3df6d6de0bd66ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Barnab=C3=A9?= Date: Mon, 11 Oct 2021 19:13:34 +0200 Subject: [PATCH] update price_line after create p_product + destroy callbacks --- app/controllers/admin/p_products_controller.rb | 3 +++ app/models/p_product.rb | 8 +++++++- app/models/p_product_ref.rb | 13 +++++++++++++ .../import_csvs/_form_temp_p_product.html.haml | 2 +- app/views/admin/p_product_refs/_form.html.haml | 4 +++- 5 files changed, 27 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/p_products_controller.rb b/app/controllers/admin/p_products_controller.rb index aef9a1a..871bd95 100644 --- a/app/controllers/admin/p_products_controller.rb +++ b/app/controllers/admin/p_products_controller.rb @@ -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 diff --git a/app/models/p_product.rb b/app/models/p_product.rb index ef6cdc6..4e501c0 100644 --- a/app/models/p_product.rb +++ b/app/models/p_product.rb @@ -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 diff --git a/app/models/p_product_ref.rb b/app/models/p_product_ref.rb index d11adc1..3203fc1 100644 --- a/app/models/p_product_ref.rb +++ b/app/models/p_product_ref.rb @@ -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 diff --git a/app/views/admin/import_csvs/_form_temp_p_product.html.haml b/app/views/admin/import_csvs/_form_temp_p_product.html.haml index afa6595..9173a3d 100644 --- a/app/views/admin/import_csvs/_form_temp_p_product.html.haml +++ b/app/views/admin/import_csvs/_form_temp_p_product.html.haml @@ -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} diff --git a/app/views/admin/p_product_refs/_form.html.haml b/app/views/admin/p_product_refs/_form.html.haml index 8b4149f..f89bf35 100644 --- a/app/views/admin/p_product_refs/_form.html.haml +++ b/app/views/admin/p_product_refs/_form.html.haml @@ -11,7 +11,9 @@ %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 :"