· 5 years ago · Dec 16, 2020, 08:56 AM
1class Demo{
2
3 enum PaymentType: String{
4 case assured = "AssuredPurchase"
5 case external = "ExternalUrl"
6 case dontSell = "DontWantSell"
7
8 func getAlias()-> String{
9 switch self {
10 case .dontSell: return "None"
11 default: return self.rawValue
12 }
13 }
14 }
15
16 enum InventoryType: String{
17 case limited = "Limited stock"
18 case unlimited = "Unlimited"
19 case outOfStock = "Out of stock"
20 }
21
22 func getInventoryType() -> InventoryType{
23 return .limited
24 }
25
26 func getPaymentType() -> PaymentType{
27 return .assured
28 }
29
30 func getKeySpecifications() -> [String: String]{
31 return [:]
32 }
33
34 func postCreateProduct(){
35 let inventoryType = getInventoryType()
36 var availabilityCount = 0
37 var isAvailable = false
38
39 if getPaymentType() == .dontSell{
40 availabilityCount = 0
41 isAvailable = false
42 }
43 else if getPaymentType() == .external{
44 availabilityCount = -1
45 isAvailable = true
46 }
47 else{
48 if inventoryType == .limited{
49 availabilityCount = Int.max// get input value
50 isAvailable = true
51 }
52 else if inventoryType == .unlimited{
53 availabilityCount = -1
54 isAvailable = true
55 }
56 else if inventoryType == .outOfStock{
57 availabilityCount = 0
58 isAvailable = false
59 }
60 }
61
62 var uniquepaymenturl /*key*/ = [String:String]()
63 uniquepaymenturl["url"] = "external url"
64 uniquepaymenturl["description"] = "website name"
65
66 let productBrand = ""
67 let productCategory = ""
68 let productName = ""
69 let productDesc = ""
70
71
72
73 var body = [String:Any]()
74 body["fpTag"] = DefaultUtils.shared.getFpTag()
75 body["clientId"] = DefaultUtils.shared.getClientId()
76
77 body["name"] = productName
78 body["description"] = productDesc
79 body["price"] = 100
80 body["discountAmount"] = 10 // int or double
81 body["currencycode"] = "INR"
82 body["tags"] = [String]()
83 body["isAvailable"] = isAvailable
84 body["availableUnits"] = availabilityCount
85 body["brandName"] = productBrand
86 body["category"] = productCategory
87 body["variants"] = false
88 body["uniquepaymenturl"] = uniquepaymenturl
89
90 let pickupAddrId = "from api"
91
92 if getPaymentType() == .assured{
93 body["pickupAddressReferenceId"] = pickupAddrId
94 }
95 body["paymentType"] = getPaymentType().getAlias()
96
97 var isCod = false // dynamic
98 var isOnlinePayment = false // dynamic
99
100 let codMaxQty = 10
101 let maxQty = 10
102 var codCount = 0
103 var onlineCount = 0
104
105 // Payment Method and Qty
106 if getPaymentType() == .assured{
107 if isCod{
108 codCount = codMaxQty
109 }
110 else{
111 codCount = 0
112 }
113
114 if isOnlinePayment{
115 onlineCount = maxQty
116 }
117 else{
118 onlineCount = 0
119 }
120 }
121
122 else if getPaymentType() == .external{
123 isOnlinePayment = false
124 onlineCount = 0
125 isCod = false
126 codCount = 0
127 }
128 else{
129 isOnlinePayment = false
130 onlineCount = 0
131 isCod = false
132 codCount = 0
133 }
134
135 if getPaymentType() == .assured {
136 body["isCodAvailable"] = isCod
137 body["isPrepaidOnlineAvailable"] = isOnlinePayment
138 body["maxCodOrders"] = codCount
139 body["maxPrepaidOnlineOrders"] = onlineCount
140 }
141 else if getPaymentType() == .external {
142 body["isCodAvailable"] = isCod
143 body["isPrepaidOnlineAvailable"] = isOnlinePayment
144 body["maxCodOrders"] = codCount
145 body["maxPrepaidOnlineOrders"] = maxQty
146 }
147 else {
148 body["isCodAvailable"] = isCod
149 body["isPrepaidOnlineAvailable"] = isOnlinePayment
150 body["maxCodOrders"] = codCount
151 body["maxPrepaidOnlineOrders"] = onlineCount
152 }
153
154 body["productType"] = "PRODUCT" // enum
155 let keySpecifications = getKeySpecifications()
156 let otherSpecifications = getKeySpecifications() // Other spe
157 body["keySpecification"] = keySpecifications
158 body["otherSpecifications"] = otherSpecifications
159 }
160
161
162
163}