1 <?php
2
3 namespace PLus\Orders\Repositories\Order;
4
5
6 7 8 9 10
11 class OrderRepository extends \PLus\Orders\Repositories\Repository {
12
13
14 function getPairData(){
15 return $this->getTable()->select('id, name')->order('name ASC')->fetchPairs('id', 'name');
16 }
17
18 19 20 21 22 23 24 25
26 function getGridSelectionSentByUnit($id,$ids){
27 return $this->getTable()
28 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
29 ->where('(order.user_id ? OR order.user_id ?)',$id,$ids)
30 ->where('order.departmentflag = 1 AND order.completeflag IS NULL');
31 }
32
33 34 35 36 37 38 39 40
41 function getGridSelectionValidateByUnit($id,$ids){
42 return $this->getTable()
43 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
44 ->where('(order.user_id ? OR order.user_id ?)',$id,$ids)
45 ->where('order.departmentflag = 1 AND order.chiefflag IS NULL AND order.completeflag IS NULL');
46 }
47
48
49 50 51 52 53 54 55 56 57
58 function getGridSelectionReturnedByUnit($id,$ids){
59 return $this->getTable()
60 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
61 ->where('(order.user_id ? OR order.user_id ?)',$id,$ids)
62 ->where('order.departmentflag IS NULL AND order.completeflag IS NULL');
63 }
64
65 66 67 68 69 70 71
72 function getGridCountReturnedByUnit($id,$ids) {
73 return $this->getTable()
74 ->select('COUNT(*) AS cnt')
75 ->where('(order.user_id ? OR order.user_id ?)',$id,$ids)
76 ->where('order.departmentflag IS NULL AND order.completeflag IS NULL')
77 ->fetch();
78 }
79
80 81 82 83 84 85 86
87 function getGridSelectionClosedByUnit($id,$ids){
88 return $this->getTable()
89 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname,'
90 . ':managerresult.approved AS mapproved')
91 ->where('(order.user_id ? OR order.user_id ?)',$id,$ids)
92 ->where('order.completeflag ?',1);
93 }
94
95
96 97 98 99 100 101 102 103
104 function getGridSelectionTraderIn(){
105 return $this->getTable()
106 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
107 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag IS NULL AND order.completeflag IS NULL')
108 ->group('order.id')
109 ->having('(COUNT(:itemorder.order_id) != COUNT(:itemorder.areaflag)) || order.returnflag = 1');
110 }
111
112 113 114 115 116
117 function getCountTraderIn() {
118 return $this->getTable()
119 ->select('COUNT(*) AS cnt')
120 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag IS NULL AND order.completeflag IS NULL')
121 ->fetch();
122 }
123
124
125
126 127 128 129 130
131 function getGridSelectionTraderWork(){
132 return $this->getTable()
133 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
134 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND (order.itemflag IS NULL OR order.traderupflag = 1)'
135 . 'AND order.completeflag IS NULL');
136 }
137
138 139 140 141 142
143 function getGridSelectionTraderToUnit(){
144 return $this->getTable()
145 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
146 ->where('order.chiefflag IS NULL AND order.returnflag = 1 AND order.completeflag IS NULL');
147 }
148
149
150 151 152 153 154
155 function getGridSelectionTraderToExpert(){
156 return $this->getTable()
157 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
158 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag IS NULL AND order.traderupflag IS NULL '
159 . 'AND order.completeflag IS NULL');
160 }
161
162 163 164 165 166
167 function getGridSelectionTraderToManager(){
168 return $this->getTable()
169 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
170 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag = 1 AND order.traderupflag = 1 '
171 . 'AND order.completeflag IS NULL');
172 }
173
174
175 176 177 178 179 180
181 function getGridSelectionTraderValidate(){
182 return $this->getTable()
183 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
184 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag = 1 '
185 . 'AND order.traderupflag IS NULL AND order.completeflag IS NULL');
186 }
187
188 189 190 191 192
193 function getCountTraderValidate() {
194 return $this->getTable()
195 ->select('COUNT(*) AS cnt')
196 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag = 1 '
197 . 'AND order.traderupflag IS NULL AND order.completeflag IS NULL')
198 ->fetch();
199 }
200
201
202 203 204 205 206 207
208 function getGridSelectionTraderToRealize($order_id){
209 return $this->getTable()
210 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
211 ->where('order.id',$order_id);
212 }
213
214 215 216 217 218 219
220 function getCountTraderToRealize($order_id) {
221 return $this->getTable()
222 ->select('COUNT(*) AS cnt')->where('order.id',$order_id)->fetch();
223 }
224
225 226 227 228 229
230 function getGridSelectionTraderClosed(){
231 return $this->getTable()
232 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname,'
233 . ':managerresult.approved AS mapproved, '
234 . 'YEAR(CASE WHEN order.managerflag IS NULL THEN order.created ELSE :managerresult.lastmod END) AS myear')
235 ->where('order.completeflag', 1);
236 }
237
238 239 240 241 242 243 244
245 function getGridSelectionExpertValidate($areas) {
246 return $this->getTable()
247 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
248 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag IS NULL '
249 . 'AND order.completeflag IS NULL')
250 ->where(':itemorder:area_itemorder.area.id ?', $areas);
251 }
252
253 254 255 256 257 258
259 function getCountExpertValidate($areas) {
260 return $this->getTable()
261 ->select('COUNT(*) AS cnt')
262 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag IS NULL '
263 . 'AND order.completeflag IS NULL')
264 ->where(':itemorder:area_itemorder.area.id ?', $areas)
265 ->fetch();
266 }
267
268 269 270 271 272 273 274 275
276 function getGridSelectionExpertValidateNew($areas) {
277 return $this->getTable()
278 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
279 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag IS NULL '
280 . 'AND order.completeflag IS NULL')
281 ->where(':itemorder:item_area_state.area_id ?', $areas)
282 ->where(':itemorder:item_area_state.isresult ?', NULL);
283 }
284
285
286 287 288 289 290 291
292 function getGridSelectionExpertClosed($areas) {
293 return $this->getTable()
294 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname,'
295 . ':managerresult.approved AS mapproved')
296 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag = 1')
297 ->where(':itemorder:area_itemorder.area.id ?', $areas);
298 }
299
300
301
302 303 304 305 306 307 308 309
310 function getGridSelectionExpertToRealize($order_id) {
311 return $this->getTable()
312 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname,'
313 . ':managerresult.approved AS mapproved')
314 ->where('order.completeflag = 1 AND :managerresult.approved = 1 AND order.realizedflag IS NULL')
315 ->where('order.id ?', $order_id);
316 }
317
318 319 320 321 322 323
324 function getCountExpertToRealize($order_id) {
325 return $this->getTable()
326 ->select('COUNT(*) AS cnt')
327 ->where('order.completeflag = 1 AND :managerresult.approved = 1 AND order.realizedflag IS NULL')
328 ->where('order.id ?', $order_id)
329 ->fetch();
330 }
331
332 333 334 335 336
337 function getGridSelectionManagerValidate(){
338 return $this->getTable()
339 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
340 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag = 1 '
341 . 'AND order.traderupflag = 1 AND order.completeflag IS NULL');
342 }
343
344 345 346 347 348
349 function getCountManagerValidate() {
350 return $this->getTable()
351 ->select('COUNT(*) AS cnt')
352 ->where('order.departmentflag = 1 AND order.chiefflag = 1 AND order.traderdownflag = 1 AND order.itemflag = 1 '
353 . 'AND order.traderupflag = 1 AND order.completeflag IS NULL')
354 ->fetch();
355 }
356
357 358 359 360 361
362 function getGridSelectionManagerClosed(){
363 return $this->getTable()
364 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname,'
365 . ':managerresult.approved AS mapproved, '
366 . 'YEAR(CASE WHEN order.managerflag IS NULL THEN order.created ELSE :managerresult.lastmod END) AS myear')
367 ->where('order.completeflag', 1);
368 }
369
370 371 372 373 374
375 function getGridSelectionOrdersAll(){
376 return $this->getTable()
377 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname,'
378 . ':managerresult.approved AS mapproved');
379 }
380
381
382 383 384 385 386
387 function getGridSelectionEconomistApproved(){
388 return $this->getTable()
389 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
390 ->where('order.completeflag = 1')
391 ->where(':managerresult.approved = 1');
392 }
393
394
395 396 397 398 399 400
401 function getGridSelectionConsultant($ids) {
402 return $this->getTable()
403 ->select('order.*, ordertype.name AS tname, realization.name AS rname, user:unit_user.unit.ns, user:unit_user.unit.name AS uname')
404 ->where('order.id', $ids);
405 }
406
407
408 409 410 411 412 413
414 public function findById($id) {
415 return $this->getTable()
416 ->select('order.*,user:unit_user.unit.name AS unitname,justify.text AS tjustify')->wherePrimary($id)->fetch();
417 }
418
419 420 421 422 423 424 425 426
427 public function findOrderUnitById($id, $userid, $ids) {
428 return $this->getTable()
429 ->select('order.*, ordertype.name AS otype, realization.name AS orealization,user:unit_user.unit.name AS unitname,'
430 . 'user:unit_user.unit.ns AS unitns, justify.text AS justify')
431 ->where('(order.user_id ? OR order.user_id ?)',$userid,$ids)
432 ->get($id);
433 }
434
435 436 437 438 439 440 441 442 443
444 public function findOrderIdUnitById($id, $userid, $ids) {
445 return $this->getTable()
446 ->select('order.id')
447 ->where('(order.user_id ? OR order.user_id ?)',$userid,$ids)
448 ->get($id);
449 }
450
451
452 453 454 455 456 457
458 public function findOrderForTrader($id) {
459 return $this->getTable()
460 ->select('order.*, ordertype.name AS otype, realization.name AS orealization,user:unit_user.unit.name AS unitname,'
461 . 'user:unit_user.unit.ns AS unitns, justify.text AS justify')
462 ->get($id);
463 }
464
465
466 467 468 469 470 471 472
473 public function findOrderForExpert($areas, $id) {
474 return $this->getTable()
475 ->select('order.*, ordertype.name AS otype, realization.name AS orealization,user:unit_user.unit.name AS unitname,'
476 . 'user:unit_user.unit.ns AS unitns, justify.text AS justify')
477 ->where(':itemorder:area_itemorder.area.id ?', $areas)
478 ->get($id);
479 }
480
481
482 483 484 485 486 487
488 public function findOrderForEconom($id) {
489 return $this->getTable()
490 ->select('order.*, ordertype.name AS otype, realization.name AS orealization,user:unit_user.unit.name AS unitname,'
491 . 'user:unit_user.unit.ns AS unitns, justify.text AS justify')
492 ->where('order.completeflag', 1)
493 ->where(':managerresult.approved', 1)
494 ->get($id);
495 }
496
497
498 499 500 501 502 503 504
505 public function findOrderForConsultant($id, $ids) {
506 return $this->getTable()
507 ->select('order.*, ordertype.name AS otype, realization.name AS orealization,user:unit_user.unit.name AS unitname,'
508 . 'user:unit_user.unit.ns AS unitns, justify.text AS justify')
509 ->where('order.id ?', $ids)
510 ->get($id);
511 }
512
513
514 515 516 517 518 519
520 public function findOrderIdForConsultant($id, $ids) {
521 return $this->getTable()
522 ->select('order.id')
523 ->where('order.id ?', $ids)
524 ->get($id);
525 }
526
527 528 529 530 531 532
533 function getAssignedDocuments($order_id) {
534 return $this->getTable()->select(':document.id')->where(':document.order_id', $order_id)->where(':document.id IS NOT NULL');
535 }
536
537
538
539
540 541 542 543 544 545
546 public function findOrderFromSearchForm($param) {
547 return $this->getTable()
548 ->select('order.*, ordertype.name AS otype, realization.name AS orealization,user:unit_user.unit.name AS unitname,'
549 . 'user:unit_user.unit.ns AS unitns')
550 ->where('order.id LIKE ? OR order.name LIKE ?', "%$param%", "%$param%");
551 }
552
553
554 555 556 557 558
559 public function getCountOrder() {
560 return $this->getTable()->select('COUNT(*) AS pocet')->fetch();
561 }
562
563 564 565 566 567
568 public function getCounterCompleteOrder() {
569 return $this->getTable()->select('COUNT(*) AS pocet')->where('completeflag', 1)->fetch();
570 }
571
572 573 574 575 576
577 public function getCounterNoCompleteOrder() {
578 return $this->getTable()->select('COUNT(*) AS pocet')->where('completeflag', NULL)->fetch();
579 }
580
581
582 583 584 585 586 587
588 public function isInvestment($order_id) {
589 return $this->getTable()->select('isinvestment')->wherePrimary($order_id)->fetch();
590 }
591
592 593 594 595
596 public function statisticOrderDonut() {
597 return $this->getTable()->select('YEAR(created) AS rok, COUNT(*) AS pocet')->group('rok')->order('rok DESC')->fetchAll();
598 }
599
600 601 602 603
604 public function statisticOrderLine() {
605 return $this->connection->query("SELECT Y.period, U.ukoncene, P.podane FROM
606 (SELECT DATE_FORMAT(ADDDATE(Now(), INTERVAL -5 MONTH),'%Y-%m') AS `period`
607 UNION
608 SELECT DATE_FORMAT(ADDDATE(Now(), INTERVAL -4 MONTH),'%Y-%m')
609 UNION
610 SELECT DATE_FORMAT(ADDDATE(Now(), INTERVAL -3 MONTH),'%Y-%m')
611 UNION
612 SELECT DATE_FORMAT(ADDDATE(Now(), INTERVAL -2 MONTH),'%Y-%m')
613 UNION
614 SELECT DATE_FORMAT(ADDDATE(Now(), INTERVAL -1 MONTH),'%Y-%m')
615 UNION
616 SELECT DATE_FORMAT(NOW(),'%Y-%m')) AS Y
617 LEFT JOIN
618 (SELECT COUNT(*) AS `ukoncene`, (CASE WHEN `order`.`managerflag` IS NULL THEN DATE_FORMAT(`order`.`created`,'%Y-%m')
619 ELSE DATE_FORMAT(`managerresult`.`lastmod`,'%Y-%m') END) AS `period`
620 FROM `order`
621 LEFT JOIN `managerresult` ON `order`.`id` = `managerresult`.`order_id`
622 WHERE (`order`.`completeflag` = 1)
623 AND ((`order`.`created` between DATE_FORMAT(ADDDATE(Now(), INTERVAL -5 MONTH),'%Y-%m-01') AND LAST_DAY(Now()))
624 OR (`managerresult`.`lastmod` between DATE_FORMAT(ADDDATE(Now(), INTERVAL -5 MONTH),'%Y-%m-01') AND LAST_DAY(Now())))
625 GROUP BY `period`) AS U ON Y.period=U.period
626 LEFT JOIN
627 (SELECT COUNT(*) AS `podane`, DATE_FORMAT(`order`.`created`,'%Y-%m') AS `period`
628 FROM `order`
629 WHERE `order`.`created` between DATE_FORMAT(ADDDATE(Now(), INTERVAL -5 MONTH),'%Y-%m-01') AND LAST_DAY(Now())
630 GROUP BY `period`) AS P on Y.period=P.period
631 ORDER BY `period`;")
632 ->fetchAll();
633 }
634 }