-
Notifications
You must be signed in to change notification settings - Fork 38
/
ppm_estimator.go
506 lines (424 loc) · 22.6 KB
/
ppm_estimator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
package ppmshipment
import (
"fmt"
"time"
"github.com/gofrs/uuid"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/models"
paymentrequesthelper "github.com/transcom/mymove/pkg/payment_request"
serviceparamvaluelookups "github.com/transcom/mymove/pkg/payment_request/service_param_value_lookups"
"github.com/transcom/mymove/pkg/route"
"github.com/transcom/mymove/pkg/services"
"github.com/transcom/mymove/pkg/services/ghcrateengine"
"github.com/transcom/mymove/pkg/unit"
)
// estimatePPM Struct
type estimatePPM struct {
checks []ppmShipmentValidator
planner route.Planner
paymentRequestHelper paymentrequesthelper.Helper
}
// NewEstimatePPM returns the estimatePPM (pass in checkRequiredFields() and checkEstimatedWeight)
func NewEstimatePPM(planner route.Planner, paymentRequestHelper paymentrequesthelper.Helper) services.PPMEstimator {
return &estimatePPM{
checks: []ppmShipmentValidator{
checkRequiredFields(),
checkEstimatedWeight(),
checkSITRequiredFields(),
},
planner: planner,
paymentRequestHelper: paymentRequestHelper,
}
}
// EstimateIncentiveWithDefaultChecks func that returns the estimate hard coded to 12K (because it'll be clear that the value is coming from the service)
func (f *estimatePPM) EstimateIncentiveWithDefaultChecks(appCtx appcontext.AppContext, oldPPMShipment models.PPMShipment, newPPMShipment *models.PPMShipment) (*unit.Cents, *unit.Cents, error) {
return f.estimateIncentive(appCtx, oldPPMShipment, newPPMShipment, f.checks...)
}
func (f *estimatePPM) FinalIncentiveWithDefaultChecks(appCtx appcontext.AppContext, oldPPMShipment models.PPMShipment, newPPMShipment *models.PPMShipment) (*unit.Cents, error) {
return f.finalIncentive(appCtx, oldPPMShipment, newPPMShipment, f.checks...)
}
func shouldSkipEstimatingIncentive(newPPMShipment *models.PPMShipment, oldPPMShipment *models.PPMShipment) bool {
return oldPPMShipment.ExpectedDepartureDate.Equal(newPPMShipment.ExpectedDepartureDate) &&
newPPMShipment.PickupPostalCode == oldPPMShipment.PickupPostalCode &&
newPPMShipment.DestinationPostalCode == oldPPMShipment.DestinationPostalCode &&
((newPPMShipment.EstimatedWeight == nil && oldPPMShipment.EstimatedWeight == nil) || (oldPPMShipment.EstimatedWeight != nil && newPPMShipment.EstimatedWeight.Int() == oldPPMShipment.EstimatedWeight.Int()))
}
func shouldSkipCalculatingFinalIncentive(newPPMShipment *models.PPMShipment, oldPPMShipment *models.PPMShipment, originalTotalWeight unit.Pound, newTotalWeight unit.Pound) bool {
// If oldPPMShipment field value is nil we know that the value has been updated and we should return false - the adjusted net weight is accounted for in the
// SumWeightTickets function and the change in weight is then checked with `newTotalWeight == originalTotalWeight`
return (oldPPMShipment.ActualMoveDate != nil && newPPMShipment.ActualMoveDate.Equal(*oldPPMShipment.ActualMoveDate)) &&
(oldPPMShipment.ActualPickupPostalCode != nil && *newPPMShipment.ActualPickupPostalCode == *oldPPMShipment.ActualPickupPostalCode) &&
(oldPPMShipment.ActualDestinationPostalCode != nil && *newPPMShipment.ActualDestinationPostalCode == *oldPPMShipment.ActualDestinationPostalCode) &&
newTotalWeight == originalTotalWeight
}
func shouldSetFinalIncentiveToNil(newPPMShipment *models.PPMShipment, newTotalWeight unit.Pound) bool {
if newPPMShipment.ActualMoveDate == nil || newPPMShipment.ActualPickupPostalCode == nil || newPPMShipment.ActualDestinationPostalCode == nil || newTotalWeight <= 0 {
return true
}
return false
}
func shouldCalculateSITCost(newPPMShipment *models.PPMShipment, oldPPMShipment *models.PPMShipment) bool {
// storage has not been selected yet or storage is not needed
if newPPMShipment.SITExpected == nil || !*newPPMShipment.SITExpected {
return false
}
// the service member could request storage but it can't be calculated until the services counselor provides info
if newPPMShipment.SITLocation == nil {
return false
}
// storage inputs weren't previously saved so we're calculating the cost for the first time
if oldPPMShipment.SITLocation == nil ||
oldPPMShipment.SITEstimatedWeight == nil ||
oldPPMShipment.SITEstimatedEntryDate == nil ||
oldPPMShipment.SITEstimatedDepartureDate == nil {
return true
}
// the shipment had a previous storage cost but some of the inputs have changed, including some of the shipment
// locations or departure date
return *newPPMShipment.SITLocation != *oldPPMShipment.SITLocation ||
*newPPMShipment.SITEstimatedWeight != *oldPPMShipment.SITEstimatedWeight ||
*newPPMShipment.SITEstimatedEntryDate != *oldPPMShipment.SITEstimatedEntryDate ||
*newPPMShipment.SITEstimatedDepartureDate != *oldPPMShipment.SITEstimatedDepartureDate ||
newPPMShipment.PickupPostalCode != oldPPMShipment.PickupPostalCode ||
newPPMShipment.DestinationPostalCode != oldPPMShipment.DestinationPostalCode ||
newPPMShipment.ExpectedDepartureDate != oldPPMShipment.ExpectedDepartureDate
}
func (f *estimatePPM) estimateIncentive(appCtx appcontext.AppContext, oldPPMShipment models.PPMShipment, newPPMShipment *models.PPMShipment, checks ...ppmShipmentValidator) (*unit.Cents, *unit.Cents, error) {
if newPPMShipment.Status != models.PPMShipmentStatusDraft && newPPMShipment.Status != models.PPMShipmentStatusSubmitted {
return oldPPMShipment.EstimatedIncentive, oldPPMShipment.SITEstimatedCost, nil
}
// Check that all the required fields we need are present.
err := validatePPMShipment(appCtx, *newPPMShipment, &oldPPMShipment, &oldPPMShipment.Shipment, checks...)
// If a field does not pass validation return nil as error handling is happening in the validator
if err != nil {
switch err.(type) {
case apperror.InvalidInputError:
return nil, nil, nil
default:
return nil, nil, err
}
}
calculateSITEstimate := shouldCalculateSITCost(newPPMShipment, &oldPPMShipment)
// Clear out any previously calculated SIT estimated costs, if SIT is no longer expected
if newPPMShipment.SITExpected != nil && !*newPPMShipment.SITExpected {
newPPMShipment.SITEstimatedCost = nil
}
skipCalculatingEstimatedIncentive := shouldSkipEstimatingIncentive(newPPMShipment, &oldPPMShipment)
if skipCalculatingEstimatedIncentive && !calculateSITEstimate {
return oldPPMShipment.EstimatedIncentive, newPPMShipment.SITEstimatedCost, nil
}
contractDate := newPPMShipment.ExpectedDepartureDate
contract, err := serviceparamvaluelookups.FetchContract(appCtx, contractDate)
if err != nil {
return nil, nil, err
}
estimatedIncentive := oldPPMShipment.EstimatedIncentive
if !skipCalculatingEstimatedIncentive {
// Clear out advance and advance requested fields when the estimated incentive is reset.
newPPMShipment.HasRequestedAdvance = nil
newPPMShipment.AdvanceAmountRequested = nil
estimatedIncentive, err = f.calculatePrice(appCtx, newPPMShipment, 0, contract)
if err != nil {
return nil, nil, err
}
}
estimatedSITCost := oldPPMShipment.SITEstimatedCost
if calculateSITEstimate {
estimatedSITCost, err = CalculateSITCost(appCtx, newPPMShipment, contract)
if err != nil {
return nil, nil, err
}
}
return estimatedIncentive, estimatedSITCost, nil
}
func (f *estimatePPM) finalIncentive(appCtx appcontext.AppContext, oldPPMShipment models.PPMShipment, newPPMShipment *models.PPMShipment, checks ...ppmShipmentValidator) (*unit.Cents, error) {
if newPPMShipment.Status != models.PPMShipmentStatusWaitingOnCustomer && newPPMShipment.Status != models.PPMShipmentStatusNeedsPaymentApproval {
return oldPPMShipment.FinalIncentive, nil
}
err := validatePPMShipment(appCtx, *newPPMShipment, &oldPPMShipment, &oldPPMShipment.Shipment, checks...)
if err != nil {
switch err.(type) {
case apperror.InvalidInputError:
return nil, err
default:
return nil, err
}
}
originalTotalWeight, newTotalWeight := SumWeightTickets(oldPPMShipment, *newPPMShipment)
isMissingInfo := shouldSetFinalIncentiveToNil(newPPMShipment, newTotalWeight)
var skipCalculateFinalIncentive bool
finalIncentive := oldPPMShipment.FinalIncentive
if !isMissingInfo {
skipCalculateFinalIncentive = shouldSkipCalculatingFinalIncentive(newPPMShipment, &oldPPMShipment, originalTotalWeight, newTotalWeight)
if !skipCalculateFinalIncentive {
contractDate := newPPMShipment.ExpectedDepartureDate
if newPPMShipment.ActualMoveDate != nil {
contractDate = *newPPMShipment.ActualMoveDate
}
contract, err := serviceparamvaluelookups.FetchContract(appCtx, contractDate)
if err != nil {
return nil, err
}
finalIncentive, err = f.calculatePrice(appCtx, newPPMShipment, newTotalWeight, contract)
if err != nil {
return nil, err
}
}
} else {
finalIncentive = nil
}
return finalIncentive, nil
}
// SumWeightTickets return the total weight of all weightTickets associated with a PPMShipment, returns 0 if there is no valid weight
func SumWeightTickets(ppmShipment, newPPMShipment models.PPMShipment) (originalTotalWeight, newTotalWeight unit.Pound) {
if len(ppmShipment.WeightTickets) >= 1 {
for _, weightTicket := range ppmShipment.WeightTickets {
if weightTicket.Status != nil && *weightTicket.Status == models.PPMDocumentStatusRejected {
originalTotalWeight += 0
} else if weightTicket.AdjustedNetWeight != nil {
originalTotalWeight += *weightTicket.AdjustedNetWeight
} else if weightTicket.FullWeight != nil && weightTicket.EmptyWeight != nil {
originalTotalWeight += *weightTicket.FullWeight - *weightTicket.EmptyWeight
}
}
}
if len(newPPMShipment.WeightTickets) >= 1 {
for _, weightTicket := range newPPMShipment.WeightTickets {
if weightTicket.Status != nil && *weightTicket.Status == models.PPMDocumentStatusRejected {
newTotalWeight += 0
} else if weightTicket.AdjustedNetWeight != nil {
newTotalWeight += *weightTicket.AdjustedNetWeight
} else if weightTicket.FullWeight != nil && weightTicket.EmptyWeight != nil {
newTotalWeight += *weightTicket.FullWeight - *weightTicket.EmptyWeight
}
}
}
return originalTotalWeight, newTotalWeight
}
// calculatePrice returns an incentive value for the ppm shipment as if we were pricing the service items for
// an HHG shipment with the same values for a payment request. In this case we're not persisting service items,
// MTOServiceItems or PaymentRequestServiceItems, to the database to avoid unnecessary work and get a quicker result.
func (f estimatePPM) calculatePrice(appCtx appcontext.AppContext, ppmShipment *models.PPMShipment, totalWeightFromWeightTickets unit.Pound, contract models.ReContract) (*unit.Cents, error) {
logger := appCtx.Logger()
serviceItemsToPrice := BaseServiceItems(ppmShipment.ShipmentID)
// Get a list of all the pricing params needed to calculate the price for each service item
paramsForServiceItems, err := f.paymentRequestHelper.FetchServiceParamsForServiceItems(appCtx, serviceItemsToPrice)
if err != nil {
logger.Error("fetching PPM estimate ServiceParams failed", zap.Error(err))
return nil, err
}
var mtoShipment models.MTOShipment
if totalWeightFromWeightTickets > 0 {
// Reassign ppm shipment fields to their expected location on the mto shipment for dates, addresses, weights ...
mtoShipment = MapPPMShipmentFinalFields(*ppmShipment, totalWeightFromWeightTickets)
} else {
// Reassign ppm shipment fields to their expected location on the mto shipment for dates, addresses, weights ...
mtoShipment = MapPPMShipmentEstimatedFields(*ppmShipment)
}
totalPrice := unit.Cents(0)
for _, serviceItem := range serviceItemsToPrice {
pricer, err := ghcrateengine.PricerForServiceItem(serviceItem.ReService.Code)
if err != nil {
logger.Error("unable to find pricer for service item", zap.Error(err))
return nil, err
}
// For the non-accessorial service items there isn't any initialization that is going to change between lookups
// for the same param. However, this is how the payment request does things and we'd want to know if it breaks
// rather than optimizing I think.
serviceItemLookups := serviceparamvaluelookups.InitializeLookups(mtoShipment, serviceItem)
// This is the struct that gets passed to every param lookup() method that was initialized above
keyData := serviceparamvaluelookups.NewServiceItemParamKeyData(f.planner, serviceItemLookups, serviceItem, mtoShipment, contract.Code)
// The distance value gets saved to the mto shipment model to reduce repeated api calls.
var shipmentWithDistance models.MTOShipment
err = appCtx.DB().Find(&shipmentWithDistance, mtoShipment.ID)
if err != nil {
logger.Error("could not find shipment in the database")
return nil, err
}
serviceItem.MTOShipment = shipmentWithDistance
// set this to avoid potential eTag errors because the MTOShipment.Distance field was likely updated
ppmShipment.Shipment = shipmentWithDistance
var paramValues models.PaymentServiceItemParams
for _, param := range paramsForServiceCode(serviceItem.ReService.Code, paramsForServiceItems) {
paramKey := param.ServiceItemParamKey
// This is where the lookup() method of each service item param is actually evaluated
paramValue, valueErr := keyData.ServiceParamValue(appCtx, paramKey.Key)
if valueErr != nil {
logger.Error("could not calculate param value lookup", zap.Error(valueErr))
return nil, valueErr
}
// Gather all the param values for the service item to pass to the pricer's Price() method
paymentServiceItemParam := models.PaymentServiceItemParam{
// Some pricers like Fuel Surcharge try to requery the shipment through the service item, this is a
// workaround to avoid a not found error because our PPM shipment has no service items saved in the db.
// I think the FSC service item should really be relying on one of the zip distance params.
PaymentServiceItem: models.PaymentServiceItem{
MTOServiceItem: serviceItem,
},
ServiceItemParamKey: paramKey,
Value: paramValue,
}
paramValues = append(paramValues, paymentServiceItemParam)
}
if len(paramValues) == 0 {
return nil, fmt.Errorf("no params were found for service item %s", serviceItem.ReService.Code)
}
centsValue, paymentParams, err := pricer.PriceUsingParams(appCtx, paramValues)
logger.Debug(fmt.Sprintf("Service item price %s %d", serviceItem.ReService.Code, centsValue))
logger.Debug(fmt.Sprintf("Payment service item params %+v", paymentParams))
if err != nil {
logger.Error("unable to calculate service item price", zap.Error(err))
return nil, err
}
totalPrice = totalPrice.AddCents(centsValue)
}
return &totalPrice, nil
}
func CalculateSITCost(appCtx appcontext.AppContext, ppmShipment *models.PPMShipment, contract models.ReContract) (*unit.Cents, error) {
logger := appCtx.Logger()
additionalDaysInSIT := additionalDaysInSIT(*ppmShipment.SITEstimatedEntryDate, *ppmShipment.SITEstimatedDepartureDate)
serviceItemsToPrice := StorageServiceItems(ppmShipment.ShipmentID, *ppmShipment.SITLocation, additionalDaysInSIT)
totalPrice := unit.Cents(0)
for _, serviceItem := range serviceItemsToPrice {
pricer, err := ghcrateengine.PricerForServiceItem(serviceItem.ReService.Code)
if err != nil {
logger.Error("unable to find pricer for service item", zap.Error(err))
return nil, err
}
var price *unit.Cents
switch serviceItemPricer := pricer.(type) {
case services.DomesticOriginFirstDaySITPricer, services.DomesticDestinationFirstDaySITPricer:
price, err = priceFirstDaySIT(appCtx, serviceItemPricer, serviceItem, ppmShipment, contract)
case services.DomesticOriginAdditionalDaysSITPricer, services.DomesticDestinationAdditionalDaysSITPricer:
price, err = priceAdditionalDaySIT(appCtx, serviceItemPricer, serviceItem, ppmShipment, additionalDaysInSIT, contract)
default:
return nil, fmt.Errorf("unknown SIT pricer type found for service item code %s", serviceItem.ReService.Code)
}
if err != nil {
return nil, err
}
logger.Debug(fmt.Sprintf("Price of service item %s %d", serviceItem.ReService.Code, *price))
totalPrice += *price
}
return &totalPrice, nil
}
func priceFirstDaySIT(appCtx appcontext.AppContext, pricer services.ParamsPricer, serviceItem models.MTOServiceItem, ppmShipment *models.PPMShipment, contract models.ReContract) (*unit.Cents, error) {
firstDayPricer, ok := pricer.(services.DomesticFirstDaySITPricer)
if !ok {
return nil, errors.New("ppm estimate pricer for SIT service item does not implement the first day pricer interface")
}
serviceAreaPostalCode := ppmShipment.PickupPostalCode
if serviceItem.ReService.Code == models.ReServiceCodeDDFSIT {
serviceAreaPostalCode = ppmShipment.DestinationPostalCode
}
serviceAreaLookup := serviceparamvaluelookups.ServiceAreaLookup{
Address: models.Address{PostalCode: serviceAreaPostalCode},
}
serviceArea, err := serviceAreaLookup.ParamValue(appCtx, contract.Code)
if err != nil {
return nil, err
}
price, pricingParams, err := firstDayPricer.Price(appCtx, contract.Code, ppmShipment.ExpectedDepartureDate, *ppmShipment.SITEstimatedWeight, serviceArea, true)
if err != nil {
return nil, err
}
appCtx.Logger().Debug(fmt.Sprintf("Pricing params for first day SIT %+v", pricingParams), zap.String("shipmentId", ppmShipment.ShipmentID.String()))
return &price, nil
}
func additionalDaysInSIT(sitEntryDate time.Time, sitDepartureDate time.Time) int {
// The cost of first day SIT is already accounted for in DOFSIT or DDFSIT service items
if sitEntryDate.Equal(sitDepartureDate) {
return 0
}
difference := sitDepartureDate.Sub(sitEntryDate)
return int(difference.Hours() / 24)
}
func priceAdditionalDaySIT(appCtx appcontext.AppContext, pricer services.ParamsPricer, serviceItem models.MTOServiceItem, ppmShipment *models.PPMShipment, additionalDaysInSIT int, contract models.ReContract) (*unit.Cents, error) {
additionalDaysPricer, ok := pricer.(services.DomesticAdditionalDaysSITPricer)
if !ok {
return nil, errors.New("ppm estimate pricer for SIT service item does not implement the additional days pricer interface")
}
serviceAreaPostalCode := ppmShipment.PickupPostalCode
if serviceItem.ReService.Code == models.ReServiceCodeDDASIT {
serviceAreaPostalCode = ppmShipment.DestinationPostalCode
}
serviceAreaLookup := serviceparamvaluelookups.ServiceAreaLookup{
Address: models.Address{PostalCode: serviceAreaPostalCode},
}
serviceArea, err := serviceAreaLookup.ParamValue(appCtx, contract.Code)
if err != nil {
return nil, err
}
price, pricingParams, err := additionalDaysPricer.Price(appCtx, contract.Code, ppmShipment.ExpectedDepartureDate, *ppmShipment.SITEstimatedWeight, serviceArea, additionalDaysInSIT, true)
if err != nil {
return nil, err
}
appCtx.Logger().Debug(fmt.Sprintf("Pricing params for additional day SIT %+v", pricingParams), zap.String("shipmentId", ppmShipment.ShipmentID.String()))
return &price, nil
}
// mapPPMShipmentEstimatedFields remaps our PPMShipment specific information into the fields where the service param lookups
// expect to find them on the MTOShipment model. This is only in-memory and shouldn't get saved to the database.
func MapPPMShipmentEstimatedFields(ppmShipment models.PPMShipment) models.MTOShipment {
ppmShipment.Shipment.ActualPickupDate = &ppmShipment.ExpectedDepartureDate
ppmShipment.Shipment.RequestedPickupDate = &ppmShipment.ExpectedDepartureDate
ppmShipment.Shipment.PickupAddress = &models.Address{PostalCode: ppmShipment.PickupPostalCode}
ppmShipment.Shipment.DestinationAddress = &models.Address{PostalCode: ppmShipment.DestinationPostalCode}
ppmShipment.Shipment.PrimeActualWeight = ppmShipment.EstimatedWeight
return ppmShipment.Shipment
}
// mapPPMShipmentFinalFields remaps our PPMShipment specific information into the fields where the service param lookups
// expect to find them on the MTOShipment model. This is only in-memory and shouldn't get saved to the database.
func MapPPMShipmentFinalFields(ppmShipment models.PPMShipment, totalWeight unit.Pound) models.MTOShipment {
ppmShipment.Shipment.ActualPickupDate = ppmShipment.ActualMoveDate
ppmShipment.Shipment.RequestedPickupDate = ppmShipment.ActualMoveDate
ppmShipment.Shipment.PickupAddress = &models.Address{PostalCode: *ppmShipment.ActualPickupPostalCode}
ppmShipment.Shipment.DestinationAddress = &models.Address{PostalCode: *ppmShipment.ActualDestinationPostalCode}
ppmShipment.Shipment.PrimeActualWeight = &totalWeight
return ppmShipment.Shipment
}
// baseServiceItems returns a list of the MTOServiceItems that makeup the price of the estimated incentive. These
// are the same non-accesorial service items that get auto-created and approved when the TOO approves an HHG shipment.
func BaseServiceItems(mtoShipmentID uuid.UUID) []models.MTOServiceItem {
return []models.MTOServiceItem{
{ReService: models.ReService{Code: models.ReServiceCodeDLH}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeFSC}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeDOP}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeDDP}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeDPK}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeDUPK}, MTOShipmentID: &mtoShipmentID},
}
}
func StorageServiceItems(mtoShipmentID uuid.UUID, locationType models.SITLocationType, additionalDaysInSIT int) []models.MTOServiceItem {
if locationType == models.SITLocationTypeOrigin {
if additionalDaysInSIT > 0 {
return []models.MTOServiceItem{
{ReService: models.ReService{Code: models.ReServiceCodeDOFSIT}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeDOASIT}, MTOShipmentID: &mtoShipmentID},
}
}
return []models.MTOServiceItem{
{ReService: models.ReService{Code: models.ReServiceCodeDOFSIT}, MTOShipmentID: &mtoShipmentID}}
}
if additionalDaysInSIT > 0 {
return []models.MTOServiceItem{
{ReService: models.ReService{Code: models.ReServiceCodeDDFSIT}, MTOShipmentID: &mtoShipmentID},
{ReService: models.ReService{Code: models.ReServiceCodeDDASIT}, MTOShipmentID: &mtoShipmentID},
}
}
return []models.MTOServiceItem{
{ReService: models.ReService{Code: models.ReServiceCodeDDFSIT}, MTOShipmentID: &mtoShipmentID}}
}
// paramsForServiceCode filters the list of all service params for service items, to only those matching the service
// item code. This allows us to make one initial query to the database instead of six just to filter by service item.
func paramsForServiceCode(code models.ReServiceCode, serviceParams models.ServiceParams) models.ServiceParams {
var serviceItemParams models.ServiceParams
for _, serviceParam := range serviceParams {
if serviceParam.Service.Code == code {
serviceItemParams = append(serviceItemParams, serviceParam)
}
}
return serviceItemParams
}