1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
<?php
MainFactory::load_class('OrderRepositoryReaderInterface');
class OrderRepositoryReader implements OrderRepositoryReaderInterface
{
protected $db;
protected $orderFactory;
protected $countryService;
protected $paymentTitleProvider;
public function __construct(CI_DB_query_builder $db,
OrderFactoryInterface $orderFactory,
CountryServiceInterface $countryService,
PaymentTitleProvider $paymentTitleProvider)
{
$this->db = $db;
$this->orderFactory = $orderFactory;
$this->countryService = $countryService;
$this->paymentTitleProvider = $paymentTitleProvider;
}
public function getById(IdType $orderId)
{
$this->db->select('orders.*, languages.code AS language_code')
->from('orders')
->join('languages', 'languages.directory = orders.language', 'inner')
->where('orders_id', $orderId->asInt());
$data = $this->db->get()->row_array();
if($data === null)
{
throw new UnexpectedValueException('The requested Order was not found in database (ID:' . $orderId->asInt()
. ')');
}
$order = $this->_createOrderByArray($data);
return $order;
}
protected function _createOrderByArray(array $data)
{
$order = $this->orderFactory->createOrder();
$order->setOrderId(new IdType($data['orders_id']));
$order->setCustomerId(new IdType($data['customers_id']));
$order->setStatusId(new IdType($data['orders_status']));
$isGuest = ((int)$data['account_type'] === 1 || (int)$data['customers_status'] === 1);
$customerStatusInformation = MainFactory::create('CustomerStatusInformation',
new IdType((int)$data['customers_status']),
new StringType((string)$data['customers_status_name']),
new StringType((string)$data['customers_status_image']),
new DecimalType((double)$data['customers_status_discount']),
new BoolType($isGuest));
$order->setCustomerStatusInformation($customerStatusInformation);
$customerAddress = $this->_getOrderAddressBlock($data, new StringType('customers'));
$order->setCustomerAddress($customerAddress);
$billingAddress = $this->_getOrderAddressBlock($data, new StringType('billing'));
$order->setBillingAddress($billingAddress);
$deliveryAddress = $this->_getOrderAddressBlock($data, new StringType('delivery'));
$order->setDeliveryAddress($deliveryAddress);
$paymentType = $this->_createOrderPaymentType($data);
$order->setPaymentType($paymentType);
$shippingType = MainFactory::create('OrderShippingType', new StringType($data['shipping_method']),
new StringType($data['shipping_class']));
$order->setShippingType($shippingType);
$order->setCustomerNumber(new StringType((string)$data['customers_cid']));
$order->setCustomerEmail(new EmailStringType($data['customers_email_address']));
$order->setCustomerTelephone(new StringType($data['customers_telephone']));
$order->setVatIdNumber(new StringType((string)$data['customers_vat_id']));
$order->setTotalWeight(new DecimalType((double)$data['order_total_weight']));
$order->setCurrencyCode(MainFactory::create('CurrencyCode', new NonEmptyStringType((string)$data['currency']),
new DecimalType((double)$data['currency_value'])));
$order->setLanguageCode(new LanguageCode(new NonEmptyStringType($data['language_code'])));
$order->setPurchaseDateTime(new EmptyDateTime((string)$data['date_purchased']));
$order->setLastModifiedDateTime(new EmptyDateTime((string)$data['last_modified']));
$order->setComment(new StringType((string)$data['comments']));
if(!empty($data['orders_hash']))
{
$order->setOrderHash(new NonEmptyStringType($data['orders_hash']));
}
return $order;
}
protected function _getOrderAddressBlock(array $data, StringType $prefix)
{
$addressPrefix = $prefix->asString() . '_';
$country = $this->countryService->findCountryByName($data[$addressPrefix . 'country']);
$zone = $this->countryService->getUnknownCountryZoneByName((string)$data[$addressPrefix . 'state']);
if($this->countryService->countryHasCountryZones($country)
&& $this->countryService->countryZoneExistsInCountry($zone, $country))
{
$zone = $this->countryService->getCountryZoneByNameAndCountry((string)$data[$addressPrefix . 'state'],
$country);
}
return MainFactory::create('AddressBlock', new CustomerGender($data[$addressPrefix . 'gender']),
new CustomerFirstname($data[$addressPrefix . 'firstname']),
new CustomerLastname($data[$addressPrefix . 'lastname']),
new CustomerCompany((string)$data[$addressPrefix . 'company']),
new CustomerB2BStatus(false),
new CustomerStreet($data[$addressPrefix . 'street_address']),
new CustomerHouseNumber($data[$addressPrefix . 'house_number']),
new CustomerAdditionalAddressInfo($data[$addressPrefix . 'additional_info']),
new CustomerSuburb((string)$data[$addressPrefix . 'suburb']),
new CustomerPostcode($data[$addressPrefix . 'postcode']),
new CustomerCity($data[$addressPrefix . 'city']), $country, $zone);
}
protected function _createOrderPaymentType(array $row)
{
$paymentMethodKey = 'payment_method';
$paymentClassKey = 'payment_class';
$explodedMethodName = explode('_', $row[$paymentMethodKey]);
$method = (count($explodedMethodName) === 2
&& $explodedMethodName[0]
=== $explodedMethodName[1]) ? $explodedMethodName[0] : $row[$paymentMethodKey];
$title = $method ? $this->paymentTitleProvider->title($method) : '';
$explodedClassName = explode('_', $row[$paymentClassKey]);
$class = (count($explodedClassName) === 2
&& $explodedClassName[0] === $explodedClassName[1]) ? $explodedClassName[0] : $row[$paymentClassKey];
$configurationValue = $this->db->get_where('configuration', [
'configuration_key' => 'MODULE_PAYMENT_' . strtoupper($class) . '_ALIAS'
])->row()->configuration_value;
$alias = $configurationValue ? new StringType($configurationValue) : null;
return MainFactory::create('OrderPaymentType', new StringType($title),
new StringType((string)$row[$paymentClassKey]), $alias);
}
}