Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Зачисление:*/
- create or replace function create_debit
- (
- _purchase_id int,
- _amount int,
- _started_at timestamp,
- _finished_at timestamp,
- _card_id int,
- _created_at timestamp
- )
- returns void as
- $body$
- declare _debit_id int, _saldo INT;
- begin
- insert into "debits"
- (
- "purchase_id",
- "amount",
- "created_at",
- "started_at",
- "finished_at"
- )
- VALUES
- (
- _purchase_id,
- _amount,
- _created_at,
- _started_at,
- _finished_at
- )
- RETURNING "id" INTO _debit_id;
- IF NOW() >= _started_at
- SELECT INTO _saldo "saldo" FROM "accounts" WHERE "card_id" = _card_id ORDER BY "created_at" DESC LIMIT 1;
- IF _saldo IS NULL
- THEN _saldo = 0;
- END IF;
- INSERT INTO "accounts"
- (
- "card_id",
- "debit_amount",
- "debit_id",
- "created_at",
- "saldo"
- )
- VALUES
- (
- _card_id,
- _amount,
- _debit_id,
- NOW(),
- _saldo + _amount
- );
- END IF;
- end
- $body$
- LANGUAGE 'plpgsql'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement