1 <?php
2 3 4 5 6 7 8 9 10
11
12 MainFactory::load_class('OrderRepositoryReaderInterface');
13
14 15 16 17 18 19 20
21 class OrderRepositoryReader implements OrderRepositoryReaderInterface
22 {
23 24 25 26
27 protected $db;
28
29 30 31 32
33 protected $orderFactory;
34
35 36 37 38
39 protected $countryService;
40
41
42 43 44 45 46 47 48
49 public function __construct(CI_DB_query_builder $db,
50 OrderFactoryInterface $orderFactory,
51 CountryServiceInterface $countryService)
52 {
53 $this->db = $db;
54 $this->orderFactory = $orderFactory;
55 $this->countryService = $countryService;
56 }
57
58
59 60 61 62 63 64 65 66
67 public function getById(IdType $orderId)
68 {
69 $this->db->select('orders.*, languages.code AS language_code')
70 ->from('orders')
71 ->join('languages', 'languages.directory = orders.language', 'inner')
72 ->where('orders_id', $orderId->asInt());
73
74 $data = $this->db->get()->row_array();
75
76 if($data === null)
77 {
78 throw new UnexpectedValueException('The requested Order was not found in database (ID:' . $orderId->asInt()
79 . ')');
80 }
81
82 $order = $this->_createOrderByArray($data);
83
84 return $order;
85 }
86
87
88 89 90 91 92 93 94
95 protected function _createOrderByArray(array $data)
96 {
97 $order = $this->orderFactory->createOrder();
98
99
100 $order->setOrderId(new IdType($data['orders_id']));
101 $order->setCustomerId(new IdType($data['customers_id']));
102 $order->setStatusId(new IdType($data['orders_status']));
103
104
105 $isGuest = ((int)$data['account_type'] === 1 || (int)$data['customers_status'] === 1);
106 $customerStatusInformation = MainFactory::create('CustomerStatusInformation',
107 new IdType((int)$data['customers_status']),
108 new StringType((string)$data['customers_status_name']),
109 new StringType((string)$data['customers_status_image']),
110 new DecimalType((double)$data['customers_status_discount']),
111 new BoolType($isGuest));
112 $order->setCustomerStatusInformation($customerStatusInformation);
113
114
115 $customerAddress = $this->_getOrderAddressBlock($data, new StringType('customers'));
116 $order->setCustomerAddress($customerAddress);
117 $billingAddress = $this->_getOrderAddressBlock($data, new StringType('billing'));
118 $order->setBillingAddress($billingAddress);
119 $deliveryAddress = $this->_getOrderAddressBlock($data, new StringType('delivery'));
120 $order->setDeliveryAddress($deliveryAddress);
121 $paymentType = MainFactory::create('OrderPaymentType', new StringType($data['payment_method']),
122 new StringType($data['payment_class']));
123
124
125 $order->setPaymentType($paymentType);
126 $shippingType = MainFactory::create('OrderShippingType', new StringType($data['shipping_method']),
127 new StringType($data['shipping_class']));
128 $order->setShippingType($shippingType);
129
130
131 $order->setCustomerNumber(new StringType((string)$data['customers_cid']));
132 $order->setCustomerEmail(new EmailStringType($data['customers_email_address']));
133 $order->setCustomerTelephone(new StringType($data['customers_telephone']));
134 $order->setVatIdNumber(new StringType((string)$data['customers_vat_id']));
135
136 $order->setCurrencyCode(MainFactory::create('CurrencyCode', new NonEmptyStringType((string)$data['currency']),
137 new DecimalType((double)$data['currency_value'])));
138 $order->setLanguageCode(new LanguageCode(new NonEmptyStringType($data['language_code'])));
139 $order->setPurchaseDateTime(new DateTime((string)$data['date_purchased']));
140 $order->setLastModifiedDateTime(new DateTime((string)$data['last_modified']));
141 $order->setComment(new StringType((string)$data['comments']));
142 $order->setOrderHash(new StringType($data['orders_hash']));
143
144 return $order;
145 }
146
147
148 149 150 151 152 153 154 155
156 protected function _getOrderAddressBlock(array $data, StringType $prefix)
157 {
158 $addressPrefix = $prefix->asString() . '_';
159
160 $country = $this->countryService->getCountryByName($data[$addressPrefix . 'country']);
161 $zone = $this->countryService->getUnknownCountryZoneByName((string)$data[$addressPrefix . 'state']);
162
163 return MainFactory::create('AddressBlock', new CustomerGender($data[$addressPrefix . 'gender']),
164 new CustomerFirstname($data[$addressPrefix . 'firstname']),
165 new CustomerLastname($data[$addressPrefix . 'lastname']),
166 new CustomerCompany((string)$data[$addressPrefix . 'company']),
167 new CustomerB2BStatus(false),
168 new CustomerStreet($data[$addressPrefix . 'street_address']),
169 new CustomerSuburb((string)$data[$addressPrefix . 'suburb']),
170 new CustomerPostcode($data[$addressPrefix . 'postcode']),
171 new CustomerCity($data[$addressPrefix . 'city']), $country, $zone);
172 }
173 }