|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package Shippo |
|
5
|
|
|
* @author Iurii Makukh <[email protected]> |
|
6
|
|
|
* @copyright Copyright (c) 2017, Iurii Makukh <[email protected]> |
|
7
|
|
|
* @license https://www.gnu.org/licenses/gpl-3.0.en.html GPL-3.0+ |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace gplcart\modules\shippo\controllers; |
|
11
|
|
|
|
|
12
|
|
|
use Exception; |
|
13
|
|
|
use gplcart\core\controllers\backend\Controller; |
|
14
|
|
|
use gplcart\core\models\Order; |
|
15
|
|
|
use gplcart\modules\shippo\models\Api; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Handles incoming requests and outputs data related to Shippo module |
|
19
|
|
|
*/ |
|
20
|
|
|
class Label extends Controller |
|
21
|
|
|
{ |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Shippo Api model instance |
|
25
|
|
|
* @var \gplcart\modules\shippo\models\Api $api |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $api; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Order model instance |
|
31
|
|
|
* @var \gplcart\core\models\Order $order |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $order; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
protected $data_limit; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Label constructor. |
|
42
|
|
|
* @param Order $order |
|
43
|
|
|
* @param Api $api |
|
44
|
|
|
*/ |
|
45
|
|
|
public function __construct(Order $order, Api $api) |
|
46
|
|
|
{ |
|
47
|
|
|
parent::__construct(); |
|
48
|
|
|
|
|
49
|
|
|
$this->api = $api; |
|
50
|
|
|
$this->order = $order; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Route page callback to display the shipping label overview page |
|
55
|
|
|
*/ |
|
56
|
|
|
public function listLabel() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->actionLabel(); |
|
59
|
|
|
$this->setTitleListLabel(); |
|
60
|
|
|
$this->setBreadcrumbListLabel(); |
|
61
|
|
|
$this->setFilterListLabel(); |
|
62
|
|
|
$this->setPagerLabel(); |
|
63
|
|
|
|
|
64
|
|
|
$this->setData('orders', $this->getOrderListLabel()); |
|
65
|
|
|
$this->setData('statuses', $this->order->getStatuses()); |
|
66
|
|
|
$this->outputListLabel(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Sets pager |
|
71
|
|
|
*/ |
|
72
|
|
|
protected function setPagerLabel() |
|
73
|
|
|
{ |
|
74
|
|
|
$this->data_limit = $this->setPager(array('total' => $this->getTotalListLabel())); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Set filter on the label overview page |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function setFilterListLabel() |
|
81
|
|
|
{ |
|
82
|
|
|
$allowed = array('store_id', 'order_id', 'status', 'created', 'tracking_number'); |
|
83
|
|
|
$this->setFilter($allowed); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Returns an array of Shippo shipping methods |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
protected function getOrderListLabel() |
|
91
|
|
|
{ |
|
92
|
|
|
$options = $this->query_filter; |
|
93
|
|
|
$options['limit'] = $this->data_limit; |
|
94
|
|
|
$options['shipping_prefix'] = 'shippo_'; |
|
95
|
|
|
|
|
96
|
|
|
return (array) $this->order->getList($options); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Returns total number of orders |
|
101
|
|
|
* @return integer |
|
102
|
|
|
*/ |
|
103
|
|
|
protected function getTotalListLabel() |
|
104
|
|
|
{ |
|
105
|
|
|
$options = array( |
|
106
|
|
|
'count' => true, |
|
107
|
|
|
'shipping_prefix' => 'shippo_' |
|
108
|
|
|
); |
|
109
|
|
|
|
|
110
|
|
|
return (int) $this->order->getList($options); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Set title on the shipping label overview page |
|
115
|
|
|
*/ |
|
116
|
|
|
protected function setTitleListLabel() |
|
117
|
|
|
{ |
|
118
|
|
|
$this->setTitle('Shipping labels'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Set breadcrumbs on the shipping label overview page |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function setBreadcrumbListLabel() |
|
125
|
|
|
{ |
|
126
|
|
|
$breadcrumbs = array(); |
|
127
|
|
|
|
|
128
|
|
|
$breadcrumbs[] = array( |
|
129
|
|
|
'text' => $this->text('Dashboard'), |
|
130
|
|
|
'url' => $this->url('admin') |
|
131
|
|
|
); |
|
132
|
|
|
|
|
133
|
|
|
$breadcrumbs[] = array( |
|
134
|
|
|
'text' => $this->text('Orders'), |
|
135
|
|
|
'url' => $this->url('admin/sale/order') |
|
136
|
|
|
); |
|
137
|
|
|
|
|
138
|
|
|
$this->setBreadcrumbs($breadcrumbs); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Handles different actions |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function actionLabel() |
|
145
|
|
|
{ |
|
146
|
|
|
$this->controlAccess('order_edit'); |
|
147
|
|
|
|
|
148
|
|
|
$order_id = $this->getQuery('order_id'); |
|
149
|
|
|
$object_id = $this->getQuery('get_label'); |
|
150
|
|
|
|
|
151
|
|
|
if (empty($object_id) || empty($order_id)) { |
|
152
|
|
|
return null; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
$order = $this->order->get($order_id); |
|
156
|
|
|
|
|
157
|
|
|
if (empty($order['order_id'])) { |
|
158
|
|
|
return null; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
try { |
|
162
|
|
|
$response = $this->api->getLabel($object_id); |
|
163
|
|
|
} catch (Exception $ex) { |
|
164
|
|
|
$this->redirect('admin/tool/shippo', $ex->getMessage(), 'warning'); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (empty($response['tracking_number']) || empty($response['label_url'])) { |
|
168
|
|
|
$this->redirect('admin/tool/shippo', $this->text('An error occurred'), 'warning'); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
$order['data']['shipping_label'] = $response['label_url']; |
|
172
|
|
|
|
|
173
|
|
|
$data = array( |
|
174
|
|
|
'data' => $order['data'], |
|
175
|
|
|
'tracking_number' => $response['tracking_number'] |
|
176
|
|
|
); |
|
177
|
|
|
|
|
178
|
|
|
$this->order->update($order_id, $data); |
|
179
|
|
|
|
|
180
|
|
|
$vars = array( |
|
181
|
|
|
'@url' => $response['label_url'], |
|
182
|
|
|
'%num' => $response['tracking_number'] |
|
183
|
|
|
); |
|
184
|
|
|
|
|
185
|
|
|
$message = $this->text('Label has been <a target="_blank" href="@url">created</a>. Tracking number: %num', $vars); |
|
186
|
|
|
$this->redirect('admin/tool/shippo', $message, 'success'); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Render and output the shipping label overview page |
|
191
|
|
|
*/ |
|
192
|
|
|
protected function outputListLabel() |
|
193
|
|
|
{ |
|
194
|
|
|
$this->output('shippo|labels'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
} |
|
198
|
|
|
|