1 <?php
2
3 /* --------------------------------------------------------------
4 OrderStatusHistoryStorage.inc.php 2016-01-04
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
14 /**
15 * Class OrderStatusHistoryStorage
16 *
17 * @category System
18 * @package Order
19 * @subpackage Storages
20 */
21 class OrderStatusHistoryStorage implements OrderStatusHistoryReaderInterface, OrderStatusHistoryWriterInterface
22 {
23 /**
24 * @var CI_DB_query_builder
25 */
26 protected $db;
27
28
29 /**
30 * OrderStatusHistoryStorage 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 * Returns a collection of order status history items by the given order item id.
42 *
43 * @param IdType $orderId Id of order item.
44 *
45 * @return OrderStatusHistoryListItemCollection
46 */
47 public function getStatusHistory(IdType $orderId)
48 {
49 $orderStatusHistoryListItems = array();
50 $result = $this->db->from('orders_status_history')
51 ->where('orders_id', $orderId->asInt())
52 ->order_by('date_added', 'ASC')
53 ->get();
54
55 if($result->num_rows())
56 {
57 foreach($result->result_array() as $row)
58 {
59 $orderStatusHistoryListItems[] = MainFactory::create('OrderStatusHistoryListItem',
60 new IdType($row['orders_status_history_id']),
61 new IdType($row['orders_status_id']),
62 new DateTime($row['date_added']),
63 new StringType($row['comments']),
64 new BoolType($row['customer_notified']));
65 }
66 }
67
68 $orderStatusHistoryListItemCollection = MainFactory::create('OrderStatusHistoryListItemCollection',
69 $orderStatusHistoryListItems);
70
71 return $orderStatusHistoryListItemCollection;
72 }
73
74
75 /**
76 * Adds an order status history item.
77 *
78 * @param IdType $orderId
79 * @param IdType $newOrderStatusId
80 * @param StringType $comment
81 * @param BoolType $customerNotified
82 */
83 public function addStatusUpdate(IdType $orderId,
84 IdType $newOrderStatusId,
85 StringType $comment,
86 BoolType $customerNotified)
87 {
88 $dateAdded = new DateTime();
89 $dateAdded = $dateAdded->format('Y-m-d H:i:s');
90
91 $this->db->insert('orders_status_history', array(
92 'orders_id' => $orderId->asInt(),
93 'orders_status_id' => $newOrderStatusId->asInt(),
94 'date_added' => $dateAdded,
95 'customer_notified' => (int)$customerNotified->asBool(),
96 'comments' => $comment->asString()
97 ));
98 }
99
100
101 /**
102 * Deletes all order status history items which are associated with the given order item id.
103 *
104 * @param IdType $orderId
105 */
106 public function deleteHistory(IdType $orderId)
107 {
108 $this->db->delete('orders_status_history', array('orders_id' => $orderId->asInt()));
109 }
110 }