module CommLogistics::Modules::FindPrice
  
  def find_price(table_params, detail_params, price_cls)
    require 'config/site_config.rb'
    # "wholesale_price" or "invoice_price"
    price_name = price_cls.to_s.underscore
    # "public_price" or "cost_price"
    base_price_name = price_name.sub("wholesale", "public").sub("invoice", "cost")
    
    target_ar = price_name=='wholesale_price' ? Customer.find(table_params[:customer_id]) : Supplier.find(table_params[:supplier_id])
    product_set_ar = ProductSet.find(detail_params['product_set_id'])
    par = product_set_ar.price_unique_code.to_i == Comm::Const::MasterCode::MCODE_PRICE_UNIQUE_PRODUCT_SET ? product_set_ar : Product.find(detail_params['product_id'])
    
    price_params = {:warehouse_id => table_params[:warehouse_id], 
                    :customer_id => table_params[:customer_id], 
                    :supplier_id => table_params[:supplier_id], 
                    :currency_type_code=>table_params[:currency_type_code], 
                    :product_category_id => detail_params['product_category_id'], 
                    :product_set_id => detail_params['product_set_id'], 
                    :product_id => detail_params['product_id']}
    price = find_match_price(price_params, price_cls.find_target(price_params))
    #得意先別卸価格に見つからなかった場合
    if price == false
      price_type_ar = ($WAREHOUSE_PRICE_DEFINITION && $WAREHOUSE_PRICE_DEFINITION[price_name.tableize]) ? Warehouse.find(table_params[:warehouse_id]) : target_ar
      if price_type_ar[price_name + '_code'].to_i > 0
        #卸価格1 ~ 10
        tmp_value = par[price_name + '_' + price_type_ar[price_name + '_code'].to_s].to_f
      else
        #定価
        tmp_value = par[base_price_name].to_f
      end
    #得意先別卸価格に見つかった場合
    else
      #掛け率の場合
      if price.price_type_code.to_i == Comm::Const::MasterCode::MCODE_PRICE_DEFINE_RATE
        tmp_value = price.price.to_f * par['public_price'].to_f
      #金額の場合
      else
        tmp_value = price.price.to_f
      end
    end
    return CommLogistics::Tools::CalcDuty::round_price(tmp_value, {:unit_price_fraction_method_code=>target_ar.unit_price_fraction_method_code.to_i})
  end
  
  def find_match_price(params, pars)
    product_category_id = params[:product_category_id] || 0
    product_set_id = params[:product_set_id] || 0
    product_id = params[:product_id] || 0
    
    product_all = Comm::Const::DispNameLists::LIST_ALL
    find_match_level = 0;
    find_record_index = nil;
    #pars = WholesalePrice.find(:all, :conditions => [" warehouse_id = ? AND customer_id = ? AND supplier_id = ?", warehouse_id, customer_id, supplier_id])
    if pars.length > 0
      pars.each_with_index do |ar, index|
        ar_product_category_id = ar.product_category_id.to_i
        ar_product_set_id = ar.product_set_id.to_i
        ar_product_id = ar.product_id.to_i
        if ar_product_category_id==product_category_id && ar_product_set_id==product_set_id && ar_product_id==product_id
          find_record_index = index
          find_match_level = 4
          break
        elsif ar_product_category_id==product_category_id && ar_product_set_id==product_set_id && ar_product_id==product_all
          find_record_index = index
          find_match_level = 3
        elsif ar_product_category_id==product_category_id && ar_product_set_id==product_all && ar_product_id==product_all
          if find_match_level < 3
            find_record_index = index
            find_match_level = 2
          end
        elsif ar_product_category_id==product_all && ar_product_set_id==product_all && ar_product_id==product_all
          if find_match_level < 2
            find_record_index = index
            find_match_level = 1
          end
        end
      end
      if find_match_level > 0
        return pars[find_record_index]
      end
    end
    return false
  end

end

