#
#= payables モデル
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.11.5   Sumiyo Yamamoto        新規登録
#-------------------------------------------------------------------------------
#++
class Payable < CommLogistics::Base::Model::ReceivablePayable
  has_many :child_details,
           :class_name => 'PayableDetail',
           :dependent => :destroy
 
  named_scope :all_search, lambda {|target_id, date| {
    :conditions => ["supplier_id = ? AND cutoff_date = ?", target_id, date.to_s]
  }}
  
  named_scope :target_charge, lambda {|target_id, date| {
    :conditions => ["supplier_id = ? AND  cutoff_date >= ?", target_id, date.to_s],
    :order => 'cutoff_date DESC'
  }}

  def self.pre_search(target_id = nil, date = nil)
    result =  find(:first,
                   :conditions => ["supplier_id = ? AND cutoff_date < ?", target_id, date],
                   :order => "cutoff_date DESC")
    return result
  end

  def self.next_search(target_id = nil, date = nil)
   result = find(:first,
                 :conditions => ["supplier_id = ? AND cutoff_date > ?", target_id, date],
                 :order => "cutoff_date ASC")
    return result
  end
  
  def self.group_total(group_id, cutoff_date, supplier_id=nil)
    sql = "SELECT IFNULL(SUM(total_price + total_duty + total_carry + total_carry_duty), 0) AS group_total FROM payables "
    sql << "WHERE supplier_group_id = ? AND cutoff_date = ? "
    sql << "AND supplier_id != #{supplier_id} " if supplier_id
    sql << "GROUP BY supplier_group_id, cutoff_date"
    sum = self.find_by_sql([ sql, group_id, cutoff_date])
    total = sum.first ? sum.first.group_total.to_i : 0
    logger.debug('group_total:'+total.inspect)
    return total
  end
  
  def self.group_total_without_duty(group_id, cutoff_date, supplier_id=nil)
    sql = "SELECT IFNULL(SUM(total_price + total_carry), 0) AS group_total FROM payables "
    sql << "WHERE supplier_group_id = ? AND cutoff_date = ? "
    sql << "AND supplier_id != #{supplier_id} " if supplier_id
    sql << "GROUP BY supplier_group_id, cutoff_date"
    sum = self.find_by_sql([ sql, group_id, cutoff_date])
    total = sum.first ? sum.first.group_total.to_i : 0
    logger.debug('group_total_without_duty:'+total.inspect)
    return total
  end
  
  def self.group_members(group_id, cutoff_date)
    result = find(:all,
                  :conditions => ["supplier_group_id = ? AND cutoff_date = ?", group_id, cutoff_date])
    return result
  end
  
  def update_info
    #return update_records(SupplierGroupPayment, self.supplier_group_id, false)
    return update_records(Supplier, self.supplier_group_id, false)
  end
#    result = false
#    
#    payments = SupplierGroupPayment.group_is(self.supplier_group_id)
#    #payment = get_match_payment(payments, this_total)
#    group_total = Payable.group_total(self.supplier_group_id, self.cutoff_date)
#    payment = get_match_payment(payments, group_total)
#    
#    if payment
#      # payment_type_code
#      self.payment_type_code = payment.payment_type_code
#      payment_date, encachement_date = get_receivable_dates(payment, self.cutoff_date)
#      self.payment_date = payment_date
#      self.encachement_date = encachement_date
#      
#      result = true
#    end
#    
#    return result
#  end
end
