OSDN Git Service

refs #1611 メソッドを統一
[elecoma/elecoma.git] / app / models / payment.rb
1 # -*- coding: utf-8 -*-
2 class Payment < ActiveRecord::Base
3
4   acts_as_paranoid
5   acts_as_list
6                  
7   #belongs_to :delivery_trader
8   belongs_to :resource, 
9              :class_name => "ImageResource",
10              :foreign_key => "resource_id"
11   belongs_to :payment_plugin
12   has_many :orders
13   
14   validates_presence_of :name,:fee#,:delivery_trader_id
15
16   COMMON_DELIVERY_TRADER_ID = 0
17   COMMON_DELIVERY_TRADER_NAME = "共通"
18
19   def self.find_for_price_only_common_payment(price)
20     conditions = ['(upper_limit is null or upper_limit >= ?)' +
21                   ' and (lower_limit is null or lower_limit <= ?)' + 
22                   ' and delivery_trader_id = ? ',
23                   price, price, COMMON_DELIVERY_TRADER_ID]
24     find(:all, :conditions => conditions, :order => 'position')
25   end  
26
27   def get_plugin_instance
28     payment_plugin.get_plugin_instance
29   end
30
31   def order_has_datamanagement
32     obj = self.get_plugin_instance
33     if obj
34       return obj.order_has_datamanagement
35     else
36       return false
37     end
38   end
39
40   def get_datamanagement_by_order(order_code) 
41     obj = self.get_plugin_instance
42     if obj
43       return obj.get_datamanagement_by_order(order_code)
44     else
45       return nil
46     end
47   end
48    
49
50   def common_delivery?
51     return true if delivery_trader_id == COMMON_DELIVERY_TRADER_ID
52     return false
53   end
54
55   def validate
56     if upper_limit.present? && lower_limit.present? && upper_limit.to_i < lower_limit.to_i
57       errors.add(:base, "※利用条件(〜円以上)は利用条件(〜円以下)より大きい値を入力できません。")
58     end
59     errors.add(:fee,"は0以上の整数で入力してください") unless self.fee.to_i >= 0
60     errors.add(:lower_limit,"は0以上の整数で入力してください") unless self.lower_limit.to_i >= 0
61     errors.add(:upper_limit,"は0以上の整数で入力してください") unless self.upper_limit.to_i >= 0
62     
63     errors.add(:fee,"は99,999,999円以下で入力してください") if self.fee.to_i >= 0 && self.fee.to_i>99999999
64     errors.add(:lower_limit,"は99,999,999円で入力してください") if self.lower_limit.to_i >= 0 && self.lower_limit.to_i>99999999
65     errors.add(:upper_limit,"は99,999,999円で入力してください") if self.upper_limit.to_i >= 0 && self.upper_limit.to_i>99999999
66     unless self.payment_plugin_id.nil?
67       instance = get_plugin_instance
68       if instance.nil?
69         errors.add(:payment_plugin, "が無効です。")
70       else 
71         ret, reason = instance.payment_validate(self)
72         unless ret
73           errors.add(:payment_plugin, reason)
74         end
75       end
76     end
77   end
78
79   # 指定された価格に合う支払い方法の一覧
80   def self.find_for_price(price)
81     conditions = ['(upper_limit is null or upper_limit >= ?)' +
82                   ' and (lower_limit is null or lower_limit <= ?)',
83                   price, price]
84     find(:all, :conditions => conditions, :order => 'position')
85   end
86
87   alias :resource_old= :resource=
88   [:resource].each do  | method_name|
89     define_method("#{method_name}=") do | value |
90       if value.class == ActionController::UploadedStringIO || value.class == ActionController::UploadedTempfile || value.class == Tempfile
91         image_resource = ImageResource.new_file(value, value.original_filename)
92         self.send "#{method_name}_old=".intern, image_resource
93       elsif value.class == ImageResource
94         self.send "#{method_name}_old=".intern, value
95       else
96         nil
97       end
98     end
99   end
100
101 end