1 <?php
2
3 4 5 6 7 8 9 10 11
12
13 MainFactory::load_class('OrderRepositoryWriterInterface');
14
15 16 17 18 19 20 21
22 class OrderRepositoryWriter implements OrderRepositoryWriterInterface
23 {
24 25 26 27
28 protected $db;
29
30
31 32 33 34 35
36 public function __construct(CI_DB_query_builder $db)
37 {
38 $this->db = $db;
39 }
40
41
42 43 44 45 46 47 48
49 public function insert(OrderInterface $order)
50 {
51 $record = $this->_serializeOrder($order);
52
53
54 $record['orders_hash'] = time() + mt_rand();
55
56 $this->db->insert('orders', $record);
57
58 return $this->db->insert_id();
59 }
60
61
62 63 64 65 66 67 68
69 public function update(OrderInterface $order)
70 {
71 $record = $this->_serializeOrder($order);
72 $this->db->update('orders', $record, array('orders_id' => $order->getOrderId()));
73 }
74
75
76 77 78 79 80 81 82 83
84 protected function _serializeOrder(OrderInterface $order)
85 {
86
87 $language = $this->db->get_where('languages', array('code' => strtolower((string)$order->getLanguageCode())))
88 ->row_array();
89
90 $record = array(
91 'customers_id' => $order->getCustomerId(),
92 'orders_status' => $order->getStatusId(),
93 'customers_cid' => (string)$order->getCustomerNumber(),
94 'customers_vat_id' => (string)$order->getVatIdNumber(),
95 'customers_status' => $order->getCustomerStatusInformation()->getStatusId(),
96 'customers_status_name' => $order->getCustomerStatusInformation()->getStatusName(),
97 'customers_status_image' => $order->getCustomerStatusInformation()->getStatusImage(),
98 'customers_status_discount' => $order->getCustomerStatusInformation()->getStatusDiscount(),
99 'account_type' => (int)$order->getCustomerStatusInformation()->isGuest(),
100 'payment_method' => $order->getPaymentType()->getTitle(),
101 'payment_class' => $order->getPaymentType()->getModule(),
102 'shipping_method' => $order->getShippingType()->getTitle(),
103 'shipping_class' => $order->getShippingType()->getModule(),
104 'comments' => $order->getComment(),
105 'date_purchased' => $order->getPurchaseDateTime()->format('Y-m-d H:i:s'),
106 'last_modified' => $order->getLastModifiedDateTime()->format('Y-m-d H:i:s'),
107 'language' => (string)$language['directory'],
108 'currency' => (string)$order->getCurrencyCode()->getCode(),
109 'currency_value' => (float)$order->getCurrencyCode()->getCurrencyValue(),
110 'customers_telephone' => $order->getCustomerTelephone(),
111 'customers_email_address' => $order->getCustomerEmail()
112 );
113
114 $customerAddress = $this->_serializeAddressBlock($order->getCustomerAddress(), new StringType('customers'));
115 $deliveryAddress = $this->_serializeAddressBlock($order->getDeliveryAddress(), new StringType('delivery'));
116 $billingAddress = $this->_serializeAddressBlock($order->getBillingAddress(), new StringType('billing'));
117
118 $record = array_merge($record, $customerAddress, $deliveryAddress, $billingAddress);
119
120 return $record;
121 }
122
123
124 125 126 127 128 129 130 131 132
133 protected function _serializeAddressBlock(AddressBlockInterface $addressBlock, StringType $prefix)
134 {
135 $country = $this->db->select('address_format_id')
136 ->get_where('countries', array('countries_id' => $addressBlock->getCountry()->getId()))
137 ->row_array();
138
139 $addressPrefix = $prefix->asString() . '_';
140
141 $addressBlockArray = array(
142 $addressPrefix . 'name' => (string)$addressBlock->getFirstname() . ' '
143 . (string)$addressBlock->getLastname(),
144 $addressPrefix . 'firstname' => (string)$addressBlock->getFirstname(),
145 $addressPrefix . 'lastname' => (string)$addressBlock->getLastname(),
146 $addressPrefix . 'gender' => (string)$addressBlock->getGender(),
147 $addressPrefix . 'company' => (string)$addressBlock->getCompany(),
148 $addressPrefix . 'street_address' => (string)$addressBlock->getStreet(),
149 $addressPrefix . 'suburb' => (string)$addressBlock->getSuburb(),
150 $addressPrefix . 'city' => (string)$addressBlock->getCity(),
151 $addressPrefix . 'postcode' => (string)$addressBlock->getPostcode(),
152 $addressPrefix . 'state' => (string)$addressBlock->getCountryZone()->getName(),
153 $addressPrefix . 'country' => (string)$addressBlock->getCountry()->getName(),
154 $addressPrefix . 'address_format_id' => $country['address_format_id']
155 );
156
157 if($prefix->asString() !== 'customers')
158 {
159 $addressBlockArray[$addressPrefix . 'country_iso_code_2'] = $addressBlock->getCountry()->getIso2();
160 }
161
162 return $addressBlockArray;
163 }
164 }