negos_app/app/models/product_order_product.rb
Nicolas Bally c5207c343c Suite
2018-08-09 15:10:38 +02:00

140 lines
2.7 KiB
Ruby
Executable File

class ProductOrderProduct < ActiveRecord::Base
belongs_to :order
belongs_to :product_order
belongs_to :product
belongs_to :product_size
belongs_to :product_option
belongs_to :archived_product, class_name: "Product"
belongs_to :archived_product_size, class_name: "ProductSize"
belongs_to :archived_product_option, class_name: "ProductOption"
has_many :sub_products
before_save do
self.price_ht = self.dyn_price_ht
end
def sage_reference
if self.product
self.product.sage_reference
end
end
def tva
20.0
end
def tva_archived
self.tva
end
def price_ttc_with_qty
qty.to_f * product.price_ttc.to_f
end
def final_price_ttc_with_qty
qty.to_f * product.final_price_ttc.to_f
end
def final_price_ht_with_qty
qty.to_f * dyn_price_ht.to_f
end
def dyn_price_ht
if self.product_stock
if product_degressif = self.product_stock.product_degressifs.where("nbr <= ?", self.qty).order("nbr DESC").first
price = product_degressif.price
end
end
if !price
price = self.product.final_price_ht.to_f
end
return price
end
def dyn_price_ttc
dyn_price_ht * self.tva
end
def archived_price_ttc_with_qty
if self.price_ht?
qty.to_f * self.price_ht*self.tva_archived
else
if archived_product
qty.to_f * archived_product.price_ttc.to_f
else
0
end
end
end
def archived_final_price_ttc_with_qty
if self.price_ht?
qty.to_f * self.price_ht*self.tva_archived
else
if archived_product
qty.to_f * archived_product.final_price_ttc.to_f
else
0
end
end
end
def archived_final_price_ht_with_qty
if self.price_ht?
qty.to_f * self.price_ht
else
if archived_product
qty.to_f * archived_product.final_price_ht.to_f
else
0
end
end
end
def verify_qty
product_stock_t = product_stock = self.product_stock
if product_stock_t and self.qty > product_stock_t.stock_th
self.qty = product_stock_t.stock_th
elsif !product_stock_t
self.qty = 0
end
end
def update_stock_th
product_stock_t = product_stock = self.product_stock
if product_stock_t
product_stock_t.stock_th = product_stock_t.stock_th - self.qty
product_stock_t.save
end
end
def update_stock
product_stock = self.product_stock
if product_stock
product_stock.stock = product_stock.stock - self.qty
product_stock.save
end
end
def product_stock
self.product.product_stocks.where(:product_option_id => self.product_option_id,:product_size_id => self.product_size_id).first
end
end