diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index e3188786f..d8e77ca95 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -30,7 +30,13 @@ class Epp::DomainsController < EppController ActiveRecord::Base.transaction do if @domain.save # TODO: Maybe use validate: false here because we have already validated the domain? - current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}", AccountActivity::CREATE) + current_user.registrar.debit!({ + sum: @domain_pricelist.price.amount, + description: "#{I18n.t('create')} #{@domain.name}", + activity_type: AccountActivity::CREATE, + log_pricelist_id: @domain_pricelist.id + }) + render_epp_response '/epp/domains/create' else handle_errors(@domain) @@ -102,7 +108,13 @@ class Epp::DomainsController < EppController fail ActiveRecord::Rollback end - current_user.registrar.debit!(@domain_price, "#{I18n.t('renew')} #{@domain.name}", AccountActivity::RENEW) + current_user.registrar.debit!({ + sum: @domain_pricelist.price.amount, + description: "#{I18n.t('renew')} #{@domain.name}", + activity_type: AccountActivity::RENEW, + log_pricelist_id: @domain_pricelist.id + }) + render_epp_response '/epp/domains/renew' else handle_errors(@domain) @@ -212,8 +224,8 @@ class Epp::DomainsController < EppController end def balance_ok?(operation, period = nil, unit = nil) - @domain_price = @domain.price(operation, period.try(:to_i), unit).amount - if current_user.registrar.balance < @domain_price + @domain_pricelist = @domain.pricelist(operation, period.try(:to_i), unit) + if current_user.registrar.balance < @domain_pricelist.price.amount epp_errors << { code: '2104', msg: I18n.t('billing_failure_credit_balance_low') diff --git a/app/models/domain.rb b/app/models/domain.rb index 49dcb9cfc..f21e00f4e 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -397,7 +397,7 @@ class Domain < ActiveRecord::Base DomainMailer.pending_deleted(self).deliver_now end - def price(operation, period_i = nil, unit = nil) + def pricelist(operation, period_i = nil, unit = nil) period_i ||= period unit ||= period_unit @@ -413,7 +413,7 @@ class Domain < ActiveRecord::Base p = "#{p}year" end - Pricelist.price_for(zone, operation, p) + Pricelist.pricelist_for(zone, operation, p) end ### VALIDATIONS ### diff --git a/app/models/pricelist.rb b/app/models/pricelist.rb index 4bd1847cd..bb37ad0e1 100644 --- a/app/models/pricelist.rb +++ b/app/models/pricelist.rb @@ -23,10 +23,10 @@ class Pricelist < ActiveRecord::Base end class << self - def price_for(zone, operation, period) + def pricelist_for(zone, operation, period) lists = valid.where(category: zone, operation_category: operation, duration: period) - return lists.first.price if lists.count == 1 - lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first.price + return lists.first if lists.count == 1 + lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first end end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index d9d2188d0..1da3f2d20 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -123,22 +123,15 @@ class Registrar < ActiveRecord::Base accounts.find_by(account_type: Account::CASH) end - def debit!(sum, description, type = nil) - cash_account.account_activities.create!( - sum: -sum, - currency: 'EUR', - description: description, - activity_type: type - ) + def debit!(args) + args[:sum] *= -1 + args[:currency] = 'EUR' + cash_account.account_activities.create!(args) end - def credit!(sum, description, type = nil) - cash_account.account_activities.create!( - sum: sum, - currency: 'EUR', - description: description, - activity_type: type - ) + def credit!(args) + args[:currency] = 'EUR' + cash_account.account_activities.create!(args) end def domain_transfers diff --git a/db/migrate/20150713113436_add_log_pricelist_id_to_account_activity.rb b/db/migrate/20150713113436_add_log_pricelist_id_to_account_activity.rb new file mode 100644 index 000000000..083101d04 --- /dev/null +++ b/db/migrate/20150713113436_add_log_pricelist_id_to_account_activity.rb @@ -0,0 +1,5 @@ +class AddLogPricelistIdToAccountActivity < ActiveRecord::Migration + def change + add_column :account_activities, :log_pricelist_id, :integer + end +end diff --git a/db/schema-read-only.rb b/db/schema-read-only.rb index f01042849..9859d8a1d 100644 --- a/db/schema-read-only.rb +++ b/db/schema-read-only.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150709092549) do +ActiveRecord::Schema.define(version: 20150713113436) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -29,6 +29,7 @@ ActiveRecord::Schema.define(version: 20150709092549) do t.string "creator_str" t.string "updator_str" t.string "activity_type" + t.integer "log_pricelist_id" end add_index "account_activities", ["account_id"], name: "index_account_activities_on_account_id", using: :btree @@ -935,14 +936,14 @@ ActiveRecord::Schema.define(version: 20150709092549) do end create_table "que_jobs", id: false, force: :cascade do |t| - t.integer "priority", limit: 2, default: 100, null: false - t.datetime "run_at", default: "now()", null: false - t.integer "job_id", limit: 8, default: "nextval('que_jobs_job_id_seq'::regclass)", null: false - t.text "job_class", null: false - t.json "args", default: [], null: false - t.integer "error_count", default: 0, null: false + t.integer "priority", limit: 2, default: 100, null: false + t.datetime "run_at", default: '2015-06-30 14:16:50', null: false + t.integer "job_id", limit: 8, default: 0, null: false + t.text "job_class", null: false + t.json "args", default: [], null: false + t.integer "error_count", default: 0, null: false t.text "last_error" - t.text "queue", default: "", null: false + t.text "queue", default: "", null: false end create_table "registrant_verifications", force: :cascade do |t| diff --git a/db/structure.sql b/db/structure.sql index 698250c1a..f16eada5e 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -220,7 +220,8 @@ CREATE TABLE account_activities ( description character varying, creator_str character varying, updator_str character varying, - activity_type character varying + activity_type character varying, + log_pricelist_id integer ); @@ -2420,8 +2421,8 @@ ALTER SEQUENCE pricelists_id_seq OWNED BY pricelists.id; CREATE TABLE que_jobs ( priority smallint DEFAULT 100 NOT NULL, - run_at timestamp with time zone DEFAULT now() NOT NULL, - job_id bigint NOT NULL, + run_at timestamp without time zone DEFAULT '2015-06-30 14:16:50.905537'::timestamp without time zone NOT NULL, + job_id bigint DEFAULT 0 NOT NULL, job_class text NOT NULL, args json DEFAULT '[]'::json NOT NULL, error_count integer DEFAULT 0 NOT NULL, @@ -2430,32 +2431,6 @@ CREATE TABLE que_jobs ( ); --- --- Name: TABLE que_jobs; Type: COMMENT; Schema: public; Owner: - --- - -COMMENT ON TABLE que_jobs IS '3'; - - --- --- Name: que_jobs_job_id_seq; Type: SEQUENCE; Schema: public; Owner: - --- - -CREATE SEQUENCE que_jobs_job_id_seq - START WITH 1 - INCREMENT BY 1 - NO MINVALUE - NO MAXVALUE - CACHE 1; - - --- --- Name: que_jobs_job_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - --- - -ALTER SEQUENCE que_jobs_job_id_seq OWNED BY que_jobs.job_id; - - -- -- Name: registrant_verifications; Type: TABLE; Schema: public; Owner: -; Tablespace: -- @@ -3211,13 +3186,6 @@ ALTER TABLE ONLY people ALTER COLUMN id SET DEFAULT nextval('people_id_seq'::reg ALTER TABLE ONLY pricelists ALTER COLUMN id SET DEFAULT nextval('pricelists_id_seq'::regclass); --- --- Name: job_id; Type: DEFAULT; Schema: public; Owner: - --- - -ALTER TABLE ONLY que_jobs ALTER COLUMN job_id SET DEFAULT nextval('que_jobs_job_id_seq'::regclass); - - -- -- Name: id; Type: DEFAULT; Schema: public; Owner: - -- @@ -3737,14 +3705,6 @@ ALTER TABLE ONLY pricelists ADD CONSTRAINT pricelists_pkey PRIMARY KEY (id); --- --- Name: que_jobs_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: --- - -ALTER TABLE ONLY que_jobs - ADD CONSTRAINT que_jobs_pkey PRIMARY KEY (queue, priority, run_at, job_id); - - -- -- Name: registrant_verifications_pkey; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: -- @@ -4849,26 +4809,14 @@ INSERT INTO schema_migrations (version) VALUES ('20150520164507'); INSERT INTO schema_migrations (version) VALUES ('20150521120145'); -INSERT INTO schema_migrations (version) VALUES ('20150522164020'); - -INSERT INTO schema_migrations (version) VALUES ('20150525075550'); - -INSERT INTO schema_migrations (version) VALUES ('20150601083516'); - -INSERT INTO schema_migrations (version) VALUES ('20150601083800'); - INSERT INTO schema_migrations (version) VALUES ('20150603141549'); INSERT INTO schema_migrations (version) VALUES ('20150603211318'); INSERT INTO schema_migrations (version) VALUES ('20150603212659'); -INSERT INTO schema_migrations (version) VALUES ('20150609093515'); - INSERT INTO schema_migrations (version) VALUES ('20150609103333'); -INSERT INTO schema_migrations (version) VALUES ('20150610111019'); - INSERT INTO schema_migrations (version) VALUES ('20150610112238'); INSERT INTO schema_migrations (version) VALUES ('20150610144547'); @@ -4877,12 +4825,8 @@ INSERT INTO schema_migrations (version) VALUES ('20150611124920'); INSERT INTO schema_migrations (version) VALUES ('20150612123111'); -INSERT INTO schema_migrations (version) VALUES ('20150612125720'); - INSERT INTO schema_migrations (version) VALUES ('20150701074344'); -INSERT INTO schema_migrations (version) VALUES ('20150703084206'); - INSERT INTO schema_migrations (version) VALUES ('20150703084632'); INSERT INTO schema_migrations (version) VALUES ('20150706091724'); @@ -4893,3 +4837,5 @@ INSERT INTO schema_migrations (version) VALUES ('20150707154543'); INSERT INTO schema_migrations (version) VALUES ('20150709092549'); +INSERT INTO schema_migrations (version) VALUES ('20150713113436'); + diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 2885b1a16..e95bfd726 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -5,9 +5,9 @@ describe 'EPP Domain', epp: true do @xsd = Nokogiri::XML::Schema(File.read('doc/schemas/domain-eis-1.0.xsd')) @epp_xml = EppXml.new(cl_trid: 'ABC-12345') @registrar1 = Fabricate(:registrar1, code: 'REGDOMAIN1') - @registrar1.credit!(10000, '') + @registrar1.credit!({ sum: 10000 }) @registrar2 = Fabricate(:registrar2, code: 'REGDOMAIN2') - @registrar2.credit!(10000, '') + @registrar2.credit!({ sum: 10000 }) Fabricate(:api_user, username: 'registrar1', registrar: @registrar1) Fabricate(:api_user, username: 'registrar2', registrar: @registrar2) diff --git a/spec/models/domain_spec.rb b/spec/models/domain_spec.rb index 40deb363a..30531bff0 100644 --- a/spec/models/domain_spec.rb +++ b/spec/models/domain_spec.rb @@ -193,13 +193,13 @@ describe Domain do }) domain = Fabricate(:domain) - domain.price('create').should == 1.50 + domain.pricelist('create').price.amount.should == 1.50 domain = Fabricate(:domain, period: 12, period_unit: 'm') - domain.price('create').should == 1.50 + domain.pricelist('create').price.amount.should == 1.50 domain = Fabricate(:domain, period: 365, period_unit: 'd') - domain.price('create').should == 1.50 + domain.pricelist('create').price.amount.should == 1.50 Fabricate(:pricelist, { category: 'ee', @@ -211,13 +211,13 @@ describe Domain do }) domain = Fabricate(:domain, period: 2) - domain.price('create').should == 3.0 + domain.pricelist('create').price.amount.should == 3.0 domain = Fabricate(:domain, period: 24, period_unit: 'm') - domain.price('create').should == 3.0 + domain.pricelist('create').price.amount.should == 3.0 domain = Fabricate(:domain, period: 730, period_unit: 'd') - domain.price('create').should == 3.0 + domain.pricelist('create').price.amount.should == 3.0 Fabricate(:pricelist, { category: 'ee', @@ -229,13 +229,13 @@ describe Domain do }) domain = Fabricate(:domain, period: 3) - domain.price('create').should == 6.0 + domain.pricelist('create').price.amount.should == 6.0 domain = Fabricate(:domain, period: 36, period_unit: 'm') - domain.price('create').should == 6.0 + domain.pricelist('create').price.amount.should == 6.0 domain = Fabricate(:domain, period: 1095, period_unit: 'd') - domain.price('create').should == 6.0 + domain.pricelist('create').price.amount.should == 6.0 end it 'should know its renew price' do @@ -249,13 +249,13 @@ describe Domain do }) domain = Fabricate(:domain) - domain.price('renew').should == 1.30 + domain.pricelist('renew').price.amount.should == 1.30 domain = Fabricate(:domain, period: 12, period_unit: 'm') - domain.price('renew').should == 1.30 + domain.pricelist('renew').price.amount.should == 1.30 domain = Fabricate(:domain, period: 365, period_unit: 'd') - domain.price('renew').should == 1.30 + domain.pricelist('renew').price.amount.should == 1.30 Fabricate(:pricelist, { category: 'ee', @@ -267,13 +267,13 @@ describe Domain do }) domain = Fabricate(:domain, period: 2) - domain.price('renew').should == 3.1 + domain.pricelist('renew').price.amount.should == 3.1 domain = Fabricate(:domain, period: 24, period_unit: 'm') - domain.price('renew').should == 3.1 + domain.pricelist('renew').price.amount.should == 3.1 domain = Fabricate(:domain, period: 730, period_unit: 'd') - domain.price('renew').should == 3.1 + domain.pricelist('renew').price.amount.should == 3.1 Fabricate(:pricelist, { category: 'ee', @@ -285,13 +285,13 @@ describe Domain do }) domain = Fabricate(:domain, period: 3) - domain.price('renew').should == 6.1 + domain.pricelist('renew').price.amount.should == 6.1 domain = Fabricate(:domain, period: 36, period_unit: 'm') - domain.price('renew').should == 6.1 + domain.pricelist('renew').price.amount.should == 6.1 domain = Fabricate(:domain, period: 1095, period_unit: 'd') - domain.price('renew').should == 6.1 + domain.pricelist('renew').price.amount.should == 6.1 end context 'about registrant update confirm' do diff --git a/spec/models/pricelist_spec.rb b/spec/models/pricelist_spec.rb index 4e841ed94..f2c155846 100644 --- a/spec/models/pricelist_spec.rb +++ b/spec/models/pricelist_spec.rb @@ -70,7 +70,7 @@ describe Pricelist do end it 'should return correct price' do - expect { Pricelist.price_for('ee', 'create', '1year') }.to raise_error(NoMethodError) + Pricelist.pricelist_for('ee', 'create', '1year').should == nil Fabricate(:pricelist, { category: 'ee', @@ -81,7 +81,7 @@ describe Pricelist do valid_to: Time.zone.parse('2199-01-01') }) - expect { Pricelist.price_for('ee', 'create', '1year') }.to raise_error(NoMethodError) + Pricelist.pricelist_for('ee', 'create', '1year').should == nil Fabricate(:pricelist, { category: 'ee', @@ -92,7 +92,7 @@ describe Pricelist do valid_to: nil }) - Pricelist.price_for('ee', 'create', '1year').should == 1.50 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.50 Fabricate(:pricelist, { category: 'ee', @@ -103,7 +103,7 @@ describe Pricelist do valid_to: Time.zone.parse('2999-01-01') }) - Pricelist.price_for('ee', 'create', '1year').should == 1.30 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.30 Fabricate.create(:pricelist, { category: 'ee', @@ -114,7 +114,7 @@ describe Pricelist do valid_to: Time.zone.parse('2999-01-01') }) - Pricelist.price_for('ee', 'create', '1year').should == 1.20 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.20 Fabricate.create(:pricelist, { category: 'ee', @@ -125,7 +125,7 @@ describe Pricelist do valid_to: Time.zone.parse('2999-01-01') }) - Pricelist.price_for('ee', 'create', '1year').should == 1.20 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.20 Fabricate.create(:pricelist, { category: 'ee', @@ -136,7 +136,7 @@ describe Pricelist do valid_to: Time.zone.parse('2999-01-01') }) - Pricelist.price_for('ee', 'create', '1year').should == 1.20 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.20 Fabricate.create(:pricelist, { category: 'ee', @@ -147,7 +147,7 @@ describe Pricelist do valid_to: nil }) - Pricelist.price_for('ee', 'create', '1year').should == 1.20 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.20 Fabricate.create(:pricelist, { category: 'ee', @@ -158,7 +158,7 @@ describe Pricelist do valid_to: Time.zone.parse('2999-01-01') }) - Pricelist.price_for('ee', 'create', '1year').should == 1.10 + Pricelist.pricelist_for('ee', 'create', '1year').price.amount.should == 1.10 Fabricate.create(:pricelist, { category: 'ee', @@ -169,6 +169,6 @@ describe Pricelist do valid_to: Time.zone.parse('2999-01-01') }) - Pricelist.price_for('ee', 'create', '2years').should == 1.20 + Pricelist.pricelist_for('ee', 'create', '2years').price.amount.should == 1.20 end end diff --git a/spec/models/registrar_spec.rb b/spec/models/registrar_spec.rb index 0cf04b2d2..cc470dd0b 100644 --- a/spec/models/registrar_spec.rb +++ b/spec/models/registrar_spec.rb @@ -146,14 +146,14 @@ describe Registrar do end it 'should credit and debit registrar cash account' do - @registrar.credit!(13.32, 'Add money') + @registrar.credit!({ sum: 13.32, description: 'Add money' }) @registrar.balance.should == BigDecimal.new('13.32') @registrar.cash_account.account_activities.count.should == 1 a = @registrar.cash_account.account_activities.last a.description.should == 'Add money' a.sum.should == BigDecimal.new('13.32') - @registrar.debit!(10.31, 'Remove money') + @registrar.debit!({ sum: 10.31, description: 'Remove money' }) @registrar.balance.should == BigDecimal.new('3.01') @registrar.cash_account.account_activities.count.should == 2 a = @registrar.cash_account.account_activities.last