diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index c2e92ea49..a50b96003 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -30,7 +30,7 @@ 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}") + current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}", AccountActivity::CREATE) render_epp_response '/epp/domains/create' else handle_errors(@domain) @@ -102,7 +102,7 @@ class Epp::DomainsController < EppController fail ActiveRecord::Rollback end - current_user.registrar.debit!(@domain_price, "#{I18n.t('renew')} #{@domain.name}") + current_user.registrar.debit!(@domain_price, "#{I18n.t('renew')} #{@domain.name}", AccountActivity::RENEW) render_epp_response '/epp/domains/renew' else handle_errors(@domain) diff --git a/app/models/account_activity.rb b/app/models/account_activity.rb index d6feb50de..ecfae8270 100644 --- a/app/models/account_activity.rb +++ b/app/models/account_activity.rb @@ -4,6 +4,10 @@ class AccountActivity < ActiveRecord::Base belongs_to :bank_transaction belongs_to :invoice + CREATE = 'create' + RENEW = 'renew' + ADD_CREDIT = 'add_credit' + after_create :update_balance def update_balance account.balance += sum diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index cd94b0220..2e5b90a2e 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -79,7 +79,8 @@ class BankTransaction < ActiveRecord::Base invoice: invoice, sum: invoice.sum_without_vat, currency: currency, - description: description + description: description, + activity_type: AccountActivity::ADD_CREDIT ) end end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 5d89816d8..d9d2188d0 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -123,19 +123,21 @@ class Registrar < ActiveRecord::Base accounts.find_by(account_type: Account::CASH) end - def debit!(sum, description) + def debit!(sum, description, type = nil) cash_account.account_activities.create!( sum: -sum, currency: 'EUR', - description: description + description: description, + activity_type: type ) end - def credit!(sum, description) + def credit!(sum, description, type = nil) cash_account.account_activities.create!( sum: sum, currency: 'EUR', - description: description + description: description, + activity_type: type ) end diff --git a/db/migrate/20150706091724_add_activity_type_to_account_activities.rb b/db/migrate/20150706091724_add_activity_type_to_account_activities.rb new file mode 100644 index 000000000..df6f21f3a --- /dev/null +++ b/db/migrate/20150706091724_add_activity_type_to_account_activities.rb @@ -0,0 +1,5 @@ +class AddActivityTypeToAccountActivities < ActiveRecord::Migration + def change + add_column :account_activities, :activity_type, :string + end +end diff --git a/db/schema-read-only.rb b/db/schema-read-only.rb index e316c3816..74097a123 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: 20150703105159) do +ActiveRecord::Schema.define(version: 20150706091724) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -27,6 +27,7 @@ ActiveRecord::Schema.define(version: 20150703105159) do t.string "description" t.string "creator_str" t.string "updator_str" + t.string "activity_type" end add_index "account_activities", ["account_id"], name: "index_account_activities_on_account_id", using: :btree @@ -932,14 +933,14 @@ ActiveRecord::Schema.define(version: 20150703105159) 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 25c13a804..64275585f 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -74,7 +74,7 @@ CREATE FUNCTION generate_zonefile(i_origin character varying) RETURNS text SELECT concat(d.name_puny, '. IN NS ', ns.hostname, '.') FROM domains d JOIN nameservers ns ON ns.domain_id = d.id - WHERE d.name LIKE include_filter AND d.name NOT LIKE exclude_filter OR d.name = i_origin + WHERE d.name LIKE include_filter AND d.name NOT LIKE exclude_filter ORDER BY d.name ), chr(10) @@ -205,7 +205,8 @@ CREATE TABLE account_activities ( updated_at timestamp without time zone, description character varying, creator_str character varying, - updator_str character varying + updator_str character varying, + activity_type character varying ); @@ -2404,8 +2405,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, @@ -2414,32 +2415,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: -- @@ -3195,13 +3170,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: - -- @@ -3721,14 +3689,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: -- @@ -4851,13 +4811,7 @@ INSERT INTO schema_migrations (version) VALUES ('20150612123111'); INSERT INTO schema_migrations (version) VALUES ('20150701074344'); -INSERT INTO schema_migrations (version) VALUES ('20150703074448'); - INSERT INTO schema_migrations (version) VALUES ('20150703084632'); -INSERT INTO schema_migrations (version) VALUES ('20150703090039'); - -INSERT INTO schema_migrations (version) VALUES ('20150703104149'); - -INSERT INTO schema_migrations (version) VALUES ('20150703105159'); +INSERT INTO schema_migrations (version) VALUES ('20150706091724'); diff --git a/spec/epp/domain_spec.rb b/spec/epp/domain_spec.rb index 46f8886a8..d4430ac15 100644 --- a/spec/epp/domain_spec.rb +++ b/spec/epp/domain_spec.rb @@ -356,6 +356,7 @@ describe 'EPP Domain', epp: true do a = @registrar1.cash_account.account_activities.last a.description.should == "Create #{Domain.last.name}" a.sum.should == -BigDecimal.new('10.0') + a.activity_type = AccountActivity::CREATE end it 'creates a domain with longer periods' do @@ -372,6 +373,7 @@ describe 'EPP Domain', epp: true do a = @registrar1.cash_account.account_activities.last a.description.should == "Create #{Domain.last.name}" a.sum.should == -BigDecimal.new('20.0') + a.activity_type = AccountActivity::CREATE end it 'creates a domain with longer periods' do @@ -388,6 +390,7 @@ describe 'EPP Domain', epp: true do a = @registrar1.cash_account.account_activities.last a.description.should == "Create #{Domain.last.name}" a.sum.should == -BigDecimal.new('30.0') + a.activity_type = AccountActivity::CREATE end it 'does not create a domain with invalid period' do @@ -2059,6 +2062,7 @@ describe 'EPP Domain', epp: true do a = @registrar1.cash_account.account_activities.last a.description.should == "Renew #{Domain.last.name}" a.sum.should == -BigDecimal.new('15.0') + a.activity_type = AccountActivity::RENEW end it 'renews a domain with 2 year period' do @@ -2089,6 +2093,7 @@ describe 'EPP Domain', epp: true do a = @registrar1.cash_account.account_activities.last a.description.should == "Renew #{Domain.last.name}" a.sum.should == -BigDecimal.new('35.0') + a.activity_type = AccountActivity::CREATE end it 'renews a domain with 3 year period' do @@ -2119,6 +2124,7 @@ describe 'EPP Domain', epp: true do a = @registrar1.cash_account.account_activities.last a.description.should == "Renew #{Domain.last.name}" a.sum.should == -BigDecimal.new('62.0') + a.activity_type = AccountActivity::CREATE end it 'does not renew a domain if credit balance low' do diff --git a/spec/models/bank_statement_spec.rb b/spec/models/bank_statement_spec.rb index d5812fbeb..77ffffd1e 100644 --- a/spec/models/bank_statement_spec.rb +++ b/spec/models/bank_statement_spec.rb @@ -62,6 +62,11 @@ describe BankStatement do AccountActivity.count.should == 1 + a = AccountActivity.last + a.description.should == "Invoice no. #{invoice.number}" + a.sum.should == BigDecimal.new('200.0') + a.activity_type = AccountActivity::ADD_CREDIT + r.cash_account.balance.should == 200.0 bs.bank_transactions.unbinded.count.should == 1