| 160 | |
| 161 | == Функција за пресметување на вкупен износ на нарачка == |
| 162 | |
| 163 | {{{#!sql |
| 164 | drop function calculate_cost_of_order_food(order_id int); |
| 165 | |
| 166 | |
| 167 | create function calculate_cost_of_order_food(order_id int) |
| 168 | returns table( |
| 169 | vkupno_hrana int |
| 170 | ) |
| 171 | language plpgsql |
| 172 | as |
| 173 | $$ |
| 174 | begin |
| 175 | return query select kolicina * iznos as vkupno |
| 176 | from naracka n join naracka_sodrzi_hrana nih on n.naracka_id = nih.naracka_id |
| 177 | join hrana h on h.id_stavka = nih.id_stavka |
| 178 | join cena c on (c.id_stavka = h.id_stavka and vazi_do is null) |
| 179 | or (c.id_stavka = h.id_stavka and vazi_do > now()) |
| 180 | where n.naracka_id = order_id; |
| 181 | |
| 182 | |
| 183 | |
| 184 | end |
| 185 | $$ |
| 186 | |
| 187 | drop function calculate_cost_of_order_packets(order_id int); |
| 188 | |
| 189 | create function calculate_cost_of_order_packets(order_id int) |
| 190 | returns table ( |
| 191 | vkupno_paketi bigint |
| 192 | ) |
| 193 | language plpgsql |
| 194 | as |
| 195 | $$ |
| 196 | begin |
| 197 | return query select sum(iznos) * nip.kolicina as vkupno_paketi |
| 198 | from naracka n join naracka_ima_paket nip on n.naracka_id = nip.naracka_id |
| 199 | join paket pa on pa.paket_id = nip.paket_id |
| 200 | join paket_sodrzi_hrana psh on psh.paket_id = pa.paket_id |
| 201 | join hrana h on h.id_stavka = psh.id_stavka |
| 202 | join cena c on (c.id_stavka = h.id_stavka and vazi_do is null) |
| 203 | or (c.id_stavka = h.id_stavka and vazi_do > now()) |
| 204 | where n.naracka_id = order_id |
| 205 | group by n.naracka_id, pa.paket_id, nip.kolicina ; |
| 206 | end |
| 207 | $$ |
| 208 | |
| 209 | |
| 210 | drop function calculate_order_cost(orider_id int); |
| 211 | |
| 212 | create function calculate_order_cost(order_id int) |
| 213 | returns bigint |
| 214 | language plpgsql |
| 215 | as |
| 216 | $$ |
| 217 | declare |
| 218 | vkupno_cena_hrana bigint; |
| 219 | vkupno_cena_paketi bigint; |
| 220 | begin |
| 221 | select sum(vkupno_hrana) into vkupno_cena_hrana from calculate_cost_of_order_food(order_id); |
| 222 | select sum(vkupno_paketi) into vkupno_cena_paketi from calculate_cost_of_order_packets(order_id); |
| 223 | return vkupno_cena_hrana + vkupno_cena_paketi; |
| 224 | |
| 225 | end |
| 226 | $$ |
| 227 | |
| 228 | }}} |