1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2025-01-18 07:52:23 +01:00

(bug) unable to set amount to 4.85

This commit is contained in:
Sylvain 2022-12-01 15:43:16 +01:00
parent 963d2e93bd
commit 7b30086a34
12 changed files with 56 additions and 29 deletions

View File

@ -1,5 +1,6 @@
# Changelog Fab-manager
- Fix a bug: setting somes decimal amounts (e.g. 4,85) result in another amount (e.g. 4,84)
- Fix a bug: unable to export statistics
- Fix a bug: soft destroyed machines and spaces are still reported in the OpenAPI

View File

@ -3,6 +3,8 @@
# API Controller for resources of type Coupon
# Coupons are used in payments
class API::CouponsController < API::ApiController
include ApplicationHelper
before_action :authenticate_user!, except: %i[validate]
before_action :set_coupon, only: %i[show update destroy]
@ -31,14 +33,13 @@ class API::CouponsController < API::ApiController
if @coupon.nil?
render json: { status: 'rejected' }, status: :not_found
else
_user_id = if current_user&.admin?
params[:user_id]
else
current_user&.id
end
user_id = if current_user&.admin?
params[:user_id]
else
current_user&.id
end
amount = params[:amount].to_f * 100.0
status = @coupon.status(_user_id, amount)
status = @coupon.status(user_id, to_centimes(params[:amount]))
if status == 'active'
render :validate, status: :ok, location: @coupon
else
@ -89,7 +90,7 @@ class API::CouponsController < API::ApiController
@parameters
else
@parameters = params
@parameters[:coupon][:amount_off] = @parameters[:coupon][:amount_off].to_f * 100.0 if @parameters[:coupon][:amount_off]
@parameters[:coupon][:amount_off] = to_centimes(@parameters[:coupon][:amount_off]) if @parameters[:coupon][:amount_off]
@parameters = @parameters.require(:coupon).permit(:name, :code, :percent_off, :amount_off, :validity_per_user, :valid_until,
:max_usages, :active)

View File

@ -4,7 +4,9 @@
# Plan are used to define subscription's characteristics.
# PartnerPlan is a special kind of plan which send notifications to an external user
class API::PlansController < API::ApiController
before_action :authenticate_user!, except: [:index, :durations]
include ApplicationHelper
before_action :authenticate_user!, except: %i[index durations]
def index
@plans = Plan.includes(:plan_file)
@ -70,10 +72,10 @@ class API::PlansController < API::ApiController
@parameters
else
@parameters = params
@parameters[:plan][:amount] = @parameters[:plan][:amount].to_f * 100.0 if @parameters[:plan][:amount]
@parameters[:plan][:amount] = to_centimes(@parameters[:plan][:amount]) if @parameters[:plan][:amount]
if @parameters[:plan][:prices_attributes]
@parameters[:plan][:prices_attributes] = @parameters[:plan][:prices_attributes].map do |price|
{ amount: price[:amount].to_f * 100.0, id: price[:id] }
{ amount: to_centimes(price[:amount]), id: price[:id] }
end
end

View File

@ -3,6 +3,8 @@
# API Controller for resources of type PrepaidPack
# PrepaidPacks are used to provide discounts to users that bought many hours at once
class API::PrepaidPacksController < API::ApiController
include ApplicationHelper
before_action :authenticate_user!, except: :index
before_action :set_pack, only: %i[show update destroy]
@ -46,7 +48,7 @@ class API::PrepaidPacksController < API::ApiController
def pack_params
pack_params = params
pack_params[:pack][:amount] = pack_params[:pack][:amount].to_f * 100.0 if pack_params[:pack][:amount]
pack_params[:pack][:amount] = to_centimes(pack_params[:pack][:amount]) if pack_params[:pack][:amount]
params.require(:pack).permit(:priceable_id, :priceable_type, :group_id, :amount, :minutes, :validity_count, :validity_interval,
:disabled)
end

View File

@ -3,12 +3,14 @@
# API Controller for resources of type Price
# Prices are used in reservations (Machine, Space)
class API::PricesController < API::ApiController
include ApplicationHelper
before_action :authenticate_user!
before_action :set_price, only: %i[update destroy]
def create
@price = Price.new(price_create_params)
@price.amount *= 100
@price.amount = to_centimes(price_create_params[:amount])
authorize @price
@ -26,7 +28,7 @@ class API::PricesController < API::ApiController
def update
authorize Price
price_parameters = price_params
price_parameters[:amount] = price_parameters[:amount] * 100
price_parameters[:amount] = to_centimes(price_parameters[:amount])
if @price.update(price_parameters)
render status: :ok
else

View File

