/
print_return_label.php
222 lines (181 loc) · 4.22 KB
/
print_return_label.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
require_once('config.php');
require_once('includes/database.php');
require_once('RestApi.php');
session_cache_expire( $cache_expire );
session_start();
// Set the timezone
// Europe/London
date_default_timezone_set('Europe/London');
//retrieve values for session variables
if (isset($_SESSION['password']))
{
$password = $_SESSION['password'];
$username = $_SESSION['username'];
$api_key = $_SESSION['api_key'];
$session_life = time() - $_SESSION['start'];
if($session_life > $inactive)
{
header("Location: logout.php");
}
$_SESSION['start'] = time();
}
else
{
header("Location: 404_error.php");
}
// Set the variables to NULL to allow for checking later.
$return_code = null;
$parcels = null;
if(isset($_POST['return_code']))
{
$return_code = $_POST['return_code'];
}
if(isset($_POST['parcel_c']))
{
$parcels = $_POST['parcel_c'];
}
$params = array(
'token' => $api_key,
'methodType' => 'GET',
'params' => array(
'format' => 'Pdf',
'type' => 'normal'
)
);
if($return_code != null)
{
$params['url'] = $base_url . 'reverselogistics/' .
$return_code . '/label.json';
}
else
{
if(count($parcels) > 1)
{
$ret = print_many_labels($params, $parcels, $username);
update_parcel_status($ret);
header("Location: r_orders.php");
return;
}
else
{
$params['url'] = $base_url . 'reverselogistics/' .
$parcels[0] . '/label.json';
}
}
$restApi2 = new RestApi($params);
$timestamp = date('dmy_His', time());
$file_name = "InPost_Return_Label" . $timestamp . ".pdf";
$pdf = $restApi2->getResponse();
$info = $restApi2->getInfo();
//echo '<pre>Response = ';
//echo print_r($pdf);
//echo '</pre>';
//echo '<pre>Info = ';
//echo print_r($info);
//echo '</pre>';
if($info['http_code'] != 200)
{
$_SESSION['error_message'] = 'Failed to create your labels ' .
$info["http_code"] . ' ' . $pdf;
header("Location: r_orders.php");
return;
}
update_parcel_status($parcels);
$binary = base64_decode($pdf);
header('Content-type: application/pdf');
header("Content-Disposition:attachment; filename=$file_name");
$tmpfname = "tmp" . session_id() . "fred.pdf";
$handle = fopen($tmpfname, "wb");
fwrite($handle, $binary);
fclose($handle);
// do here something
header("location: $tmpfname");
//unlink($tmpfname);
//echo $binary;
header("Refresh:0; url=http://inpost247.uk/b2a/r_orders.php");
return;
///
// print_many_labels
//
function print_many_labels(&$params, &$parcels, $username)
{
require("config.php");
$return = array();
$zipname = "test_" . $username . '_' . session_id() . ".zip";
$zip = new ZipArchive;
$res = $zip->open($zipname, ZipArchive::CREATE);
if ($res === TRUE)
{
$timestamp = date('dmy_His', time());
foreach($parcels as $key => $parcel)
{
$params['url'] = $base_url . 'reverselogistics/' .
$parcel . '/label.json';
//echo json_encode($params);
$restApi2 = new RestApi($params);
$file_name = "InPost_Return_Label" . $timestamp .
$key . ".pdf";
$pdf = $restApi2->getResponse();
$info = $restApi2->getInfo();
if($info['http_code'] != 200)
{
$_SESSION['error_message'] = 'Failed to create your labels ' .
$info["http_code"];
continue;
}
$binary = base64_decode($pdf);
// Add to the list of parcels that need their status
// updated.
$return[] = $parcel;
// Add the PDF to the main ZIP file.
$zip->addFromString($file_name, $binary);
}
$zip->close();
}
else
{
$_SESSION['error_message'] = 'Failed to create ZIP file for your labels.';
header("Location: r_orders.php");
return $return;
}
return $return;
}
///
// update_parcel_status
//
// @param array of parcel codes to be updated.
//
function update_parcel_status($parcels)
{
require('config.php');
if(count($parcels) > 0)
{
tep_db_connect();
}
else
{
return;
}
$the_date = date('Y-m-d H:i:s');
foreach($parcels as $parcel)
{
tep_db_query("UPDATE $table_prefix$return_table SET label_printed=1, dispatch_date='$the_date' WHERE code='$parcel'");
}
}
?>
<html>
<head>
<script type="text/javascript">
function Redirect()
{
window.location="r_orders.php";
}
</script>
</head>
<body>
<script type="text/javascript">
setTimeout('Redirect()', 3000);
</script>
</body>
</html>