require_dependency "lib/comm_logistics/models/shipping.rb"

class Shipping
  def self.picking_printed_search(tar)
    #着荷場所が営業店止め(運送業者が設定されているような場所)であれば、受取人まで一致するか見る。
    results = find_same_delivery(tar['id'], tar, false)
    print_results = []
    wait_sum = 0 
    picking_sum = 0
    comp_sum = 0
    results.each do |ar|
      print_results.push(ar) unless ar.print_picking_date.blank?
      wait_sum = wait_sum + ar.total_quantity.to_i if ar.shipping_state_code==MCODE_SHIPPING_STATE_WAIT || ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_DIRECTION
      picking_sum = picking_sum + ar.total_quantity.to_i if ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_PICK
      comp_sum = comp_sum + ar.total_quantity.to_i if ar.shipping_state_code==MCODE_SHIPPING_STATE_COMP
    end
    #対象伝票
    tar.update_attributes(:wait_quantity => wait_sum, :picking_quantity => picking_sum, :comp_quantity => comp_sum)
    if tar.shipping_state_code == MCODE_SHIPPING_STATE_BEFORE_DIRECTION
      unless tar.update_attributes(:shipping_state_code => MCODE_SHIPPING_STATE_BEFORE_PICK)
        raise 'state logging failed'
      end
    end
    #対象伝票以外
    results.each do |ar|
      tmp_wait = wait_sum - (ar.shipping_state_code==MCODE_SHIPPING_STATE_WAIT || ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_DIRECTION ? ar.total_quantity : 0 ) + ((tar.shipping_state_code==MCODE_SHIPPING_STATE_WAIT || ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_DIRECTION) ? tar.total_quantity : 0)
      tmp_picking = picking_sum - (ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_PICK ? ar.total_quantity : 0) + (tar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_PICK ? tar.total_quantity : 0)
      tmp_comp = comp_sum - (ar.shipping_state_code==MCODE_SHIPPING_STATE_COMP ? ar.total_quantity : 0) + (tar.shipping_state_code==MCODE_SHIPPING_STATE_COMP ? tar.total_quantity : 0)
      ar.update_attributes(
      :wait_quantity => tmp_wait,
      :picking_quantity => tmp_picking,
      :comp_quantity => tmp_comp
      )
    end
    return print_results
  end
  
  def update_stock_info_with_check_delivery(main, details, is_create=true)
    
    update_stock_info_without_check_delivery(main, details, is_create)
    
    if main['shipping_type_code'].to_i==MCODE_SHIPPING_TYPE_MOVE || main['shipping_state_code'].to_i==MCODE_SHIPPING_STATE_BO
      return
    end 
    
    results = Shipping.find_same_delivery(self.id, main)#対象伝票を無条件に含んで検索
    
    wait_sum = 0 
    picking_sum = 0
    comp_sum = 0
    tar = nil #対象伝票
    results.each do |ar|
      if ar.id == self.id
        tar = ar
      else
        wait_sum = wait_sum + ar.total_quantity.to_i if ar.shipping_state_code==MCODE_SHIPPING_STATE_WAIT || ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_DIRECTION
        picking_sum = picking_sum + ar.total_quantity.to_i if ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_PICK
        comp_sum = comp_sum + ar.total_quantity.to_i if ar.shipping_state_code==MCODE_SHIPPING_STATE_COMP
      end
    end
    
    results.each do |ar|
      if ar.id == self.id #対象伝票だったら
        ar.update_attributes(:wait_quantity => wait_sum, :picking_quantity => picking_sum, :comp_quantity => comp_sum)
      else #対象伝票以外は
        ar.update_attributes(:wait_quantity => 
                                wait_sum - (ar.shipping_state_code==MCODE_SHIPPING_STATE_WAIT || ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_DIRECTION ? ar.total_quantity : 0 ) + (is_create && tar && (tar.shipping_state_code==MCODE_SHIPPING_STATE_WAIT || tar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_DIRECTION) ? tar.total_quantity : 0),
                             :picking_quantity => 
                                picking_sum - (ar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_PICK ? ar.total_quantity : 0) + (is_create && tar && tar.shipping_state_code==MCODE_SHIPPING_STATE_BEFORE_PICK ? tar.total_quantity : 0),
                             :comp_quantity =>
                                comp_sum - (ar.shipping_state_code==MCODE_SHIPPING_STATE_COMP ? ar.total_quantity : 0) + (is_create && tar && tar.shipping_state_code==MCODE_SHIPPING_STATE_COMP ? tar.total_quantity : 0)
                            )
      end
    end
  end
  alias_method_chain :update_stock_info, :check_delivery
  
  def self.find_same_delivery(id, params, include_own=true)
    if params['delivery_id'].blank?
      delivery_id_str = "delivery_id IS NULL"
      receiver_str = ""
    else
      delivery_id_str = "delivery_id = #{params['delivery_id']}"
      dar = Delivery.find_by_id(params['delivery_id'])
      receiver_str = dar && dar.trucking_id ? "receiver_name='#{params['receiver_name']}' AND " : ""
    end
    if include_own
      id_str = " "
    else
      id_str = " AND id != #{id} "
    end
    result = Shipping.find_by_sql(["SELECT id,total_quantity,print_picking_date,invalid_flag_code,shipping_state_code FROM shippings
                           WHERE target_date = ? AND
                                 shipment_customer_id = ? AND
                                 #{delivery_id_str} AND
                                 shipping_type_code != #{MCODE_SHIPPING_TYPE_MOVE} AND
                                 #{receiver_str}
                                 invalid_flag_code = #{MCODE_FLAG_OFF} 
                                 #{id_str}", 
                           params['target_date'],
                           params['shipment_customer_id']])
    return result
  end
end