class PPayment < ApplicationRecord
  belongs_to :p_payment_type
  belongs_to :p_customer
  validates :p_customer_id, :presence => true
  validates :amount, :presence => true
  validates :paid_at, :presence => true, :if => :paid_date_needed?
  #validates :theo_date, :presence => true, :if => :theo_date_needed?
  
  belongs_to :p_remise
  
  belongs_to :p_sheet_line
  belongs_to :p_ship_tour_truck_sheet_line
  
  
  has_many :p_payment_documents, :dependent => :destroy
  accepts_nested_attributes_for :p_payment_documents, allow_destroy: true
  
  has_one :p_compta_element, :as => :element, :dependent => :destroy
  
  
  after_save do
    self.generate_p_compta_element
  end
  
  def generate_p_compta_element
    
    if self.p_compta_element
      p_compta_element = self.p_compta_element
    else
      p_compta_element = PComptaElement.new(:p_customer => self.p_customer, :element => self)
    end
    
    p_compta_element.amount = (self.amount)
    if self.paid_at?
      p_compta_element.date = self.paid_at
    elsif self.theo_paid_at?
      p_compta_element.date = self.theo_paid_at
    else
      p_compta_element.date = self.created_at
    end
    puts p_compta_element.save

  end
  
  
  after_initialize do
    self.p_payment_type_id = 41 if !self.p_payment_type_id?
    self.paid = true if !self.id
  end
  
  
  def auto_affect(p_document)

    
    totals =  PSheetLine.totals(p_document.p_sheet_lines)
    
    total = totals[:ok_total_ttc]
    
    if self.reste_to_affect > 0.0 and p_document.total_du_th > 0.0
      
      
      
      if self.reste_to_affect > p_document.total_du_th
        total_to_affect_now = p_document.total_du_th
      else
        total_to_affect_now = self.reste_to_affect
      end
      
      total_to_affect_now = total_to_affect_now.to_f
      
      self.p_payment_documents.create(:p_document => p_document, :amount => total_to_affect_now)
      
      
    end
    
    
    
  end
  
  before_save do
    self.paid = true
    if self.p_remise
      self.remise = true
    else
      self.remise = false
    end
    true
  end
  before_validation do
    if self.p_payment_type and self.amount
      if self.p_payment_type.signe == 1
        if self.amount < 0
          self.amount = self.amount * -1
          
        end
        
      elsif self.p_payment_type.signe == 2
        if self.amount > 0
          self.amount = self.amount * -1
          
        end
        
      end
      
    end
    
    
    
    if self.p_remise
      self.remise = true
    else
      self.remise = false
    end
    
    if self.p_ship_tour_truck_sheet_line
      
      
      self.p_sheet_line = p_ship_tour_truck_sheet_line.p_sheet_line
      
      if self.p_sheet_line
        self.p_customer = self.p_sheet_line.p_customer
      end
      
      #self.amount =123
      
    end
    
    if self.reste_to_affect.to_f != 0.0
      
      self.affected = false
    else
      self.affected = true
    end
    
    
    
    if self.amount and (((self.amount < 0 and self.total_affected < self.amount.to_f) or (self.amount > 0 and self.total_affected > self.amount.to_f)) and !self.imported)
      errors.add(:amount, "Ne doit pas être inférieur aux affectations (manque #{(self.total_affected - self.amount.to_f)}€)") 
    end
    true
    
  
  end
  
  after_save do 
    self.p_payment_documents.each do |p_payment_document|
      p_payment_document.save
    end
    
    self.p_customer.update_caches if self.p_customer
    true
  end
  
  def total_affected 
    r = 0.0
    self.p_payment_documents.each do |ppd|
      r += ppd.amount.to_f if !ppd.marked_for_destruction?
    end
    return r
  end
  
  def paid_date_needed?
    true if self.paid and !self.imported
  end
  
  def theo_date_needed?
    true if self.p_payment_type and !self.p_payment_type.comptant and !self.imported
  end
  
  def reste_to_affect
    (self.amount.to_f - self.total_affected.to_f).round(2)
  end
  
end
