-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTZOrder.class.php
342 lines (271 loc) · 8.69 KB
/
TZOrder.class.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
<?php
class TZOrder extends TPage {
/*
* для задачи используем две таблицы СУБД в одном классе для упрощения
*/
public $id=0;
public $orderdate='';
public $d_user=0;
public $id_customer=0;
public $orderprice=0;
// данные шапки
public $username = '';
public $customername = '';
public function __construct() {
parent::__construct();
parent::getPDO();
}
public function __destruct() {
}
/*
* шапка заказа
*/
public function getOrder($id) {
if(!$id) return 0;
$sql = "SELECT
zo.id,
zo.orderdate,
zo.id_user,
u.fullname AS username,
zo.id_customer,
c.fullname AS customername,
c.address,
c.contacts,
zo.orderprice
FROM z_order AS zo
INNER JOIN z_users AS u ON u.id=zo.id_user
INNER JOIN z_customer AS c ON c.id=zo.id_customer
WHERE zo.id = :id";
$stm = $this->pdo->prepare($sql);
$stm->bindParam(':id',$id,PDO::PARAM_INT);
$rowcnt = 0;
if($stm->execute()) {
$rowcnt = $stm->rowCount();
if($rowcnt) {
$row = $stm->fetch(PDO::FETCH_ASSOC);
// общие параметры страницы
$this->id = $row['id'];
$this->orderdate = $row['orderdate'];
$this->id_user = $row['id_user'];
$this->username = $row['username'];
$this->id_customer = $row['id_customer'];
$this->customername = $row['customername'];
$this->orderprice = $row['orderprice'];
}
}
return $rowcnt;
}
// табличная часть заказа
public function getTableOrder() {
$data = array();
if(!$this->id) return $data;
$sql = "SELECT
ot.id_order,
ot.numline,
ot.id_service,
s.fullname,
s.price AS priceplan,
ot.price
FROM z_order_table AS ot
INNER JOIN z_service AS s ON s.id=ot.id_service
WHERE ot.id_order = :id";
$stm = $this->pdo->prepare($sql);
$stm->bindParam(':id',$this->id,PDO::PARAM_INT);
if($stm->execute()) {
$rowcnt = $stm->rowCount();
if($rowcnt) {
$data = $stm->fetchALL(PDO::FETCH_ASSOC);
}
}
unset($stm);
return $data;
}
public function getListOrder() {
$sql = "SELECT
zo.id,
zo.orderdate,
zo.id_user,
u.fullname AS username,
zo.id_customer,
c.fullname AS customername,
c.address,
c.contacts,
zo.orderprice
FROM z_order AS zo
INNER JOIN z_users AS u ON u.id=zo.id_user
INNER JOIN z_customer AS c ON c.id=zo.id_customer";
$result = $this->pdo->query($sql);
$data = $result->fetchALL(PDO::FETCH_ASSOC);
unset($result);
return $data;
}
/**
* templates создавать надо время, поэтому делаем в
*/
// список покупателей
public function getSelectCustomer($mode,$id_customer) {
$sql = "SELECT
id,
fullname,
address,
contacts
FROM z_customer";
$result = $this->pdo->query($sql);
$data = $result->fetchALL(PDO::FETCH_ASSOC);
unset($result);
$out = '<select name="id_customer" id="id_customer">';
$out .= '<option value="0">Выберите покупателя</option>';
foreach($data AS $row) {
$out .= '<option '.($id_customer==$row['id']?'selected="selected" ':'').'value="'.$row['id'].'">'.$row['fullname'].'</option>';
}
$out .= '</select>';
return $out;
}
// новый заказ
public function insertOrder($data) {
$id_order = 0;
$id_customer = (int) $data[0]['id_customer'];
unset($data[0]);
// заполнение шапки заказа
// запись шапки заказа
$sql="INSERT INTO z_order
SET orderdate=DATE_FORMAT(NOW(),'%Y-%m-%d'),
id_user=:id_user,
id_customer=:$id_customer,
orderprice=:orderprice";
$stm = $this->pdo->prepare($sql);
$stm->bindParam(':id_user',$id_user,PDO::PARAM_INT);
$stm->bindParam(':id_customer',$id_customer,PDO::PARAM_INT);
$stm->bindParam(':orderprice',$orderprice,PDO::PARAM_INT);
if($stm->execute()) {
$id_order = $this->pdo->lastInsertId();
} else {
return -1;
}
// заполнение таблицы заказа
$sql="INSERT INTO z_order_table
SET id_order=:id_order,
numline=:numline,
id_service=:id_service,
price=:price";
$stm = $this->pdo->prepare($sql);
$numline = 0;
foreach($data AS $row) {
$numline++;
$stm->bindParam(':id_service',$row['id_service'],PDO::PARAM_INT);
$stm->bindParam(':price',$row['price'],PDO::PARAM_INT);
$stm->bindParam(':id_order',$id_order,PDO::PARAM_INT);
$stm->bindParam(':numline',$numline,PDO::PARAM_INT);
if($stm->execute()) {
$orderprice +=(int)$row['price'];
} else {
return -1;
}
}
}
// изменение заказа
public function updateOrder($data) {
$id_order = (int) $data[0]['id_order'];
$id_customer = (int) $data[0]['id_customer'];
unset($data[0]);
$orderprice = 0;
// id_service=:id_service,
$sql="UPDATE z_order_table
SET price=:price
WHERE id_order=:id_order AND numline=:numline";
$stm = $this->pdo->prepare($sql);
foreach($data AS $row) {
//$stm->bindParam(':id_service',$row['id_service'],PDO::PARAM_INT);
$stm->bindParam(':price',$row['price'],PDO::PARAM_INT);
$stm->bindParam(':id_order',$id_order,PDO::PARAM_INT);
$stm->bindParam(':numline',$row['numline'],PDO::PARAM_INT);
if($stm->execute()) {
$orderprice +=(int)$row['price'];
} else {
return -1;
}
}
// запись шапки заказа
$sql="UPDATE z_order
SET id_customer=:id_customer,
orderprice=:orderprice
WHERE id=:id";
$stm = $this->pdo->prepare($sql);
$stm->bindParam(':id_customer',$id_customer,PDO::PARAM_INT);
$stm->bindParam(':orderprice',$orderprice,PDO::PARAM_INT);
$stm->bindParam(':id',$id_order,PDO::PARAM_INT);
if($stm->execute()) {
return $orderprice;
} else {
return -1;
}
}
// получить список заказов с плановыми ценами
public function getProfitReport($data) {
$where = '';
if(count($data)>0) {
$where .=" WHERE o.id>0 ";
if(isset($data['id_user'])) {
$where.="AND o.id_user=".$data['id_user']." ";
}
if(isset($data['id_customer'])) {
$where.="AND o.id_customer=".$data['id_customer']." ";
}
}
$sql = "SELECT
o.id,
o.orderdate,
o.id_user,
c.fullname AS customername,
o.id_customer,
u.fullname AS username,
o.orderprice,
SUM(t.price) AS sumprice,
SUM(s.price) AS sumplanprice,
p.sumpay
FROM z_order AS o
INNER JOIN z_order_table AS t ON t.id_order=o.id
INNER JOIN z_service AS s ON s.id=t.id_service
INNER JOIN z_customer AS c ON c.id=o.id_customer
INNER JOIN z_users AS u ON u.id=o.id_user
LEFT JOIN (
SELECT id_order,SUM(amount) AS sumpay FROM z_payment GROUP BY id_order
) AS p ON p.id_order=o.id
".$where."
GROUP BY o.id";
$stm = $this->pdo->prepare($sql);
//$stm->bindParam(':id',$this->id,PDO::PARAM_INT);
if($stm->execute()) {
$rowcnt = $stm->rowCount();
if($rowcnt) {
$data = $stm->fetchALL(PDO::FETCH_ASSOC);
}
}
unset($stm);
return $data;
}
// список оплат по заказу
public function getPayList($id_order) {
$sql = "SELECT
p.id_order,
p.description,
p.paytape,
p.paydate,
p.amount,
p.id_user,
u.fullname
FROM z_payment p
INNER JOIN z_users AS u ON u.id=p.id_user
WHERE p.id_order=:id_order";
$stm = $this->pdo->prepare($sql);
$stm->bindParam(':id_order',$id_order,PDO::PARAM_INT);
if($stm->execute()) {
$rowcnt = $stm->rowCount();
if($rowcnt) {
$data = $stm->fetchALL(PDO::FETCH_ASSOC);
}
}
unset($stm);
return $data;
}
}