Skip to content

Commit db48e93

Browse files
authored
[Woo POS][Historical Orders] Order Details (Wireframe UI) (#16072)
2 parents 2e05cde + dc6e7c7 commit db48e93

24 files changed

+1375
-458
lines changed

Modules/Sources/Yosemite/PointOfSale/OrderList/POSOrder.swift

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,47 @@ public struct POSOrder: Equatable, Hashable {
1111
public let number: String
1212
public let dateCreated: Date
1313
public let status: OrderStatusEnum
14-
public let total: String
14+
public let formattedTotal: String
15+
public let formattedSubtotal: String
1516
public let customerEmail: String?
1617
public let paymentMethodID: String
1718
public let paymentMethodTitle: String
1819
public let lineItems: [POSOrderItem]
1920
public let refunds: [POSOrderRefund]
20-
public let currency: String
21-
public let currencySymbol: String
21+
public let formattedDiscountTotal: String?
22+
public let formattedTotalTax: String
23+
public let formattedPaymentTotal: String
24+
public let formattedNetAmount: String?
2225

2326
public init(id: Int64,
2427
number: String,
2528
dateCreated: Date,
2629
status: OrderStatusEnum,
27-
total: String,
30+
formattedTotal: String,
31+
formattedSubtotal: String,
2832
customerEmail: String? = nil,
2933
paymentMethodID: String,
3034
paymentMethodTitle: String,
3135
lineItems: [POSOrderItem] = [],
3236
refunds: [POSOrderRefund] = [],
33-
currency: String,
34-
currencySymbol: String) {
37+
formattedTotalTax: String,
38+
formattedDiscountTotal: String?,
39+
formattedPaymentTotal: String,
40+
formattedNetAmount: String? = nil) {
3541
self.id = id
3642
self.number = number
3743
self.dateCreated = dateCreated
3844
self.status = status
39-
self.total = total
45+
self.formattedTotal = formattedTotal
46+
self.formattedSubtotal = formattedSubtotal
4047
self.customerEmail = customerEmail
4148
self.paymentMethodID = paymentMethodID
4249
self.paymentMethodTitle = paymentMethodTitle
4350
self.lineItems = lineItems
4451
self.refunds = refunds
45-
self.currency = currency
46-
self.currencySymbol = currencySymbol
47-
}
48-
}
49-
50-
// MARK: - Conversion from NetworkingCore.Order
51-
public extension POSOrder {
52-
init(from order: NetworkingCore.Order) {
53-
// Extract customer email from billing address
54-
let customerEmail = order.billingAddress?.email
55-
56-
// Convert line items to POS format
57-
let posLineItems = order.items.map { POSOrderItem(from: $0) }
58-
59-
// Convert refunds to POS format
60-
let posRefunds = order.refunds.map { POSOrderRefund(from: $0) }
61-
62-
self.init(
63-
id: order.orderID,
64-
number: order.number,
65-
dateCreated: order.dateCreated,
66-
status: order.status,
67-
total: order.total,
68-
customerEmail: customerEmail,
69-
paymentMethodID: order.paymentMethodID,
70-
paymentMethodTitle: order.paymentMethodTitle,
71-
lineItems: posLineItems,
72-
refunds: posRefunds,
73-
currency: order.currency,
74-
currencySymbol: order.currencySymbol
75-
)
52+
self.formattedTotalTax = formattedTotalTax
53+
self.formattedDiscountTotal = formattedDiscountTotal
54+
self.formattedPaymentTotal = formattedPaymentTotal
55+
self.formattedNetAmount = formattedNetAmount
7656
}
7757
}
Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
import Foundation
2-
import struct NetworkingCore.OrderItem
32

43
public struct POSOrderItem: Equatable, Hashable {
54
public let itemID: Int64
65
public let name: String
6+
// periphery:ignore - Will be used for images
7+
public let productID: Int64
8+
// periphery:ignore - Will be used for images
9+
public let variationID: Int64
710
public let quantity: Decimal
8-
public let total: String
11+
public let formattedPrice: String
12+
public let formattedTotal: String
13+
public let attributes: [OrderItemAttribute]
914

1015
public init(itemID: Int64,
1116
name: String,
17+
productID: Int64,
18+
variationID: Int64,
1219
quantity: Decimal,
13-
total: String) {
20+
formattedPrice: String,
21+
formattedTotal: String,
22+
attributes: [OrderItemAttribute]) {
1423
self.itemID = itemID
1524
self.name = name
25+
self.productID = productID
26+
self.variationID = variationID
1627
self.quantity = quantity
17-
self.total = total
18-
}
19-
}
20-
21-
// MARK: - Conversion from NetworkingCore.OrderItem
22-
public extension POSOrderItem {
23-
init(from orderItem: OrderItem) {
24-
self.init(
25-
itemID: orderItem.itemID,
26-
name: orderItem.name,
27-
quantity: orderItem.quantity,
28-
total: orderItem.total
29-
)
28+
self.formattedPrice = formattedPrice
29+
self.formattedTotal = formattedTotal
30+
self.attributes = attributes
3031
}
3132
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import Foundation
2+
import class WooFoundationCore.CurrencyFormatter
3+
import struct NetworkingCore.Order
4+
import struct NetworkingCore.OrderItem
5+
import struct NetworkingCore.OrderItemAttribute
6+
import struct NetworkingCore.OrderRefundCondensed
7+
8+
struct POSOrderMapper {
9+
private let currencyFormatter: CurrencyFormatter
10+
11+
init(currencyFormatter: CurrencyFormatter) {
12+
self.currencyFormatter = currencyFormatter
13+
}
14+
15+
func map(order: NetworkingCore.Order) -> POSOrder {
16+
let customerEmail = order.billingAddress?.email
17+
18+
let posLineItems = order.items.map { map(orderItem: $0, currency: order.currency) }
19+
20+
let posRefunds = order.refunds.map { map(orderRefund: $0, currency: order.currency) }
21+
22+
let formattedDiscountTotal: String? = {
23+
guard let discountTotalValue = Double(order.discountTotal), discountTotalValue > 0 else {
24+
return nil
25+
}
26+
return currencyFormatter.formatAmount(order.discountTotal, with: order.currency, isNegative: true) ?? ""
27+
}()
28+
29+
let formattedNetAmount: String? = {
30+
guard !order.refunds.isEmpty else {
31+
return nil
32+
}
33+
return order.netAmount(currencyFormatter: currencyFormatter)
34+
}()
35+
36+
return POSOrder(
37+
id: order.orderID,
38+
number: order.number,
39+
dateCreated: order.dateCreated,
40+
status: order.status,
41+
formattedTotal: currencyFormatter.formatAmount(order.total, with: order.currency) ?? "",
42+
formattedSubtotal: order.subtotalValue(currencyFormatter: currencyFormatter),
43+
customerEmail: customerEmail,
44+
paymentMethodID: order.paymentMethodID,
45+
paymentMethodTitle: order.paymentMethodTitle,
46+
lineItems: posLineItems,
47+
refunds: posRefunds,
48+
formattedTotalTax: currencyFormatter.formatAmount(order.totalTax, with: order.currency) ?? "",
49+
formattedDiscountTotal: formattedDiscountTotal,
50+
formattedPaymentTotal: order.paymentTotal(currencyFormatter: currencyFormatter),
51+
formattedNetAmount: formattedNetAmount
52+
)
53+
}
54+
55+
private func map(orderItem: NetworkingCore.OrderItem, currency: String) -> POSOrderItem {
56+
return POSOrderItem(
57+
itemID: orderItem.itemID,
58+
name: orderItem.name,
59+
productID: orderItem.productID,
60+
variationID: orderItem.variationID,
61+
quantity: orderItem.quantity,
62+
formattedPrice: currencyFormatter.formatAmount(orderItem.price, with: currency) ?? "",
63+
formattedTotal: currencyFormatter.formatAmount(orderItem.total, with: currency) ?? "",
64+
attributes: orderItem.attributes
65+
)
66+
}
67+
68+
private func map(orderRefund: NetworkingCore.OrderRefundCondensed, currency: String) -> POSOrderRefund {
69+
return POSOrderRefund(
70+
refundID: orderRefund.refundID,
71+
formattedTotal: currencyFormatter.formatAmount(orderRefund.total, with: currency) ?? "",
72+
reason: orderRefund.reason
73+
)
74+
}
75+
}
Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
import Foundation
22
import struct NetworkingCore.OrderRefundCondensed
3+
import class WooFoundationCore.CurrencyFormatter
34

45
public struct POSOrderRefund: Equatable, Hashable {
56
public let refundID: Int64
6-
public let total: String
7+
public let formattedTotal: String
78
public let reason: String?
89

910
public init(refundID: Int64,
10-
total: String,
11+
formattedTotal: String,
1112
reason: String? = nil) {
1213
self.refundID = refundID
13-
self.total = total
14+
self.formattedTotal = formattedTotal
1415
self.reason = reason
1516
}
1617
}
17-
18-
// MARK: - Conversion from NetworkingCore.OrderRefundCondensed
19-
public extension POSOrderRefund {
20-
init(from refund: OrderRefundCondensed) {
21-
self.init(
22-
refundID: refund.refundID,
23-
total: refund.total,
24-
reason: refund.reason
25-
)
26-
}
27-
}
Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Foundation
22
import class Networking.AlamofireNetwork
33
import class Networking.OrdersRemote
4+
import class WooFoundationCore.CurrencyFormatter
45

56
public protocol PointOfSaleOrderListFetchStrategyFactoryProtocol {
67
func defaultStrategy() -> PointOfSaleOrderListFetchStrategy
@@ -9,16 +10,24 @@ public protocol PointOfSaleOrderListFetchStrategyFactoryProtocol {
910
public final class PointOfSaleOrderListFetchStrategyFactory: PointOfSaleOrderListFetchStrategyFactoryProtocol {
1011
private let siteID: Int64
1112
private let ordersRemote: OrdersRemote
13+
private let currencyFormatter: CurrencyFormatter
1214

1315
public init(siteID: Int64,
14-
credentials: Credentials?) {
16+
credentials: Credentials?,
17+
currencyFormatter: CurrencyFormatter) {
1518
self.siteID = siteID
1619
let network = AlamofireNetwork(credentials: credentials)
1720
self.ordersRemote = OrdersRemote(network: network)
21+
self.currencyFormatter = currencyFormatter
1822
}
1923

2024
public func defaultStrategy() -> PointOfSaleOrderListFetchStrategy {
21-
PointOfSaleDefaultOrderListFetchStrategy(orderListService: PointOfSaleOrderListService(siteID: siteID,
22-
ordersRemote: ordersRemote))
25+
PointOfSaleDefaultOrderListFetchStrategy(
26+
orderListService: PointOfSaleOrderListService(
27+
siteID: siteID,
28+
ordersRemote: ordersRemote,
29+
currencyFormatter: currencyFormatter
30+
)
31+
)
2332
}
2433
}

Modules/Sources/Yosemite/PointOfSale/OrderList/PointOfSaleOrderListService.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@ import enum Alamofire.AFError
33
import struct NetworkingCore.PagedItems
44
import struct NetworkingCore.Order
55
import protocol NetworkingCore.POSOrdersRemoteProtocol
6+
import class WooFoundationCore.CurrencyFormatter
67

78
public final class PointOfSaleOrderListService: PointOfSaleOrderListServiceProtocol {
89
private let ordersRemote: POSOrdersRemoteProtocol
910
private let siteID: Int64
11+
private let mapper: POSOrderMapper
1012

11-
public init(siteID: Int64, ordersRemote: POSOrdersRemoteProtocol) {
13+
public init(
14+
siteID: Int64,
15+
ordersRemote: POSOrdersRemoteProtocol,
16+
currencyFormatter: CurrencyFormatter
17+
) {
1218
self.siteID = siteID
1319
self.ordersRemote = ordersRemote
20+
self.mapper = POSOrderMapper(currencyFormatter: currencyFormatter)
1421
}
1522

1623
public func providePointOfSaleOrders(pageNumber: Int = 1) async throws -> PagedItems<POSOrder> {
@@ -26,7 +33,7 @@ public final class PointOfSaleOrderListService: PointOfSaleOrderListServiceProto
2633
}
2734

2835
// Convert Order objects to POSOrder objects
29-
let posOrders = pagedOrders.items.map { POSOrder(from: $0) }
36+
let posOrders = pagedOrders.items.map { mapper.map(order: $0) }
3037

3138
return .init(items: posOrders,
3239
hasMorePages: pagedOrders.hasMorePages,

Modules/Sources/Yosemite/Stores/Order/Order+CurrencyFormattedValues.swift

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,49 @@ public extension Order {
77
}
88

99
var netAmount: String? {
10-
guard let netDecimal = calculateNetAmount() else {
10+
netAmount(currencyFormatter: currencyFormatter)
11+
}
12+
13+
var totalValue: String {
14+
totalValue(currencyFormatter: currencyFormatter)
15+
}
16+
17+
var subtotal: Decimal {
18+
let subtotal = items.reduce(.zero) { (output, item) in
19+
let itemSubtotal = Decimal(string: item.subtotal) ?? .zero
20+
return output + itemSubtotal
21+
}
22+
23+
return subtotal
24+
}
25+
26+
func netAmount(currencyFormatter: CurrencyFormatter) -> String? {
27+
guard let netDecimal = calculateNetAmount(currencyFormatter: currencyFormatter) else {
1128
return nil
1229
}
1330

1431
return currencyFormatter.formatAmount(netDecimal, with: currency)
1532
}
1633

17-
var paymentTotal: String {
34+
func paymentTotal(currencyFormatter: CurrencyFormatter) -> String {
1835
if datePaid == nil {
1936
return currencyFormatter.formatAmount("0.00", with: currency) ?? String()
2037
}
2138

22-
return totalValue
39+
return totalValue(currencyFormatter: currencyFormatter)
2340
}
2441

25-
var totalValue: String {
42+
func totalValue(currencyFormatter: CurrencyFormatter) -> String {
2643
return currencyFormatter.formatAmount(total, with: currency) ?? String()
2744
}
2845

29-
private func calculateNetAmount() -> NSDecimalNumber? {
46+
func subtotalValue(currencyFormatter: CurrencyFormatter) -> String {
47+
let subAmount = NSDecimalNumber(decimal: subtotal).stringValue
48+
49+
return currencyFormatter.formatAmount(subAmount, with: currency) ?? String()
50+
}
51+
52+
private func calculateNetAmount(currencyFormatter: CurrencyFormatter) -> NSDecimalNumber? {
3053
guard let orderTotal = currencyFormatter.convertToDecimal(total) else {
3154
return .zero
3255
}

0 commit comments

Comments
 (0)