class SaleAudit < ApplicationRecord
  self.primary_key = "sale_audit_id"

  #primary key - need to be unique generated for SaleAudit
  before_create :generate_custom_id

  belongs_to :sale

  def self.record_audit_void(sale_id, void_by, approved_by, reason)
    #sale_audit
    sale_audit             = SaleAudit.new()
    sale_audit.sale_id     = sale_id
    sale_audit.action      = "SALEVOID"
    sale_audit.action_at   = DateTime.now.utc
    sale_audit.action_by   = void_by
    sale_audit.approved_by = approved_by
    sale_audit.remark      = reason
    sale_audit.save!
    #sale_audit.
  end

  def self.record_audit_complete(sale_id, remark, action_by)
    sale_audit             = SaleAudit.new()
    sale_audit.sale_id     = sale_id
    sale_audit.action      = "SALECOMPLETE"
    sale_audit.action_at   = DateTime.now.utc
    sale_audit.action_by   = action_by
    sale_audit.remark      = remark
    sale_audit.approved_by = Time.now
    sale_audit.save!
  end

  def record_audit_discount(sale_id, discount_by, approved_by, reason)
    #sale_audit
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = "SALEDISCOUNT"
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = discount_by
    sale_audit.approved_by = approved_by
    sale_audit.remark = reason
    sale_audit.save!
    #sale_audit.
  end

  def record_audit_foc(sale_id, cashier_id, approved_by, reason)
    #sale_audit
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = "SALEFOC"
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = cashier_id
    sale_audit.approved_by = approved_by
    sale_audit.remark = reason
    sale_audit.save!
  end

  def self.record_payment(sale_id, remark, action_by)
    sale_audit = SaleAudit.new()
    sale_audit.sale_id = sale_id
    sale_audit.action = "SALEPAYMENT"
    sale_audit.action_at = DateTime.now.utc
    sale_audit.action_by = action_by
    sale_audit.remark = remark
    sale_audit.approved_by = Time.now
    sale_audit.save!
  end

  private
  def generate_custom_id
    self.sale_audit_id = SeedGenerator.generate_id(self.class.name, "SAI")
  end
end
