1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopUnleashed\Task; |
4
|
|
|
|
5
|
|
|
use AntonyThorpe\Consumer\Consumer; |
6
|
|
|
use AntonyThorpe\SilverShopUnleashed\BulkLoader\OrderBulkLoader; |
7
|
|
|
use AntonyThorpe\SilvershopUnleashed\Defaults; |
8
|
|
|
use AntonyThorpe\SilverShopUnleashed\Task\UnleashedBuildTask; |
9
|
|
|
use AntonyThorpe\SilverShopUnleashed\UnleashedAPI; |
10
|
|
|
use DateTime; |
11
|
|
|
use GuzzleHttp\Exception\RequestException; |
12
|
|
|
use Psr\Http\Message\ResponseInterface; |
13
|
|
|
use SilverShop\Extension\ShopConfigExtension; |
14
|
|
|
use SilverStripe\Control\Email\Email; |
15
|
|
|
use SilverStripe\Dev\Debug; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Update Order with fresh data from Unleashed's Sales Orders |
19
|
|
|
*/ |
20
|
|
|
abstract class UnleashedUpdateOrderTask extends UnleashedBuildTask |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $title = "Unleashed: Update Orders"; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $description = "Update Orders in Silvershop with with data received from Unleashed. Will update the OrderStatus of the Silvershop items."; |
31
|
|
|
|
32
|
|
|
protected $email_subject = "API Unleashed Software - Update Order Results"; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Order status map from Unleashed to Silvershop |
36
|
|
|
* |
37
|
|
|
* For converting from Unleashed Sales Order Status to Silvershop |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $order_status_map = [ |
41
|
|
|
'Open' => 'Unpaid', |
42
|
|
|
'Parked' => 'Paid', |
43
|
|
|
'Backordered' => 'Processing', |
44
|
|
|
'Placed' => 'Processing', |
45
|
|
|
'Picking' => 'Processing', |
46
|
|
|
'Picked' => 'Processing', |
47
|
|
|
'Packed' => 'Processing', |
48
|
|
|
'Dispatched' => 'Sent', |
49
|
|
|
'Complete' => 'Complete', |
50
|
|
|
'Deleted' => 'MemberCancelled' |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
public function run($request) |
54
|
|
|
{ |
55
|
|
|
// Definitions |
56
|
|
|
$default_source_id = Defaults::config()->source_id; |
57
|
|
|
$query = []; |
58
|
|
|
$consumer = Consumer::get()->find('Title', 'OrderUpdate'); // to get modifiedSince |
59
|
|
|
|
60
|
|
|
if ($consumer->Count()) { |
61
|
|
|
$date = new DateTime($consumer->ExternalLastEdited); |
62
|
|
|
$query['modifiedSince'] = substr($date->format('Y-m-d\TH:i:s.u'), 0, 23); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
if ($default_source_id) { |
66
|
|
|
$query['sourceId'] = $default_source_id; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$response = UnleashedAPI::sendCall( |
70
|
|
|
'GET', |
71
|
|
|
'https://api.unleashedsoftware.com/SalesOrders', |
72
|
|
|
['query' => $query] |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
if ($response->getStatusCode() == '200') { |
76
|
|
|
$apidata_array = json_decode($response->getBody()->getContents(), true); |
77
|
|
|
$apidata = $apidata_array['Items']; |
78
|
|
|
$pagination = $apidata_array['Pagination']; |
79
|
|
|
$numberofpages = (int) $pagination['NumberOfPages']; |
80
|
|
|
|
81
|
|
|
if ($numberofpages > 1) { |
82
|
|
|
for ($i = 2; $i <= $numberofpages; $i++) { |
83
|
|
|
$response = UnleashedAPI::sendCall( |
84
|
|
|
'GET', |
85
|
|
|
'https://api.unleashedsoftware.com/SalesOrders/' . $i, |
86
|
|
|
['query' => $query] |
87
|
|
|
); |
88
|
|
|
if ($response->getStatusCode() == '200') { |
89
|
|
|
$apidata_array = json_decode($response->getBody()->getContents(), true); |
90
|
|
|
$apidata = array_merge($apidata, $apidata_array['Items']); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$this->log('<h3>Update the OrderStatus in Silvershop</h3>'); |
96
|
|
|
$loader = OrderBulkLoader::create('SilverShop\Model\Order'); |
97
|
|
|
$loader->transforms = [ |
98
|
|
|
'Status' => [ |
99
|
|
|
'callback' => function ($value) { |
100
|
|
|
// convert from Unleashed Sales Order status to Silvershop |
101
|
|
|
return $this->order_status_map[$value]; |
102
|
|
|
} |
103
|
|
|
] |
104
|
|
|
]; |
105
|
|
|
$results = $loader->updateRecords($apidata, $this->preview); |
106
|
|
|
|
107
|
|
|
if ($results->UpdatedCount()) { |
108
|
|
|
$this->log(Debug::text($results->getData())); |
109
|
|
|
} |
110
|
|
|
$this->log("Done"); |
111
|
|
|
|
112
|
|
|
// Send email |
113
|
|
|
if ($results->Count() && $this->email_subject && !$this->preview && Email::config()->admin_email) { |
114
|
|
|
$data = $results->getData(); |
115
|
|
|
$email = Email::create( |
116
|
|
|
ShopConfigExtension::config()->email_from ? ShopConfigExtension::config()->email_from : Email::config()->admin_email, |
117
|
|
|
Email::config()->admin_email, |
118
|
|
|
$this->email_subject, |
119
|
|
|
Debug::text($data) |
120
|
|
|
); |
121
|
|
|
$dispatched = $email->send(); |
122
|
|
|
if ($dispatched) { |
123
|
|
|
$this->log("Email sent"); |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
// Create/update Consumer |
128
|
|
|
if (!$this->preview && $apidata) { |
129
|
|
|
if (!$consumer->Count()) { |
130
|
|
|
$consumer = Consumer::create([ |
131
|
|
|
'Title' => 'OrderUpdate', |
132
|
|
|
'ExternalLastEditedKey' => 'LastModifiedOn' |
133
|
|
|
]); |
134
|
|
|
} |
135
|
|
|
$consumer->setMaxExternalLastEdited($apidata); |
136
|
|
|
$consumer->write(); |
137
|
|
|
} |
138
|
|
|
} // end if response == 200 |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|