-
Notifications
You must be signed in to change notification settings - Fork 38
/
ppm_shipment_router.go
166 lines (137 loc) · 5.02 KB
/
ppm_shipment_router.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
package ppmshipment
import (
"fmt"
"time"
"github.com/transcom/mymove/pkg/appcontext"
"github.com/transcom/mymove/pkg/apperror"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
)
// ppmShipmentRouter is the concrete struct implementing the services.PPMShipmentRouter interface
type ppmShipmentRouter struct {
services.ShipmentRouter
}
// NewPPMShipmentRouter creates a new ppmShipmentRouter
func NewPPMShipmentRouter(shipmentRouter services.ShipmentRouter) services.PPMShipmentRouter {
return &ppmShipmentRouter{
ShipmentRouter: shipmentRouter,
}
}
// SetToDraft sets the PPM shipment to the DRAFT status
func (p *ppmShipmentRouter) SetToDraft(_ appcontext.AppContext, ppmShipment *models.PPMShipment) error {
if ppmShipment.Status != "" {
return apperror.NewConflictError(
ppmShipment.ID,
fmt.Sprintf("PPM shipment can't be set to %s because it's not new.", models.PPMShipmentStatusDraft),
)
}
ppmShipment.Status = models.PPMShipmentStatusDraft
// TODO: this should be done using the shipment router, but it currently doesn't have a way of setting this.
ppmShipment.Shipment.Status = models.MTOShipmentStatusDraft
return nil
}
// Submit sets the PPM shipment to the SUBMITTED status
func (p *ppmShipmentRouter) Submit(appCtx appcontext.AppContext, ppmShipment *models.PPMShipment) error {
if ppmShipment.Status != "" && ppmShipment.Status != models.PPMShipmentStatusDraft {
return apperror.NewConflictError(
ppmShipment.ID,
fmt.Sprintf(
"PPM shipment can't be set to %s because it's not new or in the %s status.",
models.PPMShipmentStatusSubmitted,
models.PPMShipmentStatusDraft,
),
)
}
err := p.ShipmentRouter.Submit(appCtx, &ppmShipment.Shipment)
if err != nil {
return err
}
ppmShipment.Status = models.PPMShipmentStatusSubmitted
return nil
}
// SendToCustomer sets the PPM shipment to the WAITING_ON_CUSTOMER status
func (p *ppmShipmentRouter) SendToCustomer(appCtx appcontext.AppContext, ppmShipment *models.PPMShipment) error {
if ppmShipment.Status != models.PPMShipmentStatusSubmitted && ppmShipment.Status != models.PPMShipmentStatusNeedsPaymentApproval {
return apperror.NewConflictError(
ppmShipment.ID,
fmt.Sprintf(
"PPM shipment can't be set to %s because it's not in a %s or %s status.",
models.PPMShipmentStatusWaitingOnCustomer,
models.PPMShipmentStatusSubmitted,
models.PPMShipmentStatusNeedsPaymentApproval,
),
)
}
if ppmShipment.Shipment.Status != models.MTOShipmentStatusApproved {
err := p.ShipmentRouter.Approve(appCtx, &ppmShipment.Shipment)
if err != nil {
return err
}
}
ppmShipment.Status = models.PPMShipmentStatusWaitingOnCustomer
if ppmShipment.ApprovedAt == nil {
ppmShipment.ApprovedAt = models.TimePointer(*ppmShipment.Shipment.ApprovedDate)
}
return nil
}
// SubmitCloseOutDocumentation sets the PPM shipment to the NEEDS_PAYMENT_APPROVAL status
func (p *ppmShipmentRouter) SubmitCloseOutDocumentation(_ appcontext.AppContext, ppmShipment *models.PPMShipment) error {
if ppmShipment.Status != models.PPMShipmentStatusWaitingOnCustomer {
return apperror.NewConflictError(
ppmShipment.ID,
fmt.Sprintf(
"PPM shipment can't be set to %s because it's not in the %s status.",
models.PPMShipmentStatusNeedsPaymentApproval,
models.PPMShipmentStatusWaitingOnCustomer,
),
)
}
ppmShipment.Status = models.PPMShipmentStatusNeedsPaymentApproval
if ppmShipment.SubmittedAt == nil {
ppmShipment.SubmittedAt = models.TimePointer(time.Now())
}
return nil
}
// SubmitReviewedDocuments sets the PPM shipment status to the PAYMENT_APPROVED if all docs approved otherwise WAITING_ON_CUSTOMER
func (p *ppmShipmentRouter) SubmitReviewedDocuments(_ appcontext.AppContext, ppmShipment *models.PPMShipment) error {
if ppmShipment.Status != models.PPMShipmentStatusNeedsPaymentApproval {
return apperror.NewConflictError(
ppmShipment.ID,
fmt.Sprintf(
"PPM shipment documents cannot be submitted because it's not in the %s status.",
models.PPMShipmentStatusNeedsPaymentApproval,
),
)
}
hasRejectedDocuments := false
if len(ppmShipment.WeightTickets) >= 1 {
for _, weightTicket := range ppmShipment.WeightTickets {
if weightTicket.Status != nil && *weightTicket.Status == models.PPMDocumentStatusRejected {
hasRejectedDocuments = true
break
}
}
}
if len(ppmShipment.ProgearWeightTickets) >= 1 && !hasRejectedDocuments {
for _, progear := range ppmShipment.ProgearWeightTickets {
if progear.Status != nil && *progear.Status == models.PPMDocumentStatusRejected {
hasRejectedDocuments = true
break
}
}
}
if len(ppmShipment.MovingExpenses) >= 1 && !hasRejectedDocuments {
for _, movingExpenses := range ppmShipment.MovingExpenses {
if movingExpenses.Status != nil && *movingExpenses.Status == models.PPMDocumentStatusRejected {
hasRejectedDocuments = true
break
}
}
}
if hasRejectedDocuments {
ppmShipment.Status = models.PPMShipmentStatusWaitingOnCustomer
} else {
ppmShipment.Status = models.PPMShipmentStatusPaymentApproved
}
return nil
}