/
Order.php
244 lines (187 loc) · 7.34 KB
/
Order.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
<?php
namespace pistol88\order;
use pistol88\order\models\Order as OrderModel;
use pistol88\order\models\Element;
use pistol88\order\logic\Filling;
use pistol88\order\logic\OrderCancel;
use pistol88\order\logic\OrderRecovery;
use pistol88\order\logic\SetSeller;
use pistol88\order\logic\SetClient;
use pistol88\order\logic\ChangeStatus;
use pistol88\order\logic\ElementCancel;
use pistol88\order\logic\CountCalculate;
use pistol88\order\logic\CostCalculate;
use pistol88\order\interfaces\Order as OrderInterface;
use pistol88\order\interfaces\Element as ElementInterface;
use pistol88\order\interfaces\User as UserInterface;
use yii\base\Component;
use yii\db\Query;
use yii;
class Order extends Component
{
private $organization_id;
private $is_assigment = false;
public $payedStatus = 'payed';
public $halfpayedStatus = 'halfpayed';
public function init()
{
parent::init();
}
public function filling(OrderInterface $order)
{
return yii::createObject(['class' => Filling::class, 'order' => $order])->execute();
}
public function cancel(OrderInterface $order)
{
return yii::createObject(['class' => OrderCancel::class, 'order' => $order])->execute();
}
public function recovery(OrderInterface $order)
{
return yii::createObject(['class' => OrderRecovery::class, 'order' => $order])->execute();
}
public function cancelElement(ElementInterface $element)
{
return yii::createObject(['class' => OrderRecovery::class, 'element' => $element])->execute();
}
public function setClient(OrderInterface $order, UserInterface $client)
{
return yii::createObject(['class' => SetClient::class, 'order' => $order, 'client' => $client])->execute();
}
public function setSeller(OrderInterface $order, UserInterface $seller)
{
return yii::createObject(['class' => SetSeller::class, 'order' => $order, 'seller' => $seller])->execute();
}
public function changeStatus(OrderInterface $order, $status)
{
return yii::createObject(['class' => ChangeStatus::class, 'order' => $order, 'status' => $status])->execute();
}
private function buildQuery($query)
{
if($this->organization_id) {
$query->andWhere('(organization_id IS NULL OR organization_id = :organization_id)', [':organization_id' => $this->organization_id]);
}
$query->andWhere('(order.is_deleted IS NULL OR order.is_deleted != 1)');
if($this->is_assigment) {
$query->andWhere('order.is_assigment = 1');
} else {
$query->andWhere('(order.is_assigment IS NULL OR order.is_assigment != 1)');
}
return $query;
}
private function orderQuery()
{
$return = (new Query())->from('order');
return $this->buildQuery($return);
}
private function orderFinder()
{
$return = OrderModel::find();
return $this->buildQuery($return);
}
public function resetConditions()
{
$this->organization_id = null;
$this->is_assigment = false;
return $this;
}
public function setOrganization($organization_id)
{
$this->organization_id = $organization_id;
return $this;
}
public function assigment()
{
$this->is_assigment = true;
return $this;
}
public function get($id)
{
return OrderModel::findOne($id);
}
public function getOrdersByDatePeriod($dateStart, $dateStop, $where = null)
{
if($dateStop == '0000-00-00 00:00:00' | empty($dateStop)) {
$dateStop = date('Y-m-d H:i:s');
}
$query = $this->orderFinder()
->andWhere('DATE_FORMAT(date,\'%Y-%m-%d\') >= :dateStart AND DATE_FORMAT(date,\'%Y-%m-%d\') <= :dateStop', [':dateStart' => $dateStart, ':dateStop' => $dateStop])
->orderBy('timestamp ASC');
if($where) {
$query->andWhere($where);
}
$this->resetConditions();
return $query->all();
}
public function getStatInMoth($month = null, $where = null)
{
if(!$month) {
$month = date('Y-m');
}
$query = $this->orderQuery();
$query->addSelect(['sum(cost) as total, sum(count) as count_elements, COUNT(DISTINCT id) as count_orders'])
->from(['order'])
->andWhere('DATE_FORMAT(date, "%Y-%m") = :date', [':date' => $month]);
if($where) {
$query->andWhere($where);
}
$result = $query->one();
$this->resetConditions();
return array_map('intval', $result);
}
public function getStatByDate($date, $where = null)
{
$query = $this->orderQuery();
$query->addSelect(['sum(cost) as total, sum(count) as count_elements, COUNT(DISTINCT id) as count_orders'])
->from(['order'])
->andWhere(' DATE_FORMAT(date, "%Y-%m-%d") = :date', [':date' => $date]);
if($where) {
$query->andWhere($where);
}
$result = $query->one();
$this->resetConditions();
return array_map('intval', $result);
}
public function getStatByDatePeriod($dateStart, $dateStop = null, $where = null)
{
if($dateStop == '0000-00-00 00:00:00' | empty($dateStop)) {
$dateStop = date('Y-m-d H:i:s');
}
$query = $this->orderQuery();
$query->addSelect(['sum(cost) as total, sum(count) as count_elements, COUNT(DISTINCT id) as count_orders'])
->from(['order']);
if($dateStart == $dateStop) {
$query->andWhere('DATE_FORMAT(date,\'%Y-%m-%d\') = :date', [':date' => $dateStart]);
} else {
$query->andWhere('DATE_FORMAT(date,\'%Y-%m-%d\') >= :dateStart AND DATE_FORMAT(date,\'%Y-%m-%d\') <= :dateStop', [':dateStart' => $dateStart, ':dateStop' => $dateStop]);
}
if($where) {
$query->andWhere($where);
}
$result = $query->one();
$this->resetConditions();
return array_map('intval', $result);
}
public function getStatByModelAndDatePeriod($model, $dateStart, $dateStop, $where = null)
{
if($dateStop == '0000-00-00 00:00:00' | empty($dateStop)) {
$dateStop = date('Y-m-d H:i:s');
}
$query = $this->orderQuery();
$query->addSelect(['sum(e.count*e.price) as total, sum(e.count) as count_elements, COUNT(DISTINCT order_id) as count_orders'])
->from (['order_element e'])
->leftJoin('order', 'order.id = e.order_id')
//->groupBy('order.id')
->andWhere('order.is_assigment != 1')
->andWhere('order.date >= :dateStart AND order.date <= :dateStop', [':dateStart' => $dateStart, ':dateStop' => $dateStop])
->andWhere(['e.model' => $model]);
if($where) {
$query->andWhere($where);
}
$result = $query->one();
$this->resetConditions();
if(!$result) {
return ['total' => 0, 'count_elements' => 0, 'count_orders' => 0];
}
return array_map('intval', $result);
}
}