class PaymentInvoice < ActiveRecord::Base

  include PublicActivity::Model
	belongs_to :organisation
  belongs_to :router_inventory
  belongs_to :resourceable, polymorphic: true
  belongs_to :purchase_order
  after_save :change_purchase_order_status
  # after_save :change_auto_recharge_status
  # By default payment status will be "New". 
  #when they make the payment successfully the payment status will change to "success"
 
  tracked owner: ->(controller, model) { controller && controller.tracked_current_user },params:
  { :attributes => proc {|controller, model_instance| {"payment_invoice(#{model_instance.invoice_number})" => model_instance.changes}}},organisation_id: ->(controller, model) { controller && controller.tracked_current_user.organisation_id },:location_network_id => nil

  tracked assumed_by: proc {|controller, model| controller.user_assumed_by if controller}

  def change_purchase_order_status
    if self.payment_status == "success"
    	self.purchase_order.update_attribute(:payment_status, "paid")
    else
      self.purchase_order.update_attribute(:payment_status, "Not paid")
    end 
  end

  # def change_auto_recharge_status
  #   debugger
  #   if self.payment_status == "success"
  #     debugger
  #     if self.purchase_order.auto_recharge == true
  #     debugger
  #       self.purchase_order.purchase_order_items.where(:itemable_type=>"SimManagement").map(&:itemable).to_a.first.update_attribute(:auto_recharge, true)
  #     else
  #       self.purchase_order.purchase_order_items.where(:itemable_type=>"SimManagement").map(&:itemable).to_a.first.update_attribute(:auto_recharge, false)
  #     end  
  #   else 
  #     self.purchase_order.purchase_order_items.where(:itemable_type=>"SimManagement").map(&:itemable).to_a.first.update_attribute(:auto_recharge, false)
  #   end 
  # end



end
