class ProductOrder < ActiveRecord::Base
  has_many :product_order_products

  has_many :product_order_payments
  belongs_to :product_order_payment_ok, :class_name => "ProductOrderPayment"

  belongs_to :particular_bill, :class_name => "Particular"

  accepts_nested_attributes_for :particular_bill

  belongs_to :particular_send, :class_name => "Particular"

  accepts_nested_attributes_for :particular_send

  belongs_to :product_customer

  accepts_nested_attributes_for :product_customer
  
  belongs_to :product_fdp
  belongs_to :product_fdp_archived, :class_name => "ProductFdp"
  
  belongs_to :vourcher_code
  belongs_to :vourcher_code_archived, :class_name => "VourcherCode"
  
  validates :product_fdp, :presence => true, :if => :force_fdp_validation?
  
  validates :cgv, :presence => {:message => "Vous devez accepter les conditions générales de vente."}, :if => :force_cgv_validation?
  
  attr_accessor :force_fdp_validation, :force_payment, :force_cgv_validation
  
  def total_ttc
    total_articles_ttc + total_fdp - vourcher_reduction_ttc
  end
  
  def total_ttc_without_fdp
    total_articles_ttc - vourcher_reduction_ttc
  end
  
  def total_ht_without_fdp
    total_articles_ht - vourcher_reduction_ht
  end
  
  def vourcher_reduction_ttc
    if self.vourcher_code and 1==1
      (total_articles_ttc * (self.vourcher_code.percent / 100 )).round(2)
      
    else
      0.0
    end
  end
  
  def vourcher_reduction_ht
    if self.vourcher_code and 1==1
      (total_articles_ht * (self.vourcher_code.percent / 100 )).round(2)
      
    else
      0.0
    end
  end
  
  
  def archived_vourcher_reduction_ttc
    if self.vourcher_code_archived and 1==1
      (total_articles_ttc * (self.vourcher_code_archived.percent / 100 )).round(2)
      
    else
      0.0
    end
  end
  
  
  
  def archived_total_ttc
    archived_total_articles_ttc + archived_total_fdp - archived_vourcher_reduction_ttc
  end
  
  
  def archived_total_articles_ttc
    archived_total = 0.0
    product_order_products.each do |p|
      archived_total += p.archived_final_price_ttc_with_qty
    end
    archived_total
  end
  
  def archived_total_fdp(fdp=self.product_fdp_archived)
    if fdp
      if self.archived_total_articles_ttc < fdp.price_max
        fdp.price
      else
        0.0
      end
    else
      0.0
    end
    
  end
  
  
  
  def after_paid
    self.product_order_products.each do |p|
    
      p.update_stock_th
      
    end
  end
  
  def archive_now
		self.product_order_products.each do |p|
      
      # Archive the product
      p.archived_product = p.product.dup
      p.archived_product.archived = true
      p.verify_qty
      p.save

      # Archive product langs

      p.product.product_langs.each do |pl|
        dup_pl = pl.dup
        dup_pl.archived = true
        p.archived_product.product_langs << dup_pl
        p.save
      end

      # Archive the pop size
      
      if p.product_size
        dup_product_size = p.product_size.dup
        dup_product_size.archived = true
        p.archived_product_size  = dup_product_size 
      end
      
      if p.product_option# Archive the pop option
      
        dup_product_option = p.product_option.dup
        dup_product_option.product_id = nil
        dup_product_option.archived = true
        p.archived_product_option  = dup_product_option 

        p.product_option.product_option_langs.each do |pl|
          p.archived_product_option.product_option_langs << pl.dup
          p.save
        end
		  end
      
      

      # save
      p.save


    end
    
    if self.product_fdp
      a_product_fdp = self.product_fdp.dup
      a_product_fdp.archived = true
      a_product_fdp.save
      self.product_fdp_archived_id = a_product_fdp.id
      self.save
      
    end
    
    if self.vourcher_code
      a_vourcher_code = self.vourcher_code.dup
      a_vourcher_code.archived = true
      a_vourcher_code.save
      self.vourcher_code_archived_id = a_vourcher_code.id
      self.save
      
    end
    
  end
  
  def total_articles_ttc
    total = 0.0
    product_order_products.each do |p|
      total += p.final_price_ttc_with_qty
    end
    total
  end
  
  
  def total_articles_ht
    total = 0.0
    product_order_products.each do |p|
      total += p.final_price_ht_with_qty
    end
    total
  end
  
  def total_fdp(fdp=self.product_fdp)
    if fdp
      if self.total_ttc_without_fdp < fdp.price_max
        fdp.price
      else
        0.0
      end
    else
      0.0
    end
    
  end
  
  def force_fdp_validation?
    true if self.force_fdp_validation
  end
  
  def force_cgv_validation?
    true if self.force_cgv_validation
  end
  
  before_validation do
     self.verify
       
  end
  
  
  def commission_mlm_now
    if self.product_customer.parent
      i = 0
      parent_to_commission = self.product_customer.parent
      
      while parent_to_commission and i <= 12
        i += 1
        
        if parent_to_commission.mlm_nbr_niveaux >= i
          com_percent = (parent_to_commission.callif_now[:percents][i].to_f/100)
          com = (com_percent*self.total_ht_without_fdp.to_f()).round(2)
          puts "Commission à #{parent_to_commission.pseudo}: génération #{i}, #{com_percent}%, #{com}"
          
          parent_to_commission.commissions.create(:product_order => self, :commission_type_id => 10, :amount => com, :percent => com_percent, :generation => i, :devise_id => 1)
          
        end
        
        parent_to_commission = parent_to_commission.parent
        
        
      end
      
      
    end
  end
  
  
  
  protected
  
  	def verify(size=16)
  		if !self.token
  			s = ""
  			size.times { s << (i = Kernel.rand(62); i += ((i < 10) ? 48 : ((i < 36) ? 55 : 61 ))).chr }
  			self.token = s
	    end
	  end
  
  
  
end
