summaryrefslogtreecommitdiff
path: root/sql/Pg-upgrade-2.6.1-2.6.2.sql
blob: 391366008a0102c8d882562fd63b0733057aefc0 (plain)
  1. --
  2. drop function avgcost(int);
  3. CREATE FUNCTION avgcost(int) RETURNS FLOAT AS '
  4. DECLARE
  5. v_cost float;
  6. v_qty float;
  7. v_parts_id alias for $1;
  8. BEGIN
  9. SELECT INTO v_cost, v_qty SUM(i.sellprice * i.qty), SUM(i.qty)
  10. FROM invoice i
  11. JOIN ap a ON (a.id = i.trans_id)
  12. WHERE i.parts_id = v_parts_id;
  13. IF v_cost IS NULL THEN
  14. v_cost := 0;
  15. END IF;
  16. IF NOT v_qty IS NULL THEN
  17. IF v_qty = 0 THEN
  18. v_cost := 0;
  19. ELSE
  20. v_cost := v_cost/v_qty;
  21. END IF;
  22. END IF;
  23. RETURN v_cost;
  24. END;
  25. ' language 'plpgsql';
  26. -- end function
  27. --
  28. drop function lastcost(int);
  29. CREATE FUNCTION lastcost(int) RETURNS FLOAT AS '
  30. DECLARE
  31. v_cost float;
  32. v_parts_id alias for $1;
  33. BEGIN
  34. SELECT INTO v_cost sellprice FROM invoice i
  35. JOIN ap a ON (a.id = i.trans_id)
  36. WHERE i.parts_id = v_parts_id
  37. ORDER BY a.transdate desc, a.id desc
  38. LIMIT 1;
  39. IF v_cost IS NULL THEN
  40. v_cost := 0;
  41. END IF;
  42. RETURN v_cost;
  43. END;
  44. ' language 'plpgsql';
  45. -- end function
  46. --
  47. update defaults set version = '2.6.2';