Skip to content

Commit e0d70c5

Browse files
committed
Handle splitting shipments
1 parent cfa117a commit e0d70c5

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

WooCommerce/Classes/ViewRelated/Orders/Order Details/Shipping Labels/WooShipping Create Shipping Labels/WooShipping Split Shipments/WooShippingSplitShipmentsViewModel.swift

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@ final class WooShippingSplitShipmentsViewModel: ObservableObject {
1414

1515
@Published private(set) var shipments: [Shipment]
1616

17-
@Published var selectedShipmentIndex: Int? = 0
17+
@Published var selectedShipmentIndex: Int? = 0 {
18+
didSet {
19+
moveToNoticeViewModel = nil
20+
configureSelectionCallback()
21+
}
22+
}
1823

1924
/// Label with the total number of items to ship.
2025
@Published private(set) var itemsCountLabel: String = ""
@@ -103,7 +108,7 @@ private extension WooShippingSplitShipmentsViewModel {
103108
}
104109

105110
func showMoveToNotice() {
106-
let shipmentIndex = selectedShipmentIndex ?? 0
111+
let currentIndex = selectedShipmentIndex ?? 0
107112
let selectedItemsCount = currentShipment
108113
.map(\.numberOfSelectedItems)
109114
.reduce(0, +)
@@ -114,18 +119,53 @@ private extension WooShippingSplitShipmentsViewModel {
114119

115120
moveToNoticeViewModel = MoveToShipmentNoticeViewModel(selectedItemsCount: selectedItemsCount,
116121
existingShipmentsCount: shipments.count,
117-
currentShipmentIndex: shipmentIndex,
122+
currentShipmentIndex: currentIndex,
118123
actionHandler: { [weak self] moveTo in
119124
guard let self else { return }
120125

121126
moveToNoticeViewModel = nil
122127
instructions = nil
123128

129+
// Step 1: Split items
130+
var newShipment = Shipment()
131+
var movedItems = [CollapsibleShipmentItemCardViewModel]()
132+
for item in currentShipment {
133+
let initialQuantity = item.packageItem.quantity.intValue
134+
let selectedQuantity = item.numberOfSelectedItems
135+
let remainingQuantity = initialQuantity - selectedQuantity
136+
if remainingQuantity == 0 {
137+
movedItems.append(
138+
CollapsibleShipmentItemCardViewModel(item: item.packageItem, currency: self.order.currency)
139+
)
140+
} else if selectedQuantity > 0 {
141+
let newItem = ShippingLabelPackageItem(copy: item.packageItem, quantity: Decimal(remainingQuantity))
142+
newShipment.append(
143+
CollapsibleShipmentItemCardViewModel(item: newItem, currency: self.order.currency)
144+
)
145+
let movedItem = ShippingLabelPackageItem(copy: item.packageItem, quantity: Decimal(selectedQuantity))
146+
movedItems.append(
147+
CollapsibleShipmentItemCardViewModel(item: movedItem, currency: self.order.currency)
148+
)
149+
} else if selectedQuantity == 0 {
150+
newShipment.append(
151+
CollapsibleShipmentItemCardViewModel(item: item.packageItem, currency: self.order.currency)
152+
)
153+
}
154+
}
155+
156+
// Step 2: Update the current shipment
157+
shipments[currentIndex] = newShipment
158+
configureSelectionCallback()
159+
160+
// Step 3: Add new or update existing shipment
124161
switch moveTo {
125-
case .existingShipment:
126-
break
127162
case .newShipment:
128-
break
163+
shipments.append(movedItems)
164+
165+
case .existingShipment(let index):
166+
var previousShipment = shipments[index]
167+
previousShipment.append(contentsOf: movedItems)
168+
shipments[index] = previousShipment
129169
}
130170
})
131171
}

0 commit comments

Comments
 (0)