PFM63 pricing

This commit is contained in:
2025-06-16 13:01:59 +02:00
parent b422c8adc3
commit 53d3a7f51f
6 changed files with 421 additions and 67 deletions

View File

@@ -1,9 +1,13 @@
package pricing
import "fmt"
import (
"fmt"
"github.com/paulmach/orb/geojson"
)
type PricingService interface {
Prices(vars map[string]any) (map[string]Price, error)
Prices(params PricingParams) (map[string]Price, error)
}
func NewPricingService(implementation string) (PricingService, error) {
@@ -17,3 +21,32 @@ func NewPricingService(implementation string) (PricingService, error) {
}
}
type PricingParams struct {
MobilityType string
SharedMobility SharedMobilityParams
Beneficiary BeneficiaryParams
Data map[string]any // other data (specific)
}
type SharedMobilityParams struct {
DriverDistance int64
PassengerDistance int64
Departure GeographyParams
Destination GeographyParams
OutwardOnly bool
}
type BeneficiaryParams struct {
Address GeographyParams
Priority bool
History int
}
type GeographyParams struct {
Location *geojson.Feature
CityCode string // INSEE code
IntercommunalityCode string // SIRENnumber
RegionCode string
DepartmentCode string
}

View File

@@ -1,34 +1,19 @@
package pricing
import (
"fmt"
"github.com/manterfield/go-mapreader"
)
type MMS43PricingService struct{}
func NewMMS43PricingService() (*MMS43PricingService, error) {
return &MMS43PricingService{}, nil
}
func (s *MMS43PricingService) Prices(vars map[string]any) (map[string]Price, error) {
driver_distance, err := mapreader.IntErr(vars, "journey.driver_distance")
if err != nil {
return nil, fmt.Errorf("parameter not found : %v", err)
}
passenger_distance, err := mapreader.IntErr(vars, "journey.passenger_distance")
if err != nil {
return nil, fmt.Errorf("parameter not found : %v", err)
}
func (s *MMS43PricingService) Prices(params PricingParams) (map[string]Price, error) {
return map[string]Price{
"passenger": {
Amount: 0.32 * float64(passenger_distance),
Amount: 0.32 * float64(params.SharedMobility.PassengerDistance),
Currency: "EUR/2",
},
"driver": {
Amount: 0.32 * float64(driver_distance),
Amount: 0.32 * float64(params.SharedMobility.DriverDistance),
Currency: "EUR/2",
},
}, nil

View File

@@ -1,66 +1,54 @@
package pricing
import (
"fmt"
"slices"
"github.com/manterfield/go-mapreader"
"github.com/rs/zerolog/log"
)
const (
PFM63_CC_CHAVAILLON_COMBAILLES_VOLCANS = "200071215"
PFM63_CC_DOMES_SANCY_ARTENSE = "200069169"
PFM63_CC_AMBERT_LIVRADOIS_FOREZ = "200070761"
PFM63_COM_CLERMONT = "63113"
PFM63_COM_USSEL = "19275"
PFM63_COM_AUBUSSON = "23008"
PFM63_COM_RIOM = "63300"
)
type PFM63PricingService struct{}
func NewPFM63PricingService() (*PFM63PricingService, error) {
return &PFM63PricingService{}, nil
}
func (s *PFM63PricingService) Prices(vars map[string]any) (map[string]Price, error) {
journey_type, err := mapreader.StrErr(vars, "journey_type")
if err != nil {
return nil, fmt.Errorf("journey_type parameter not found : %v", err)
}
fmt.Println(journey_type)
passenger_priority, err := mapreader.BoolErr(vars, "passenger.priority")
if err != nil {
return nil, fmt.Errorf("passenger_priority parameter not found : %v", err)
}
ret, err := mapreader.BoolErr(vars, "return")
if err != nil {
return nil, fmt.Errorf("passenger_priority parameter not found : %v", err)
}
passenger_history, err := mapreader.IntErr(vars, "passenger.history")
if err != nil {
return nil, fmt.Errorf("passenger_history parameter not found : %v", err)
}
destination_locality, err := mapreader.StrErr(vars, "journey.passenger_drop.properties.locality")
if err != nil {
return nil, fmt.Errorf("destination_locality parameter not found : %v", err)
}
// passenger_locality, err := mapreader.StrErr(vars, "passenger.locality")
// if err != nil {
// return nil, fmt.Errorf("destination_locality parameter not found : %v", err)
// }
driver_distance, err := mapreader.IntErr(vars, "journey.driver_distance")
if err != nil {
return nil, fmt.Errorf("driver_distance parameter not found : %v", err)
}
passenger_distance, err := mapreader.IntErr(vars, "journey.passenger_distance")
if err != nil {
return nil, fmt.Errorf("passenger_distance parameter not found : %v", err)
func (s *PFM63PricingService) Prices(params PricingParams) (map[string]Price, error) {
log.Debug().Any("params", params).Msg("get price")
free_trips := 0
fixedCityPrices := []string{}
switch params.Beneficiary.Address.IntercommunalityCode {
case PFM63_CC_CHAVAILLON_COMBAILLES_VOLCANS:
fixedCityPrices = []string{PFM63_COM_CLERMONT, PFM63_COM_AUBUSSON, PFM63_COM_RIOM, PFM63_COM_USSEL}
case PFM63_CC_DOMES_SANCY_ARTENSE:
fixedCityPrices = []string{PFM63_COM_CLERMONT, PFM63_COM_USSEL}
case PFM63_CC_AMBERT_LIVRADOIS_FOREZ:
free_trips = 1
}
passenger_distance := params.SharedMobility.PassengerDistance
pricing := 0.0
log.Debug().Str("destination loc", destination_locality).Msg("debuf pricing")
if passenger_history == 0 {
if params.Beneficiary.History < free_trips {
pricing = 0
} else if passenger_priority && passenger_distance <= 5 {
} else if params.Beneficiary.Priority && params.Beneficiary.History <= 5 {
pricing = 0
} else if slices.Contains([]string{"Clermont-Ferrand", "Ussel", "Aubusson", "Riom"}, destination_locality) {
if ret {
pricing = 10
} else {
} else if slices.Contains(fixedCityPrices, params.SharedMobility.Destination.CityCode) {
if params.SharedMobility.OutwardOnly {
pricing = 5
} else {
pricing = 10
}
} else if passenger_distance < 15 {
pricing = 2
@@ -80,12 +68,13 @@ func (s *PFM63PricingService) Prices(vars map[string]any) (map[string]Price, err
pricing = 15
} else if passenger_distance < 140 {
pricing = 17
} else {
pricing = 17
log.Error().Msg("no pricing case found")
}
fmt.Println(pricing)
kmCompensation := 0.2
if journey_type == "carpool" {
kmCompensation := 0.22
if params.MobilityType == "carpool" {
kmCompensation = 0.09
}
@@ -95,7 +84,7 @@ func (s *PFM63PricingService) Prices(vars map[string]any) (map[string]Price, err
Currency: "EUR/2",
},
"driver": {
Amount: kmCompensation * float64(driver_distance),
Amount: kmCompensation * float64(params.SharedMobility.DriverDistance),
Currency: "EUR/2",
},
}, nil

View File

@@ -0,0 +1,248 @@
package pricing
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestPrices(t *testing.T) {
testCases := []struct {
name string
params PricingParams
expectedPrices map[string]Price
expectedError bool
}{
{
name: "Chavallon Combailes Volcans, Clermont destination, outward only",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: PFM63_CC_CHAVAILLON_COMBAILLES_VOLCANS,
},
History: 0,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 10,
Destination: GeographyParams{CityCode: PFM63_COM_CLERMONT},
OutwardOnly: true,
DriverDistance: 10,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 5, Currency: "EUR/2"},
"driver": {Amount: 2.2, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Chavallon Combailes Volcans, Aubusson destination, outward only",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: PFM63_CC_CHAVAILLON_COMBAILLES_VOLCANS,
},
History: 0,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 10,
Destination: GeographyParams{CityCode: PFM63_COM_AUBUSSON},
OutwardOnly: true,
DriverDistance: 10,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 5, Currency: "EUR/2"},
"driver": {Amount: 2.2, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Domes Sancy Artense, Ussel destination, round trip",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: PFM63_CC_DOMES_SANCY_ARTENSE,
},
History: 0,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 10,
Destination: GeographyParams{CityCode: PFM63_COM_USSEL},
OutwardOnly: false,
DriverDistance: 10,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 10, Currency: "EUR/2"},
"driver": {Amount: 2.2, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Ambert Livradois Artense, Clermont destination, round trip",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: PFM63_CC_AMBERT_LIVRADOIS_FOREZ,
},
History: 10,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 150,
Destination: GeographyParams{CityCode: PFM63_COM_CLERMONT},
OutwardOnly: true,
DriverDistance: 200,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 17, Currency: "EUR/2"},
"driver": {Amount: 44, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Ambert Livradois Forez, free trip",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: PFM63_CC_AMBERT_LIVRADOIS_FOREZ,
},
History: 0,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 10,
Destination: GeographyParams{CityCode: "63113"},
OutwardOnly: true,
DriverDistance: 10,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 0, Currency: "EUR/2"},
"driver": {Amount: 2.2, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Priority beneficiary, history <= 5",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: "unknown",
},
History: 3,
Priority: true,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 10,
Destination: GeographyParams{CityCode: "63113"},
OutwardOnly: true,
DriverDistance: 10,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 0, Currency: "EUR/2"},
"driver": {Amount: 2.2, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Passenger distance < 15",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: "unknown",
},
History: 6,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 10,
Destination: GeographyParams{CityCode: "63113"},
OutwardOnly: true,
DriverDistance: 10,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 2, Currency: "EUR/2"},
"driver": {Amount: 2.2, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Passenger distance < 30",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: "unknown",
},
History: 6,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 20,
Destination: GeographyParams{CityCode: "63113"},
OutwardOnly: true,
DriverDistance: 20,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 4, Currency: "EUR/2"},
"driver": {Amount: 4.4, Currency: "EUR/2"},
},
expectedError: false,
},
{
name: "Passenger distance > 140",
params: PricingParams{
Beneficiary: BeneficiaryParams{
Address: GeographyParams{
IntercommunalityCode: "unknown",
},
History: 6,
Priority: false,
},
SharedMobility: SharedMobilityParams{
PassengerDistance: 150,
Destination: GeographyParams{CityCode: "63113"},
OutwardOnly: true,
DriverDistance: 200,
},
MobilityType: "solidarity_transport",
},
expectedPrices: map[string]Price{
"passenger": {Amount: 17, Currency: "EUR/2"},
"driver": {Amount: 44.0, Currency: "EUR/2"},
},
expectedError: false,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
pricingService, err := NewPFM63PricingService()
assert.NoError(t, err)
prices, err := pricingService.Prices(tc.params)
if tc.expectedError {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedPrices, prices)
}
})
}
}