Advertisement
Guest User

Untitled

a guest
Oct 12th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.03 KB | None | 0 0
  1. /*Зачисление:*/
  2. create or replace function create_debit
  3. (
  4. _purchase_id int,
  5. _amount int,
  6. _started_at timestamp,
  7. _finished_at timestamp,
  8. _card_id int,
  9. _created_at timestamp
  10. )
  11. returns void as
  12. $body$
  13. declare _debit_id int, _saldo INT;
  14. begin
  15. insert into "debits"
  16. (
  17. "purchase_id",
  18. "amount",
  19. "created_at",
  20. "started_at",
  21. "finished_at"
  22. )
  23. VALUES
  24. (
  25. _purchase_id,
  26. _amount,
  27. _created_at,
  28. _started_at,
  29. _finished_at
  30. )
  31. RETURNING "id" INTO _debit_id;
  32.  
  33. IF NOW() >= _started_at
  34. SELECT INTO _saldo "saldo" FROM "accounts" WHERE "card_id" = _card_id ORDER BY "created_at" DESC LIMIT 1;
  35.  
  36. IF _saldo IS NULL
  37. THEN _saldo = 0;
  38. END IF;
  39.  
  40. INSERT INTO "accounts"
  41. (
  42. "card_id",
  43. "debit_amount",
  44. "debit_id",
  45. "created_at",
  46. "saldo"
  47. )
  48. VALUES
  49. (
  50. _card_id,
  51. _amount,
  52. _debit_id,
  53. NOW(),
  54. _saldo + _amount
  55. );
  56. END IF;
  57. end
  58. $body$
  59. LANGUAGE 'plpgsql'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement