1: <?php
2: namespace Budovy;
3:
4: class ScheduledTaskRealizationRepository extends Repository {
5: 6: 7: 8: 9:
10: public function findById($id) {
11: return $this->getTable()->select('scheduledtask_realization.*, :scheduledtask_realization_contact.contact.id AS id_contact')->get($id);
12: }
13:
14: 15: 16: 17: 18: 19: 20: 21: 22:
23: function getGridSelection($filter, $order, $page, $limit) {
24: $selection = $this->getTable()->select('scheduledtask_realization.*, scheduledtask.name, :scheduledtask_realization_contact.contact.id AS id_contact, CONCAT(:scheduledtask_realization_contact.contact.firstname, \' \', :scheduledtask_realization_contact.contact.lastname) AS contact');
25:
26: if (isset($order[0]))
27: $selection->order(implode(' ', $order));
28:
29: $selection->page($page, $limit);
30:
31: return $this->addGridFilter($selection, $filter);
32: }
33:
34: 35: 36: 37: 38: 39:
40: function getGridCount($filter) {
41: $selection = $this->getTable()->select('COUNT(*) AS cnt');
42:
43: $result = $this->addGridFilter($selection, $filter)->fetch();
44:
45: return $result->cnt;
46: }
47:
48: 49: 50: 51: 52: 53: 54:
55: private function addGridFilter(\Nette\Database\Table\Selection $selection, $filter) {
56: $filters = array();
57:
58: foreach ($filter as $k => $v) {
59: if ($k == 'contact')
60: $filters[':scheduledtask_realization_contact.contact.id=?'] = $v;
61: else if ($k == 'name')
62: $filters['id_scheduledtask=?'] = $v;
63: else if ($k == 'date')
64: $filters['`scheduledtask_realization`.date=?'] = $v;
65: else
66: $filters['`scheduledtask_realization`.' . $k . ' LIKE ?'] = "%$v%";
67: }
68:
69: return $selection->where($filters);
70: }
71:
72: 73: 74: 75: 76: 77:
78: function getAssignedDocuments($id_scheduledtask_realization) {
79: return $this->getTable()->select(':scheduledtask_realization_document.document.id')->where('scheduledtask_realization.id', $id_scheduledtask_realization)->where(':scheduledtask_realization_document.document.id IS NOT NULL');
80: }
81:
82: 83: 84: 85: 86: 87:
88: function assignDocument($id_scheduledtask_realization, $id_document) {
89: $this->connection->table('scheduledtask_realization_document')->insert(array('id_scheduledtask_realization' => $id_scheduledtask_realization, 'id_document' => $id_document));
90: }
91:
92: 93: 94: 95: 96: 97:
98: function removeAssignedDocument($id_scheduledtask_realization, $id_document) {
99: $this->connection->table('scheduledtask_realization_document')->where(array('id_scheduledtask_realization' => $id_scheduledtask_realization, 'id_document' => $id_document))->delete();
100: }
101:
102: 103: 104: 105: 106: 107:
108: function getAssignedContact($id_scheduledtask_realization) {
109: return $this->getTable()->select(':scheduledtask_realization_contact.contact.*')->where('scheduledtask_realization.id', $id_scheduledtask_realization)->where(':scheduledtask_realization_contact.contact.id IS NOT NULL')->fetch();
110: }
111:
112: 113: 114: 115: 116: 117: 118:
119: function assignContact($id_scheduledtask_realization, $id_contact) {
120: try {
121: $this->connection->table('scheduledtask_realization_contact')->insert(array('id_scheduledtask_realization' => $id_scheduledtask_realization, 'id_contact' => $id_contact));
122: } catch (\PDOException $e) {
123: if ($e->getCode() == '23000')
124: $this->connection->table('scheduledtask_realization_contact')->where('id_scheduledtask_realization', $id_scheduledtask_realization)->update(array('id_contact' => $id_contact));
125: else
126: throw $e;
127: }
128: }
129:
130: 131: 132: 133: 134:
135: function removeAssignedContact($id_scheduledtask_realization) {
136: $this->connection->table('scheduledtask_realization_contact')->where(array('id_scheduledtask_realization' => $id_scheduledtask_realization))->delete();
137: }
138:
139: 140: 141: 142: 143: 144:
145: function getTasksByDocument($id_document) {
146: return $this->getTable()->select('scheduledtask.name, scheduledtask_realization.date, scheduledtask_realization.id')->where(':scheduledtask_realization_document.id_document=?', $id_document);
147: }
148:
149: 150: 151: 152: 153: 154: 155: 156:
157: function getPriceStatistic($start, $end, $id_place) {
158: $selection = $this->getTable()
159: ->select('SUM(scheduledtask_realization.price) AS price, MONTH(`date`) AS `month`, YEAR(`date`) AS `year`')
160: ->where('`date` BETWEEN ? AND ?', $start, $end)
161: ->group('MONTH(`date`), YEAR(`date`)')
162: ->order('`year` ASC, `month` ASC');
163:
164: if ($id_place > 0)
165: $selection->where('scheduledtask.unit.place.id', $id_place);
166:
167: return $selection;
168: }
169:
170: 171: 172: 173: 174: 175: 176: 177:
178: function getUnitStatistic($start, $end, $id_place) {
179: $selection = $this->getTable()
180: ->select('SUM(scheduledtask_realization.price) AS price, scheduledtask.unit.name')
181: ->where('`date` BETWEEN ? AND ?', $start, $end)
182: ->group('scheduledtask.id_unit')
183: ->order('name');
184:
185: if ($id_place > 0)
186: $selection->where('scheduledtask.unit.place.id', $id_place);
187:
188: return $selection;
189: }
190:
191: 192: 193: 194: 195: 196: 197: 198:
199: function getOperationStatistic($start, $end, $id_place) {
200: $selection = $this->getTable()
201: ->select('SUM(scheduledtask_realization.price) AS price, scheduledtask.operation.name')
202: ->where('`date` BETWEEN ? AND ?', $start, $end)
203: ->group('scheduledtask.id_operation')
204: ->order('name');
205:
206: if ($id_place > 0)
207: $selection->where('scheduledtask.unit.place.id', $id_place);
208:
209: return $selection;
210: }
211:
212: 213: 214: 215: 216: 217: 218: 219:
220: function getContactStatistic($start, $end, $id_place) {
221: $selection = $this->getTable()
222: ->select('SUM(scheduledtask_realization.price) AS price, :scheduledtask_realization_contact.contact.*')
223: ->where('`date` BETWEEN ? AND ?', $start, $end)
224: ->group(':scheduledtask_realization_contact.id_contact')
225: ->order(':scheduledtask_realization_contact.contact.lastname, :scheduledtask_realization_contact.contact.firstname');
226:
227: if ($id_place > 0)
228: $selection->where('scheduledtask.unit.place.id', $id_place);
229:
230: return $selection;
231: }
232: }