1
- using System ;
1
+ using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using Newtonsoft . Json . Linq ;
4
5
using Umbraco . Commerce . Core . Api ;
5
6
using Umbraco . Commerce . Core . Models ;
6
7
using Umbraco . Commerce . Deploy . Artifacts ;
7
8
using Umbraco . Commerce . Deploy . Configuration ;
8
-
9
9
using Umbraco . Cms . Core ;
10
10
using Umbraco . Cms . Core . Deploy ;
11
11
12
+ using StringExtensions = Umbraco . Commerce . Extensions . StringExtensions ;
13
+
12
14
namespace Umbraco . Commerce . Deploy . Connectors . ServiceConnectors
13
15
{
14
16
[ UdiDefinition ( UmbracoCommerceConstants . UdiEntityType . ShippingMethod , UdiType . GuidUdi ) ]
@@ -61,6 +63,11 @@ public override ShippingMethodArtifact GetArtifact(GuidUdi udi, ShippingMethodRe
61
63
Alias = entity . Alias ,
62
64
Sku = entity . Sku ,
63
65
ImageId = entity . ImageId , // Could be a UDI?
66
+ CalculationMode = entity . CalculationMode ,
67
+ ShippingProviderAlias = entity . ShippingProviderAlias ,
68
+ ShippingProviderSettings = new SortedDictionary < string , string > ( entity . ShippingProviderSettings
69
+ . Where ( x => ! StringExtensions . InvariantContains ( _settingsAccessor . Settings . ShippingMethods . IgnoreSettings , x . Key ) ) // Ignore any settings that shouldn't be transfered
70
+ . ToDictionary ( x => x . Key , x => x . Value ) ) , // Could contain UDIs?
64
71
SortOrder = entity . SortOrder
65
72
} ;
66
73
@@ -75,12 +82,16 @@ public override ShippingMethodArtifact GetArtifact(GuidUdi udi, ShippingMethodRe
75
82
artifact . TaxClassUdi = taxClassDepUdi ;
76
83
}
77
84
78
- // Service prices
79
- if ( entity . Prices . Count > 0 )
85
+ // Only the fixed rate shipping provider has delcared dependencies
86
+ // that we can deserialize. Dynamic can have a dependency, but because
87
+ // it's config is all plugin based, we can't be certain of the data structure
88
+ // and so we just have to pass the value through. Realtime rates don't currently
89
+ // have any dependencies.
90
+ if ( entity . CalculationMode == ShippingCalculationMode . Fixed && entity . CalculationConfig is FixedRateShippingCalculationConfig calcConfig )
80
91
{
81
92
var servicesPrices = new List < ServicePriceArtifact > ( ) ;
82
93
83
- foreach ( var price in entity . Prices )
94
+ foreach ( var price in calcConfig . Prices )
84
95
{
85
96
var spArtifact = new ServicePriceArtifact { Value = price . Value } ;
86
97
@@ -117,7 +128,15 @@ public override ShippingMethodArtifact GetArtifact(GuidUdi udi, ShippingMethodRe
117
128
servicesPrices . Add ( spArtifact ) ;
118
129
}
119
130
120
- artifact . Prices = servicesPrices ;
131
+ artifact . CalculationConfig = JObject . FromObject ( new FixedRateShippingCalculationConfigArtifact
132
+ {
133
+ Prices = servicesPrices
134
+ } ) ;
135
+ }
136
+ else
137
+ {
138
+ // No additional processing required
139
+ artifact . CalculationConfig = JObject . FromObject ( entity . CalculationConfig ) ;
121
140
}
122
141
123
142
// Allowed country regions
@@ -183,11 +202,16 @@ private void Pass2(ArtifactDeployState<ShippingMethodArtifact, ShippingMethodRea
183
202
artifact . Udi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . ShippingMethod ) ;
184
203
artifact . StoreUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . Store ) ;
185
204
186
- var entity = state . Entity ? . AsWritable ( uow ) ?? ShippingMethod . Create ( uow , artifact . Udi . Guid , artifact . StoreUdi . Guid , artifact . Alias , artifact . Name ) ;
205
+ var entity = state . Entity ? . AsWritable ( uow ) ?? ShippingMethod . Create ( uow , artifact . Udi . Guid , artifact . StoreUdi . Guid , artifact . Alias , artifact . Name , artifact . ShippingProviderAlias , artifact . CalculationMode ) ;
206
+
207
+ var settings = artifact . ShippingProviderSettings
208
+ . Where ( x => ! StringExtensions . InvariantContains ( _settingsAccessor . Settings . ShippingMethods . IgnoreSettings , x . Key ) ) // Ignore any settings that shouldn't be transfered
209
+ . ToDictionary ( x => x . Key , x => x . Value ) ;
187
210
188
211
entity . SetName ( artifact . Name , artifact . Alias )
189
212
. SetSku ( artifact . Sku )
190
213
. SetImage ( artifact . ImageId )
214
+ . SetSettings ( settings , SetBehavior . Merge )
191
215
. SetSortOrder ( artifact . SortOrder ) ;
192
216
193
217
_umbracoCommerceApi . SaveShippingMethod ( entity ) ;
@@ -209,62 +233,48 @@ private void Pass4(ArtifactDeployState<ShippingMethodArtifact, ShippingMethodRea
209
233
if ( artifact . TaxClassUdi != null )
210
234
{
211
235
artifact . TaxClassUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . TaxClass ) ;
212
- // TODO: Check the payment method exists?
236
+
213
237
entity . SetTaxClass ( artifact . TaxClassUdi . Guid ) ;
214
238
}
215
239
else
216
240
{
217
241
entity . ClearTaxClass ( ) ;
218
242
}
219
243
220
- // Prices
221
- var pricesToRemove = entity . Prices
222
- . Where ( x => artifact . Prices == null || ! artifact . Prices . Any ( y => y . CountryUdi ? . Guid == x . CountryId
223
- && y . RegionUdi ? . Guid == x . RegionId
224
- && y . CurrencyUdi . Guid == x . CurrencyId ) )
225
- . ToList ( ) ;
226
-
227
- if ( artifact . Prices != null )
244
+ // Calculation config
245
+ if ( artifact . CalculationConfig != null )
228
246
{
229
- foreach ( var price in artifact . Prices )
247
+ if ( artifact . CalculationMode == ShippingCalculationMode . Fixed )
230
248
{
231
- price . CurrencyUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . Currency ) ;
249
+ var cfgArtifact = artifact . CalculationConfig . ToObject < FixedRateShippingCalculationConfigArtifact > ( ) ;
250
+ var prices = new List < ServicePrice > ( ) ;
232
251
233
- if ( price . CountryUdi == null && price . RegionUdi == null )
252
+ foreach ( var price in cfgArtifact . Prices )
234
253
{
235
- entity . SetDefaultPriceForCurrency ( price . CurrencyUdi . Guid , price . Value ) ;
236
- }
237
- else
238
- {
239
- price . CountryUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . Country ) ;
254
+ price . CurrencyUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . Currency ) ;
255
+
256
+ if ( price . CountryUdi != null )
257
+ price . CountryUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . Country ) ;
240
258
241
259
if ( price . RegionUdi != null )
242
- {
243
260
price . RegionUdi . EnsureType ( UmbracoCommerceConstants . UdiEntityType . Region ) ;
244
261
245
- entity . SetRegionPriceForCurrency ( price . CountryUdi . Guid , price . RegionUdi . Guid , price . CurrencyUdi . Guid , price . Value ) ;
246
- }
247
- else
248
- {
249
- entity . SetCountryPriceForCurrency ( price . CountryUdi . Guid , price . CurrencyUdi . Guid , price . Value ) ;
250
- }
262
+ prices . Add ( new ServicePrice ( price . Value , price . CurrencyUdi . Guid , price . CountryUdi ? . Guid , price . RegionUdi ? . Guid ) ) ;
251
263
}
252
- }
253
- }
254
264
255
- foreach ( var price in pricesToRemove )
256
- {
257
- if ( price . CountryId == null && price . RegionId == null )
265
+ entity . SetCalculationConfig ( new FixedRateShippingCalculationConfig ( prices ) ) ;
266
+ }
267
+ else if ( artifact . CalculationMode == ShippingCalculationMode . Dynamic )
258
268
{
259
- entity . ClearDefaultPriceForCurrency ( price . CurrencyId ) ;
269
+ entity . SetCalculationConfig ( artifact . CalculationConfig . ToObject < DynamicRateShippingCalculationConfig > ( ) ) ;
260
270
}
261
- else if ( price . CountryId != null && price . RegionId == null )
271
+ else if ( artifact . CalculationMode == ShippingCalculationMode . Realtime )
262
272
{
263
- entity . ClearCountryPriceForCurrency ( price . CountryId . Value , price . CurrencyId ) ;
273
+ entity . SetCalculationConfig ( artifact . CalculationConfig . ToObject < RealtimeRateShippingCalculationConfig > ( ) ) ;
264
274
}
265
275
else
266
276
{
267
- entity . ClearRegionPriceForCurrency ( price . CountryId . Value , price . RegionId . Value , price . CurrencyId ) ;
277
+ throw new ApplicationException ( $ "Unknown calculation mode: { artifact . CalculationMode } " ) ;
268
278
}
269
279
}
270
280
0 commit comments