From 909c204af5f530257aca96d7973d8c56c7a6522a Mon Sep 17 00:00:00 2001 From: Peng DU Date: Wed, 20 Jul 2016 15:07:43 +0200 Subject: [PATCH] show invoice download link in wallet transaction debit operator --- app/assets/templates/wallet/transactions.html.erb | 9 ++++++--- app/controllers/api/wallet_controller.rb | 2 +- app/models/invoice.rb | 1 + app/models/reservation.rb | 6 ++++-- app/models/subscription.rb | 14 +++++++++++--- app/models/wallet_transaction.rb | 1 + app/views/api/wallet/transactions.json.jbuilder | 9 ++++----- ...0720124355_add_wallet_transaction_to_invoice.rb | 5 +++++ db/schema.rb | 5 ++++- .../reservations/create_as_admin_test.rb | 1 + .../subscriptions/create_as_user_test.rb | 1 + 11 files changed, 39 insertions(+), 15 deletions(-) create mode 100644 db/migrate/20160720124355_add_wallet_transaction_to_invoice.rb diff --git a/app/assets/templates/wallet/transactions.html.erb b/app/assets/templates/wallet/transactions.html.erb index 011005b51..4bd4b35ea 100644 --- a/app/assets/templates/wallet/transactions.html.erb +++ b/app/assets/templates/wallet/transactions.html.erb @@ -10,16 +10,19 @@ - {{ t.created_at | amDateFormat:'L' }} + {{ ::t.created_at | amDateFormat:'L' }} {{ 'credit' }} {{ 'debit' }} + + {{::t.invoice.reference}} + - {{ t.user.full_name }} + {{ ::t.user.full_name }} + - - {{ t.amount | currency }} + {{ ::t.amount | currency }} diff --git a/app/controllers/api/wallet_controller.rb b/app/controllers/api/wallet_controller.rb index 9753bf63d..5ff142155 100644 --- a/app/controllers/api/wallet_controller.rb +++ b/app/controllers/api/wallet_controller.rb @@ -10,7 +10,7 @@ class API::WalletController < API::ApiController def transactions @wallet = Wallet.find(params[:id]) authorize @wallet - @wallet_transactions = @wallet.wallet_transactions.includes(:transactable, user: [:profile]).order(created_at: :desc) + @wallet_transactions = @wallet.wallet_transactions.includes(:invoice, user: [:profile]).order(created_at: :desc) end def credit diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 301ac0cba..839f0c5e8 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -7,6 +7,7 @@ class Invoice < ActiveRecord::Base has_many :invoice_items, dependent: :destroy accepts_nested_attributes_for :invoice_items belongs_to :user + belongs_to :wallet_transaction has_one :avoir, class_name: 'Invoice', foreign_key: :invoice_id, dependent: :destroy diff --git a/app/models/reservation.rb b/app/models/reservation.rb index bcadc12fd..f3af73157 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -364,8 +364,10 @@ class Reservation < ActiveRecord::Base def debit_user_wallet if @wallet_amount_debit.present? and @wallet_amount_debit != 0 amount = @wallet_amount_debit / 100.0 - WalletService.new(user: user, wallet: user.wallet).debit(amount, self) - self.invoice.update_columns(wallet_amount: @wallet_amount_debit) if !user.invoicing_disabled? + wallet_transaction = WalletService.new(user: user, wallet: user.wallet).debit(amount, self) + if !user.invoicing_disabled? and wallet_transaction + self.invoice.update_columns(wallet_amount: @wallet_amount_debit, wallet_transaction_id: wallet_transaction.id) + end end end end diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 0a25d5a21..9ea2a2110 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -48,7 +48,11 @@ class Subscription < ActiveRecord::Base if invoice invoc = generate_invoice(stp_invoice.id) # debit wallet - invoc.wallet_amount = @wallet_amount_debit if debit_user_wallet + wallet_transaction = debit_user_wallet + if wallet_transaction + invoc.wallet_amount = @wallet_amount_debit + invoc.wallet_transaction_id = wallet_transaction.id + end invoc.save end # cancel subscription after create @@ -101,7 +105,11 @@ class Subscription < ActiveRecord::Base if invoice invoc = generate_invoice # debit wallet - invoc.wallet_amount = @wallet_amount_debit if debit_user_wallet + wallet_transaction = debit_user_wallet + if wallet_transaction + invoc.wallet_amount = @wallet_amount_debit + invoc.wallet_transaction_id = wallet_transaction.id + end invoc.save end return true @@ -243,7 +251,7 @@ class Subscription < ActiveRecord::Base def debit_user_wallet if @wallet_amount_debit.present? and @wallet_amount_debit != 0 amount = @wallet_amount_debit / 100.0 - WalletService.new(user: user, wallet: user.wallet).debit(amount, self) + return WalletService.new(user: user, wallet: user.wallet).debit(amount, self) end end end diff --git a/app/models/wallet_transaction.rb b/app/models/wallet_transaction.rb index 21c70eef4..0de5feb3b 100644 --- a/app/models/wallet_transaction.rb +++ b/app/models/wallet_transaction.rb @@ -5,6 +5,7 @@ class WalletTransaction < ActiveRecord::Base belongs_to :wallet belongs_to :reservation belongs_to :transactable, polymorphic: true + has_one :invoice validates_inclusion_of :transaction_type, in: %w( credit debit ) validates :user, :wallet, presence: true diff --git a/app/views/api/wallet/transactions.json.jbuilder b/app/views/api/wallet/transactions.json.jbuilder index 050cb137f..14a484915 100644 --- a/app/views/api/wallet/transactions.json.jbuilder +++ b/app/views/api/wallet/transactions.json.jbuilder @@ -4,9 +4,8 @@ json.array!(@wallet_transactions) do |t| json.id t.user.id json.full_name t.user.profile.full_name end - json.transactable do - if t.transactable_type == 'Reservation' - json.reservable_type t.transactable.reservable_type - end - end if t.transactable + json.invoice do + json.id t.invoice.id + json.reference t.invoice.reference + end if t.invoice end diff --git a/db/migrate/20160720124355_add_wallet_transaction_to_invoice.rb b/db/migrate/20160720124355_add_wallet_transaction_to_invoice.rb new file mode 100644 index 000000000..b0fdac46d --- /dev/null +++ b/db/migrate/20160720124355_add_wallet_transaction_to_invoice.rb @@ -0,0 +1,5 @@ +class AddWalletTransactionToInvoice < ActiveRecord::Migration + def change + add_reference :invoices, :wallet_transaction, index: true, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 63222c2c7..a66af403b 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160718165434) do +ActiveRecord::Schema.define(version: 20160720124355) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -221,10 +221,12 @@ ActiveRecord::Schema.define(version: 20160718165434) do t.boolean "subscription_to_expire" t.text "description" t.integer "wallet_amount" + t.integer "wallet_transaction_id" end add_index "invoices", ["invoice_id"], name: "index_invoices_on_invoice_id", using: :btree add_index "invoices", ["user_id"], name: "index_invoices_on_user_id", using: :btree + add_index "invoices", ["wallet_transaction_id"], name: "index_invoices_on_wallet_transaction_id", using: :btree create_table "licences", force: :cascade do |t| t.string "name", limit: 255, null: false @@ -737,6 +739,7 @@ ActiveRecord::Schema.define(version: 20160718165434) do add_foreign_key "availability_tags", "tags" add_foreign_key "events_event_themes", "event_themes" add_foreign_key "events_event_themes", "events" + add_foreign_key "invoices", "wallet_transactions" add_foreign_key "o_auth2_mappings", "o_auth2_providers" add_foreign_key "open_api_calls_count_tracings", "open_api_clients" add_foreign_key "prices", "groups" diff --git a/test/integration/reservations/create_as_admin_test.rb b/test/integration/reservations/create_as_admin_test.rb index af3e59243..6e5589371 100644 --- a/test/integration/reservations/create_as_admin_test.rb +++ b/test/integration/reservations/create_as_admin_test.rb @@ -283,6 +283,7 @@ module Reservations assert_equal transaction.transaction_type, 'debit' assert_equal transaction.amount, 10 assert_equal transaction.amount, invoice.wallet_amount / 100.0 + assert_equal transaction.id, invoice.wallet_transaction_id end test "user without subscription and with invoicing disabled reserves a machine and pay wallet with success" do diff --git a/test/integration/subscriptions/create_as_user_test.rb b/test/integration/subscriptions/create_as_user_test.rb index 1d3907438..f9c8d32fc 100644 --- a/test/integration/subscriptions/create_as_user_test.rb +++ b/test/integration/subscriptions/create_as_user_test.rb @@ -141,5 +141,6 @@ class Subscriptions::CreateAsUserTest < ActionDispatch::IntegrationTest assert_equal transaction.transaction_type, 'debit' assert_equal transaction.amount, 10 assert_equal transaction.amount, invoice.wallet_amount / 100.0 + assert_equal transaction.id, invoice.wallet_transaction_id end end