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

(feat) machine limit override category limit

This commit is contained in:
Sylvain 2023-03-14 12:35:58 +01:00
parent 0ac99e3b6b
commit 8a32c029d3
5 changed files with 30 additions and 14 deletions

View File

@ -173,7 +173,7 @@ export const PlanLimitForm = <TContext extends object> ({ register, control, for
{fields.filter(f => f.limitable_type === 'MachineCategory' && !f._modified).length > 0 &&
<div className='plan-limit-list'>
<p className="title">{t('app.admin.plan_limit_form.by_categories')}</p>
<p className="title">{t('app.admin.plan_limit_form.by_category')}</p>
{fields.map((limitation, index) => {
if (limitation.limitable_type !== 'MachineCategory' || limitation._modified) return false;

View File

@ -91,7 +91,7 @@ export const PlanLimitModal: React.FC<PlanLimitModalProps> = ({ isOpen, toggleMo
<button onClick={evt => toggleLimitType(evt, 'MachineCategory')}
className={limitType === 'MachineCategory' ? 'is-active' : ''}
disabled={!!limitation}>
{t('app.admin.plan_limit_modal.by_categories')}
{t('app.admin.plan_limit_modal.by_category')}
</button>
<button onClick={evt => toggleLimitType(evt, 'Machine')}
className={limitType === 'Machine' ? 'is-active' : ''}
@ -99,7 +99,7 @@ export const PlanLimitModal: React.FC<PlanLimitModalProps> = ({ isOpen, toggleMo
{t('app.admin.plan_limit_modal.by_machine')}
</button>
</div>
<FabAlert level='info'>{t('app.admin.plan_limit_modal.machine_info')}</FabAlert>
<FabAlert level='info'>{limitType === 'Machine' ? t('app.admin.plan_limit_modal.machine_info') : t('app.admin.plan_limit_modal.categories_info')}</FabAlert>
<FormInput register={register} id="id" type="hidden" />
<FormInput register={register} id="limitable_type" type="hidden" />
<FormSelect options={buildOptions()}

View File

@ -13,12 +13,13 @@ class ReservationLimitService
return true if reservation.nil? || !reservation.is_a?(CartItem::Reservation)
plan.plan_limitations.filter { |limit| limit.reservables.include?(reservation.reservable) }.each do |limit|
reservation.cart_item_reservation_slots.group_by { |sr| sr.slot.start_at.to_date }.each_pair do |date, reservation_slots|
daily_duration = reservations_duration(customer, date, reservation, cart_items) +
(reservation_slots.map { |sr| sr.slot.duration }.reduce(:+) || 0)
return false if Rational(daily_duration / 3600).to_f > limit.limit
end
limit = limit(plan, reservation.reservable)
return true if limit.nil?
reservation.cart_item_reservation_slots.group_by { |sr| sr.slot.start_at.to_date }.each_pair do |date, reservation_slots|
daily_duration = reservations_duration(customer, date, reservation, cart_items) +
(reservation_slots.map { |sr| sr.slot.duration }.reduce(:+) || 0)
return false if Rational(daily_duration / 3600).to_f > limit
end
true
@ -30,7 +31,8 @@ class ReservationLimitService
def limit(plan, reservable)
return nil unless plan&.limiting
plan&.plan_limitations&.find { |limit| limit.reservables.include?(reservable) }&.limit
limitations = plan&.plan_limitations&.filter { |limit| limit.reservables.include?(reservable) }
limitations&.find { |limit| limit.limitable_type != 'MachineCategory' }&.limit || limitations&.first&.limit
end
private

View File

@ -201,7 +201,7 @@ en:
usage_limitation_switch: "Restrict machine reservations to a number of hours per day."
new_usage_limitation: "Add a limitation of use"
all_limitations: "All limitations"
by_categories: "By machines categories"
by_category: "By machine category"
by_machine: "By machine"
category: "Machines category"
machine: "Machine name"
@ -215,12 +215,12 @@ en:
plan_limit_modal:
title: "Manage limitation of use"
limit_reservations: "Limit reservations"
by_categories: "By machines categories"
by_category: "By machines category"
by_machine: "By machine"
category: "Machines category"
machine: "Machine name"
categories_info: "If you select all machine categories, the limits will apply across the board. Please note that if you have already created limitations for specific categories, these will be permanently overwritten."
machine_info: "If you select all machines, the limits will apply across the board. Please note that if you have already created limitations for machines, these will be permanently overwritten."
categories_info: "If you select all machine categories, the limits will apply across the board."
machine_info: "Please note that if you have already created a limitation for the machines category including the selected machine, it will be permanently overwritten."
max_hours_per_day: "Maximum number of reservation hours per day"
confirm: "Confirm"
partner_modal:

View File

@ -124,4 +124,18 @@ class ReservationLimitServiceTest < ActiveSupport::TestCase
test 'get plan without limit' do
assert_nil ReservationLimitService.limit(@plan, @machine)
end
test 'get category limit' do
category = MachineCategory.find(1)
category.update(machine_ids: [@machine.id])
@plan.update(limiting: true, plan_limitations_attributes: [{ limitable: category, limit: 4 }])
assert_equal 4, ReservationLimitService.limit(@plan, @machine)
end
test 'machine limit should override the category limit' do
category = MachineCategory.find(1)
category.update(machine_ids: [@machine.id])
@plan.update(limiting: true, plan_limitations_attributes: [{ limitable: @machine, limit: 2 }, { limitable: category, limit: 4 }])
assert_equal 2, ReservationLimitService.limit(@plan, @machine)
end
end