-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload_invoices.php
67 lines (58 loc) · 1.86 KB
/
upload_invoices.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
<?php
//TODO: Better error/success messages
require 'vendor/autoload.php';
require_once 'settings.php';
use Mirakl\Core\Domain\Collection\DocumentCollection;
use Mirakl\Core\Domain\Document;
use Mirakl\MMP\Shop\Client\ShopApiClient;
use Mirakl\MMP\Shop\Request\Order\Document\UploadOrdersDocumentsRequest;
try {
$directory = '../PDF-OrderNumber-Converter/pdf/';
//Cycle throught files
$filepaths = array();
$filenames = array();
$ordernumbers = array();
foreach (scandir($directory) as $file) {
if ($file !== '.' && $file !== '..') {
$filepaths[] = $directory . $file;
$filenames[] = $file;
$ordernumbers[] = str_replace('.pdf', '', $file);
}
}
/*
echo "<pre>";
var_dump($filepaths);
var_dump($filenames);
var_dump($ordernumbers);
echo "</pre>";
*/
for($i = 0; $i < sizeof($filepaths); $i++){
try{
//Upload file to Völkner
$api = new ShopApiClient($api_url, $api_key, null);
$file = new \SplFileObject($filepaths[$i]);
$docs = new DocumentCollection();
$docs->add(new Document($file, $filenames[$i], 'CUSTOMER_INVOICE'));
$request = new UploadOrdersDocumentsRequest($docs, $ordernumbers[$i]);
$result = $api->uploadOrderDocuments($request);
} catch (\Exception $e) {
echo "[!] Unable to upload " . $filenames[$i] . " to order number " . $ordernumbers[$i] . "\r\n";
continue;
}
try{
//Delete file after upload
unlink($filepaths[$i]);
echo "Added invoice " . $filenames[$i] . " to order number " . $ordernumbers[$i] . "\r\n";
} catch (\Exception $e) {
echo "[!] Added invoice " . $filenames[$i] . " to order number " . $ordernumbers[$i] . ", but couldn't delete invoice pdf!\r\n";
}
}
} catch (\Exception $e) {
// An exception is thrown if the requested object is not found or if an error occurs
echo "[!] Unknown error! Try again with advanced errors.";
/*
echo "<pre>";
var_dump($e);
echo "</pre>";
*/
}