| | 77 | |
| | 78 | **Динамична пресметка на цена преку просторна дистанца** |
| | 79 | |
| | 80 | Креирана е нова функција calculate_price која ја пресметува цената на возењето. Таа користи ST_Distance со кастирање во geography за да ја добие точната воздушна дистанца во километри помеѓу почетната и крајната дестинација, по што го множи растојанието со соодветната тарифа на компанијата или фриленсерот. Тарифата може да биде цена за секоја минута возење или цена за секој изминат километар. |
| | 81 | |
| | 82 | {{{ |
| | 83 | CREATE OR REPLACE FUNCTION calculate_price(request_id int4, id_company int4, id_area int4, freelance_driver_id int4) |
| | 84 | RETURNS numeric(19,2) |
| | 85 | LANGUAGE plpgsql |
| | 86 | AS |
| | 87 | $$ |
| | 88 | DECLARE |
| | 89 | distance_km numeric; |
| | 90 | price_per_km numeric; |
| | 91 | price_per_min numeric; |
| | 92 | BEGIN |
| | 93 | SELECT |
| | 94 | ST_Distance( |
| | 95 | start_location::geography, |
| | 96 | end_location::geography |
| | 97 | ) / 1000.0 |
| | 98 | INTO distance_km |
| | 99 | FROM request |
| | 100 | WHERE id = request_id; |
| | 101 | |
| | 102 | IF distance_km IS NULL THEN |
| | 103 | RAISE EXCEPTION 'Request % not found', request_id; |
| | 104 | END IF; |
| | 105 | |
| | 106 | SELECT value |
| | 107 | INTO price_per_km |
| | 108 | FROM pricinginfo p |
| | 109 | JOIN company_area c on p.id=c.pricing_info_id |
| | 110 | WHERE c.company_id=id_company and c.area_id=id_area and unit='kilometer' |
| | 111 | LIMIT 1; |
| | 112 | |
| | 113 | SELECT value |
| | 114 | INTO price_per_km |
| | 115 | FROM pricinginfo p |
| | 116 | JOIN freelancedriver d on p.id=d.pricing_info_id |
| | 117 | WHERE d.driver_user_id=freelance_driver_id and unit='kilometer' |
| | 118 | LIMIT 1; |
| | 119 | |
| | 120 | SELECT value |
| | 121 | INTO price_per_min |
| | 122 | FROM pricinginfo p |
| | 123 | JOIN company_area c on p.id=c.pricing_info_id |
| | 124 | WHERE c.company_id=id_company and c.area_id=id_area and unit='minute' |
| | 125 | LIMIT 1; |
| | 126 | |
| | 127 | SELECT value |
| | 128 | INTO price_per_min |
| | 129 | FROM pricinginfo p |
| | 130 | JOIN freelancedriver d on p.id=d.pricing_info_id |
| | 131 | WHERE d.driver_user_id=freelance_driver_id and unit='minute' |
| | 132 | LIMIT 1; |
| | 133 | |
| | 134 | IF price_per_km IS NULL and price_per_min IS NULL |
| | 135 | THEN |
| | 136 | RAISE EXCEPTION 'Pricing info not found'; |
| | 137 | end if; |
| | 138 | |
| | 139 | IF price_per_km IS NOT NULL |
| | 140 | THEN |
| | 141 | RETURN ROUND((distance_km * price_per_km)::numeric, 2); |
| | 142 | ELSE |
| | 143 | RETURN ROUND((distance_km/40 * price_per_min * 60)::numeric, 2); |
| | 144 | end if; |
| | 145 | END; |
| | 146 | $$; |
| | 147 | }}} |