101 lines
2.0 KiB
Ruby
101 lines
2.0 KiB
Ruby
class PProductBrutStock < ApplicationRecord
|
|
belongs_to :p_brut_product
|
|
belongs_to :p_fournisseur_order
|
|
belongs_to :p_tank
|
|
|
|
validates :p_brut_product, :presence => true
|
|
validates :p_tank_id, :presence => true, :if => :validate_tank?
|
|
|
|
has_many :p_sheet_line_stocks #, :dependent => :destroy
|
|
|
|
scope :oks, -> {where(:ok => true).order("ok_at DESC, id DESC")}
|
|
scope :not_oks, -> {where(:ok => false).order("ok_at DESC, id DESC")}
|
|
|
|
scope :between, lambda { |start, stop|
|
|
after(start).before(stop)
|
|
}
|
|
|
|
scope :after, lambda { |date|
|
|
where("(ok_at >= ?)", date )
|
|
}
|
|
scope :before, lambda { |date|
|
|
where("(ok_at <= ?)", date )
|
|
}
|
|
|
|
validates :ok_at, :presence => true, :if => :validate_ok?
|
|
validates :ok_price, :presence => true, :if => :validate_ok?
|
|
validates :ok_qte, :presence => true, :if => :validate_ok?
|
|
|
|
before_validation do
|
|
if validate_tank?
|
|
errors.add(:p_tank_id, 'doit correspondre au produit') if self.p_brut_product and self.p_tank and self.p_tank.p_brut_product and self.p_tank.p_brut_product != self.p_brut_product
|
|
|
|
else
|
|
self.p_tank = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
before_save do
|
|
#commentaire enlevé
|
|
self.qte_restant = self.ok_qte.to_f - self.qte_used_calc
|
|
end
|
|
|
|
def qte_used_calc
|
|
PSheetLineStock.where(:p_product_brut_stock_id => self.id).sum(:qte_ok)
|
|
end
|
|
|
|
after_save do
|
|
if self.ok and !self.lock
|
|
self.qte_restant = self.ok_qte
|
|
self.lock = true
|
|
self.save
|
|
end
|
|
end
|
|
|
|
def validate_ok?
|
|
if self.ok
|
|
true
|
|
else
|
|
false
|
|
end
|
|
end
|
|
|
|
def validate_tank?
|
|
if self.externe
|
|
false
|
|
else
|
|
true
|
|
end
|
|
end
|
|
|
|
def tva
|
|
0.2
|
|
end
|
|
|
|
def tot
|
|
(self.qte.to_f * self.price.to_f).round(2)
|
|
end
|
|
|
|
def tot_ttc
|
|
(self.tot * (1+self.tva).round(2))
|
|
end
|
|
|
|
def tot_ok
|
|
(self.ok_qte.to_f * self.ok_price.to_f).round(2)
|
|
end
|
|
|
|
def tot_ttc_ok
|
|
(self.tot_ok * (1+self.tva)).round(2)
|
|
end
|
|
|
|
def tva_ok
|
|
(tot_ttc_ok - tot_ok).round(2)
|
|
end
|
|
|
|
|
|
end
|