olwen_demo_app/app/models/p_product.rb
Nicolas Bally 7b2e6811dd initial
2020-06-05 11:10:02 +02:00

141 lines
3.7 KiB
Ruby

class PProduct < ApplicationRecord
has_many :p_product_p_normes, :dependent => :destroy
has_many :p_product_specs, :through => :p_product_p_normes
has_many :p_product_refs, :dependent => :destroy
accepts_nested_attributes_for :p_product_refs, allow_destroy: true
belongs_to :p_product_cat
belongs_to :s_brand
validates :name, :presence => true
validates :code, :presence => true, :uniqueness => true
has_many :tvable_tva_rates, :as => :tvable
has_many :tva_rates, :through => :tvable_tva_rates
has_many :p_product_p_product_sub_cats, :dependent => :destroy
has_many :p_product_sub_cats, :through => :p_product_p_product_sub_cats
has_many :p_product_p_customer_cats, :dependent => :destroy
has_many :p_customer_cats, :through => :p_product_p_customer_cats
def self.qi_table_order
{
:code => {:name => "Code", :reorder => true},
:name => {:name => "Nom", :reorder => true},
:enabled => {:name => "Actif ?", :reorder => true, :format => :boolean},
:p_customer_cats => {:name => "Cat. client", :reorder => false},
:p_product_cat => {:name => "Catégorie", :reorder => false},
:s_brand => {:name => "Marque", :reorder => true, :sort_name => "s_brands.name"},
:uv => {:name => "Format", :reorder => true, :sort_name => "p_product_refs.uv"},
:capacite => {:name => "PCB", :reorder => true, :sort_name => "p_product_refs.capacite"},
:price_calc => {:name => "Calc. prix", :reorder => true},
:ct_price_ht => {:name => "Prix HT", :reorder => true, :sort_name => "p_product_refs.ct_price_ht"},
:price_calc => {:name => "Prix HT [Kg]", :reorder => false},
:tva_rate => {:name => "TVA Fr", :reorder => false},
:weight => {:name => "Poids", :reorder => true, :sort_name => "p_product_refs.weight"},
:country => {:name => "Pays", :reorder => true},
:sub_country => {:name => "Region", :reorder => true},
:domaine => {:name => "Domaine", :reorder => true},
:actions => {:name => "Actions", :reorder => true}
#:created_at => {:name => "Date", :reorder => true},
}
#, :sort_name => "code"
end
# "p_product_refs.ct_price_ht","s_brands.name", "p_product_refs.uv","p_product_refs.capacite"]
def self.valid_sort
r = []
self.qi_table_order.each do |key, value|
if value.instance_of? Hash
if value[:reorder] == false
elsif value[:reorder] == true
if value[:sort_name]
r << value[:sort_name]
else
r << key.to_s
end
end
end
end
return r
end
def self.for_search(search)
PProduct.where("name LIKE ? or code LIKE ?", "%#{search}%", "%#{search}%").where(:enabled => true)
end
def self.convert_price_kg
PProduct.where(:price_calc => "Kg").all.each do |p_product|
weight = nil
p_product.p_product_refs.each do |p_product_ref|
if p_product_ref.weight.to_f != 0.0
weight = p_product_ref.weight
p_product_ref.ct_price_ht = weight * p_product_ref.ct_price_ht
p_product_ref.save
end
end
if weight
p_product.ct_purchase_price_ht = p_product.ct_purchase_price_ht*weight
end
p_product.save
end
end
after_create do
end
after_save do
self.p_product_refs.each do |ppr|
ppr.save
end
end
def tva_rate(accounting_zone_id)
if r = self.tva_rates.where(:accounting_zone_id => accounting_zone_id).first
elsif self.p_product_cat and r = self.p_product_cat.tva_rates.where(:accounting_zone_id => accounting_zone_id).first
else
r = nil
end
return r
end
end