| 199 | | if new.b_status = 'expired' then |
| 200 | | raise exception 'билетот е веќе истечен'; |
| 201 | | end if; |
| 202 | | |
| 203 | | if new.b_status = 'inactive' and new.b_datum_aktivacija is null then |
| 204 | | new.b_datum_aktivacija = now(); |
| 205 | | new.b_status = 'active'; |
| 206 | | end if; |
| 207 | | |
| 208 | | if new.b_status = 'active' and new.b_datum_aktivacija is not null then |
| 209 | | if exists ( |
| 210 | | select 1 |
| 211 | | from tipbilet tb |
| 212 | | where tb.tb_id = new.tb_id |
| 213 | | and (new.b_datum_aktivacija + (tb.tb_trajnost || ' minutes')::interval) < now() |
| 214 | | ) then |
| 215 | | new.b_status = 'expired'; |
| 216 | | raise exception 'билетот е истечен'; |
| 217 | | end if; |
| | 196 | if exists ( |
| | 197 | select 1 |
| | 198 | from vozenje v |
| | 199 | where v.patnik_id = new.patnik_id |
| | 200 | and v.status = 'active' |
| | 201 | and v.vozenje_id != new.vozenje_id |
| | 202 | ) then |
| | 203 | update vozenje |
| | 204 | set end_date = new.start_date, |
| | 205 | status = 'finished' |
| | 206 | where patnik_id = new.patnik_id |
| | 207 | and status = 'active' |
| | 208 | and vozenje_id != new.vozenje_id; |
| | 209 | end if; |
| | 210 | |
| | 211 | if exists ( |
| | 212 | select 1 |
| | 213 | from bilet b |
| | 214 | join tipbilet tb on b.tb_id = tb.tb_id |
| | 215 | where b.b_id = new.b_id |
| | 216 | and b.status = 'active' |
| | 217 | and (b.datum_na_kupuvanje + (tb.tb_trajnost || ' seconds')::interval) < now() |
| | 218 | ) then |
| | 219 | update bilet |
| | 220 | set status = 'expired' |
| | 221 | where b_id = new.b_id; |
| | 222 | |
| | 223 | raise exception 'ticket % is expired', new.b_id; |
| 227 | | execute function bilet_status(); |
| 228 | | }}} |
| 229 | | }}} |
| 230 | | |
| 231 | | === 2. Промена на статус на билет === |
| 232 | | |
| 233 | | {{{#!div |
| 234 | | {{{#!sql |
| 235 | | create or replace function vozenje_status() |
| 236 | | returns trigger as $$ |
| 237 | | begin |
| 238 | | update vozenje |
| 239 | | set vozenje_end = now(), vozenje_status = 'finished' |
| 240 | | where patnik_k_id = new.patnik_k_id |
| 241 | | and vozenje_start is not null |
| 242 | | and vozenje_end is null |
| 243 | | and vozenje_status = 'active' |
| 244 | | and vozenje_id != new.vozenje_id; |
| 245 | | |
| 246 | | if new.vozenje_end is not null and new.vozenje_status != 'finished' then |
| 247 | | new.vozenje_status = 'finished'; |
| 248 | | end if; |
| 249 | | |
| 250 | | if new.vozenje_end is null then |
| 251 | | new.vozenje_status = 'active'; |
| 252 | | end if; |
| 253 | | |
| 254 | | return new; |
| 255 | | end; |
| 256 | | $$ language plpgsql; |
| 257 | | |
| 258 | | create or replace trigger vozenje_proverka |
| 259 | | before insert or update on vozenje |
| 260 | | for each row |
| 261 | | execute function vozenje_status(); |
| | 233 | execute function proverka_bilet_azuriranje_vozenja(); |
| | 234 | }}} |
| | 235 | }}} |
| | 236 | |
| | 237 | {{{#!div |
| | 238 | {{{#!sql |
| | 239 | |