#
#= receivables モデル
# Authors:: Sumiyo Yamamoto
# Copyright:: Copyright (C) OrbusNeich Medical K.K.  2010.
#--
# date        name                   note
# 2010.11.5   Sumiyo Yamamoto        新規登録
#-------------------------------------------------------------------------------
#++
class Receivable < CommLogistics::Base::Model::ReceivablePayable
  has_many :child_details,
           :class_name => 'ReceivableDetail',
           :dependent => :destroy
  
  named_scope :all_search, lambda {|target_id, date| {
    :conditions => ["customer_id = ? AND cutoff_date = ?", target_id, date.to_s]
  }}
  
  named_scope :target_charge, lambda {|target_id, date| {
    :conditions => ["customer_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 => ["customer_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 => ["customer_id = ? AND cutoff_date > ?", target_id, date],
                 :order => "cutoff_date ASC")
    return result
  end
  
  def self.group_total(group_id, cutoff_date, customer_id=nil)
    sql = "SELECT IFNULL(SUM(total_price + total_duty + total_carry + total_carry_duty), 0) AS group_total FROM receivables "
    sql << "WHERE customer_group_id = ? AND cutoff_date = ? "
    sql << "AND customer_id != #{customer_id} " if customer_id
    sql << "GROUP BY customer_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, customer_id=nil)
    sql = "SELECT IFNULL(SUM(total_price + total_carry), 0) AS group_total FROM receivables "
    sql << "WHERE customer_group_id = ? AND cutoff_date = ? "
    sql << "AND customer_id != #{customer_id} " if customer_id
    sql << "GROUP BY customer_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 => ["customer_group_id = ? AND cutoff_date = ?", group_id, cutoff_date])
    return result
  end
  
  def update_info
    #return update_records(CustomerGroupPayment, self.customer_group_id)
    return update_records(Customer, self.customer_group_id)
#    result = false
#    
#    payments = CustomerGroupPayment.customer_group_is(self.customer_group_id).all
#    #payment = get_match_payment(payments, this_total)
#    group_total = Receivable.group_total(self.customer_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
#      # update same group
#      customers = Receivable.customers(self.customer_group_id, self.cutoff_date)
#      customers.delete(self)
#      logger.debug('customers:'+customers.inspect)
#      if customers
#        customers.each do |cm|
#          cm.payment_type_code = payment.payment_type_code
#          cm.payment_date = payment_date
#          cm.encachement_date = encachement_date
#          cm.save
#        end
#      end
#      result = true
#    end
#    
#    return result
  end
end
