mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-02-07 01:54:16 +01:00
(feat) machine limit override category limit
This commit is contained in:
parent
0ac99e3b6b
commit
8a32c029d3
@ -173,7 +173,7 @@ export const PlanLimitForm = <TContext extends object> ({ register, control, for
|
|||||||
|
|
||||||
{fields.filter(f => f.limitable_type === 'MachineCategory' && !f._modified).length > 0 &&
|
{fields.filter(f => f.limitable_type === 'MachineCategory' && !f._modified).length > 0 &&
|
||||||
<div className='plan-limit-list'>
|
<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) => {
|
{fields.map((limitation, index) => {
|
||||||
if (limitation.limitable_type !== 'MachineCategory' || limitation._modified) return false;
|
if (limitation.limitable_type !== 'MachineCategory' || limitation._modified) return false;
|
||||||
|
|
||||||
|
@ -91,7 +91,7 @@ export const PlanLimitModal: React.FC<PlanLimitModalProps> = ({ isOpen, toggleMo
|
|||||||
<button onClick={evt => toggleLimitType(evt, 'MachineCategory')}
|
<button onClick={evt => toggleLimitType(evt, 'MachineCategory')}
|
||||||
className={limitType === 'MachineCategory' ? 'is-active' : ''}
|
className={limitType === 'MachineCategory' ? 'is-active' : ''}
|
||||||
disabled={!!limitation}>
|
disabled={!!limitation}>
|
||||||
{t('app.admin.plan_limit_modal.by_categories')}
|
{t('app.admin.plan_limit_modal.by_category')}
|
||||||
</button>
|
</button>
|
||||||
<button onClick={evt => toggleLimitType(evt, 'Machine')}
|
<button onClick={evt => toggleLimitType(evt, 'Machine')}
|
||||||
className={limitType === 'Machine' ? 'is-active' : ''}
|
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')}
|
{t('app.admin.plan_limit_modal.by_machine')}
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</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="id" type="hidden" />
|
||||||
<FormInput register={register} id="limitable_type" type="hidden" />
|
<FormInput register={register} id="limitable_type" type="hidden" />
|
||||||
<FormSelect options={buildOptions()}
|
<FormSelect options={buildOptions()}
|
||||||
|
@ -13,12 +13,13 @@ class ReservationLimitService
|
|||||||
|
|
||||||
return true if reservation.nil? || !reservation.is_a?(CartItem::Reservation)
|
return true if reservation.nil? || !reservation.is_a?(CartItem::Reservation)
|
||||||
|
|
||||||
plan.plan_limitations.filter { |limit| limit.reservables.include?(reservation.reservable) }.each do |limit|
|
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|
|
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) +
|
daily_duration = reservations_duration(customer, date, reservation, cart_items) +
|
||||||
(reservation_slots.map { |sr| sr.slot.duration }.reduce(:+) || 0)
|
(reservation_slots.map { |sr| sr.slot.duration }.reduce(:+) || 0)
|
||||||
return false if Rational(daily_duration / 3600).to_f > limit.limit
|
return false if Rational(daily_duration / 3600).to_f > limit
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
true
|
true
|
||||||
@ -30,7 +31,8 @@ class ReservationLimitService
|
|||||||
def limit(plan, reservable)
|
def limit(plan, reservable)
|
||||||
return nil unless plan&.limiting
|
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
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
@ -201,7 +201,7 @@ en:
|
|||||||
usage_limitation_switch: "Restrict machine reservations to a number of hours per day."
|
usage_limitation_switch: "Restrict machine reservations to a number of hours per day."
|
||||||
new_usage_limitation: "Add a limitation of use"
|
new_usage_limitation: "Add a limitation of use"
|
||||||
all_limitations: "All limitations"
|
all_limitations: "All limitations"
|
||||||
by_categories: "By machines categories"
|
by_category: "By machine category"
|
||||||
by_machine: "By machine"
|
by_machine: "By machine"
|
||||||
category: "Machines category"
|
category: "Machines category"
|
||||||
machine: "Machine name"
|
machine: "Machine name"
|
||||||
@ -215,12 +215,12 @@ en:
|
|||||||
plan_limit_modal:
|
plan_limit_modal:
|
||||||
title: "Manage limitation of use"
|
title: "Manage limitation of use"
|
||||||
limit_reservations: "Limit reservations"
|
limit_reservations: "Limit reservations"
|
||||||
by_categories: "By machines categories"
|
by_category: "By machines category"
|
||||||
by_machine: "By machine"
|
by_machine: "By machine"
|
||||||
category: "Machines category"
|
category: "Machines category"
|
||||||
machine: "Machine name"
|
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."
|
categories_info: "If you select all machine categories, the limits will apply across the board."
|
||||||
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."
|
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"
|
max_hours_per_day: "Maximum number of reservation hours per day"
|
||||||
confirm: "Confirm"
|
confirm: "Confirm"
|
||||||
partner_modal:
|
partner_modal:
|
||||||
|
@ -124,4 +124,18 @@ class ReservationLimitServiceTest < ActiveSupport::TestCase
|
|||||||
test 'get plan without limit' do
|
test 'get plan without limit' do
|
||||||
assert_nil ReservationLimitService.limit(@plan, @machine)
|
assert_nil ReservationLimitService.limit(@plan, @machine)
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
x
Reference in New Issue
Block a user