-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommerce_amazon_sp_api.module
More file actions
138 lines (124 loc) · 3.68 KB
/
commerce_amazon_sp_api.module
File metadata and controls
138 lines (124 loc) · 3.68 KB
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
<?php
/**
* @file
* Provides a UI for managing amazon entities.
*/
use Drupal\commerce_amazon_sp_api\Exception\AmazonApiException;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* Implements hook_theme().
*/
function commerce_amazon_sp_api_theme() {
return [
'commerce_amazon_marketplace_form' => [
'render element' => 'form',
],
];
}
/**
* Implements hook_cron().
*/
function commerce_amazon_sp_api_cron() {
/** @var \Drupal\commerce_amazon_sp_api\Entity\AmazonMarketplaceInterface[] $marketplaces */
$marketplaces = Drupal::entityTypeManager()->getStorage('commerce_amazon_marketplace')->loadByProperties(['status' => TRUE]);
$key_value = \Drupal::keyValue('commerce_amazon_sp_api');
foreach ($marketplaces as $marketplace) {
$sync_next_run = $key_value->get($marketplace->id());
if (time() > $sync_next_run) {
$sync_period = $marketplace->getInventorySyncPeriod();
$sync_run = time() + $sync_period;
try {
\Drupal::service('commerce_amazon_sp_api.inventory')->sync($marketplace);
}
catch (AmazonApiException $exception) {
Drupal::logger('commerce_amazon_sp_api')->error($exception->getMessage());
return;
}
try {
\Drupal::service('commerce_amazon_sp_api.fulfillment_order')->syncFulfillments($marketplace);
}
catch (AmazonApiException $exception) {
Drupal::logger('commerce_amazon_sp_api')->error($exception->getMessage());
return;
}
$key_value->set($marketplace->id(), $sync_run);
}
}
}
/**
* Implements hook_views_data().
*/
function commerce_amazon_sp_api_views_data() {
$data['commerce_amazon_inventory']['table']['group'] = t('Amazon inventory');
$data['commerce_amazon_inventory']['table']['join'] = [
'commerce_amazon_item' => [
'left_field' => 'sku',
'field' => 'sku',
'left_table' => 'commerce_amazon_item',
'table' => 'commerce_amazon_inventory',
'extra' => 'commerce_amazon_inventory.marketplace = commerce_amazon_item.marketplace',
'type' => 'LEFT',
],
];
$data['commerce_amazon_inventory']['fulfillableQuantity'] = [
'title' => t('Fulfillable Quantity'),
'help' => t('The total number of items which are fulfillable.'),
'field' => [
'id' => 'numeric',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
];
$data['commerce_amazon_inventory']['timestamp'] = [
'title' => t('Last update'),
'help' => t('The most recent time the inventory has been updated.'),
'field' => [
'id' => 'timestamp',
'click sortable' => TRUE,
],
'filter' => [
'id' => 'date',
],
'argument' => [
'id' => 'date',
],
'sort' => [
'id' => 'standard',
],
];
return $data;
}
/**
* Implements hook_entity_operation().
*/
function commerce_amazon_sp_api_entity_operation(EntityInterface $entity) {
$current_user = \Drupal::currentUser();
$operations = [];
if ($entity->getEntityTypeId() === 'commerce_amazon_marketplace' &&
$current_user->hasPermission('edit commerce_amazon_marketplace')) {
$operations['items'] = [
'title' => t('Items'),
'url' => Url::fromRoute('entity.commerce_amazon_marketplace.canonical', [
'commerce_amazon_marketplace' => $entity->id(),
]),
'weight' => 50,
];
$operations['settings'] = [
'title' => t('Settings'),
'url' => Url::fromRoute('entity.commerce_amazon_marketplace.integration', [
'commerce_amazon_marketplace' => $entity->id(),
]),
'weight' => 50,
];
}
return $operations;
}