-
Notifications
You must be signed in to change notification settings - Fork 252
Expand file tree
/
Copy pathPromotionStorage.php
More file actions
176 lines (154 loc) · 6.02 KB
/
PromotionStorage.php
File metadata and controls
176 lines (154 loc) · 6.02 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
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
<?php
namespace Drupal\commerce_promotion;
use Drupal\commerce\CommerceContentEntityStorage;
use Drupal\commerce_order\Entity\OrderTypeInterface;
use Drupal\commerce_promotion\Entity\CouponInterface;
use Drupal\commerce_store\Entity\StoreInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/**
* Defines the promotion storage.
*/
class PromotionStorage extends CommerceContentEntityStorage implements PromotionStorageInterface {
/**
* The usage.
*
* @var \Drupal\commerce_promotion\PromotionUsageInterface
*/
protected $usage;
/**
* Constructs a new PromotionStorage object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Database\Connection $database
* The database connection to be used.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache
* The cache backend to be used.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
* The event dispatcher.
* @param \Drupal\commerce_promotion\PromotionUsageInterface $usage
* The usage.
*/
public function __construct(EntityTypeInterface $entity_type, Connection $database, EntityManagerInterface $entity_manager, CacheBackendInterface $cache, LanguageManagerInterface $language_manager, EventDispatcherInterface $event_dispatcher, PromotionUsageInterface $usage) {
parent::__construct($entity_type, $database, $entity_manager, $cache, $language_manager, $event_dispatcher);
$this->usage = $usage;
}
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('database'),
$container->get('entity.manager'),
$container->get('cache.entity'),
$container->get('language_manager'),
$container->get('event_dispatcher'),
$container->get('commerce_promotion.usage')
);
}
/**
* {@inheritdoc}
*/
public function loadValid(OrderTypeInterface $order_type, StoreInterface $store) {
$query = $this->buildLoadQuery($order_type, $store);
// Only load promotions without coupons. Promotions with coupons are loaded
// coupon-first in a different process.
$query->notExists('coupons');
$result = $query->execute();
if (empty($result)) {
return [];
}
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface[] $promotions */
$promotions = $this->loadMultiple($result);
// Remove any promotions that have hit their usage limit.
$usages = $this->usage->getUsageMultiple($promotions);
foreach ($promotions as $promotion_id => $promotion) {
$usage_limit = $promotion->getUsageLimit();
if ($usage_limit && $usage_limit <= $usages[$promotion_id]) {
unset($promotions[$promotion_id]);
}
}
return $promotions;
}
/**
* {@inheritdoc}
*/
public function loadByCoupon(OrderTypeInterface $order_type, StoreInterface $store, CouponInterface $coupon) {
$query = $this->buildLoadQuery($order_type, $store);
$query->condition('coupons', $coupon->id());
$result = $query->execute();
if (empty($result)) {
return [];
}
$promotions = $this->loadMultiple($result);
return reset($promotions);
}
/**
* Builds a query that will load all promotions that are no longer valid
* determined by the base field limiting values.
*
* @param bool $only_enabled
* Only include currently enabled promotions that have expired.
* @return array|bool|\Drupal\Core\Entity\EntityInterface[]
* The expired promotion entities. Returns FALSE if none found.
*/
public function loadExpired($only_enabled = TRUE) {
// Subquery to determine the amount of times a coupon has been used.
$usage_query = $this->database->select('commerce_promotion_usage', 'cpu');
$usage_query->addExpression('COUNT(cpu.promotion_id)', 'count');
$usage_query->where('cpu.promotion_id = cpd.promotion_id');
$usage_query->groupBy('cpu.promotion_id');
$query = $this->database->select($this->getDataTable(), 'cpd');
// We want to get results of queries that have passed their final date or
// have met their max usage.
$or_condition = $query->orConditionGroup()
->condition('cpd.end_date', gmdate('Y-m-d'), '<=')
->condition('cpd.usage_limit', $usage_query, '<=');
$query->addField('cpd', 'promotion_id');
$query->condition($or_condition);
if ($only_enabled) {
$query->condition('cpd.status', 1);
}
$result = $query->execute()->fetchCol(0);
if (empty($result)) {
return FALSE;
}
return $this->loadMultiple($result);
}
/**
* Builds the base query for loading valid promotions.
*
* @param \Drupal\commerce_order\Entity\OrderTypeInterface $order_type
* The order type.
* @param \Drupal\commerce_store\Entity\StoreInterface $store
* The store.
*
* @return \Drupal\Core\Entity\Query\QueryInterface
* The entity query.
*/
protected function buildLoadQuery(OrderTypeInterface $order_type, StoreInterface $store) {
$query = $this->getQuery();
$or_condition = $query->orConditionGroup()
->condition('end_date', gmdate('Y-m-d'), '>=')
->notExists('end_date', gmdate('Y-m-d'));
$query
->condition('stores', [$store->id()], 'IN')
->condition('order_types', [$order_type->id()], 'IN')
->condition('start_date', gmdate('Y-m-d'), '<=')
->condition('status', TRUE)
->condition($or_condition);
$query->sort('weight', 'ASC');
return $query;
}
}