Source of file OrderHelpers.php
Size: 6,311 Bytes - Last Modified: 2021-12-24T07:10:20+00:00
/var/www/docs.ssmods.com/process/src/src/Api/OrderHelpers.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 | <?php namespace Sunnysideup\EcommerceAdvanceRetailConnector\Api; use SilverStripe\Core\Config\Config; use Sunnysideup\Ecommerce\Config\EcommerceConfig; use Sunnysideup\Ecommerce\Model\Order; use Sunnysideup\EcommerceDelivery\Modifiers\PickUpOrDeliveryModifier; use Sunnysideup\EcommerceTax\Modifiers\GSTTaxModifier; /* * A set of static functions to convert * SilverSripe Order data into the array data required by the AR API */ class OrderHelpers { private static $shipping_product_item_id = 0; public static function get_line_items(?Order $order, ?int $branchId, ?int $workstationId) { $lineItems = []; $isTaxInclusive = ! (bool) EcommerceConfig::inst()->ShopPricesAreTaxExclusive; $taxModifier = null; $shippingModifier = null; $orderItems = $order->OrderItems(); //find out if there is a shiping cost that needs to be added as a line item $modifiers = $order->Modifiers(); foreach ($modifiers as $modifier) { if (is_a($modifier, PickUpOrDeliveryModifier::class) && ! $shippingModifier) { $shippingModifier = $modifier; } if (is_a($modifier, GSTTaxModifier::class) && ! $taxModifier) { $taxModifier = $modifier; } } $lineNumber = 0; if ($orderItems->exists()) { $lineNumber = 1; foreach ($orderItems as $orderItem) { $lineItems[] = [ 'branchId' => $branchId, 'workstationId' => $workstationId, 'customerOrderId' => 0, // what should we put here? 'lineNumber' => $lineNumber, 'itemId' => $orderItem->getInternalItemID(), 'description' => $orderItem->getBuyableMoreDetails(), 'quantity' => $orderItem->Quantity, 'unitPrice' => $orderItem->getUnitPrice(), 'unitCost' => 0, // what should we put here? 'tax' => round($taxModifier->getTotalTaxPerLineItem($orderItem)), 'isTaxInclusive' => $isTaxInclusive, 'note' => '', 'promoCode' => 0, //to be added 'lineReference' => 0, // what should we put here? 'purchaseOrderId' => 0, // what should we put here? 'takenQuantity' => 0, // what should we put here? 'totalAmount' => $orderItem->CalculatedTotal, ]; ++$lineNumber; } } $shippingItemID = Config::inst()->get(OrderHelpers::class, 'shipping_product_item_id'); if ($shippingItemID && $shippingModifier) { //find out if there is a shiping cost that needs to be added as a line item if ($shippingModifier->CalculatedTotal > 0) { $lineItems[] = [ 'branchId' => $branchId, 'workstationId' => $workstationId, 'customerOrderId' => 0, 'lineNumber' => $lineNumber, 'itemId' => $shippingItemID, //what is the itemID for outside of NZ? 'description' => $shippingModifier->Name, 'quantity' => 1, 'unitPrice' => $shippingModifier->CalculatedTotal, 'unitCost' => 0, 'tax' => round($taxModifier->simpleTaxCalculation($shippingModifier->CalculatedTotal)), 'isTaxInclusive' => $isTaxInclusive, 'note' => '', 'promoCode' => 0, 'lineReference' => 0, 'purchaseOrderId' => 0, 'takenQuantity' => 0, 'totalAmount' => $shippingModifier->CalculatedTotal, ]; } } return $lineItems; } public static function get_payments(?Order $order) { $payments = []; $orderPayments = $order->Payments(); if ($orderPayments->exists()) { foreach ($orderPayments as $orderPayment) { $payments[] = [ 'id' => '', //what should this be? - if it is more than one character we get this error: The field TenderId must be a string or array type with a maximum length of '1' 'description' => $orderPayment->Message, 'amount' => $orderPayment->AmountAmount, 'reference' => $orderPayment->Status, // or should it be the credit card reference? 'currencyCode' => $orderPayment->AmountCurrency, ]; } } return $payments; } public static function get_address(?Order $order, ?string $addressType = 'BillingAddress') { //required fields $address = [ 'id' => 0, 'name' => '', 'address' => [ '', ], 'postCode' => '', 'phone' => '', 'extension' => '', 'fax' => '', 'mobile' => '', 'email' => '', 'dpid' => '', 'barcode' => '', 'narrative' => '', ]; if ('ShippingAddress' === $addressType && ! $order->UseShippingAddress) { $addressType = 'BillingAddress'; } $orderAddress = $order->{$addressType}(); if ($orderAddress && $orderAddress->exists()) { $address = [ 'id' => 0, //should this come from AR? 'name' => $orderAddress->FirstName . ' ' . $orderAddress->Surname, 'address' => [ $orderAddress->Address, $orderAddress->Address2, $orderAddress->City, $orderAddress->Country, ], 'postCode' => $orderAddress->PostalCode, 'phone' => $orderAddress->Phone, 'extension' => '', 'fax' => '', 'mobile' => '', 'email' => $orderAddress->Email, 'dpid' => '', 'barcode' => '', 'narrative' => '', ]; } return $address; } } |