@ -1,8 +1,12 @@
# frozen_string_literal: true
# @deprecated
# <b>DEPRECATED:</b> Please use <tt>API::PriceController</tt> instead.
# API Controller for managing Plans prices
class API::PricingController < API::ApiController
before_action :authenticate_user!, except: %i[index show]
include ApplicationHelper
before_action :authenticate_user!, except: %i[index update]
def index
@group_pricing = Group.includes(:plans, :trainings_pricings)
@ -19,10 +23,10 @@ class API::PricingController < API::ApiController
next unless group
training_pricing = group.trainings_pricings.find_or_initialize_by(training_id: training.id)
training_pricing.amount = amount * 100
training_pricing.amount = to_centimes(amount)
training_pricing.save
end
end
head 200
head :ok
end
end

View File

@ -1,7 +1,11 @@
# frozen_string_literal: true
# @deprecated
# <b>DEPRECATED:</b> Please use <tt>API::PriceController</tt> instead.
# API Controller for managing Training prices
class API::TrainingsPricingsController < API::ApiController
include ApplicationHelper
before_action :authenticate_user!
def index
@ -12,14 +16,14 @@ class API::TrainingsPricingsController < API::ApiController
if current_user.admin?
@trainings_pricing = TrainingsPricing.find(params[:id])
trainings_pricing_parameters = trainings_pricing_params
trainings_pricing_parameters[:amount] = trainings_pricing_parameters[:amount] * 100
trainings_pricing_parameters[:amount] = to_centimes(trainings_pricing_parameters[:amount])
if @trainings_pricing.update(trainings_pricing_parameters)
render status: :ok
else
render status: :unprocessable_entity
end
else
head 403
head :forbidden
end
end

View File

@ -14,7 +14,7 @@ class API::WalletController < API::ApiController
def transactions
@wallet = Wallet.find(params[:id])
authorize @wallet
@wallet_transactions = @wallet.wallet_transactions.includes(:invoice, :invoicing_profile).order(created_at: :desc)
@wallet_transactions = @wallet.wallet_transactions.includes(:invoice, :invoicing_profile, :payment_schedule).order(created_at: :desc)
end
def credit
@ -23,7 +23,7 @@ class API::WalletController < API::ApiController
@wallet = Wallet.find(credit_params[:id])
authorize @wallet
service = WalletService.new(user: current_user, wallet: @wallet)
transaction = service.credit(credit_params[:amount].to_f)
transaction = service.credit(credit_params[:amount])
if transaction
service.create_avoir(transaction, credit_params[:avoir_date], credit_params[:avoir_description]) if credit_params[:avoir]
render :show

View File

@ -78,6 +78,11 @@ module ApplicationHelper
res
end
# Return the given amount in centimes, without floating-point imprecision errors
def to_centimes(amount)
(BigDecimal(amount.to_s) * 100.0).to_f
end
private
## inspired by gems/actionview-4.2.5/lib/action_view/helpers/translation_helper.rb

View File

@ -5,13 +5,14 @@ module AmountConcern
extend ActiveSupport::Concern
included do
validates_numericality_of :amount, greater_than_or_equal_to: 0
include ApplicationHelper
validates :amount, numericality: { greater_than_or_equal_to: 0 }
def amount=(amount)
if amount.nil?
write_attribute(:amount, amount)
else
write_attribute(:amount, (amount * 100).to_i)
write_attribute(:amount, to_centimes(amount))
end
end

View File

@ -1,8 +1,12 @@
# frozen_string_literal: true
require './app/helpers/application_helper'
# Provides helper methods for Events resources and properties
class EventService
class << self
include ApplicationHelper
def process_params(params)
# handle dates & times (whole-day events or not, maybe during many days)
range = EventService.date_range({ date: params[:start_date], time: params[:start_time] },
@ -14,14 +18,14 @@ class EventService
available_type: 'event' })
.extract!(:start_date, :end_date, :start_time, :end_time, :all_day)
# convert main price to centimes
params[:amount] = (params[:amount].to_f * 100 if params[:amount].present?)
params[:amount] = to_centimes(params[:amount]) if params[:amount].present?
# delete non-complete "other" prices and convert them to centimes
unless params[:event_price_categories_attributes].nil?
params[:event_price_categories_attributes].delete_if do |price_cat|
price_cat[:price_category_id].empty? || price_cat[:amount].empty?
end
params[:event_price_categories_attributes].each do |price_cat|
price_cat[:amount] = price_cat[:amount].to_f * 100
price_cat[:amount] = to_centimes(price_cat[:amount])
end
end
# return the resulting params object

View File

@ -1,8 +1,12 @@
# frozen_string_literal: true
require './app/helpers/application_helper'
# Provides methods for Product
class ProductService
class << self
include ApplicationHelper
PRODUCTS_PER_PAGE = 12
MOVEMENTS_PER_PAGE = 10
@ -28,11 +32,8 @@ class ProductService
# amount params multiplied by hundred
def amount_multiplied_by_hundred(amount)
if amount.present?
v = amount.to_f
return to_centimes(amount) if amount.present?
return v * 100
end
nil
end