1 <?php
2
3 /* --------------------------------------------------------------
4 OrderListGenerator.inc.php 2015-11-06 gm
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2015 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 MainFactory::load_class('OrderListGeneratorInterface');
14
15 /**
16 * Class OrderListGenerator
17 *
18 * @category System
19 * @package Order
20 */
21 class OrderListGenerator implements OrderListGeneratorInterface
22 {
23 /**
24 * @var CI_DB_query_builder
25 */
26 protected $db;
27
28
29 /**
30 * OrderListGenerator Constructor
31 *
32 * @param \CI_DB_query_builder $db
33 */
34 public function __construct(CI_DB_query_builder $db)
35 {
36 $this->db = $db;
37 }
38
39
40 /**
41 * Get Order List Items
42 *
43 * Returns an order list item collection.
44 *
45 * @param array $conditions Associative array which holds the CI_DB_query_builder conditions (default empty
46 * array).
47 * @param \IntType $startIndex The start index of the wanted array to be returned (default = null).
48 * @param \IntType $maxCount Maximum amount of items which should be returned (default = null).
49 * @param \StringType $orderBy A string which defines how the items should be ordered (default = null).
50 *
51 * @return \OrderListItemCollection
52 */
53 public function getOrderListByConditions(array $conditions = array(),
54 IntType $startIndex = null,
55 IntType $maxCount = null,
56 StringType $orderBy = null)
57 {
58 $this->_select()->_limit($startIndex, $maxCount)->_order($orderBy)->_group();
59
60 if(count($conditions) > 0)
61 {
62 $this->db->where($conditions);
63 }
64
65 $result = $this->db->get()->result_array();
66
67 return $this->_prepareCollection($result);
68 }
69
70
71 /**
72 * Filter records by a single keyword string.
73 *
74 * @param \StringType $keyword Keyword string to be used for searching in order records.
75 * @param \IntType|null $startIndex The start index of the wanted array to be returned (default = null).
76 * @param \IntType|null $maxCount Maximum amount of items which should be returned (default = null).
77 * @param \StringType|null $orderBy A string which defines how the items should be ordered (default = null).
78 *
79 * @return mixed
80 */
81 public function getOrderListByKeyword(StringType $keyword,
82 IntType $startIndex = null,
83 IntType $maxCount = null,
84 StringType $orderBy = null)
85 {
86 $this->_select()->_limit($startIndex, $maxCount)->_order($orderBy)->_group();
87
88 $match = $keyword->asString();
89
90 $this->db->where('orders_total.class', 'ot_total')
91 ->like('orders.orders_id', $match)
92 ->or_like('orders.customers_id', $match)
93 ->or_like('orders.date_purchased', $match)
94 ->or_like('orders.payment_class', $match)
95 ->or_like('orders.payment_method', $match)
96 ->or_like('orders.shipping_class', $match)
97 ->or_like('orders.shipping_method', $match)
98 ->or_like('orders.customers_firstname', $match)
99 ->or_like('orders.customers_lastname', $match)
100 ->or_like('orders_total.value', $match)
101 ->or_like('orders_status.orders_status_id', $match)
102 ->or_like('orders_status.orders_status_name', $match);
103
104 $result = $this->db->get()->result_array();
105
106 return $this->_prepareCollection($result);
107 }
108
109
110 /**
111 * Execute the select and join methods.
112 *
113 * @return OrderListGenerator Returns the instance object for method chaining.
114 */
115 protected function _select()
116 {
117 $this->db->select('orders.orders_id, orders.customers_id, orders.date_purchased, orders.payment_class,
118 orders.payment_method, orders.shipping_class, orders.shipping_method, orders.customers_firstname,
119 orders.customers_lastname, orders_status.orders_status_id, orders_status.orders_status_name,
120 orders_total.value AS total_sum')
121 ->from('orders')
122 ->join('orders_status', 'orders_status.orders_status_id = orders.orders_status', 'inner')
123 ->join('orders_total', 'orders_total.orders_id = orders.orders_id', 'left');
124
125 return $this;
126 }
127
128
129 /**
130 * Add limit configuration to the database object.
131 *
132 * @param \IntType $startIndex
133 * @param \IntType $maxCount
134 *
135 * @return OrderListGenerator Returns the instance object for method chaining.
136 */
137 protected function _limit(IntType $startIndex = null, IntType $maxCount = null)
138 {
139 if($maxCount && $startIndex)
140 {
141 $this->db->limit($maxCount->asInt(), $startIndex->asInt());
142 }
143 else
144 {
145 if($maxCount && !$startIndex)
146 {
147 $this->db->limit($maxCount->asInt());
148 }
149 }
150
151 return $this;
152 }
153
154
155 /**
156 * Set the order by clause of the query.
157 *
158 * @param \StringType $orderBy
159 *
160 * @return OrderListGenerator Returns the instance object for method chaining.
161 */
162 protected function _order(StringType $orderBy = null)
163 {
164 if($orderBy)
165 {
166 $this->db->order_by($orderBy->asString());
167 }
168
169 return $this;
170 }
171
172
173 /**
174 * Execute the group by statement.
175 *
176 * @return OrderListGenerator Returns the instance object for method chaining.
177 */
178 protected function _group()
179 {
180 $this->db->group_by('orders.orders_id');
181
182 return $this;
183 }
184
185
186 /**
187 * Prepare the OrderListItemCollection object.
188 *
189 * This method will prepare the collection object which is going to be returned by both
190 * the "get" and "filter" methods. The following values are required to be present in
191 * each row of the $result parameter:
192 * - orders_id
193 * - customers_id
194 * - customers_firstname
195 * - customers_lastname
196 * - date_purchased
197 * - payment_class
198 * - payment_method
199 * - shipping_class
200 * - shipping_method
201 * - orders_status_id
202 * - orders_status_name
203 * - total_sum
204 *
205 * @param array $result Contains the order data.
206 *
207 * @return \OrderListItemCollection
208 */
209 protected function _prepareCollection(array $result)
210 {
211 $listItems = array();
212
213 foreach($result as $row)
214 {
215 $orderId = new IdType((int)$row['orders_id']);
216 $customerId = new IdType((int)$row['customers_id']);
217 $customerName = new StringType($row['customers_firstname'] . ' ' . $row['customers_lastname']);
218 $totalSum = new DecimalType((float)$row['total_sum']);
219 $paymentType = MainFactory::create('OrderPaymentType', new StringType($row['payment_method']),
220 new StringType($row['payment_class']));
221 $shippingType = MainFactory::create('OrderShippingType', new StringType($row['shipping_method']),
222 new StringType($row['shipping_class']));
223 $purchaseDateTime = new DateTime($row['date_purchased']);
224 $orderStatusId = new IdType((int)$row['orders_status_id']);
225 $orderStatusName = new StringType($row['orders_status_name']);
226
227 $orderListItem = MainFactory::create('OrderListItem', $orderId, $customerId, $customerName, $totalSum,
228 $paymentType, $shippingType, $purchaseDateTime, $orderStatusId,
229 $orderStatusName);
230 $listItems[] = $orderListItem;
231 }
232
233 $collection = MainFactory::create('OrderListItemCollection', $listItems);
234
235 return $collection;
236 }
237 }