/
shipment_billable_weight.go
66 lines (59 loc) · 2.72 KB
/
shipment_billable_weight.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
package mtoshipment
import (
"github.com/gofrs/uuid"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
"github.com/transcom/mymove/pkg/unit"
)
// shipmentBillableWeightCalculator handles the db connection
type shipmentBillableWeightCalculator struct {
}
// NewShipmentBillableWeightCalculator updates the address for an MTO Shipment
func NewShipmentBillableWeightCalculator() services.ShipmentBillableWeightCalculator {
return &shipmentBillableWeightCalculator{}
}
// CalculateShipmentBillableWeight calculates a shipment's billable weight.
// If there's a billableWeightCap set that takes precedence.
// Warning: The reweigh object is assumed to have been loaded for the passed in shipment for this service to
// guarantee that the correct calculated weight is returned!
// Without reweigh EagerPreload(ed) there is the risk of miscalculation.
// Due to the nature of EagerPreload, we can no longer tell if Reweigh was NOT preloaded
// OR if the shipment does not have a Reweigh (there is no good way to do this as of this PR)
// https://github.com/transcom/mymove/pull/10780
func (f *shipmentBillableWeightCalculator) CalculateShipmentBillableWeight(shipment *models.MTOShipment) services.BillableWeightInputs {
var calculatedWeight *unit.Pound
var reweighWeight *unit.Pound
var primeActualWeight *unit.Pound
// Warning: This function assumes that the shipment Reweigh was eager loaded!
if shipment.Reweigh != nil && shipment.Reweigh.ID != uuid.Nil {
if shipment.Reweigh.Weight != nil && shipment.PrimeActualWeight != nil {
reweighWeight = shipment.Reweigh.Weight
primeActualWeight = shipment.PrimeActualWeight
if int(*primeActualWeight) < int(*reweighWeight) {
calculatedWeight = primeActualWeight
} else if int(*reweighWeight) > 0 {
// Only use the reweigh weight if it's greater than 0
calculatedWeight = reweighWeight
} else {
// If the prime actual weight is not lower than the reweigh weight, but the
// reweigh weight is 0, use the prime actual weight.
calculatedWeight = primeActualWeight
}
} else if shipment.Reweigh.Weight == nil && shipment.PrimeActualWeight != nil {
// if there is no reweigh weight, use the prime actual weight if it is not nil.
calculatedWeight = shipment.PrimeActualWeight
}
} else if shipment.BillableWeightCap == nil {
calculatedWeight = shipment.PrimeActualWeight
}
if shipment.BillableWeightCap != nil {
calculatedWeight = shipment.BillableWeightCap
}
hasOverride := shipment.BillableWeightCap != nil
return services.BillableWeightInputs{
CalculatedBillableWeight: calculatedWeight,
OriginalWeight: shipment.PrimeActualWeight,
ReweighWeight: reweighWeight,
HadManualOverride: &hasOverride,
}
}