All pastes #32150 Raw Edit

Mine

public text v1 · immutable
#32150 ·published 2005-12-02 21:21 UTC
rendered paste body
CREATE FUNCTION apply_billing_stop_function() RETURNS TRIGGER AS $apply_billing_stop_function$
  DECLARE
    result RECORD;
  BEGIN
    SELECT
      route, class, destid, description, init, inc, rate, cost,
      CEIL(NEW.duration::float / routes.inc) * routes.inc AS billtime,
      ((CEIL(NEW.duration::float / routes.inc) * routes.inc)::float / 60) * routes.rate AS total
    INTO result FROM routes
    WHERE
      routes.class = (SELECT class FROM clients WHERE prefix = NEW.prefix) AND
      SUBSTR(NEW.callee, (CASE WHEN (SELECT strip1 FROM providers WHERE destid = routes.destid) AND SUBSTR(NEW.callee, 1, 1) = '1' THEN 2 ELSE 1 END)) LIKE routes.route || '%' AND
      active
    ORDER BY routes.route DESC, routes.rate ASC LIMIT 1;

    IF result.billtime < result.init AND result.billtime > 0 THEN
      result.billtime := result.init;
    END IF;

    NEW.client := (SELECT name FROM clients WHERE prefix = NEW.prefix);

    NEW.route := result.route;
    NEW.class := result.class;
    NEW.description := result.description;
    NEW.init := result.init;
    NEW.inc := result.inc;
    NEW.billtime := result.billtime;
    NEW.rate := result.rate;
    NEW.cost := result.cost;
    NEW.total := result.total;

    DELETE FROM cdrs_current WHERE channel = NEW.channel;

    RETURN NEW;
  END;
$apply_billing_stop_function$ LANGUAGE 'plpgsql';