22
33namespace AntonyThorpe \SilverShopUnleashed \Extension ;
44
5+ use SilverStripe \Core \Extension ;
56use DateTime ;
67use SilverStripe \Security \Member ;
78use SilverStripe \ORM \FieldType \DBDatetime ;
8- use SilverStripe \ORM \DataExtension ;
9- use SilverStripe \ORM \DataObject ;
109use SilverShop \Extension \ShopConfigExtension ;
1110use SilverShop \Model \Address ;
11+ use SilverShop \Model \Order ;
1212use AntonyThorpe \SilverShopUnleashed \UnleashedAPI ;
1313use AntonyThorpe \SilverShopUnleashed \Defaults ;
1414use AntonyThorpe \SilverShopUnleashed \Utils ;
1515
16- class Order extends DataExtension
16+
17+ /**
18+ * @property ?string $OrderSentToUnleashed
19+ * @extends Extension<Order&static>
20+ */
21+ class OrderExtension extends Extension
1722{
1823 /**
1924 * Record when an order is sent to Unleashed
@@ -28,7 +33,6 @@ class Order extends DataExtension
2833 */
2934 public function onBeforeWrite (): void
3035 {
31- parent ::onBeforeWrite ();
3236 if (!$ this ->getOwner ()->getField ("Guid " )) {
3337 $ this ->getOwner ()->Guid = Utils::createGuid ();
3438 }
@@ -44,14 +48,17 @@ public function getAddressName(array|Address $address): string
4448 if ($ address ['StreetAddress2 ' ]) {
4549 $ address_name .= ' ' . $ address ['StreetAddress2 ' ];
4650 }
51+
4752 $ address_name .= ' ' . $ address ['City ' ];
4853 } else {
4954 $ address_name = $ address ->Address ;
5055 if ($ address ->AddressLine2 ) {
5156 $ address_name .= ' ' . $ address ->AddressLine2 ;
5257 }
58+
5359 $ address_name .= ' ' . $ address ->City ;
5460 }
61+
5562 return $ address_name ;
5663 }
5764
@@ -65,16 +72,17 @@ public function matchCustomerAddress(array $items, array|Address $shipping_addre
6572 if ($ address ['AddressType ' ] != "Physical " && isset ($ items [0 ]['Addresses ' ][1 ])) {
6673 $ address = $ items [0 ]['Addresses ' ][1 ];
6774 }
75+
6876 return strtoupper ($ this ->getAddressName ($ shipping_address )) === strtoupper ($ this ->getAddressName ($ address ));
6977 }
7078
7179 /**
7280 * add the address components to the body array
7381 * $type is either Postal or Physical
7482 */
75- public function setBodyAddress (array $ body , DataObject $ order , string $ type ): array
83+ public function setBodyAddress (array $ body , Order $ order , string $ type ): array
7684 {
77- $ countries = (array ) ShopConfigExtension::config ()->iso_3166_country_codes ;
85+ $ countries = (array ) ShopConfigExtension::config ()->get ( ' iso_3166_country_codes ' ) ;
7886
7987 if ($ type === 'Postal ' ) {
8088 $ address = $ order ->BillingAddress ();
@@ -128,7 +136,7 @@ public function setBodyAddress(array $body, DataObject $order, string $type): ar
128136 /**
129137 * Add the currency code to the body array
130138 */
131- public function setBodyCurrencyCode (array $ body , DataObject $ order ): array
139+ public function setBodyCurrencyCode (array $ body , Order $ order ): array
132140 {
133141 $ body ['Currency ' ]['CurrencyCode ' ] = $ order ->Currency ();
134142 return $ body ;
@@ -137,39 +145,41 @@ public function setBodyCurrencyCode(array $body, DataObject $order): array
137145 /**
138146 * Add the Customer Code/Name (use Company field of BillingAddress to allow for B2B eCommerce sites)
139147 */
140- public function setBodyCustomerCodeAndName (array $ body , DataObject $ order ): array
148+ public function setBodyCustomerCodeAndName (array $ body , Order $ order ): array
141149 {
142- $ billing_address = $ order ->BillingAddress ();
143- if ($ billing_address ->Company ) {
150+ $ address = $ order ->BillingAddress ();
151+ if ($ address ->Company ) {
144152 // use Organisation name
145- $ body ['CustomerCode ' ] = $ billing_address ->Company ;
146- $ body ['CustomerName ' ] = $ billing_address ->Company ;
153+ $ body ['CustomerCode ' ] = $ address ->Company ;
154+ $ body ['CustomerName ' ] = $ address ->Company ;
147155 } else {
148156 // use Contact full name instead
149157 $ body ['CustomerCode ' ] = $ order ->getName ();
150158 $ body ['CustomerName ' ] = $ order ->getName ();
151159 }
160+
152161 return $ body ;
153162 }
154163
155164 /**
156165 * Set Delivery Method and Delivery Name
157166 * Allow for the SilverShop Shipping module
158167 */
159- public function setBodyDeliveryMethodAndDeliveryName (array $ body , DataObject $ order , string $ shipping_modifier_class_name ): array
168+ public function setBodyDeliveryMethodAndDeliveryName (array $ body , Order $ order , string $ shipping_modifier_class_name ): array
160169 {
161170 $ shipping_modifier = $ order ->getModifier ($ shipping_modifier_class_name );
162171 if (!empty ($ shipping_modifier )) {
163172 $ body ['DeliveryMethod ' ] = $ shipping_modifier ::config ()->product_code ;
164173 $ body ['DeliveryName ' ] = $ shipping_modifier ::config ()->product_code ;
165174 }
175+
166176 return $ body ;
167177 }
168178
169179 /**
170180 * Set Sales Order Lines
171181 */
172- public function setBodySalesOrderLines (array $ body , DataObject $ order , string $ tax_modifier_class_name , int $ rounding_precision ): array
182+ public function setBodySalesOrderLines (array $ body , Order $ order , string $ tax_modifier_class_name , int $ rounding_precision ): array
173183 {
174184 $ line_number = 0 ;
175185
@@ -198,14 +208,15 @@ public function setBodySalesOrderLines(array $body, DataObject $order, string $t
198208 );
199209 $ sales_order_line ['LineTaxCode ' ] = $ body ['Tax ' ]['TaxCode ' ];
200210 }
211+
201212 $ body ['SalesOrderLines ' ][] = $ sales_order_line ;
202213 }
203214
204215 // Add Modifiers that have a product_code
205216 foreach ($ order ->Modifiers ()->sort ('Sort ' )->getIterator () as $ modifier ) {
206217 $ line_total = round (floatval ($ modifier ->Amount ), $ rounding_precision );
207218
208- if ($ modifier ::config ()->product_code &&
219+ if ($ modifier ::config ()->get ( ' product_code ' ) &&
209220 $ modifier ->Type !== 'Ignored ' &&
210221 !empty ($ line_total )
211222 ) {
@@ -218,7 +229,7 @@ public function setBodySalesOrderLines(array $body, DataObject $order, string $t
218229 'LineType ' => null ,
219230 'OrderQuantity ' => 1 ,
220231 'Product ' => [
221- 'ProductCode ' => $ modifier ::config ()->product_code ,
232+ 'ProductCode ' => $ modifier ::config ()->get ( ' product_code ' ) ,
222233 ],
223234 'UnitPrice ' => round (floatval ($ modifier ->Amount ), $ rounding_precision )
224235 ];
@@ -230,16 +241,18 @@ public function setBodySalesOrderLines(array $body, DataObject $order, string $t
230241 );
231242 $ sales_order_line ['LineTaxCode ' ] = $ body ['Tax ' ]['TaxCode ' ];
232243 }
244+
233245 $ body ['SalesOrderLines ' ][] = $ sales_order_line ;
234246 }
235247 }
248+
236249 return $ body ;
237250 }
238251
239252 /**
240253 * Set the Tax Codes
241254 */
242- public function setBodyTaxCode (array $ body , DataObject $ order , string $ tax_modifier_class_name ): array
255+ public function setBodyTaxCode (array $ body , Order $ order , string $ tax_modifier_class_name ): array
243256 {
244257 if ($ tax_modifier_class_name !== '' && $ tax_modifier_class_name !== '0 ' ) {
245258 $ tax_modifier = $ order ->getModifier ($ tax_modifier_class_name );
@@ -248,14 +261,15 @@ public function setBodyTaxCode(array $body, DataObject $order, string $tax_modif
248261 $ body ['Tax ' ]['TaxCode ' ] = $ tax_modifier ::config ()->tax_code ;
249262 }
250263 }
264+
251265 return $ body ;
252266 }
253267
254268
255269 /**
256270 * Calculate the SubTotal and TaxTotal
257271 */
258- public function setBodySubTotalAndTax (array $ body , DataObject $ order , string $ tax_modifier_class_name , int $ rounding_precision ): array
272+ public function setBodySubTotalAndTax (array $ body , Order $ order , string $ tax_modifier_class_name , int $ rounding_precision ): array
259273 {
260274 if ($ tax_modifier_class_name !== '' && $ tax_modifier_class_name !== '0 ' ) {
261275 $ tax_modifier = $ order ->getModifier ($ tax_modifier_class_name );
@@ -276,6 +290,7 @@ public function setBodySubTotalAndTax(array $body, DataObject $order, string $ta
276290 $ rounding_precision
277291 );
278292 }
293+
279294 $ body ['TaxTotal ' ] = $ tax_total ;
280295 $ body ['SubTotal ' ] = $ sub_total ;
281296
@@ -290,6 +305,7 @@ public function setBodySubTotalAndTax(array $body, DataObject $order, string $ta
290305 } else {
291306 $ body ['SubTotal ' ] = round (floatval ($ order ->Total ()), $ rounding_precision );
292307 }
308+
293309 return $ body ;
294310 }
295311
@@ -299,11 +315,10 @@ public function setBodySubTotalAndTax(array $body, DataObject $order, string $ta
299315 */
300316 public function onAfterWrite (): void
301317 {
302- parent ::onAfterWrite ();
303318 $ config = $ this ->getOwner ()->config ();
304- $ defaults = Defaults::config ();
319+ $ configForClass = Defaults::config ();
305320
306- if ($ defaults ->get ('send_sales_orders_to_unleashed ' )
321+ if ($ configForClass ->get ('send_sales_orders_to_unleashed ' )
307322 && $ this ->getOwner ()->Status == 'Paid '
308323 && !$ this ->getOwner ()->OrderSentToUnleashed ) {
309324 // Definitions
@@ -319,10 +334,10 @@ public function onAfterWrite(): void
319334 'Guid ' => $ order ->Guid ,
320335 'OrderDate ' => $ date_placed ->format ('Y-m-d\TH:i:s ' ),
321336 'OrderNumber ' => $ order ->Reference ,
322- 'OrderStatus ' => $ defaults ->get ('order_status ' ),
337+ 'OrderStatus ' => $ configForClass ->get ('order_status ' ),
323338 'PaymentDueDate ' => $ date_paid ->format ('Y-m-d\TH:i:s ' ),
324- 'PaymentTerm ' => $ defaults ->get ('payment_term ' ),
325- 'PrintPackingSlipInsteadOfInvoice ' => $ defaults ->get ('print_packingslip_instead_of_invoice ' ),
339+ 'PaymentTerm ' => $ configForClass ->get ('payment_term ' ),
340+ 'PrintPackingSlipInsteadOfInvoice ' => $ configForClass ->get ('print_packingslip_instead_of_invoice ' ),
326341 'ReceivedDate ' => $ date_placed ->format ('Y-m-d\TH:i:s ' ),
327342 'SalesOrderLines ' => [],
328343 'SellPriceTier ' => ShopConfigExtension::current ()->CustomerGroup ()->Title ,
@@ -335,38 +350,40 @@ public function onAfterWrite(): void
335350 $ body = $ this ->setBodyAddress ($ body , $ order , 'Physical ' );
336351 $ body = $ this ->setBodyCurrencyCode ($ body , $ order );
337352 $ body = $ this ->setBodyCustomerCodeAndName ($ body , $ order );
338- $ body = $ this ->setBodyDeliveryMethodAndDeliveryName ($ body , $ order , $ defaults ->get ('shipping_modifier_class_name ' ));
339- $ body = $ this ->setBodyTaxCode ($ body , $ order , $ defaults ->get ('tax_modifier_class_name ' ));
340- $ body = $ this ->setBodySalesOrderLines ($ body , $ order , $ defaults ->get ('tax_modifier_class_name ' ), $ config ->get ('rounding_precision ' ));
341- $ body = $ this ->setBodySubTotalAndTax ($ body , $ order , $ defaults ->get ('tax_modifier_class_name ' ), $ config ->get ('rounding_precision ' ));
353+ $ body = $ this ->setBodyDeliveryMethodAndDeliveryName ($ body , $ order , $ configForClass ->get ('shipping_modifier_class_name ' ));
354+ $ body = $ this ->setBodyTaxCode ($ body , $ order , $ configForClass ->get ('tax_modifier_class_name ' ));
355+ $ body = $ this ->setBodySalesOrderLines ($ body , $ order , $ configForClass ->get ('tax_modifier_class_name ' ), $ config ->get ('rounding_precision ' ));
356+ $ body = $ this ->setBodySubTotalAndTax ($ body , $ order , $ configForClass ->get ('tax_modifier_class_name ' ), $ config ->get ('rounding_precision ' ));
342357
343358 // Add optional defaults
344- if ($ defaults ->get ('created_by ' )) {
345- $ body ['CreatedBy ' ] = $ defaults ->get ('created_by ' );
359+ if ($ configForClass ->get ('created_by ' )) {
360+ $ body ['CreatedBy ' ] = $ configForClass ->get ('created_by ' );
346361 }
347362
348- if ($ defaults ->get ('customer_type ' )) {
349- $ body ['CustomerType ' ] = $ defaults ->get ('customer_type ' );
363+ if ($ configForClass ->get ('customer_type ' )) {
364+ $ body ['CustomerType ' ] = $ configForClass ->get ('customer_type ' );
350365 }
351366
352- if ($ defaults ->get ('sales_order_group ' )) {
353- $ body ['SalesOrderGroup ' ] = $ defaults ->get ('sales_order_group ' );
367+ if ($ configForClass ->get ('sales_order_group ' )) {
368+ $ body ['SalesOrderGroup ' ] = $ configForClass ->get ('sales_order_group ' );
354369 }
355370
356- if ($ defaults ->get ('source_id ' )) {
357- $ body ['SourceId ' ] = $ defaults ->get ('source_id ' );
371+ if ($ configForClass ->get ('source_id ' )) {
372+ $ body ['SourceId ' ] = $ configForClass ->get ('source_id ' );
358373 }
359374
360375 // add phone number if available
361- if ($ order ->BillingAddress ()->Phone ) {
362- $ body ['PhoneNumber ' ] = $ order ->BillingAddress ()->Phone ;
376+ $ billing_address = $ order ->BillingAddress ();
377+ if ($ billing_address ->exists () && $ billing_address ->Phone ) {
378+ $ body ['PhoneNumber ' ] = $ billing_address ->Phone ;
363379 }
364380
365381 // add required date
366382 $ date_required = new DateTime ($ order ->Paid );
367- if ($ defaults ->get ('expected_days_to_deliver ' )) {
368- $ date_required ->modify ('+ ' . $ defaults ->get ('expected_days_to_deliver ' ) . 'day ' );
383+ if ($ configForClass ->get ('expected_days_to_deliver ' )) {
384+ $ date_required ->modify ('+ ' . $ configForClass ->get ('expected_days_to_deliver ' ) . 'day ' );
369385 }
386+
370387 $ body ['RequiredDate ' ] = $ date_required ->format ('Y-m-d\TH:i:s ' );
371388
372389 if ($ order ->Notes ) {
@@ -416,6 +433,7 @@ public function onAfterWrite(): void
416433 if ($ body ['Comments ' ]) {
417434 $ body ['Comments ' ] .= '. ' ;
418435 }
436+
419437 $ body ['Comments ' ] .= _t (
420438 'UnleashedAPI.addEmailToCustomerComment ' ,
421439 'Add email to Customer: {email_address} ' ,
0 commit comments