# frozen_string_literal: true module PlanetExpress class LShipmentDiscountRule < Rule protected def applicable_to?(shipment) lp_provider_shipment?(shipment) && third_l_shipment_in_month?(shipment) && rule_not_applied_in_month_already?(shipment) end def discount_for(shipment) Discount.new(discount_amount(shipment), self) end private def lp_provider_shipment?(shipment) shipment.lp_provider? end def third_l_shipment_in_month?(shipment) provider = shipment.provider month = shipment.month count = shipment_repository.l_shipment_count_for_provider_and_month(provider, month) (count % 3) == 2 end def rule_not_applied_in_month_already?(shipment) !shipment_repository.rule_applied_in_month?(shipment.month, self.class) end def discount_amount(shipment) desired_amount = price(shipment) discount_budget(shipment).maximum_possible_discount(desired_amount) end end end