1 class Supplier < ActiveRecord::Base
5 #DEFAULT_IDは仕入先不使用時のIDとして定義
7 DEFAULT_SUPPLIER_ID = 1
8 SHISYAGONYU, KIRISUTE, KIRIAGE = 0, 1, 2
9 TAX_RULE_NAMES = { SHISYAGONYU => "四捨五入", KIRISUTE => "切り捨て", KIRIAGE => "切り上げ"}
11 validates_presence_of :name
12 validates_uniqueness_of :name
13 validates_length_of :name, :maximum => 50
15 validates_presence_of :zipcode01, :zipcode02
16 validates_numericality_of :zipcode01, :zipcode02, :allow_blank=>true
17 validates_length_of :zipcode01, :is => 3, :allow_blank=>true
18 validates_length_of :zipcode02, :is => 4, :allow_blank=>true
19 validates_associated :prefecture
20 validates_presence_of :prefecture_id,:address_city, :address_detail
21 validates_length_of :address_city, :address_detail, :maximum => 100
23 validates_presence_of :contact_name
24 validates_length_of :contact_name, :maximum => 50
26 validates_presence_of :tel01, :tel02, :tel03
27 validates_numericality_of :tel01, :tel02, :tel03, :allow_blank => true
28 validates_length_of :tel01, :tel02, :tel03, :maximum => 6, :allow_blank => true
30 validates_numericality_of :fax01, :fax02, :fax03, :allow_blank => true
31 validates_length_of :fax01, :fax02, :fax03, :maximum => 6, :allow_blank => true
33 validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :allow_blank => true
35 validates_inclusion_of :percentage, :in => (0..100), :allow_blank => true
37 validates_length_of :free_comment, :maximum => 10000 , :allow_blank => true
38 validates_inclusion_of :tax_rule, :in => [SHISYAGONYU, KIRISUTE, KIRIAGE] , :allow_blank => true
40 before_update :check_default
41 before_destroy :check_default_and_products
45 # FAX どれかが入力されている時だけ検証
46 if not [fax01, fax02, fax03].all?(&:blank?)
47 fax_items = %w(fax01 fax02 fax03)
48 errors.add_on_blank fax_items, "が入力されていません"
53 "#{tel01}-#{tel02}-#{tel03}" unless tel01.blank? or tel02.blank? or tel03.blank?
57 "#{fax01}-#{fax02}-#{fax03}" unless fax01.blank? or fax02.blank? or fax03.blank?
61 prefecture && prefecture.name
65 TAX_RULE_NAMES[tax_rule] unless tax_rule.blank?
67 def check_default_and_products
69 #直接URL入力で商品を持っている仕入先を削除防止するため
70 unless self.products.blank?
71 raise ActiveRecord::ReadOnlyRecord
75 #id=1のデータはデフォルトの状態のみで、編集不可、削除不可
76 if self.id == DEFAULT_SUPPLIER_ID
77 raise ActiveRecord::ReadOnlyRecord