qi6_app/app/models/p_ship_tour.rb
2019-05-17 12:30:45 +02:00

208 lines
5.1 KiB
Ruby

class PShipTour < ApplicationRecord
has_many :p_ship_tour_trucks, :dependent => :destroy
has_many :p_documents, :through => :p_ship_tour_trucks
has_many :p_sheet_lines, :through => :p_ship_tour_trucks
has_many :p_customer_sheets, :through => :p_sheet_lines
has_many :all_p_documents, :through => :p_customer_sheets, :source => :p_documents
accepts_nested_attributes_for :p_ship_tour_trucks, allow_destroy: true
validates_associated :p_ship_tour_trucks
has_many :p_volucompteurs, :through => :p_ship_tour_trucks
before_validation do
# errors.add(:created_at, 'Bloc')
end
def generate_bills
self.p_customer_sheets.where(:state => "livrée").uniq.each do |p_customer_sheet|
p_customer_sheet.generate_f
end
end
#validates :code, :presence => true
def reset_validation
self.p_ship_tour_trucks.each{|child| child.valid? }
self.valid?
end
def to_ship
self.p_ship_tour_trucks.each do |p_ship_tour_truck|
p_ship_tour_truck.p_ship_tour_truck_sheet_lines.order("position ASC").each do |p_ship_tour_truck_sheet_line|
p_ship_tour_truck_sheet_line.p_sheet_line.to_ship(p_ship_tour_truck)
end
end
self.state = "en-cours"
self.save(:validate => false)
end
def to_unship
self.p_sheet_lines.each do |p_sheet_line|
p_sheet_line.lock = nil
p_sheet_line.save
p_sheet_line.ungenerate_bl
p_sheet_line.bl = false
p_sheet_line.save
if p_sheet_line.p_customer_sheet.state == "livraison-en-cours"
if p_sheet_line.p_customer_sheet.p_sheet_lines.where(:bl => true).count == 0
p_sheet_line.p_customer_sheet.state = "commande"
p_sheet_line.p_customer_sheet.save
end
end
end
self.state = nil
if self.save(:validate => false)
else
puts self.errors.messages
sfdfsdf =sffsd
end
end
def done_return
self.p_ship_tour_trucks.each do |p_ship_tour_truck|
p_ship_tour_truck.p_ship_tour_truck_sheet_lines.each do |p_ship_tour_truck_sheet_line|
p_sheet_line = p_ship_tour_truck_sheet_line.p_sheet_line
if p_ship_tour_truck_sheet_line.canceled
p_sheet_line.lock = nil
else
p_sheet_line.shiped = true
p_sheet_line.lock = true
p_sheet_line.ok = false
p_sheet_line.ok_qte = p_ship_tour_truck_sheet_line.qte_shiped
p_sheet_line.ok_at = p_ship_tour_truck_sheet_line.p_ship_tour_truck.p_ship_tour.start_at
p_sheet_line.ok_price = p_ship_tour_truck_sheet_line.price_shiped
p_sheet_line.ref = p_ship_tour_truck_sheet_line.ref
end
p_sheet_line.save
#p_sheet_line.update_stock if !p_ship_tour_truck_sheet_line.canceled
end
end
self.state = "livrée"
self.save
self.p_customer_sheets.where(:state => "livraison-en-cours").all.each do |pcs|
pcs.update_ship_status
end
end
def undone_return
self.p_ship_tour_trucks.each do |p_ship_tour_truck|
p_ship_tour_truck.p_ship_tour_truck_sheet_lines.each do |p_ship_tour_truck_sheet_line|
p_sheet_line = p_ship_tour_truck_sheet_line.p_sheet_line
p_ship_tour_truck_sheet_line.canceled = nil
p_sheet_line.lock = true
#####
p_sheet_line.shiped = false
p_sheet_line.ok = nil
p_sheet_line.ok_qte = p_ship_tour_truck_sheet_line.qte_shiped
p_sheet_line.ok_at = nil
p_sheet_line.ok_price = p_sheet_line.price
###
p_sheet_line.save
if p_sheet_line.p_customer_sheet.state == "livrée"
p_sheet_line.p_customer_sheet.state = "livraison-en-cours"
p_sheet_line.p_customer_sheet.save
end
#p_sheet_line.update_stock if !p_ship_tour_truck_sheet_line.canceled
end
p_ship_tour_truck.p_volucompteurs.each do |p_volucompteur|
p_volucompteur.end_vol = nil
p_volucompteur.save(:validate => false)
p_volucompteur.p_vol_excepts.destroy_all
end
end
self.state = "en-cours"
self.save(:validate => false)
puts self.errors.messages
end
def state_html
if self.state == "offre"
'<span class="badge badge-warning">Offre</span>'
elsif self.state == "en-cours"
'<span class="badge badge-success" style="background:#036516;">Livraison programmée</span>'
elsif self.state == "livrée"
'<span class="badge badge-info">livrée</span>'
elsif self.state =="facturée"
'<span class="badge badge-primary">Facturée</span>'
elsif self.state == "annulée"
'<span class="badge badge-dark">Annulée</span>'
elsif self.state == "refusée"
'<span class="badge badge-danger">Refusée</span>'
else
'<span class="badge badge-pill badge-warning">Brouillon</span>'
end
end
end